![]() |
#2
op1232022-06-14 21:02
|

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
/*定义通讯录结构体*/
typedef struct record
{
char name[30];
char phonenumber[11];
char address[3][30]; //定义?维 0-省 1-市 2-街道
char email[6];
struct record *next;
} record;
void mainmenu();//声明通讯录主菜单
void alterstring(record *head);
void browsemenu(record *head, const int *total);//浏览通讯录主菜单
record *newrecord(record *head, int *total);//添加联系?信息
record *deleterecord(record *head,int *total);//删除联系?信息
record *modifyrecord(record *head);//修改联系?信息
record *searchrecord(record *head, int onlyonerecord) ;//查找联系?信息
record *importrecords(record *head, int *total);//导?联系?信息
record *exportrecords(record *head);//导出联系?信息
/*定义主函数执?通讯录功能*/
int main()
{
char mima[10]={0};
int i=0;
printf("请输?密码:\n");
for(i=0;i<3;i++)
{
scanf("%s",mima);
if(strcmp(mima,"123456")==0)
{
printf("登录成功,亲爱的?猪崽!\n");
break;
}
else
{
printf("密码错误,笨熊!请重新输?密码:\n");
}
}
if(3==i)
{
printf("登录失败!?笨熊\n");
exit(1);
}
system("pause");
int total = 0, choice;
record *head = NULL;
printf("\n\t\t\t 欢迎使?通讯录系统!\n");
printf("\n\t\t\t********************************************\n");
do
{
mainmenu();
scanf("%d", &choice);
system("cls");
switch (choice)
{
case 0:
break;
case 1:
browsemenu(head, &total);
break;
case 2:
head = newrecord(head, &total);
break;
case 3:
head = deleterecord(head,&total);
break;
case 4:
head = modifyrecord(head);
break;
case 5:
searchrecord(head,0);
break;
case 6:
head = importrecords(head,&total);
break;
case 7:
exportrecords(head)
break;
default:
printf("\n\n**对不起,输?错误,请输?0~7\n");
};
}
while (choice != 0);
return 0;
}
/*通讯录界?*/
void mainmenu()
{
printf("\n");
printf("\n\t\t\t****************1.浏览通讯录****************\n");
printf("\n\t\t\t**************2.增加联系?信息**************\n");
printf("\n\t\t\t**************3.删除联系?信息**************\n");
printf("\n\t\t\t**************4.修改联系?信息**************\n");
printf("\n\t\t\t**************5.查找联系?信息**************\n");
printf("\n\t\t\t*************6.从?件中导?记录*************\n");
printf("\n\t\t\t*************7.从记录导出到?件*************\n");
printf("\n\t\t\t********************0.退出******************\n");
printf("\n\t\t\t********************************************\n");
printf("\n\t\t\t请输?0~7选择功能 :");
}
/*定义链表?地址,遇到回车键跳转下?个成员*/
void alterstring(record *head) {
int m;
record *p1 = head;
while (p1 != NULL)
{
for (m = 0; m < 30; m++)
{
if (*((p1->name) + m) == '\n')
{
*((p1->name) + m) = '\0';
}
}
for (m = 0; m < 11; m++)
{
if (*((p1->phonenumber) + m) == '\n')
{
*((p1->phonenumber) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[0]) + m) == '\n')
{
*((p1->address[0]) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[1]) + m) == '\n')
{
*((p1->address[1]) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[2]) + m) == '\n')
{
*((p1->address[2]) + m) = '\0';
}
}
for (m = 0; m < 6; m++)
{
if (*((p1->email) + m) == '\n')
{
*((p1->email) + m) = '\0';
}
}
p1 = p1->next;
}
}
/*添加联系?信息*/
record *newrecord(record *head, int *total) //链表?地址,总数地址
{
int i = *total;
char inputchar;
record *p = head, *input = (record *) malloc(sizeof(record));
printf("\n**请输?联系?信息\n");
/*如果已经有联系?信息,则输出现有的所有联系?信息 */
/*如果已经有联系?信息,则输出现有的所有联系?信
if (*total)
{
printf("**共有 %d 个联系?信息\n\n", *total);
}
do
{
//输?联系?信息
printf("请输?第%d个联系?的名字:", i + 1);
fflush(stdin);//清理标准输?流,把多余未被保存的数据丢掉
fgets(input->name, 31, stdin);//输?长度为31的字符串
printf("请输?第%d个联系?的联系?式:", i + 1);
fflush(stdin);
fgets(input->phonenumber,31, stdin);
printf("请输?第%d个联系?的家庭地址:\n", i + 1);
printf("*请输?第%d个联系?所在省份:", i + 1);
fflush(stdin);
fgets(input->address[0], 31, stdin);
printf("*请输?第%d个联系?所在城市:", i + 1);
fflush(stdin);
fgets(input->address[1], 31, stdin);
printf("*请输?第%d个联系?所在街道:", i + 1);
fflush(stdin);
fgets(input->address[2], 31, stdin);
printf("请输?第%d个联系?的电?邮件:", i + 1);
fflush(stdin);
fgets(input->email, 7, stdin);
input->next = NULL; //插?时放?链表的最后
//插?数据,分为?数据和??数据
if (head == NULL)
{
head = input;
p = input;
}
else
{
while (p->next != NULL
{
p = p->next;
}
p->next = input;
}
(*total)++;//计数-联系?的总?数
printf("\n**是否继续?(Y/N):");
scanf(" %c", &inputchar);
/*如果?getchar只能输??写Y才可以继续*/
if (inputchar=='Y' || inputchar=='y')
{
input = (record *) malloc(sizeof(record));//创建新的空间
i++;
}
else
{
break;
}
}
while (1);
//按回车键跳转
alterstring(head);
return head;
}
void browsemenu(record *head, const int *total)
{
int page = 1, firstindex = 0, i, pageamount = *total / 10 + 1;//定义联系?为?页
record *p = head;
do
{
system("cls");
/*输?页?的页数,不能过?或过?*/
if (page > pageamount)
{
printf("**对不起,页数的最?值为%d\n", pageamount);
}
else if (page < 0)
{
printf("**对不起,输?的数字必须为正数\n");
}
else
{
//处理分页,?个联系??页
firstindex = 10 * (page - 1);
printf("NO.\t姓名\t联系电话\t省\t市\t街道\t电?邮件\t\n");
//处理前置数据
p = head;
for (i = 0; i < firstindex; ++i)
{
p = p->next;
}
i = 0;
//输出数据
while (p!=NULL && i<10)
{
i++;
printf("NO.%d\t%s\t%s\t\t%s\t%s\t%s\t%s\t\n", i+firstindex,p->name, p->phonenumber, p->address[0], p->address[1],
p->address[2],
p->email);
p = p->next;
}
printf("** Page %d (共 %d 页)\n ", page, pageamount);
}
printf("** 请输?跳转页?(按0返回通讯录主菜单):");
scanf("%d", &page);
}
while (page);
}
/*删除联系?信息*/
record *deleterecord(record *head,int *total)
{
record *p1 = head, *p2,*searchrestlt;
searchrestlt = searchrecord(head, 1);
while (p1 != NULL && p1 != searchrestlt)
{
p2 = p1; //p2的上?个节点
p1 = p1->next; //p1的下?个节点
}
if (p1 == head)
{
head = p1->next;
free(p1);
(*total)--;
printf("\n**删除成功!\n");
}
else if (p1 != NULL)
{
p2->next = p1->next;
free(p1);
(*total)--;
printf("\n* *删除成功!\n");
}
else
{
printf("\n**对不起,没有找到该联系?!\n");
}
return head;
}
//输出联系?信息
void printonerecord(record *p)
{
printf("姓名:%s\t联系电话:%s\t省:%s\t市:%s\t街道::%s\t电?邮件:%s\t\n", p->name, p->phonenumber,
p->address[0], p->address[1], p->address[2], p->email);
}
/*修改联系?信息*/
record *modifyrecord(record *head)
{
record *p1 = head, *p2,*searchrestlt,*input = (record *) malloc(sizeof(record));
//返回需要修改的数组地址
searchrestlt = searchrecord(head, 1);
if (!searchrestlt)
{
return head;
}
//输?联系?信息
printf("\n请输?修改的联系?姓名:");
fflush(stdin);
fgets(input->name, 30 + 1, stdin);
printf("请输?修改的联系?的联系电话:");
fflush(stdin);
fgets(input->phonenumber,30 + 1, stdin);
printf("请输?修改的联系?的地址:\n");
printf("请输?修改的联系?的省份:");
fflush(stdin);
fgets(input->address[0], 30 + 1, stdin);
printf("请输?修改的联系?的城市:");
fflush(stdin);
fgets(input->address[1], 30 + 1, stdin);
printf("请输?修改的联系?的街道:");
fflush(stdin);
fgets(input->address[2], 30 + 1, stdin);
printf("请输?修改的联系?的电?邮件:");
fflush(stdin);
fgets(input->email, 7, stdin);
//插?时放于链表的最后
input->next = NULL;
while (p1 != NULL && p1 != searchrestlt)
{
p2 = p1; //p2上?个节点
p1 = p1->next; //p1下?个节点
}
if (p1 == head)
{
head = input;
input->next = p1->next;
free(p1);
printf("\n**修改成功!\n");
}
else if (p1 != NULL)
else if (p1 != NULL)
{
p2->next = input;
input->next = p1->next
{
p2->next = input;
input->next = p1->next;
free(p1);
printf("\n**修改成功!\n");
}
else
{
printf("\n-- Do not find this id!\n");
}
alterstring(head);
return head;
}
/*查找联系?信息*/
record *searchrecord(record *head, int onlyonerecord)
{
int amount = 0, i = 0, choice = 0; //i,p1循环变量
char input[30];
record *p1 = head, *results[100] = {NULL}; //result是record类型的指针数组
printf("\n查找联系?:");
setbuf(stdin, NULL);//关闭输?缓冲区
fgets(input, 30 + 1, stdin);
for (i = 0; i < 30; ++i)
{
if (*((input) + i) == '\n')
{
*((input) + i) = '\0';
}
}
//遍历搜索
while (p1 != NULL)
{
if (strstr(p1->name, input) || //strstr()判断是否为?串
strstr(p1->phonenumber, input) ||
strstr(p1->address[0], input)
strstr(p1->address[1], input) ||
strstr(p1->address[2], input) ||
strstr(p1->email, input)) {
results[amount] = p1;
amount++;
}
p1 = p1->next;
}
//若有同名同信息,根据编号选择联系?
if (amount > 1)
{
printf("\n查找结果:\n");
for (i = 0; i < amount; i++)
{
printf("NO.%d\t", i + 1);
printonerecord(results[i]);
}
if (!onlyonerecord)
{
return NULL; //如果不需要去重,则返回NULL
}
printf("\n**请输?你要删除的联系?编号: ");
scanf("%d", &choice);
//若输?联系?编号不正确,默认删除第?位联系?
if (choice-1>amount || choice<0)
{
printf("\n**输?错误(默认删除第?位联系?)");
return results[0];
}
return results[choice - 1];
}
else if (!amount)
{
printf("\n**对不起,没有找到该联系?!");
return NULL;
}
else
{
printf("\n** 查找结果:\n");
printonerecord(results[0]);
return results[0];
}
}
#include<stdlib.h>
#include<string.h>
#include<conio.h>
/*定义通讯录结构体*/
typedef struct record
{
char name[30];
char phonenumber[11];
char address[3][30]; //定义?维 0-省 1-市 2-街道
char email[6];
struct record *next;
} record;
void mainmenu();//声明通讯录主菜单
void alterstring(record *head);
void browsemenu(record *head, const int *total);//浏览通讯录主菜单
record *newrecord(record *head, int *total);//添加联系?信息
record *deleterecord(record *head,int *total);//删除联系?信息
record *modifyrecord(record *head);//修改联系?信息
record *searchrecord(record *head, int onlyonerecord) ;//查找联系?信息
record *importrecords(record *head, int *total);//导?联系?信息
record *exportrecords(record *head);//导出联系?信息
/*定义主函数执?通讯录功能*/
int main()
{
char mima[10]={0};
int i=0;
printf("请输?密码:\n");
for(i=0;i<3;i++)
{
scanf("%s",mima);
if(strcmp(mima,"123456")==0)
{
printf("登录成功,亲爱的?猪崽!\n");
break;
}
else
{
printf("密码错误,笨熊!请重新输?密码:\n");
}
}
if(3==i)
{
printf("登录失败!?笨熊\n");
exit(1);
}
system("pause");
int total = 0, choice;
record *head = NULL;
printf("\n\t\t\t 欢迎使?通讯录系统!\n");
printf("\n\t\t\t********************************************\n");
do
{
mainmenu();
scanf("%d", &choice);
system("cls");
switch (choice)
{
case 0:
break;
case 1:
browsemenu(head, &total);
break;
case 2:
head = newrecord(head, &total);
break;
case 3:
head = deleterecord(head,&total);
break;
case 4:
head = modifyrecord(head);
break;
case 5:
searchrecord(head,0);
break;
case 6:
head = importrecords(head,&total);
break;
case 7:
exportrecords(head)
break;
default:
printf("\n\n**对不起,输?错误,请输?0~7\n");
};
}
while (choice != 0);
return 0;
}
/*通讯录界?*/
void mainmenu()
{
printf("\n");
printf("\n\t\t\t****************1.浏览通讯录****************\n");
printf("\n\t\t\t**************2.增加联系?信息**************\n");
printf("\n\t\t\t**************3.删除联系?信息**************\n");
printf("\n\t\t\t**************4.修改联系?信息**************\n");
printf("\n\t\t\t**************5.查找联系?信息**************\n");
printf("\n\t\t\t*************6.从?件中导?记录*************\n");
printf("\n\t\t\t*************7.从记录导出到?件*************\n");
printf("\n\t\t\t********************0.退出******************\n");
printf("\n\t\t\t********************************************\n");
printf("\n\t\t\t请输?0~7选择功能 :");
}
/*定义链表?地址,遇到回车键跳转下?个成员*/
void alterstring(record *head) {
int m;
record *p1 = head;
while (p1 != NULL)
{
for (m = 0; m < 30; m++)
{
if (*((p1->name) + m) == '\n')
{
*((p1->name) + m) = '\0';
}
}
for (m = 0; m < 11; m++)
{
if (*((p1->phonenumber) + m) == '\n')
{
*((p1->phonenumber) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[0]) + m) == '\n')
{
*((p1->address[0]) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[1]) + m) == '\n')
{
*((p1->address[1]) + m) = '\0';
}
}
for (m = 0; m < 30; m++)
{
if (*((p1->address[2]) + m) == '\n')
{
*((p1->address[2]) + m) = '\0';
}
}
for (m = 0; m < 6; m++)
{
if (*((p1->email) + m) == '\n')
{
*((p1->email) + m) = '\0';
}
}
p1 = p1->next;
}
}
/*添加联系?信息*/
record *newrecord(record *head, int *total) //链表?地址,总数地址
{
int i = *total;
char inputchar;
record *p = head, *input = (record *) malloc(sizeof(record));
printf("\n**请输?联系?信息\n");
/*如果已经有联系?信息,则输出现有的所有联系?信息 */
/*如果已经有联系?信息,则输出现有的所有联系?信
if (*total)
{
printf("**共有 %d 个联系?信息\n\n", *total);
}
do
{
//输?联系?信息
printf("请输?第%d个联系?的名字:", i + 1);
fflush(stdin);//清理标准输?流,把多余未被保存的数据丢掉
fgets(input->name, 31, stdin);//输?长度为31的字符串
printf("请输?第%d个联系?的联系?式:", i + 1);
fflush(stdin);
fgets(input->phonenumber,31, stdin);
printf("请输?第%d个联系?的家庭地址:\n", i + 1);
printf("*请输?第%d个联系?所在省份:", i + 1);
fflush(stdin);
fgets(input->address[0], 31, stdin);
printf("*请输?第%d个联系?所在城市:", i + 1);
fflush(stdin);
fgets(input->address[1], 31, stdin);
printf("*请输?第%d个联系?所在街道:", i + 1);
fflush(stdin);
fgets(input->address[2], 31, stdin);
printf("请输?第%d个联系?的电?邮件:", i + 1);
fflush(stdin);
fgets(input->email, 7, stdin);
input->next = NULL; //插?时放?链表的最后
//插?数据,分为?数据和??数据
if (head == NULL)
{
head = input;
p = input;
}
else
{
while (p->next != NULL
{
p = p->next;
}
p->next = input;
}
(*total)++;//计数-联系?的总?数
printf("\n**是否继续?(Y/N):");
scanf(" %c", &inputchar);
/*如果?getchar只能输??写Y才可以继续*/
if (inputchar=='Y' || inputchar=='y')
{
input = (record *) malloc(sizeof(record));//创建新的空间
i++;
}
else
{
break;
}
}
while (1);
//按回车键跳转
alterstring(head);
return head;
}
void browsemenu(record *head, const int *total)
{
int page = 1, firstindex = 0, i, pageamount = *total / 10 + 1;//定义联系?为?页
record *p = head;
do
{
system("cls");
/*输?页?的页数,不能过?或过?*/
if (page > pageamount)
{
printf("**对不起,页数的最?值为%d\n", pageamount);
}
else if (page < 0)
{
printf("**对不起,输?的数字必须为正数\n");
}
else
{
//处理分页,?个联系??页
firstindex = 10 * (page - 1);
printf("NO.\t姓名\t联系电话\t省\t市\t街道\t电?邮件\t\n");
//处理前置数据
p = head;
for (i = 0; i < firstindex; ++i)
{
p = p->next;
}
i = 0;
//输出数据
while (p!=NULL && i<10)
{
i++;
printf("NO.%d\t%s\t%s\t\t%s\t%s\t%s\t%s\t\n", i+firstindex,p->name, p->phonenumber, p->address[0], p->address[1],
p->address[2],
p->email);
p = p->next;
}
printf("** Page %d (共 %d 页)\n ", page, pageamount);
}
printf("** 请输?跳转页?(按0返回通讯录主菜单):");
scanf("%d", &page);
}
while (page);
}
/*删除联系?信息*/
record *deleterecord(record *head,int *total)
{
record *p1 = head, *p2,*searchrestlt;
searchrestlt = searchrecord(head, 1);
while (p1 != NULL && p1 != searchrestlt)
{
p2 = p1; //p2的上?个节点
p1 = p1->next; //p1的下?个节点
}
if (p1 == head)
{
head = p1->next;
free(p1);
(*total)--;
printf("\n**删除成功!\n");
}
else if (p1 != NULL)
{
p2->next = p1->next;
free(p1);
(*total)--;
printf("\n* *删除成功!\n");
}
else
{
printf("\n**对不起,没有找到该联系?!\n");
}
return head;
}
//输出联系?信息
void printonerecord(record *p)
{
printf("姓名:%s\t联系电话:%s\t省:%s\t市:%s\t街道::%s\t电?邮件:%s\t\n", p->name, p->phonenumber,
p->address[0], p->address[1], p->address[2], p->email);
}
/*修改联系?信息*/
record *modifyrecord(record *head)
{
record *p1 = head, *p2,*searchrestlt,*input = (record *) malloc(sizeof(record));
//返回需要修改的数组地址
searchrestlt = searchrecord(head, 1);
if (!searchrestlt)
{
return head;
}
//输?联系?信息
printf("\n请输?修改的联系?姓名:");
fflush(stdin);
fgets(input->name, 30 + 1, stdin);
printf("请输?修改的联系?的联系电话:");
fflush(stdin);
fgets(input->phonenumber,30 + 1, stdin);
printf("请输?修改的联系?的地址:\n");
printf("请输?修改的联系?的省份:");
fflush(stdin);
fgets(input->address[0], 30 + 1, stdin);
printf("请输?修改的联系?的城市:");
fflush(stdin);
fgets(input->address[1], 30 + 1, stdin);
printf("请输?修改的联系?的街道:");
fflush(stdin);
fgets(input->address[2], 30 + 1, stdin);
printf("请输?修改的联系?的电?邮件:");
fflush(stdin);
fgets(input->email, 7, stdin);
//插?时放于链表的最后
input->next = NULL;
while (p1 != NULL && p1 != searchrestlt)
{
p2 = p1; //p2上?个节点
p1 = p1->next; //p1下?个节点
}
if (p1 == head)
{
head = input;
input->next = p1->next;
free(p1);
printf("\n**修改成功!\n");
}
else if (p1 != NULL)
else if (p1 != NULL)
{
p2->next = input;
input->next = p1->next
{
p2->next = input;
input->next = p1->next;
free(p1);
printf("\n**修改成功!\n");
}
else
{
printf("\n-- Do not find this id!\n");
}
alterstring(head);
return head;
}
/*查找联系?信息*/
record *searchrecord(record *head, int onlyonerecord)
{
int amount = 0, i = 0, choice = 0; //i,p1循环变量
char input[30];
record *p1 = head, *results[100] = {NULL}; //result是record类型的指针数组
printf("\n查找联系?:");
setbuf(stdin, NULL);//关闭输?缓冲区
fgets(input, 30 + 1, stdin);
for (i = 0; i < 30; ++i)
{
if (*((input) + i) == '\n')
{
*((input) + i) = '\0';
}
}
//遍历搜索
while (p1 != NULL)
{
if (strstr(p1->name, input) || //strstr()判断是否为?串
strstr(p1->phonenumber, input) ||
strstr(p1->address[0], input)
strstr(p1->address[1], input) ||
strstr(p1->address[2], input) ||
strstr(p1->email, input)) {
results[amount] = p1;
amount++;
}
p1 = p1->next;
}
//若有同名同信息,根据编号选择联系?
if (amount > 1)
{
printf("\n查找结果:\n");
for (i = 0; i < amount; i++)
{
printf("NO.%d\t", i + 1);
printonerecord(results[i]);
}
if (!onlyonerecord)
{
return NULL; //如果不需要去重,则返回NULL
}
printf("\n**请输?你要删除的联系?编号: ");
scanf("%d", &choice);
//若输?联系?编号不正确,默认删除第?位联系?
if (choice-1>amount || choice<0)
{
printf("\n**输?错误(默认删除第?位联系?)");
return results[0];
}
return results[choice - 1];
}
else if (!amount)
{
printf("\n**对不起,没有找到该联系?!");
return NULL;
}
else
{
printf("\n** 查找结果:\n");
printonerecord(results[0]);
return results[0];
}
}