weixin_44421208 2019-01-06 12:44
浏览 473

为什么记录一次会出现重复的数据

#include
#include
#include //定义杂项函数及内存分配函数 如malloc、free、system、atoi、atol、rand、exit等
#include
#include
//#define NULL 0
#define LEN sizeof(struct message)

struct message //定义结构体
{
char name[15];
char number[15];
char phone[15];
char qq[13];
char addr[25];
struct message *next; //指向下一数据

};

int n;

void creat() //建立电话簿
{
int num;
char choice[5]={'2'};
FILE *fp;
struct message *head;
struct message *p,*q;
n = 0;
p = q = (struct message *) malloc(LEN);
system("title 建立电话簿 ");//设置标题
system("mode con cols=84 lines=30");//设置窗口大小
system("color 5f");
fp=fopen("通讯录1.txt","wb");
system("cls");
printf("\n请输入姓名:");

scanf("%s",p->name);
printf("\n请输入电话号码:");
scanf("%s",p->phone);
printf("\n请输入学号:");
scanf("%s",p->number);
printf("\n请输入QQ:");
scanf("%s",p->qq);
printf("\n请输入住址:");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);

head = 0;
num = 1;
while(num!=0)
    {
        n = n+1;
        if(n==1) head = p; 
        else q->next = p;
        q = p;
        p = (struct message *) malloc(LEN);                //分配存储区

        while(choice[0]!='1' && choice[0]!='0')                     //判断是否继续
        {
            printf("\n1 继续输入\n 0 返回\n");
            printf("请选择操作:");          
            scanf("%s",choice);

        }

        switch(choice[0])
        {
            case '1':     
                   {    system("title 建立电话簿 ");//设置标题
                        system("mode con cols=84 lines=30");//设置窗口大小
                        system("color 5f");
                        fp=fopen("通讯录1.txt","rb"); 
                        system("cls");
                        printf("\n请输入姓名:");                
                        scanf("%s",p->name);
                        printf("\n请输入电话号码:");
                        scanf("%s",p->phone);
                        printf("\n请输入学号:");
                        scanf("%s",p->number);
                        printf("\n请输入QQ:");
                        scanf("%s",p->qq);
                        printf("\n请输入住址:");
                        scanf("%s",p->addr);
                        fwrite(p,LEN,1,fp); 
                        fclose(fp);
                        choice[0] = '2';
                   }    continue;

            case '0': num = 0;
                    system("cls");
        }
    }
q->next = 0;

}

void add() //添加通讯录
{
int num;
char choice[5]={'2'};
FILE *fp;
struct message *head;
struct message *p,*q;
n = 0;
p = q = (struct message *) malloc(LEN); //分配空间
system("title 添加通讯录 ");//设置标题
system("mode con cols=84 lines=30");//设置窗口大小
system("color 5f");
fp=fopen("通讯录1.txt","ab");
system("cls");
printf("\n请输入姓名:"); //输入内容
scanf("%s",p->name);
printf("\n请输入电话号码:");
scanf("%s",p->phone);
printf("\n请输入学号:");
scanf("%s",p->number);
printf("\n请输入QQ:");
scanf("%s",p->qq);
printf("\n请输入住址:");
scanf("%s",p->addr);
fwrite(p,LEN,1,fp);
fclose(fp);

head = 0;
num = 1;
while(num!=0)
    {
        n = n+1;
        if(n==1) head = p; 
        else q->next = p;
        q = p;
        p = (struct message *) malloc(LEN);                       //分配空间

        while(choice[0]!='1' && choice[0]!='0')                           //判断是否继续 
        {
            printf("\n1 继续输入\n 0 返回\n");
            printf("请选择操作:");          
            scanf("%s",choice);             
        }

        switch(choice[0])
        {
            case '1':     
                   {    
                        fp=fopen("通讯录1.txt","rb"); 
                        system("cls");
                        printf("\n请输入姓名:");                
                        scanf("%s",p->name);
                        printf("\n请输入电话号码:");
                        scanf("%s",p->phone);
                        printf("\n请输入学号:");
                        scanf("%s",p->number);
                        printf("\n请输入QQ:");
                        scanf("%s",p->qq);
                        printf("\n请输入住址:");
                        scanf("%s",p->addr);
                        fwrite(p,LEN,1,fp); 
                        fclose(fp);
                        choice[0] = '2';
                   }    continue;

            case '0': num = 0;
                    system("cls");
        }
    }
q->next = 0;

}

void findname(struct message * fphead) //按姓名查找
{
struct message *p;
int a = 0;
char findname[20];

system("cls");
printf("请输入要查找的姓名:\n");
scanf("%s",findname);


for(p=fphead->next;p!=NULL;p=p->next)
{
    if(strcmp(p->name,findname)==0)
    {
        printf("姓名            电话号码           学号            QQ              住址\n");
        printf("%-15s %-15s %-15s %-13s %-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
        a = 1; 
    }         
}
if(a != 1)
    printf("无此通讯录!\n");     

}
void findphone(struct message * fphead) //按电话查找
{
struct message *p;
int a = 0;
char findphone[15];

system("cls");
printf("请输入要查找的电话:\n");
scanf("%s",findphone);


for(p=fphead->next;p!=NULL;p=p->next)
{
    if(strcmp(p->phone,findphone)==0)
    {
        printf("姓名            电话号码           学号            QQ              住址\n");
        printf("%-15s %-15s %-15s %-13s %-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
        a = 1; 
    }         
}
if(a != 1)
    printf("无此通讯录!\n");     

}

void findnumber(struct message * fphead) //按学号查找
{
struct message *p;
int a = 0;
char findnumber[15];

system("cls");
printf("请键入要查找的学生学号:\n");
scanf("%s",findnumber);


for(p=fphead->next;p!=NULL;p=p->next)
{
    if(strcmp(p->number,findnumber)==0)
    {
        printf("姓名            电话号码           学号            QQ              住址\n");
        printf("%-15s %-15s %-15s %-13s %-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
        a = 1; 
    }         
}
if(a != 1)
    printf("无此通讯录!\n");     

}

void modify(struct message * fphead) //修改记录
{
FILE *fp;
struct message *p,*q;
int a = 0;
char findname[15];
system("title 查询通讯录 ");//设置标题
system("mode con cols=84 lines=30");//设置窗口大小
system("color 5f");
system("cls");
printf("请键入要查找的姓名:\n");
scanf("%s",findname);

for(p=fphead->next;p!=NULL;p=p->next)
{

    if(strcmp(p->name,findname)==0)
    {

        printf("姓名            电话号码           学号          QQ         住址\n");
        printf("%-15s %-15s %-15s %-13s %-25s\n\n\n\n",p->name,p->phone,p->number,p->qq,p->addr);

        printf("\n请键入新姓名:");               
        scanf("%s",p->name);
        printf("\n请键入新电话号码:");
        scanf("%s",p->phone);
        printf("\n请键入新学号:");
        scanf("%s",p->number);
        printf("\n请键入新QQ:");
        scanf("%s",p->qq);
        printf("\n请键入新住址:");
        scanf("%s",p->addr);

        fp = fopen("通讯录2.txt","wb");
        q = fphead->next;                     //P为链表头指针 
        while(q!=0)
        {
            fwrite(q,LEN,1,fp);
            if(q->next!=0)
                q = q->next;
            else 
                q = 0;   
        }
        fclose(fp);
        unlink("通讯录1.txt");                //用新文件覆盖原文件 
        rename("通讯录2.txt", "通讯录1.txt");
        printf("修改成功!");      
        a = 1; 
    }         
}
if(a != 1)
    printf("无此通讯录!\n");     

}

void del(struct message * fphead) //删除记录
{
FILE *fp,*fq;
struct message *p,*q,*r;
char sname[15];
p = q = (struct message *) malloc(LEN);
p = fphead;
system("title 删除记录 ");//设置标题
system("mode con cols=84 lines=30");//设置窗口大小
system("color 5f");
system("cls");
fp = fopen("通讯录1.txt","rb");
fq = fopen("通讯录2.txt","wb");
printf("请输入欲删除记录的姓名:");
scanf("%s",sname);

while(strcmp(p->name,sname)!=0 && p->next!=0)  //查找节点并删除该节点 
{
    q = p;
    p = p->next;
}
if(strcmp(p->name,sname)==0)                    
{
    if(p==fphead) 
        fphead = p->next;
    else 
    {
        q->next = p->next;
    }

    r = fphead->next;                          //r为链表头指针 
    while(r!=0)
    {
        fwrite(r,LEN,1,fq);
        if(r->next!=0)
            r = r->next;
        else 
            r = 0;   
    }
    printf("删除成功!");             
}
else
{
    printf("无此通讯录!");    
}

fclose(fp);
fclose(fq);
unlink("通讯录1.txt");                            //用新文件覆盖原文件 
rename("通讯录2.txt", "通讯录1.txt");             

}

struct message * open(void) //打开文件,载入链表
{
FILE *fp;
struct message *p=NULL,*q=NULL,*temp=NULL,*fphead=NULL;
fp=fopen("通讯录1.txt","rb");
fphead=(struct message *)malloc(LEN); //分配空间
temp = q = fphead;

while(! feof(fp))                            //循环读取,载入链表 
{
    p=(struct message *)malloc(LEN);
    temp=q;
    q->next=p;
    q=p;
    fread(p,LEN,1,fp);
}
temp->next=NULL;
fclose(fp);
return(fphead);                              //返回链表头指针     

}

void print() //显示通讯录
{
struct message *p;
FILE *fp;
system("title 显示通讯录 ");//设置标题
system("mode con cols=84 lines=30");//设置窗口大小
system("color 5f");
system("cls");

if((fp=fopen("通讯录1.txt","rb"))!=NULL)
{
    printf("姓名              电话号码            学号            QQ          住址\n");
    p = (struct message *)malloc(LEN);                   //分配空间
    while(!feof(fp))
    { 
        fread(p,LEN,1,fp);  
        printf("%-15s %-15s %-15s %-13s %-25s\n",p->name,p->phone,p->number,p->qq,p->addr);
    }
    fclose(fp); 
}
else
{
     printf("未建立通讯录!");    
}

}

main() //主函数
{

FILE * fp;
struct message * fphead;
int begin = 1,f;
char choice[5];

while(begin == 1)
{
    system("title 欢迎使用学生通讯录");//设置标题
    system("mode con cols=84 lines=30");//设置窗口大小
    system("color 5f");
    printf("\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    printf("\t\t\t\t欢迎使用学生通讯录\n");
    printf("\t\t<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n");
    printf("\t\t\t*主窗口菜单*\n\n");
    printf("\t\t\t1:显示通讯录中所有记录;\n\n");
    printf("\t\t\t2:建立新通讯录;\n\n");
    printf("\t\t\t3:添加新记录;\n\n");
    printf("\t\t\t4:查询通讯录中的记录;\n\n");
    printf("\t\t\t5:修改通讯录中的记录;\n\n");
    printf("\t\t\t6:删除通讯录中的记录;\n\n");
    printf("\t\t\t0:退出通讯录\n\n");

    printf("请选择操作:");
    scanf("%s",choice);

    switch(choice[0])
    {
        case '1':
               {
                   print();
                   printf("按任意键返回主菜单!\n");  
                   getch();
                   system("cls");
               }   continue;
        case '2':
               {
                   creat();
               }   continue;

        case '3':
               {
                   add();     
               }   continue;
        case '4':
               {
                   if((fp = fopen("通讯录1.txt","rb")) != NULL)
                   {
                        fclose(fp);
                        system("title 查询通讯录 ");//设置标题
                        system("mode con cols=84 lines=30");//设置窗口大小
                        system("color 5f");  
                        system("cls");
                        printf("1按姓名查询;\n2按电话查询;\n3按学号查询;\n");
                        printf("请输入查询方式:");
                        scanf("%d",&f);
                        if(f==1)
                        {
                           fphead = open();
                           findname(fphead);
                        }
                       if(f==2)
                       {
                           fphead = open();
                           findphone(fphead);
                       }  
                       else if(f==3)
                       {
                           fphead = open();
                           findnumber(fphead);
                       }    
                   }
                   else
                   {
                       system("cls");
                       printf("未建立通讯录!");
                   }
                   printf("按任意键返回主菜单!\n");  
                   getch();
                   system("cls");
               }   continue;
        case '5':
               {
                   if((fp = fopen("通讯录1.txt","rb")) != NULL)
                   {
                       fclose(fp);
                       fphead = open();
                       modify(fphead);
                   }
                   else
                   {
                       system("cls");
                       printf("未建立通讯录!");
                   }
                   printf("按任意键返回主菜单!\n");  
                   getch();
                   system("cls");
               }continue;                   
        case '6':
               {
                   if((fp = fopen("通讯录1.txt","rb")) != NULL)
                   {
                       fclose(fp);
                       fphead = open();
                       del(fphead);
                   }
                   else
                   {
                       system("cls");
                       printf("未建立通讯录!");
                   }
                   printf("按任意键返回主菜单!\n");  
                   getch();
                   system("cls");
               }   continue;


        case '0': exit(0);


        default:
                {
                    system("cls");
                    printf("输入错误,按任意键返回主菜单!\n");
                    getch();
                    system("cls");
                }
    }

}
system("pause");

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 反编译微信小游戏求指导
    • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
    • ¥15 学不会递归,理解不了汉诺塔参数变化
    • ¥30 软件自定义无线电该怎样使用
    • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
    • ¥15 Jenkins+k8s部署slave节点offline
    • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
    • ¥15 WPF使用Canvas绘制矢量图问题
    • ¥15 用三极管设计一个单管共射放大电路
    • ¥15 孟德尔随机化r语言运行问题