# 通讯录管理系统课程设计

## Qusetion

1：针对手机号的排序问题（即交换结构体）
2：文件写入和调用时乱码的问题
3：界面美化问题和用户体验感优化

#### 一：针对手机号的排序问题（即交换结构体）

``````void swap(person *p){                      //交换结构体
person *temp;
temp = p->next->next;                      //temp=p3
p->next->next = p->next->next->next;       //p3=p4
temp->next = p->next;                      //p4=p2
p->next = temp;                            //p2=p3
}
``````

emmm，你可以试着想想四个杯子在一起放着（p1-p4），然后再拿出一个空的(temp)，把第三个杯子的水倒入空杯子中，然后第四个杯子倒入第三个，第二个倒入第四个，第三个倒入第二个，即完成结构体的交换= = 好了自己理解理解~

``````    while (p2->next->next != NULL){
if (atoi(p2->next->phone)>atoi(p2->next->next->phone)){          //防止输入非int型数据
swap(p2); k++;
}
p2 = p2->next;
}
``````

#### 二：文件写入和调用时乱码的问题

``````void save(person *head){                                                //**保存文件信息**
FILE *fp;
if ((fp = fopen("1.txt", "w")) == NULL){
cout << "The file  1.txt can not be opened." << endl;
return;
}
while (p != NULL){
fprintf(fp, "%s %s %s %s \n", p->name, p->city, p->phone,p->num);
p = p->next;
}
fclose(fp);
}
``````

``````struct person {
char name[20];
char city[20];
char phone[20];
int  num;
struct person *next;
};
``````

``````void duqu(person *head){                                                //**读取文件信息**
FILE *fp;
if ((fp = fopen("1.txt", "r")) == NULL){
return;
}
while (!feof(fp)){
person *p2 = new person;
fscanf(fp, "%s %s %s %d", p2->name, p2->city, p2->phone,p2->num);
while (p->next != NULL){
p = p->next;
}
p->next = p2;
p2->next = NULL;
}
fclose(fp);
}
``````

## 三：界面美化问题和用户体验感优化

``````void duqu(person *head){                                                  //**读取文件信息**
FILE *fp;
if ((fp = fopen("1.txt", "r+")) == NULL){
cout<<"\n\n\n\n\n\n\n                   初次启动 系统加载中";
Sleep(500);cout << "●";Sleep(500);cout << "●";Sleep(500);cout << "●";
Sleep(500);cout << "●";Sleep(500);cout << "●";Sleep(500);cout << "●";
return;
}
else{
while (!(feof(fp))){
person *p2 = new person;
fscanf(fp, "%s %s %s %d\n", &p2->name, &p2->city, &p2->phone, &p2->num);
p->next = p2;
p = p->next;
p->next = NULL;
}
}
fclose(fp);
}
``````

``````    while (p->next != NULL){
if (strcmp(p->next->name, p2->name) == 0){
cout << "此联系人已经存在\n即将返回主窗口" << endl;
Sleep(1000);
return;
}
p = p->next;
}
``````

``````        while (p3->next != NULL){
if (strcmp(p3->next->name, p2->name) == 0){
cout << "与其他联系人姓名重复 即将返回主菜单";
Sleep(1000);
return;
}
p3 = p3->next;
}
``````

Wpsec