首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

兼套施用Switch不能传值,求解

2013-04-09 
兼套使用Switch不能传值,求解。#includestdio.h#includestdlib.h#includestring.h#define N 2struct

兼套使用Switch不能传值,求解。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 2
struct Student
{
int number;
char name[20];
float chinese,math,english;
};
void login();
void display_menu();
void showMenu();
int inputInfo(struct Student student[],int length);
int outputInfo(struct Student student[],int length);
int statistic(struct Student student[],int length);
int bubblesort(struct Student student[],int length);
int main()
{
login();
int choice;char name[20];
struct Student student[N];
display_menu();
start:
printf("Please input your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:inputInfo(student,N);break;
case 2:output(student,N);break;
case 3:queryInfo(student,name);break;
case 4:statistic(student,N);break;
case 5:exit(0);
default:printf("Wrong input!");break;
}
goto start;
return 0;
}
void login()    
{
int password;
printf("请输入口令:");
scanf("%d",&password);
if (password==123||password==111)
printf("The key is right,Welcome to use this system !\n");
else
printf("The key is wrong,Please check it and try again!\n");
}
void display_menu()
{
printf("= = = = = = 学生成绩管理系统= = = = = =\n");
printf("=                                     =\n");
printf("=               1  录入               =\n");
printf("=               2  输出               =\n");
printf("=               3  查询               =\n");
printf("=               4  汇总统计           =\n");
printf("=               5  结束               =\n");
printf("=                                     =\n");
printf("= = = = = =       HCG       = = = = = =\n");

}
int inputInfo(struct Student student[],int length)
{
int i;
printf("Please input the student's Infomation(enter 0 to end):\n");
for(i=0;i<length;i++)
{
fflush(stdin);
printf("学号:");
scanf("%d",&student[i].number);
if(student[i].number==0)break;
printf("Name:");
scanf("%s",student[i].name);
printf("Chinese:");


scanf("%f",&student[i].chinese);
printf("Math:");
scanf("%f",&student[i].math);
printf("English:");
scanf("%f",&student[i].english);
}
}
int output(struct Student student[],int length)
{
int i;
for(i=0;i<N;i++)
{
printf("Student number:%d\n",student[i].number);
printf("Name:%s\n",student[i].name);
printf("Chinese score:%f\n",student[i].chinese);
printf("Math score:%f\n",student[i].math);
printf("English score:%f\n",student[i].english);
}
}
int queryInfo(struct Student find[],char n[])
{
int i;
puts("Please enter the students name you want to find:");
scanf("%s",n);
for(i=0;i<N;i++)
{
if(strcmp(n,find[i].name)==0)
{
printf("student number:%d \n name:%s \n chinese score:%f \n math score:%f \n English score:%f",find[i].number,
find[i].name,find[i].chinese,find[i].math,find[i].english);
}
if(i>=N)puts("Not exist!\n"); 
}
}
int statistic(struct Student student[],int length)
{
int choice;
showMenu();
start:
printf("Please input your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:{bubblesort(student,5);break;}
case 4:exit(0);
default:printf("Haven't developed yet !\n");break;
}
goto start;
}
int bubblesort(struct Student student[],int length)
{
int i,max,temp,n;
for(i=0;i<length;i++)
{
for(max=0;max<length;max++)
{
if(student[i].chinese>student[max].chinese)
{
temp=student[i].number;
student[i].number=student[max].number;
student[max].number=temp;
}
}
}
for(n=0;i<length;i++)
{
printf("%d",student[i].number); 
}
}
void showMenu()
{
printf("= = = = = = 学生成绩管理系统= = = = = =\n");
printf("=                                     =\n");
printf("=               1  语文成绩           =\n");
printf("=               2  数学成绩           =\n");
printf("=               3  英语成绩           =\n");
printf("=               4  结束               =\n");
printf("=                                     =\n");
printf("= = = = = =       HCG       = = = = = =\n");
}


录入后选择4的汇总统计,再选择1的语文成绩进行冒泡排序。bubblesort函数里的结构体student没有接收录入的值是什么情况??难倒兼套使用switch后值就清零了?还是我哪里弄错了。 struct menu


[解决办法]
跟switch没有关系,你自己断点调试一下
[解决办法]
for(i=0;i<length-1;i++)    
{        
for(max=0;max<length-i-1;max++)        
{       
if(student[max].chinese>student[max+1].chinese)        
{           
temp=student[max].chinese;            
    student[max].chinese=student[max+1].chinese;           
    student[max+1].chinese=temp;       
}       
}   
}   这是冒泡法,
 for(n=0;i<length;i++)    {    printf("%d",student[i].number);     }你把里面的i改成n或者把n改成i就行了。
其实你的switch没错,还有给你点建议:可以用while代替goto,能少用goto就尽量少用吧

热点排行