#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
typedef struct Phone{
char name[20];
char num[10];
char tel[10];
struct Phone *next;
}tp;
int n=0;
tp *create(){ //创建
tp *head,*r,*stu;
int i=0;
char choice;
head=(tp*)malloc(sizeof(tp));
head->next=NULL;
r=head;
do{
stu=(tp*)malloc(sizeof(tp));
printf("\n\n请输入第%d个记录:\n",++i);
printf("\n姓名:");
scanf("%s",stu->name);
printf("\n学号:");
scanf("%s",stu->num);
printf("\n电话号码:");
scanf("%s",stu->tel);
r->next=stu;
r=stu;
n++;
printf("是否继续输入?(Y/N)?");
choice=getche();
}while(choice=='Y'||choice=='y');
r->next=NULL;
return(head);
}
print(tp *head){ //显示
tp *p;
if(head!=NULL){
p=head;
printf("本通讯录现在共有%d人:\n",n);
printf("姓名 学号 电话号码\n");
do{
for(p=head->next;p!=NULL;p=p->next)
printf("%s\t%s %s\n",p->name,p->num,p->tel);
}while(p!=NULL);
}
else
printf("通讯录为空,无法输出!\n");
}
save(tp *h){ //保存
tp *stu;
FILE *fp;
char wenjian[40];
printf("\n请输入要保存的文件名:");
scanf("%s",wenjian);
if((fp=fopen("wenjian","wt"))==NULL){
printf("\n 文件打开失败!\n");
getch();
exit(1);
}
for(stu=h->next;stu!=NULL;stu=stu->next)
fprintf(fp,"%s %s %s\n",stu->name,stu->num,stu->tel);
printf("文件已保存成功,按任意键返回!");
getch();
fclose(fp);
}
tp *insert(tp *head){ // 插入
tp *pnew,*p1,*p2;
char name[20];
p1=head;
printf("请输入增加的内容:\n");
printf("请输入姓名:");
gets(name);
pnew=(tp *)malloc(sizeof(tp));
strcpy(pnew->name,name);
printf("请输入学号:");
gets(p1->num);
printf("请输入电话号码");
gets(p1->tel);
n=n+1;
if(head=NULL){
head=pnew;
pnew->next=NULL;
return (head);
}
else{
while(strcmp(pnew->name,p1->name)>0&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
}
if(strcmp(pnew->name,p1->name)<0||strcmp(pnew->name,p1->name)==0){
if(head==p1){
head=pnew;
}
else{
p2->next=pnew;
}
pnew->next=p1;
}
else{
p1->next=pnew;
pnew->next=NULL;
}
return head;
}
}/*
tp *sort(tp *head){ //排序
tp *p1,*p2;
int i,j;
typedef struct Phone1{
char name[20];
char num[20];
char tel[20];
}tp1;
tp1 sort[200];
tp1 temp;
if(head==NULL){
printf("通讯录为空,无法排序!\n");
return head;
}
p1=head;
for(i=0;i<n;i++){
strcpy(sort[i].name,p1->name);
strcpy(sort[i].num,p1->num);
strcpy(sort[i].tel,p1->tel);
p2=p1;
p1=p1->next;
}//youdongxiyaojia
char c;
if(c=1)
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(strcmp(sort[i].name,sort[j].name)<0){
sort[i]=temp;
temp=sort[j];
sort[j]=temp;
}
}
}
p1=(tp *)malloc(sizeof(tp));
p2=p1;
strcpy(p->name,sort[0].name);
strcpy(p->num,sort[0].num);
strcpy(p->tel,sort[0].tel);
}*/
tp *dele(tp *head){ //删除
tp *q,*p;
char name[20];
if(head==NULL){
printf("通讯录为空,无法删除!\n");
return head;
}
p=head;
printf("请输入要删除的人名:");
gets(name);
n--;
if(strcmp(head->name,name)==0){
head=head->next;
free(p);
printf("删除操作成功!\n");
return head;
}
else{
q=head;
p=head->next;
while(p!=NULL){
if(strcmp(p->name,name)==0){
q->next=p->next;
free(p);
printf("删除操作成功!\n");
return head;
}
p=p->next;
q=q->next;
}
}
}
main()
{
tp *head=NULL;
char num[10];
system("color 3a");
while(1){
printf("\t\t\t---------------------------------\n");
printf("\t\t\t--------请输入选项编号(1-7)----\n");
printf("\t\t\t---------------------------------\n");
printf("\t\t\t-------- 1.创建通讯录 -------\n");
printf("\t\t\t-------- 2.排序 -------\n");
printf("\t\t\t-------- 3.增加 -------\n");
printf("\t\t\t-------- 4.保存通讯录 -------\n");
printf("\t\t\t-------- 5.打开通讯录 -------\n");
printf("\t\t\t-------- 6.删除通讯录 -------\n");
printf("\t\t\t-------- 7.退出 -------\n");
printf("\t\t\t---------------------------------\n");
printf("请输入你选择的操作:");
gets(num);
switch(*num){
case'1':{
head=create();
print(head);
}
case'4':{
save(head);
print(head);
}
case'6':{
head=dele(head);
print(head);
}
}
}
}
还没有完善
最新推荐文章于 2022-01-21 21:37:01 发布