**
题目:假设微信朋友圈中朋友的信息都存储在下面的结构体中:
**
struct friends
{
char name[20]; //存储朋友姓名
char sex; //f or m
int age;
char phone[21];
char email[20];
char birthday[20];
int countEmail; //邮件记录
char textMessage[500]; //存储短信内容
int countPhone; //电话记录
char emailMessage[500];
}
编程要求:
(1)设计一个Friend类,用来读信息到结构体数组中;
(2)设计一个Master类,满足以下四个功能:
a. 添加功能
作为主人登录时,添加一个朋友的基本信息,包括姓名、性别、年龄、电话、邮箱、生日等,并选择要添加的分组。
b. 删除功能
作为主人登录时,按分组和姓名对一个朋友的信息进行删除。
c. 修改功能
作为主人登录时,按分组和姓名对一个朋友的信息进行修改。
d. 显示功能
作为主人登录时,按分组有条理显示某一组内所有朋友的基本信息,包括姓名、性别、年龄、电话、邮箱、生日等。
// Created by Peggy on 2020/3/12.
// Question number: experiment 3 number 2
//Question : built a addresss book
#include <iostream>
#include<string> // used 'strcmp'
using namespace std;
struct friends
{
char name[20]; //record friend's name
char sex; //f or m
int age;
char phone[21], email[20], birthday[20];
int countEmail; //the number of email
char textMessage[500]; //record short massage
int countPhone; //recard phone massage
char emailMessage[500]; //E-mail recorder
};
//Class of Friend, being used to send informations to friends
class Friend{
public:
friends everyGroup[10];
void changeAge();
};
void Friend::changeAge(){
for(int i = 0; i < 10;i++){
everyGroup[i].age = -1;
}
}
//Class Master has four functions
class Master{
public:
void AddFriend(Friend &); //used & to change information
void DeleteFriend(Friend &, char[]);
void ModifyFriend(Friend &, char[]);
void ListAll(Friend []);
};
//Add a new friend to the group.
void Master::AddFriend(Friend &theGroup){
for(int i = 0;i < 10; i++){
if(theGroup.everyGroup[i].age == -1){ // if the group has a room for add a new one
cout << "Name Sex Age Phone Email Birthday"<<endl;
cin >> theGroup.everyGroup[i].name;
cin.ignore();
cin >> theGroup.everyGroup[i].sex;
cin.ignore();
cin >> theGroup.everyGroup[i].age;
cin.ignore();
cin >> theGroup.everyGroup[i].phone;
cin.ignore();
cin >> theGroup.everyGroup[i].email;
cin.ignore();
cin >> theGroup.everyGroup[i].birthday;
break; // add one friend once
}
}
}
// delete a friend by its name and group
void Master::DeleteFriend(Friend &Group, char itsName[]){
for(int i = 0; i < 10; i++){
if(strcmp(itsName, Group.everyGroup[i].name) == 0){
struct friends renew;
renew.age = -1;
Group.everyGroup[i] = renew;
break; //Deleted one person by a single operation
}
}
}
// modified a friend by its name and group
void Master::ModifyFriend(Friend &Group, char itsName[]){
for(int i = 0; i < 10; i++){
if(strcmp(itsName, Group.everyGroup[i].name) == 0){
cout << "New information: ";
cout << "(Name Sex Age Phone Email Birthday)"<<endl;
cin >> Group.everyGroup[i].name;
cin.ignore();
cin >> Group.everyGroup[i].sex;
cin.ignore();
cin >> Group.everyGroup[i].age;
cin.ignore();
cin >> Group.everyGroup[i].phone;
cin.ignore();
cin >> Group.everyGroup[i].email;
cin.ignore();
cin >> Group.everyGroup[i].birthday;
break; // only modified one person's information
}
}
}
void Master::ListAll(Friend Group[]){
for(int i = 0;i < 5 && Group[i].everyGroup[0].age != -1; i++){
cout << "Group name age sex birthday phone email\n"; // output every friend information
for(int j = 0; j < 10 && Group[i].everyGroup[j].age != -1; j++){//
cout << i+1 <<" "<<Group[i].everyGroup[j].name << " " << Group[i].everyGroup[j].age << " " << Group[i].everyGroup[j].birthday <<" "<< Group[i].everyGroup[j].phone << " " <<Group[i].everyGroup[j].email<<endl;
}
}
}
int main(void){
Friend Group[5]; //these are five groups
for(int i = 0; i < 5; i++){
Group[i].changeAge(); // renew every room's age to -1
}
int group;
char oper, Name[21];
cout << "a.Add a new friend.\n" << "b.Delete a friend.\n"<< "c.Modify a friend's information.\n"
<< "d.List all friends.\n" << "Another letter, quit.\n";
cin >> oper;
cin.ignore();
while(oper){
Master person;
switch(oper){
case 'a':
cout << "Add it to a group(1-5):";
cin >> group;
person.AddFriend(Group[group-1]);
break;
case 'b':
cout << "Group(1-5):\tName:\n";
cin >> group;
cin.ignore();
cin >> Name;
person.DeleteFriend(Group[group-1], Name);
break;
case 'c':
cout << "Group(1-5):\tName:\n";
cin >> group;
cin.ignore();
cin >> Name;
person.ModifyFriend(Group[group-1], Name);
break;
case 'd':person.ListAll(Group);break;
default: exit(0);
}
cout << "a.Add a new friend.\n" << "b.Delete a friend.\n"<< "c.Modify a friend's information.\n"
<< "d.List all friends.\n" << "Another letter, quit.\n";
cin.ignore();
cin >> oper; //if not quit, continue operating
}
return 0;
}