首页 > 用户发贴区 > 编程问题提问区 > 需检查一个c程序的
2008
05-27


用vc++检查的,发现不少错误


哪位高手能帮忙全改下么?


新学的c,很多不明白!


 


#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define LEN sizeof(struct student)


struct student
{
    int num ;
    char name[16];
    char grade[10];
    char class[10];
    int English;
    int Math;
    int Physics;
    int Physical;
    int Eletric;
    int lisan;
    int cprogram;
    int lawy;
    int public;
    int sixiou;
    int average;
    int total;
    int state;
    struct student *next ;
};


/*========================================================== ===========*/
struct student *save1(struct student *head) /*
保存链表,保存到文件中*/  
{FILE *fp;
 char filepn[20];
 struct student *p; 
 p=(struct student *)malloc(LEN);
 p=head; 
 printf(“Please input the file name!:”); 
 scanf(“%s”,filepn);
 if((fp=fopen(filepn,”w+”))==NULL)
 {
  printf(“cannot open the file !\n”); 
 getch(); 
}
 fprintf(fp,”—————————————— ———————–\n”);
fprintf(fp,”    THE SYSTEM OF STUDENTS’SCORED MANGERMENT,MADE BY ZUEC\n”);
 fprintf(fp,”                   Designed by zuec !\n”);
 fprintf(fp,”—————————————— ———————–\n”);
 /*
打印表格域
*/
 while(p!=NULL)
 {fprintf(fp,”|************************|num: %d  |****************************|”,p->num);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|name:———-|%s                                               “,p->name);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|Class and grade|I’m in class %s grade %s                          “,p->class,p->grade);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|Math:———-|%5.1d           |                                 |”,p->Math);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|English:——-|%5.1d           |                                 |”,p->English);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|Physics:——-|%5.1d           |                                 |”,p->Physics);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|physical:——|%5.1d           |                                 |”,p->Physical);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|Eletric:——-|%5.1d           |                                 |”,p->Eletric);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|lisan:———|%5.1d           |                                 |”,p->lisan);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|cprogram:——|%5.1d           |                                 |”,p->cprogram);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|sixiou:——–|%5.1d           |                                 |”,p->sixiou);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|public:——–|%5.1d           |                                 |”,p->public);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|lawy:———-|%5.1d           |                                 |”,p->lawy);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|total:———|%5.1d           |                                 |”,p->total);
fprintf(fp,”\n|——————————————— ——————–|”);
fprintf(fp,”\n|avrage:——–|%5.1d           |                                 |”,p->average);
fprintf(fp,”\n|——————————————— ——————–|\n”);
 
  p=p->next;   
 
 }
 fclose(fp);
 printf(“The file have been saved!\n”); 
 getch();return(head);
}
/*========================================================== ============*/
void save2(struct student *head)
{FILE *fp;
 char filepn[20];
 struct student *p1;
 p1=head;
 printf(“Please input the file name!:”); 
 scanf(“%s”,filepn);
 if((fp=fopen(filepn,”wb+”))==NULL)
 {
  printf(“cannot open the file !\n”);  
 getch(); 
 }
while(p1!=NULL)
 {if(fwrite(p1,LEN,1,fp)!=1)
  {printf(“file write error!\n”);getch();}
  else p1=p1->next;
 }
  fclose(fp);
 printf(“The file have been saved!\n”); 
 getch();
}


/*========================================================== ============*/
 void print(struct student *head);   /*print
函数的声明*/
struct student *load()
{FILE *fp;
  struct student *p1,*p2,*head;
  int n=0;
  char filepn[20];
 printf(“Please input the file name!:”);
   scanf(“%s”,filepn);
     if((fp=fopen(filepn,”rb”))==NULL)
 {
  printf(“Cannot open the file !\n”); 
  getch();
 }
  printf(“—————————————————- ————–\n”);
  printf(“    THE SYSTEM OF STUDENTS’SCORED MANGERMENT,MADE BY ZUEC \n”);
  printf(“                     Designed by zuec                     \n”);
  printf(“—————————————————- ————–\n”);
 if(!feof(fp))
 { n=n+1; 
  p1=p2=(struct student *)malloc(LEN);
   if(fread(p1,LEN,1,fp)!=1)
   {if(feof(fp))
    printf(“filereaderror!\n”);
   }
  else {p2=p1;print(p1);getch();}
 if(n==1) head=p1;
 else p2->next=p1;
 }
else printf(“File be read error!”);
 p1->next=NULL; 
 return(head);fclose(fp);          



/*========================================================== ==========*/
/*
输入链表的函数*/
struct student *input(struct student *p)
{
     getchar();
    printf(“\nname:”);    gets(p->name);
    printf(“\ngrade:”);    gets(p->grade);
    printf(“\nclass:”);    gets(p->class);
    printf(“\nEnglish:”);    scanf(“%d”,&p->English);
    printf(“\nMath:”);    scanf(“%d”,&p->Math);
    printf(“\nPhysics:”);    scanf(“%d”,&p->Physics);
    printf(“\nPhysical:”);    scanf(“%d”,&p->Physical);
    printf(“\nEletric:”);    scanf(“%d”,&p->Eletric);
    printf(“\nlisan:”);    scanf(“%d”,&p->lisan);
    printf(“\ncprogram:”);    scanf(“%d”,&p->cprogram);
    printf(“\nsixiou:”);    scanf(“%d”,&p->sixiou);
    printf(“\npublic:”);    scanf(“%d”,&p->public);
    printf(“\nlawy:”);    scanf(“%d”,&p->lawy);
    (p->total)=(p->English)+(p->Math)+(p->Physics)+( p->Physical)+(p->Eletric)+(p->lisan)+
              (p->cprogram)+(p->sixiou)+(p->public)+(p->lawy);
    (p->average)=(p->total)/10 ;
     getch();
    return(p); 
}
 
/*========================================================== =============*/
/*
创建链表的函数,返回值为链表的头指针
*/ 
struct student *creat(void)
{
 struct student *head;
 struct student *p1,*p2,*p3,*max;
 int i,j,n,k;
 char t[10];
  n=0;
     p1=p2=p3=(struct student *)malloc(LEN);head=p3;
       printf(“input the number (if enter 0 ,exit!)\n”);
repeat1: printf(“num(num>0):”);
       scanf(“%d”,&p1->num);
     while(p1->num<0)
   {getchar();
    printf(“error! please input num angain:”);
       scanf(“%d”,&p1->num);}


  if(p1->num==0)
   goto end;
  else
  {
  p3=head;
  if(n>0)
   {for(i=0;i<n;i++)
           {if(p1->num!=p3->num)
     p3=p3->next;
     else
     {printf(“The num have exsisted,please intput angain:!\n”);
     goto repeat1;


        }
    }
   }
  }
       p1=input(p1);


head=NULL;
 while(p1->num!=0)
  {
    n=n+1;
 if(n==1)
  head=p1;
 else


    p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(LEN);
    printf(“Please input students’massage,(enter 0 exit)!\n”);
repeat2:printf(“please input the number of one student (num>0):”);
      scanf(“%d”,&p1->num);


   while(p1->num<0)
   {getchar();
    printf(“error!please input angain:”);
       scanf(“%d”,&p1->num);}


  if(p1->num==0)
   goto end;
  else
  {p3=head;
  if(n>0)
   {for(i=0;i<n;i++)
      {if(p1->num!=p3->num)
    p3=p3->next;
    else
    {printf(“the num have exsisted,please input angain:\n”);
     goto repeat2;


    }
    }
   }


  }
      p1=input(p1);


 }


end: p1=head;
     p3=p1;
  for(i=1;i<n;i++)
                {
              for(j=i+1;j<=n;j++)
                  {
       max=p1;
       p1=p1->next;


       if(max->num>p1->num) 
        {
        k=max->num;
        max->num=p1->num;
        p1->num=k;



        }


                  }
    max=head;p1=head;
  }
p2->next=NULL;
       printf(“the num of students you have inputed:%d!\n”,n);
getch();       return(head);


}



/*========================================================== =========*/
/*
插入链表,一定要有返回值*/
struct student *insert(struct student *head)
{
    struct student *p,*p1,*p2 ;
    /*
创建指针,访问链表
*/
    p=(struct student *)malloc(LEN);
    printf(“num:”);
    scanf(“%d”,p->num);
    input(p);
    p1=head ;
    if(head==NULL)  
    {
        head=p ;
        head->next=NULL ;
        return(head);
    }
    while(p->num > p1->num && p1->next!=NULL)
    {
        p2=p1 ;
        p1=p1->next ;
    }
    if(p->num<=p1->num)
    {
        /*
插入的为函数的首部
*/
        if(head==p1)
        {
             p->next=head ;
             head=p ;return(head);
        }
        else
        {
             p2->next=p ;
             p->next=p1 ;
        }
    }
    else
    {
        p1->next=p ;
        p->next=NULL ;
    }
    return(head);
}
/*========================================================== ======*/


/*打印函数*/
void print(struct student *head) 
{
    struct student *p ;
    p=head ;
printf(“|************************|num:    %d|*****************************|”,p->num);
printf(“\n|————————————————- —————-|”);
printf(“\n|name:———-| %s                                                  “,p->name);
printf(“\n|————————————————- —————-|”);
printf(“\n|Class and grade|I’m in class %s grade %s                          “,p->class,p->grade);
printf(“\n|————————————————- —————-|”);
printf(“\n|Math:———-|%5.1d          |                                 |”,p->Math);
printf(“\n|————————————————- —————-|”);
printf(“\n|English:——-|%5.1d          |                                 |”,p->English);
printf(“\n|————————————————- —————-|”);
printf(“\n|Physics:——-|%5.1d          |                                 |”,p->Physics);
printf(“\n|————————————————- —————-|”);
printf(“\n|physical:——|%5.1d          |                                 |”,p->Physical);
printf(“\n|————————————————- —————-|”);
printf(“\n|Eletric:——-|%5.1d          |                                 |”,p->Eletric);
printf(“\n|————————————————- —————-|”);
printf(“\n|lisan:———|%5.1d          |                                 |”,p->lisan);getch();
printf(“\n|————————————————- —————-|”);
printf(“\n|cprogram:——|%5.1d          |                                 |”,p->cprogram);
printf(“\n|————————————————- —————-|”);
printf(“\n|sixiou:——–|%5.1d          |                                 |”,p->sixiou);
printf(“\n|————————————————- —————-|”);
printf(“\n|public:——–|%5.1d          |                                 |”,p->public);
printf(“\n|————————————————- —————-|”);
printf(“\n|lawy:———-|%5.1d          |                                 |”,p->lawy);
printf(“\n|————————————————- —————-|”);
printf(“\n|total:———|%5.1d          |                                 |”,p->total);
printf(“\n|————————————————- —————-|”);
printf(“\n|avrage:——–|%5.1d          |                                 |”,p->average);
printf(“\n|————————————————- —————-|\n”);
    getch();
}
 
/*========================================================== ===========================*/
  
void show(void)  /*
查寻的类型列表
*/ 
{
printf(“\n|————————————————- —————-|”);
printf(“\n|a———-|input the student’s number you want to search        |”);
printf(“\n|————————————————- —————-|”);
printf(“\n|b———-|Show the massage of one student when you enter number|”);
printf(“\n|————————————————- —————-|”);
printf(“\n|c———-|input the grade,class,name to search                   |”);
printf(“\n|————————————————- —————-|”);
printf(“\n|d———-|input average scored you want to search                |”);
printf(“\n|————————————————- —————-|”);
printf(“\n|e———-|exit the choose system                                   |”);
printf(“\n|————————————————- —————-|\n”);
}
   
/*
寻找链表的具体菜单
*/
void find1(struct student *head) ;
void find12(struct student *head);
void find2(struct student *head) ; 
void find3(struct student *head) ;
void search(struct student *head)   
{
  int i=1;
   char z=’a';show();
   while(i)
   {
    switch(z=getchar())
     {case ‘a’ :find1(head);printf(“\nif you want to goto another method,enter(a,b,c,d) or e exit:”); break ; 
      case ‘b’ :find12(head);printf(“\nif you want to goto another method,enter(a,b,c,d) or e exit:”);break ;
      case ‘c’ :find2(head);printf(“\nif you want to goto another method,enter(a,b,c,d) or e exit:”);break ;
      case ‘d’ :find3(head);printf(“\nif you want to goto another method,enter(a,b,c,d) or e exit:”);break ; 
      case ‘e’ :i=0;
     }  
   }
}
void  find1(struct student *head)      /*
方法1,按学号来查找
*/ 
 { struct student *p1,*p2;    
    int num;
    printf(“\nPlease input the student’s number you want to search(enter 0 exit):”); 
    scanf(“%d”,&num);
    while(num!=0)  
    { if(head==NULL) 
      {printf(“\nNot found!”);getch();}
      p1=head;
      while(num!=p1->num&&p1->next!=NULL)
      {p2=p1;p1=p1->next;}
      if(num==p1->num) print(p1); 
       else printf(“\n%d is not found!\n”,num);    
      printf(“please input the num  you want to search(enter 0 exit) :”);  
      scanf(“%d”,&num);
    }
    getch();
}
void find12(struct student *head)
{struct student *p1,*p2;
 int i=4,num;
clrscr();
printf(“|————————————————— —————————|”);
printf(“|num (0)exit|     name      |     grade     |     class     |  total  |average |”);
printf(“|————————————————— —————————|”);
for( ; ;)
{gotoxy(1,i);printf(“|”);
gotoxy(13,i);printf(“|”); gotoxy(29,i);printf(“|”);
gotoxy(45,i);printf(“|”);       gotoxy(61,i);printf(“|”);
gotoxy(71,i);printf(“|”); gotoxy(80,i);printf(“|”);
printf(“|————————————————— —————————|”);
gotoxy(2,i );
scanf(“%d”,&num);
if(num!=0)  
    { if(head==NULL)    exit(0);
      p1=head;
      while(num!=p1->num&&p1->next!=NULL)
      {p2=p1;p1=p1->next;}
      if(num==p1->num)
    {
     gotoxy (14,i);
      printf(“%s”,p1->name);
      gotoxy( 35,i);
      printf(“%s”,p1->grade);
      gotoxy (51,i);
      printf(“%s”,p1->class);
      gotoxy (65,i);
      printf(“%d”,p1->total);
      gotoxy(76,i);
      printf(“%d\n\n”,p1->average);
      i=i+2;
     if(i==20)
{
printf(“|————————————————— —————————|”);
getch();
clrscr();i=4;
printf(“|————————————————— —————————|”);
printf(“|num(enter) |    name  (0)  |   grade  (e)  |  class  (exit)|  total  |average |”);
printf(“|————————————————— —————————|”);
}


   }


    else gotoxy(2,i );  
      scanf(“%d”,&num); 
  }else break;
}      
getch();
       
}     
void find2(struct student *head)    /*
方法2,按年级,班级和姓名来查找*/
  {struct student *find(struct student *head,char *s);
   char s[30],t[30];
   struct student *p1,*find();
   printf(“which you want find student’s message by, name or class or grade?:”);getchar();
   gets(t);
   printf(“\n input his name,grade or class:”);
   gets(s);
       if((p1=find(head,s))==NULL)
       printf(“not found\n”);
       else print(p1);
      }


struct student *find(struct student *head, char *s)
{
    struct student *p1;
    p1=head;
    while(p1)
      {if((!strcmp(s,p1->name))||(!strcmp(s,p1->class))||(!s trcmp(s,p1->grade)))


 return(p1);
       else
 p1=p1->next;
      }
    return(p1);
}


void find3(struct student *head)  /*方法3,按平均分来查找*/
  { struct student *p1,*p2 ;
    int average;
    p2=p1=head;
    if(head==NULL)    printf(“List null!”);
    printf(“\nInput the student’s average scored which you want to search:”); 
    scanf(“%d”,&average);
    while(average!=p1->average && p1->next!=NULL)   
    {   p2=p1 ;
        p1=p1->next ; 
    }
    if(average==p1->average) print(p1);  
  else printf(“\nNot be found!\n”); 
}
/*========================================================== ============*/


/*删除链表,有三种情况*/  
struct student *del(struct student *head) 
{
    struct student *p1,*p2 ; 
    int num ;
    if(head==NULL)
    {
        printf(“The list is emampty\n”);
        return(head);
    }
    printf(“input the num which you want to delete:”);
    scanf(“%d”,&num);
    p1=head ;
    /*
找到接点和搜索的终止条件
*/
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1 ;
        p1=p1->next ;
    }
    if(num==p1->num)
    {
        if(p1==head)head=p1->next ;
        /*
要找的接点在链表的开头
*/
        else p2->next=p1->next ;
        /*
在中间和结尾
*/ 
        free(p1);
        /*
释放无用的空间
*/
    }
   else printf(“\nNot be found !\n”);   
   return(head);       
}


/*菜单选择*/
void menu_selete(void)
{textcolor(9);
 c printf(“|————————————————— —————————|”);
 cprintf(“|———-THE SYSTEM OF STUDENTS’SCORED MANGERMENT,MADE BY ZUEC—————|”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|********************************menu_selete* **********************************|”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|1                 |creat the massage of one student’s scored.                     |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|2                 |insert the massage of one student’s scored.                    |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|3                 |delete the massage of one student’s scored.                    |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|4                 |search the massage of one student’s scored.                    |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|5                 |load the massage of one student’s scored.                      |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|6                 |save1 the massage of the student’s scored(wen ben)           |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|7                 |save2 the massage of the student’s scored(er jing zhi)       |”);
 c printf(“|————————————————— —————————|”);
 cprintf(“|8                 |quit the system of students’ scored manegerment.               |”);
 c printf(“|————————————————— —————————|”);
}


main()
{
struct student *head ;
int flag=1,m;
while(flag)
{
menu_selete();
cprintf(“\nInput your choice:”); 
cscanf(“%d”,&m);  
 switch(m)  
 {
     case 1 :textcolor(9);    clrscr();head=creat();                 break ;
     case 2 :textcolor(9);    clrscr();head=insert(head);          break ;
     case 3 :textcolor(9);    clrscr();head=del(head);               break ;
     case 4 :textcolor(9);    clrscr();search(head);                 break ;
     case 5 :textcolor(9);    clrscr();head=load();                  break ;
     case 6 :textcolor(9);    clrscr();save1(head);                  break ;
     case 7 :textcolor(9);    clrscr();save2(head);                  break ;
     case 8 :textcolor(9);    flag=0;
  } 
}
return(0);  
}  


用vc++检查的,发现不少错误


哪位高手能帮忙全改下么?


新学的c,很多不明白!


 



需检查一个c程序的》有 1 条评论

  1. anDonE 说:

    textcolor gotoxy不是VC++的函数。
    好像是TC下的。
    貌似这个不是你写的吧。

留下一个回复