简易 (I/O)版本通讯录

 

  1 #include <stdio.h>
  2 #include<assert.h>
  3 //#include<malloc.h>
  4 #include<string.h> 
  5 #include<windows.h>
  6 
  7 #define MailSize 100
  8 #define MaxName 10
  9 #define MaxAdress 20
 10 #define MaxPhone 20
 11 
 12 struct Person{
 13     char name[MaxName];
 14     int age;
 15     char sex;
 16     char adress[MaxAdress];
 17     char phone[MaxPhone];
 18 };
 19 typedef struct Mail{
 20     int ExistNum;
 21     //int MaxNum;
 22     struct Person peo[MailSize];
 23 }ML,*MailList;
 24 
 25 enum mem_infor{
 26     Name=1,
 27     Age,
 28     Sex,
 29     Adress,
 30     Phone,
 31 };
 32 void Show_member(MailList s){
 33     int i=0;
 34     if(0 == s->ExistNum){
 35         printf("Mail is Null!");
 36         return ;
 37     }    
 38     printf("-------------------------------------------------\n");
 39     printf("|Name  | Age| Sex  |Adress           |  Phone   |   \n");
 40 
 41     while(i<s->ExistNum){
 42        printf("%-10s",s->peo[i].name);
 43        printf("%-5d",s->peo[i].age);
 44        printf("%-5c",s->peo[i].sex);
 45        printf("%-20s",s->peo[i].adress);
 46        printf("%-20s",s->peo[i].phone); 
 47        printf("\n");
 48        i++;
 49     }
 50     
 51 }
 52 void Add_member(MailList s,int num){
 53     char na[5],sex,adr[10],pho[10];
 54     int a;
 55     printf("\nInput: Name + Age + Sex(M/W) + Adress + Phone:>\n");
 56     for(int i=s->ExistNum;i<s->ExistNum+num;++i){
 57         scanf("%s  %d  %c  %s  %s",na,&a,&sex,adr,pho); 
 58         strcpy(s->peo[i].name,na);
 59         s->peo[i].age=a;
 60         s->peo[i].sex=sex;
 61         strcpy(s->peo[i].adress,adr);
 62         strcpy(s->peo[i].phone,pho);
 63     }
 64     s->ExistNum+=num;    
 65     printf("\nAdd Successful!\n");    
 66 }
 67 int Research_member(const MailList s,char *na){
 68     assert(na);
 69     assert(s);
 70     for(int i=0;i<s->ExistNum;++i){
 71         if(!strcmp(s->peo[i].name,na) )
 72         return i;
 73     }
 74     return -1;
 75 }
 76 void  Delet_member(MailList s,char *na){
 77     assert(s);
 78     assert(na);
 79     int locate=Research_member(s,na);
 80     if(locate<0) {
 81         printf("\nDelet failed !");
 82         return ;
 83     }
 84     for(int i=locate;i<s->ExistNum;++i){
 85         s->peo[i]=s->peo[i+1];
 86     }
 87     s->ExistNum--;
 88     printf("Delet Successful!\n");            
 89 }
 90 void swap(struct Person* p1,struct Person* p2){
 91     struct Person tmp;
 92     tmp=*p1;
 93     *p1=*p2;
 94     *p2=tmp;
 95 }
 96 void Sort_member(MailList s){
 97     assert(s);
 98     MailList ret=s;
 99     for(int i=0;i<s->ExistNum-1;++i){
100         for(int j=i;j<s->ExistNum;++j){
101             if(strcmp(ret->peo[i].name,ret->peo[j].name)>0 )
102             swap(&ret->peo[i],&ret->peo[j]);   //交换两个结构体 
103         }
104     }
105     printf("\nSort Successful !");
106  }
107 
108 void Clear_member(MailList s){
109     s->ExistNum=0;
110     printf("\nClear.....");
111     Sleep(500);
112     printf("\nClear finished!");
113 }
114 void Modify_member(MailList s){
115     assert(s);
116     char na[10],sex,adr[10],pho[10];
117     int a,option,locate;
118     if(s->ExistNum <= 0){
119         printf("通讯录为空,无法修改!");
120         return;
121     } 
122     printf("\n选择修改位置:");
123      scanf("%d",&locate);    
124     if(locate < s->ExistNum && locate>=0){
125         printf("******************************\n");
126         printf("* 1--姓名   2--年龄  3--性别 *\n");
127         printf("* 4--住址            5--电话 *\n"); 
128         printf("******************************\n");
129         choose:
130         printf("\n请选择修改:>");
131         scanf("%d",&option);
132         switch(option){
133             case Name:{
134                 printf("\n姓名修改为:");
135                 scanf("%s",na);
136                 strcpy(s->peo[locate].name,na);
137                 break;
138             }
139             case Age:{
140                 printf("年龄修改为:"); 
141                 scanf("%d",&a);
142                 s->peo[locate].age=a;
143                 break;
144             }
145             case Sex:{
146                 printf("性别修改为:"); 
147                 scanf("%c",&sex);
148                 s->peo[locate].sex=sex;
149                 break;
150             }
151             case Adress:{
152                 printf("住址修改为:"); 
153                 scanf("%s",adr);
154                 strcpy(s->peo[locate].adress,adr);
155                 break;
156             }
157             case Phone:{
158                 printf("\n电话修改为:"); 
159                 scanf("%s",pho);
160                 strcpy(s->peo[locate].phone,pho);
161                 break;
162             }
163             default:
164                 printf("\n选择错误!");
165                 goto choose    ;
166         }    
167         printf("\nSuccessful !");
168     }     
169     else{
170         printf("\n无法修改!"); 
171     }
172 }
173 void LoadFile(MailList s){
174     int i=0;
175     FILE *rfp=fopen("MailList.bin","rb");  //读取二进制文件 
176     if(rfp == NULL){
177         perror("open faild");
178         exit(1); 
179     } 
180     struct Person tmp = {0};
181     while(fread(&tmp,sizeof(struct Person),1,rfp)){
182       s->peo[i] = tmp;    
183        i++;    
184     }
185     if(0 == i){
186         printf("空文件!");
187     }
188     else{
189         s->ExistNum = i;
190         Show_member(s);
191     }
192     fclose(rfp);
193 } 
194 void SaveToFile(MailList s){
195     FILE *wfp=fopen("MailList.bin","wb");   //为文件写入二进制流 
196     if(wfp == NULL){
197         perror("NULL ");
198         exit(1);
199     }
200     for(int i=0;i<s->ExistNum;++i){
201         fwrite(&s->peo[i],sizeof(struct Person),1,wfp);
202     }
203     printf("Save Success!");
204     fclose(wfp);
205 } 
206 int main(){
207                              
208     ML mail;
209     mail.ExistNum = 0;    //初始化为0 
210     int option;
211     printf("  \t****************** MailList **********************\n");
212     printf("  \t* 1--Input Information      2--Delet Information * \n");
213     printf("  \t* 3--Search Information     4--Show  Information *\n");
214     printf("  \t* 5--Sort                   6--Clear Information *\n");
215     printf("  \t* 7--Modify Information     8--Save              *\n");
216     printf("  \t* 9--Load Information       0--Eixt              *\n");
217     printf("  \t**************************************************\n");
218     while(1){
219         printf("\nPlease Choose:");
220         scanf("%d",&option);
221         switch(option){
222           case 1:
223           {    
224             int num; 
225             printf("\n录入人数:");scanf("%d",&num);
226             Add_member(&mail,num);
227           }
228           break;    
229            case 2:{
230                char na[10];
231                printf("\n输入删除人名:");
232                scanf("%s",na);
233                Delet_member(&mail,na);
234                break;
235             }
236            case 3:{
237                char na[10];
238                printf("\n输入查找人名:");
239                scanf("%s",na);
240                int ret=Research_member(&mail,na);
241                if(ret<0)
242                  printf("\n没找到!");
243                else
244                  printf("\n找到了!位置在:%d",ret); 
245                break;
246              }
247             case 4:
248                 Show_member(&mail);
249                 break;
250             case 5:
251                 Sort_member(&mail);        
252                  break;
253             case 6:
254                 Clear_member(&mail);
255                 break;
256             case 7:
257                 Modify_member(&mail);
258                 break;  
259             case 8:
260                 SaveToFile(&mail);
261                 break;
262             case 9:
263                 LoadFile(&mail);
264                 break;
265             case 0:
266                 exit(0);
267                 break; 
268             default:
269                 break ;
270         }
271    }
272     return 0;
273 } 

比较挫版本。

posted @ 2017-12-07 10:31  tp_16b  阅读(335)  评论(0编辑  收藏  举报