switch case的覆盖问题

王政 发布于 2012/06/02 09:16
阅读 592
收藏 0

我设计了一个小程序里面有一个switch……

main()
{
 int choose=0;
 PER *student=NULL;
 student=(PER *)malloc(sizeof(PER));
 student->id=NULL;
 student->last=NULL;
 student->next=NULL;
 do{
  printf("\n0.exit");
  printf("\n1.read the data from file(like c:\\example.data)");
  printf("\n2.read the data by keyboard");
  printf("\n3.output the max and min");
  printf("\n4.output the average");
  printf("\n5.output the statistics report");
  printf("\n6.save the data");
  printf("\nplease input your choose:");
  scanf("%d",&choose);
  switch(choose)
  {
   case 0:break;
   case 1:
    {
     creat_file();
     if(student->next)free(student);
     load_data(student);
     if(fclose(File))out_err(0);
     break;
    }
   case 2:
    {
     /*if(!(student->next))free(student);*/
     printf("\nread the data by keyboard until the id is \"NULL\"");
     read_data(student);
     break;
    }
   case 3:
    {
     max=0,min=100;
     if(student->next)max_min(student);
     else out_err(1);
     break;
    }
   case 4:
    {
     sum=0,student_num=0;
     if(student->next)average(student);
     else out_err(1);
     break;
    }
   case 5:
    {
     excellent=0,good=0,ok=0,pass=0,not_pass=0;
     if(student)statistics(student);
     else out_err(1);
     break;
    }
   case 6:
    {
     if(student->next)
     {
      creat_file();
      save_data(student);
      if(fclose(File))out_err(0);
     }
     else out_err(1);
     break;
    }
   default:
    {
     out_err(2);
     break;
    }
  }
 }while(choose);
}

问题在于,每次choose我输入4……他都显示当case=3的时候的结果……

运行环境是vs2010,其余相关如下

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct person
{
 unsigned score;
 char *id;
 struct person *last;
 struct person *next;
}PER;
void out_err(int num)
{
 switch(num)
 {
  case 1:
   {
    printf("\nplease input the data");
    break;
   }
  case 2:
   {
    printf("\nchoose error,please input your choose again");
    break;
   }
  case 3:
   {
    printf("\nthe file has been existting");
    system("pause");
    exit(0);
   }
  default:
   {
    printf("\nundefined error");
    system("pause");
    exit(0);
   }
 }
}
unsigned max,min,sum,student_num,excellent,good,ok,pass,not_pass;
FILE *File=NULL;
void read_data(PER *student)
{
 student->next=NULL;
 printf("\nthe id is:");
 student->id=(char *)malloc(sizeof(char));
 scanf("%s",student->id);
 if(strcmp(student->id,"NULL"))
 {
  printf("\nthe score is:");
  scanf("%d",&(student->score));
  student->next=(PER *)malloc(sizeof(PER));
  student->next->last=student;
  read_data(student->next);
 }
 else student->next=NULL;
}
void scan_id(PER *student,unsigned x)
{
 if(student)
 {
  if(student->score==x)printf("\n%s",student->id);
  scan_id(student->last,x);
 }
}
void max_min(PER *student)
{
 if(student->next)
 {
  if(student->score>max)max=student->score;
  if(student->score<min)min=student->score;
  max_min(student->next);
 }
 else
 {
  printf("\nthe max is %d and their id:",max);
  scan_id(student,max);
  printf("\nthe min is %d and their id:",min);
  scan_id(student,min);
 }
}
void average(PER *student)
{
 if(student->next)
 {
  sum+=student->score;
  student_num++;
  max_min(student->next);
 }
 else printf("\nthe average is %f:",(float)sum/student_num);
}
char *num2char(unsigned x)
{
 char *y=(char *)malloc(sizeof(char));
 if(x>=10000)
 {
  *y=48+x/10000;
  x-=x/10000;
  *(y+1)=48+x/1000;
  x-=x/1000;
  *(y+2)=48+x/100;
  x-=x/100;
  *(y+3)=48+x/10;
  x-=x/10;
  *(y+4)=48+x;
 }
 else if(x>=1000)
 {
  *y=48+x/1000;
  x-=x/1000;
  *(y+1)=48+x/100;
  x-=x/100;
  *(y+2)=48+x/10;
  x-=x/10;
  *(y+3)=48+x;
 }
 else if(x>=100)
 {
  *y=48+x/100;
  x-=x/100;
  *(y+1)=48+x/10;
  x-=x/10;
  *(y+2)=48+x;
 }
 else if(x>=10)
 {
  *y=48+x/10;
  x-=x/10;
  *(y+1)=48+x;
 }
 return y;
}
unsigned char2num(char *x)
{
 unsigned i=0,y=0;
 for(;*(x+i);i++)
 {
  y*=10;
  y+=*(x+i)-48;
 }
 return y;
}
void statistics(PER *student)
{
 if(student->next)
 {
  if(student->score>=90)excellent++;
  else if(student->score>=80)good++;
  else if(student->score>=70)ok++;
  else if(student->score>=60)pass++;
  else not_pass++;
  statistics(student->next);
 }
 else
 {
  printf("\nthe statistics report:");
  printf("\nthe excllent:%d",excellent);
  printf("\nthe good:%d",good);
  printf("\nthe ok:%d",ok);
  printf("\nthe pass:%d",pass);
  printf("\nthe not pass:%d",not_pass);
 }
}
void creat_file()
{
 char *filename=NULL;
 printf("\nread your path:");
 scanf("%s",filename);
 if(!(File=fopen(filename,"r")))
 {if(!(File=fopen(filename,"w")))out_err(0);}
 else
 {
  fclose(File);
  out_err(3);
 }
}
void save_data(PER *student)
{
 if(student->next)
 {
  fputs(student->id,File);
  fputc('\n',File);
  fputs(num2char(student->score),File);
  save_data(student->next);
 }
}
void load_data(PER *student)
{
 char *character=(char *)malloc(sizeof(char));
 long i=0,j=0;
 student->next=NULL;
 do
 {
  fread(character+i,sizeof(char),1,File);
  i++;
 }while(character+i);
 strcpy(student->id,character);
 j=i;
 do
 {
  fread(character+i,sizeof(char),1,File);
  i++;
 }while(character+i);
 student->score=char2num(character+j+1);
 student->next=(PER *)malloc(sizeof(PER));
 student->next->last=student;
 load_data(student->next);
}

 

加载中
0
xinzaibing
xinzaibing
这个问题用VS单步调试一下就可以解决了吧....?
0
王政
王政

引用来自“xinzaibing”的答案

这个问题用VS单步调试一下就可以解决了吧....?

这个……调试了……不懂那些代码嗨……给个明确答案呗……

0
xinzaibing
xinzaibing
公司的里不方便干私活...今晚我回去有空看看吧
0
王政
王政

引用来自“xinzaibing”的答案

公司的里不方便干私活...今晚我回去有空看看吧

谢谢

返回顶部
顶部