したがって、これはおそらく少し巨大で、おそらく面倒で不正確です (これは本当に初心者です)。私はちょうど C を学んでおり、私がしなければならない作業の一部には、このプログラムを C++ プログラムに切り替えることが含まれます。私がする必要がある主なことは、すべての構造体をクラスに置き換え、コードで使用されているすべての関数をクラス関数にすることです (メンバー? 思い出すと...)
私は多くの基本をかなりよく理解していますが、コンセプトはコードを「変更」することです。クラスに切り替えるだけで以前の作業を「変更」する方法がわかりません。そのままでは、クラスを使用するためにプログラムをかなり書き直す必要があるように感じます。多分私はここで単純さを逃しています。誰かに仕事をしてもらいたくありません。構造体をクラスとして機能するようにフォーマットする簡単な方法があるかどうか知りたいだけです。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
//Struct for friend array pointers.
typedef struct friendstruct{
char *firstname;
char *lastname;
char *homephone;
char *cellphone;
} frnd;
//Buffer for use in storing in the main program.
typedef struct bufferstruct{
char firstname[20];
char lastname[20];
char homephone[20];
char cellphone[20];
} frndbuff;
//Add friend function.
void addfriend(frnd friendarray[], frndbuff newfrnd, int count, int opened){
friendarray[count].firstname = malloc(sizeof(newfrnd.firstname)); //Assign memory before copying string.
friendarray[count].lastname = malloc(sizeof(newfrnd.lastname));
friendarray[count].homephone = malloc(sizeof(newfrnd.homephone));
friendarray[count].cellphone = malloc(sizeof(newfrnd.cellphone));
strcpy(friendarray[count].firstname, newfrnd.firstname);
//printf("%s", friendarray[count].firstname);
//printf("%s", newfrnd.firstname);
strcpy(friendarray[count].lastname, newfrnd.lastname);
strcpy(friendarray[count].homephone, newfrnd.homephone);
strcpy(friendarray[count].cellphone, newfrnd.cellphone);
//friendarray[count].lastname = newfrnd.lastname;
//friendarray[count].homephone = newfrnd.homephone;
//friendarray[count].cellphone = newfrnd.cellphone;
if(opened==0){
printf("\nA new friend has been added to the phonebook.");
}
}
//Deleteing friends.
int deletefriend(frnd friendarray[], frndbuff newfrnd, int count){
int n = 0;
int success = 0;
while(n<count){
if(strcmp(newfrnd.lastname,friendarray[n].lastname)==0){ //Comparing strings.
while(n<count-1){
strcpy(friendarray[n].firstname, friendarray[n+1].firstname);
strcpy(friendarray[n].lastname, friendarray[n+1].lastname);
strcpy(friendarray[n].homephone, friendarray[n+1].homephone);//Removes previously used position.
strcpy(friendarray[n].cellphone, friendarray[n+1].cellphone);
//friendarray[n].lastname = friendarray[n+1].lastname;
//friendarray[n].homephone = friendarray[n+1].homephone;
//friendarray[n].cellphone = friendarray[n+1].cellphone;
n++;
}
success = 1;
count = count - 1;
break;
}
n++;
}
if(success==1){
printf("\nThe entry for %s has been removed from the phonebook.", newfrnd.lastname);
}else{
printf("\nThat entry was not found");
}
//printf("%i", count);
return count;
}
//Show friend by last namme. Identical to delete friend, without removal.
void showfriend(frnd friendarray[], frndbuff newfrnd, int count){
int n = 0;
int success = 0;
while(n<count){
if(strcmp(newfrnd.lastname, friendarray[n].lastname)==0){
printf("\n\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone);
success = 1;
break;
}
n++;
}
if(success==0){
printf("\nThat entry was not found");
}
}
//DIsplays entire phonebook.
void phonebook(frnd friendarray[], int count){
int n = 0;
//printf("%i", count); Used in debugging.
while(n<count){
printf("\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone);
n++;
}
}
//Find friend based on last name.
void searchfriend(frnd friendarray[], frndbuff newfrnd, int count){
int n = 0;
int success = 0;
while(n<count){
if(strcmp(newfrnd.lastname, friendarray[n].lastname)==0){
printf("\n\n%s %s %s (home) %s (cell)\n", friendarray[n].firstname, friendarray[n].lastname, friendarray[n].homephone, friendarray[n].cellphone);
success = 1;
}
n++;
}
if(success==0){
printf("\nThat entry was not found");
}
}
int main(){
int option, option2;
frndbuff currentfriend;
frnd friendarray[50];
int count = 0;
int filecount = 0;
int opened = 0;
//Phonebook load previous to main loop.
printf("\nDo you have a previously saved phonebook you'd like to load?\n1) Yes\n2) No\n");
printf("\nChoose an option : ");
scanf("%i", &option2);
if(option2==1){
FILE *fileopen;
fileopen = fopen("phonebook.dat", "r"); //File open for reading.
if (fileopen != NULL){
filecount = 0;
opened = 1;
printf("\nYour previous phonebook has been loaded : ");
while(fscanf(fileopen, "%s %s %s (home) %s (work)\n",¤tfriend.firstname, ¤tfriend.lastname, ¤tfriend.homephone, ¤tfriend.cellphone)==4){
printf("\n%s %s %s (home) %s (work)\n",currentfriend.firstname, currentfriend.lastname, currentfriend.homephone, currentfriend.cellphone);
addfriend(friendarray, currentfriend, filecount, opened);
filecount++;
}
count = filecount;
}else if(fileopen == NULL){
printf("\nA previous phonebook could not be found.");
}
}
while(1==1){
opened = 0;
printf("\n\nPhone Book Application\n1) Add Friend\n2) Delete Friend\n3) Show a Friend\n4) Show phone book\n5) Search by last name\n6) Quit\n");
printf("\n\nWhat option would you like to choose : ");
scanf("%i", &option);
//Option ensuring.
if(option<1 || option>6){
printf("\nYou did not enter a valid option, please try again.");
option = 6;
}
if(option==1){
printf("\nFirst Name : ");
scanf("%s", ¤tfriend.firstname);
printf("\nLast Name : ");
scanf("%s", ¤tfriend.lastname);
printf("\nHome Phone : ");
scanf("%s", ¤tfriend.homephone);
printf("\nCell Phone : ");
scanf("%s", ¤tfriend.cellphone);
//printf("%s", currentfriend.firstname); Debugging.
addfriend(friendarray, currentfriend, count, opened);
count++;
//printf("%i", count); Debugging.
//All options call the previously made functions and pass the buffer.
}else if(option==2){
printf("\nEnter the last name of the friend you'd like to delete : ");
scanf("%s", ¤tfriend.lastname);
count = deletefriend(friendarray, currentfriend, count);
}else if(option==3){
printf("\nEnter the last name of the friend you'd like to view : ");
scanf("%s", ¤tfriend.lastname);
showfriend(friendarray, currentfriend, count);
}else if(option==4){
phonebook(friendarray, count);
}else if(option==5){
printf("\nEnter the last name you'd like to search : ");
scanf("%s", ¤tfriend.lastname);
searchfriend(friendarray, currentfriend, count);
}else if(option==6){
option2 = 0;
printf("\nWould you like to save your phonebook to a file?\n1) Yes\n2) No");
printf("\n Choose an option : ");
scanf("%i", &option2);
if(option2==1){
filecount = 0;
FILE *filesave;
filesave = fopen("phonebook.dat", "w"); //File open for writing.
while(filecount<count){
//File written in the same method it is read.
fprintf(filesave, "%s %s %s (home) %s (work)\n",friendarray[filecount].firstname, friendarray[filecount].lastname, friendarray[filecount].homephone, friendarray[filecount].cellphone);
filecount++;
}
}
printf("\nThank you for using this Phone Book Application!");
break;
}
}
//Files closed.
fclose(fileopen);
fclose(filesave);
getch();
return 0;
}