| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 301 人关注过本帖
标题:小弟是菜鸟,请高手帮我看看这个程序哪里有问题
只看楼主 加入收藏
sardine_20
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-30
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:1 
小弟是菜鸟,请高手帮我看看这个程序哪里有问题
按照书上给的题,结果总是出错,请各位大侠帮着看看哪步出了问题,改证一下,多谢,周一学校就要了,小弟在线等结果!~


        #include "stdio.h"
                         #include "stdlib.h"
                         #include "string.h"
                         #include "ctype.h"
                         #define M 500
                         typedef struct
                         { char name[20];
                           char email[20];
                           char units[20];
                           char tele[10];
                         }ADDRESS;
                         int enter(ADDRESS t[]);
                         void list(ADDRESS t[],int n);
                         void search(ADDRESS t[],int n);
                         int delete(ADDRESS t[],int n);
                         int add(ADDRESS  t[],int n);
                         void save(ADDRESS t[],int n);
                         int load(ADDRESS t[]);
                         void display(ADDRESS t[]);
                         void sort(ADDRESS t[],int n);
                         void qseek(ADDRESS t[],int n);
                         void print(ADDRESS temp);
                         int find(ADDRESS t[],int n,char *s);
                         int menu();
                         main()
                         {  int i;
                            ADDRESS adr[M];
                            int length;
                            clrscr();
                            for(;;)
                            {  switch(menu())
                               { case 0:length=enter(adr);break;
                                 case 1:list(adr,length);break;
                                 case 2:search(adr,length);break;
                                 case 3:length=delete(adr,length);break;
                                 case 4:length=add(adr,length);break;
                                 case 5:save(adr,length);break;
                                 case 6:length=load(adr);break;
                                 case 7:display(adr);break;
                                 case 8:sort(adr,length);break;
                                 case 9:qseek(adr,length);break;
                                 case 10:exit(0);
                                }
                              }
                           }
                           menu()
                           {  char s[80];
                              int c;
                              gotoxy(1,25);
                              printf("press any key enter menu...\n");
                              getch();
                              clrscr();
                              gotoxy(1,1);
                              printf("**********MENU********\n\n");
                              printf("       0.Enter record\n");
                              printf("       1.Browse the file\n");
                              printf("       2.Search record on name\n");
                              printf("       3.Delete a record\n");
                              printf("       4.add record\n");
                              printf("       5.Save the file\n");
                              printf("       6.Load the file\n");
                              printf("       7.display record on order\n");
                              printf("       8.sort to make new file\n");
                              printf("       9.Quick seek record\n");
                              printf("       10.Quit\n");
                              printf("****************************\n");
                              do
                              {printf("\n Enter you choice(0~10):");
                                 scanf("%s",s);
                                 c=atoi(s);
                               }while(c<0||c>10);
                               return c;
                               }
                               int enter(ADDRESS t[])
                               {  int i,n;
                                  char *s;
                                  clrscr();
                                  printf("\nplease input num \n");
                                  scanf("%d",&n);
                                  printf("please input record \n");
                                  printf("name   e-mail  unit  telephone\n");
                                  printf("------------------------------------\n");
                                  for(i=0;i<n;i++)
                                  {   scanf("%s%s%s%s",t[i].name,t[i].email,t[i].units,t[i].tele);
                                      printf("----------------------------------------------\n");
                                   }
                                   return n;
                                }
                                void list(ADDRESS t[],int n)
                                {  int i;
                                   clrscr();
                                   printf("\n\n********************ADDRESS*****************\n");
                                   printf("name       e-mail         unit         telephone\n");
                                   printf("-----------------------------------------------------------\n");
                                   for(i=0;i<n;i++)
                                   printf("%-20s%-20s%-20s%-10s\n",t[i].name,t[i].email,t[i].units,t[i].tele);
                                   if((i+1)%10==0)
                                   {  printf ("Press any key continue...\n");
                                      getch();
                                    }
                                    printf("********end*******\n");
                                 }
                                 void search (ADDRESS t[],int n)
                                 {  char s[20];
                                    int i;
                                    clrscr();
                                    printf("Please enter the name that you want to search:\n");
                                    scanf("%s",s);
                                    i=find(t,n,s);
                                    if(i>n-1)
                                      printf("Can not find the name who you want!\n");
                                    else
                                      printf(t[i]);
                                  }

                                 void print(ADDRESS temp)
                                 { clrscr();
                                   printf("\n\n***********************************\n");
                                   printf("name    e-mail     unit       telephone\n");
                                   printf("-----------------------------------------\n");
                                   printf("%-20s%-20s%-20s%-10s\n",temp.name,temp.email,temp.units,
                                   temp.tele);
                                   printf("***************end**************\n");
                                 }

                                 int find(ADDRESS t[],int n,char *s)
                                 { int i;
                                   for(i=0;i<n;i++)
                                   {  if(strcmp(s,t[i].name)==0)
                                      return i;
                                   }
                                   return i;
                                 }
                                 int delete(ADDRESS t[],int n)
                                 { char s[20];
                                   int ch=0;
                                   int i,j;
                                   printf("please deleted name\n");
                                   scanf("%s",s);
                                   i=find(t,n,s);
                                   if(i>n-1)
                                      printf("no found not deleted\n");
                                   else
                                   {  printf(t[i]);
                                      printf("Are you sure delete it(1/0)\n");
                                      scanf("%d",&ch);
                                      if(ch==1)
                                      {   for(j=i+1;j<n;j++)
                                          {strcpy(t[j-1].name,t[j].name);
                                           strcpy(t[j-1].email,t[j].email);
                                           strcpy(t[j-1].units,t[j].units);
                                           strcpy(t[j-1].tele,t[j].tele);
                                           }
                                        n--;
                                        }
                                     }
                                     return n;
                                 }
                                 int add(ADDRESS t[],int n)
                                 {  ADDRESS temp;
                                    int i,j;
                                    char s[20];
                                    printf("please input record\n");
                                    printf("************************\n");
                                    printf("name   email  unit   telephone\n");
                                    printf("-----------------------------------\n");
                                    scanf("%s%s%s%s",temp.name,temp.email,temp.units,temp.tele);
                                    printf("----------------------------------------\n");
                                    printf("please input locate name \n");
                                    scanf("%s",s);
                                    i=find(t,n,s);
                                    for(j=n-1;j>=i;j--)
                                    {  strcpy(t[j+1].name,t[j].name);
                                       strcpy(t[j+1].email,t[j].email);
                                       strcpy(t[j+1].units,t[j].units);
                                       strcpy(t[j+1].tele,t[j].tele);
                                    }
                                    strcpy(t[i].name,temp.name);
                                    strcpy(t[i[.email,temp.email);
                                    strcpy(t[i].units,temp.units);
                                    strcpy(t[i].tele,temp.tele);
                                    n++;
                                    return n;
                                  }
                                  void save(ADDRESS t[],int n)
                                  { int i;
                                    FILE *fp;
                                    if((fp=fopen("record.txt","wb"))==NULL)
                                    {  printf("can not open file\n");
                                       exit(1);
                                    }
                                    printf("\nSaving file\n");
                                    fprintf(fp,"%d",n);
                                    fprintf(fp,"\r\n");
                                    for(i=0;i<n;i++)
                                    {  fprintf(fp,"%-20s%-20s%-20s%-10s",t[i].name,
                                       t[i].email,t[i].units,t[i].tele);
                                       fprintf(fp,"\r\n"); /
                                     }
                                     fclose(fp);
                                     printf("****save success***\n");
                                  }
                                  int load(ADDRESS t[])
                                  {  int i,n;
                                     FILE *fp;
                                     if((fp=fopen("record.txt","rb"))==NULL) /
                                     {  printf("can not open file\n");
                                        exit(1);
                                      }
                                      fscanf(fp,"%d",&n);
                                      for(i=0;i<n;i++)
                                          fscanf(fp,"%20s%20s%20s%10s",t[i].name,t[i].email,
                                          t[i].units,t[i].tele);
                                      fclose(fp);
                                      printf("You have success read data from file!!!\n");
                                      return n;
                                  }
                                  void display(ADDRESS t[])
                                  {  int id,n;
                                     FILE *fp;
                                     if((fp=fopen("record.txt","rb"))==NULL)
                                     {  printf("can not open file\n");
                                        exit(1);
                                     }
                                     printf("Enter order number...\n");
                                     scanf("%d",&id);
                                     fscanf(fp,"%d",&n)
                                     if(id>=0&&id<n)
                                     {  fseek(fp,(id-1)*sizeof(ADDRESS),1);
                                        print(t[id]);
                                        printf("\r\n");
                                     }
                                     else
                                        printf("no %d number record!!!\n ",id);
                                     fclose(fp);
                                   }
                                   void sort(ADDRESS t[],int n)
                                   {  int i,j,flag;
                                      ADDRESS temp;
                                      for(i=0;i<n;i++)
                                      {  flag=0;
                                         for(j=0;j<n-1;j++)
                                         if((strcmp(t[j].name,t[j+1].name))>0)
                                         {  flag=1;
                                            strcpy(temp.name,t[j].name);
                                            strcpy(temp.email,t[j].email);
                                            strcpy(temp.units,t[j].units);
                                            strcpy(temp.tele,t[j].tele);
                                            strcpy(t[j].name,t[j+1].name);
                                            strcpy(t[j].email,t[j+1].email);
                                            strcpy(t[j].units,t[j+1].units);
                                            strcpy(t[j].tele,t[j+1].tele);
                                            strcpy(t[j+1].name,temp.name);
                                            strcpy(t[j+1].email,temp.email);
                                            strcpy(t[j+1].units,temp.units);
                                            strcpy(t[j+1].tele,temp.tele);
                                         }
                                         if(flag==0)break;
                                      }
                                      printf("sort success!!!\n");
                                    }
                                    void qseek(ADDRESS t[],int n)
                                    {  char s[20];
                                       int l,r,m;
                                       printf("\nPlease sort before qseek!\n");
                                       printf("please enter name for qseek\n");
                                       scanf("%s",s);
                                       l=0;r=n-1;
                                       while(l<=r)
                                       {  m=(l+r)/2;
                                          if(strcmp(t[m].name,s)==0)
                                          {   print(t[m]);
                                              return ;
                                           }
                                           if(strcmp(t[m].name,s)<0)
                                             l=m+1;
                                           else
                                             r=m-1;
                                        }
                                        if(l>r)
                                           printf("not found\n");
                                    }
2009-08-30 10:06
UserYuH
Rank: 12Rank: 12Rank: 12
来 自:毅华
等 级:火箭侠
威 望:8
帖 子:720
专家分:3300
注 册:2009-8-10
收藏
得分:50 
add函数里的
strcpy(t[i[.email,temp.email);/*括号[]有个反了。*/
·
save函数里的多了两个多余的'/'斜杆。
·
display函数里fscanf(fp,"%d",&n)语句后少了';'分号。
·
search函数里和delete函数里的printf(t[i]);语句错误。
·
·
改了这些就可以运行了,剩下的就靠你自己去调试了。你编的程序如有结果不正确,你最清楚哪里出错。

努力—前进—变老—退休—入土
2009-08-30 10:37
快速回复:小弟是菜鸟,请高手帮我看看这个程序哪里有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022666 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved