#include "stdio.h"
#include "string.h"
struct student
{
int number;
char name[10];
float math,chinese,english,average;
};
int tj[10]={0}; //储存各分数段人数
struct student input() //输入初始化结构体
{
//int i;
struct student p;
//for(i=0;i<=10;i++) p.name[i]='\0';
memset(p.name,0,10);
printf("number:"); scanf("%d",&p.number);
printf("name:"); scanf("%s",p.name);
printf("math:"); scanf("%f",&p.math);
printf("chinese:"); scanf("%f",&p.chinese);
printf("english:"); scanf("%f",&p.english);
p.average=(p.chinese+p.english+p.math)/3;
return p;
}
void chaxun(int n_0,int n,struct student *p) //查询 n_0是查询的学号 n是总人数,*p是输入结构体数组地址
{
int i;
for(i=0;i<n;i++)
if(p[i].number==n_0) //匹配学号,判断课程大小输出
{
if(p[i].math>=p[i].chinese&&p[i].chinese>=p[i].english)
printf("math:%.1f chinese:%.1f english:%.1f\n",p[i].math,p[i].chinese,p[i].english);
else if(p[i].math>=p[i].chinese&&p[i].english>p[i].chinese)
printf("math:%.1f english:%.1f chinese:%.1f\n",p[i].math,p[i].english,p[i].chinese);
else if(p[i].chinese>=p[i].math&&p[i].math>=p[i].english)
printf("chinese:%.1f math:%.1f english:%.1f\n",p[i].chinese,p[i].math,p[i].english);
else if(p[i].chinese>=p[i].math&&p[i].english>p[i].math)
printf("chinese:%.1f english:%.1f math:%.1f\n",p[i].chinese,p[i].english,p[i].math);
else if(p[i].english>=p[i].math&&p[i].math>=p[i].chinese)
printf("english:%.1f math:%.1f chinese:%.1f\n",p[i].english,p[i].math,p[i].chinese);
else
printf("english:%.1f chinese:%.1f math:%.1f\n",p[i].english,p[i].chinese,p[i].math);
i=n;
}
}
void show(struct student * p,int n) //降序输出,n是总人数,*p是输入结构体数组地址
{
int i,j,t;
struct student mid; //排序
for(i=0;i<n-1;i++)
{ t=i;
for(j=1;j<n;j++)
{ if(p[i].average<p[j].average)
t=j;
}
if(i!=t)
{
mid=p[j];
p[i]=p[j];
p[j]=mid;
}
}
for(i=0;i<n;i++) //输出
printf("number:%2d name:%s math:%.1f chinese:%.1f english:%.1f average:%.1f\n",(p+i)->number,(p+i)->name,(p+i)->math,(p+i)->chinese,(p+i)->english,(p+i)->average);
}
void tongji_0(float a) //按分数计算人数
{
if(a>=90) {tj[9]++;}
else if(a>=80) tj[8]++;
else if(a>=70) tj[7]++;
else if(a>=60) tj[6]++;
else tj[5]++;
}
void tongji( struct student *p,int n) // 统计各分数段人数,输出,是总人数,*p是输入结构体数组地址
{
int i;
for(i=0;i<n;i++)
tongji_0(p[i].math);
printf("math:\n60以下%d\n60~70:%d\n70~80:%d\n80~90:%d\n90以上:%d\n",tj[5],tj[6],tj[7],tj[8],tj[9]);
for(i=5;i<10;i++) tj[i]=0;// tj清零
for(i=0;i<n;i++)
tongji_0(p[i].chinese);
printf("chinese:\n60以下%d\n60~70:%d\n70~80:%d\n80~90:%d\n90以上:%d\n",tj[5],tj[6],tj[7],tj[8],tj[9]);
for(i=5;i<10;i++) tj[i]=0;//tj清零
for(i=0;i<n;i++)
tongji_0(p[i].english);
printf("english:\n60以下%d\n60~70:%d\n70~80:%d\n80~90:%d\n90以上:%d\n",tj[5],tj[6],tj[7],tj[8],tj[9]);
}
void main()
{
int n,i,x,xh,a=1;
printf("total number of student:");
scanf("%d",&n);
struct student qq[100]; //定义结构体数组
for(i=0;i<n;i++) //初始化结构体
{
printf("number student %d\n",i+1);
qq[i]=input();
}
while(a)
{
printf("[1]按总平均成绩降序输出\n[2]按学号查询学生信息\n[3]统计不同课程分数段人数\n[4]退出\n");
printf("请输入1~4调用菜单:");
scanf("%d",&x);
switch(x)
{
case 1 : show(qq,n);break;
case 2 : printf("输入学号:");scanf("%d",&xh); chaxun(xh,n,qq); break;
case 3 : tongji(qq,n); break;
case 4 : a=0;break;
default: printf("请输入1~4\n"); break;
}
}
}
在vc6上编译的,程序我调试了好久有时第一个人的信息输出会出现乱码原因我也不知道,如果有高手知道的号麻烦指点下小弟!不胜感激!
我按楼上大哥的建议,另外我想用gets()代替可是 编译时程序运行时总是跳过name的输入,麻烦楼上再解释下。。
不好意思楼主,我在vc编译的时候没报错,我也不知道是怎么回事!
温馨提示:答案为网友推荐,仅供参考