所以,这可能有点大,而且可能是混乱和不正确的(它真的是初学者)。我只是在学习 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;
}