day2c++学习

发布时间 2023-07-09 21:33:14作者: AuroraKelsey

通讯录

  1 #include <iostream>
  2 #include <string>
  3 
  4 using namespace std;
  5 const int N=2000;
  6 
  7 struct person{
  8     string name;
  9     string gender;
 10     int age;
 11     string phonenum;
 12     string address;
 13 };
 14 //通讯录结构体
 15 struct node{
 16     int size;
 17     struct person a[N];
 18 };
 19 
 20 void ShowMenu(){
 21     printf("************************\n");
 22     printf("***** 1.添加联系人 *****\n");
 23     printf("***** 2.显示联系人 *****\n");
 24     printf("***** 3.删除联系人 *****\n");
 25     printf("***** 4.查找联系人 *****\n");
 26     printf("***** 5.修改联系人 *****\n");
 27     printf("***** 6.清空联系人 *****\n");
 28     printf("***** 0.退出通讯录 *****\n");
 29     printf("************************\n");
 30 }
 31 
 32 void addperson(node *txl) {
 33     if(txl->size==N) {
 34         printf("通讯录已满,无法添加!");
 35         return;
 36     }
 37     int sz=++txl->size;
 38     cout<<"请输入姓名:"<<endl;
 39     cin>>txl->a[sz].name;
 40     cout<<"请输入性别:"<<endl;
 41     cin>>txl->a[sz].gender;    
 42     cout<<"请输入年龄:"<<endl;
 43     cin>>txl->a[sz].age;
 44     cout<<"请输入电话:"<<endl;
 45     cin>>txl->a[sz].phonenum;    
 46     cout<<"请输入住址:"<<endl;
 47     cin>>txl->a[sz].address;        
 48     cout<<"添加成功!"<<endl;
 49     system("pause");
 50     system("cls");//清屏
 51 }
 52 
 53 void showperson(node *txl) {
 54     if(txl->size==0) {
 55         printf("当前记录为空\n");
 56     }
 57     else {
 58         for(int i=1;i<=txl->size;i++) {
 59             cout<<"姓名:"<<txl->a[i].name<<endl;
 60             cout<<"性别:"<<txl->a[i].gender<<endl;
 61             cout<<"年龄:"<<txl->a[i].age<<endl;
 62             cout<<"电话:"<<txl->a[i].phonenum<<endl;
 63             cout<<"住址:"<<txl->a[i].address<<endl;
 64         }
 65     }
 66     system("pause");
 67     system("cls");//清屏    
 68 }
 69 
 70 int isexist(node txl,string nam) {
 71     for(int i=1;i<=txl.size;i++) {
 72         if(nam==txl.a[i].name) return i;
 73     } 
 74     return -1;
 75 }
 76 
 77 void delperson(node *txl,int id) {
 78     txl->a[id].name=txl->a[txl->size].name;
 79     txl->a[id].gender=txl->a[txl->size].gender;
 80     txl->a[id].age=txl->a[txl->size].age;
 81     txl->a[id].phonenum=txl->a[txl->size].phonenum;
 82     txl->a[id].address=txl->a[txl->size].address;
 83     txl->size--;
 84     system("pause");
 85     system("cls");//清屏    
 86 }
 87 
 88 void findperson(node *txl) {
 89     string name1;
 90     cout<<"请输入您要查找的联系人姓名:"<<endl;
 91     cin>>name1;
 92     int i=isexist(*txl,name1);
 93     if(i==-1) cout<<"查无此人"<<endl;
 94     else {
 95             cout<<"姓名:"<<txl->a[i].name<<endl;
 96             cout<<"性别:"<<txl->a[i].gender<<endl;
 97             cout<<"年龄:"<<txl->a[i].age<<endl;
 98             cout<<"电话:"<<txl->a[i].phonenum<<endl;
 99             cout<<"住址:"<<txl->a[i].address<<endl;        
100     }
101     system("pause");
102     system("cls");//清屏        
103 }
104 
105 void modifyperson(node *txl) {
106     string name1;
107     cout<<"请输入您要修改的联系人姓名:"<<endl;
108     cin>>name1;
109     int i=isexist(*txl,name1);
110     if(i==-1) cout<<"查无此人"<<endl;
111     else {
112         string s;int ag;
113         cout<<"请输入姓名:"<<endl;
114         cin>>s;
115         txl->a[i].name=s;
116         cout<<"请输入性别:"<<endl;
117         cin>>s;
118         txl->a[i].gender=s;    
119         cout<<"请输入年龄:"<<endl;
120         cin>>ag;
121         txl->a[i].age=ag;
122         cout<<"请输入电话:"<<endl;
123         cin>>s;
124         txl->a[i].phonenum=s;    
125         cout<<"请输入住址:"<<endl;
126         cin>>s;
127         txl->a[i].address=s;        
128         cout<<"修改成功!"<<endl;    
129     }
130     system("pause");
131     system("cls");//清屏    
132 }
133 
134 void clean(node *txl) {
135     cout<<"请确认是否要清空,确认请输入1,否则输入其他数字"<<endl;
136     int f;
137     cin>>f;
138     if(f==1) txl->size=0;
139     system("pause");
140     system("cls");//清屏
141 }
142 int main() {
143     node txl;
144     txl.size=0;
145 
146     while(1){
147         ShowMenu();
148         int op;
149         cin>>op;
150         switch (op)
151         {
152         case 0:
153             printf("欢迎下次使用");
154             return 0;
155             break;
156         case 1:
157             addperson(&txl);
158             break;
159         case 2:
160             showperson(&txl);
161             break;
162         case 3: {
163             cout<<"请输入要删除的联系人姓名:"<<endl;
164             string name1;
165             cin>>name1;
166             int num=isexist(txl,name1);
167             if(num==-1) cout<<"查无此人"<<endl;
168             else delperson(&txl,num);
169             break;
170         }
171         case 4: 
172             findperson(&txl);
173             break;
174         case 5:
175             modifyperson(&txl);
176             break;    
177         case 6:
178             clean(&txl);
179             break;                                                                
180         default:
181             printf("请正确输出0-6的数字实现功能\n");
182             break;
183         }
184     }
185     system("pause");
186     return 0;
187 }