C语言程序运行结果不对,跟结构数组有关
#include<stdio.h>structStudent{charname[20];charmajor[20];intEnglish;intMaths;intC;in...
#include<stdio.h>
struct Student
{char name[20];
char major[20];
int English;
int Maths;
int C;
int score;
}stu[5]={{"zhang","通信",98,88,79,0},{"li","网络",67,78,89,0},{"lin","物联网",93,68,85,0},{"sun","网络",98,93,95,0},{"min","通信",88,79,69,0}};
void main()
{
struct Student *p=stu;
struct Student count(struct Student stu[5]);
count(p);
}
struct Student count (struct Student stu[5])
{
int ave[2];
int num1[3];
int grades[3];
int i;
int socre[5];
for(i=0;i<5;i++)
{
stu[i].score=stu[i].English+stu[i].Maths+stu[i].C;
if(stu[i].major=="通信")
{num1[0]++;
grades[0]=grades[0]+stu[i].score;}
else if
(stu[i].major=="网络")
{num1[1]++;
grades[1]=grades[1]+stu[i].score;}
else if
(stu[i].major=="物联网")
{num1[2]++;
grades[2]=grades[2]+stu[i].score;}
ave[i]=grades[i]/num1[i];}
printf("通信专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[0],grades[0],ave[0]);
printf("网络专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[1],grades[1],ave[1]);
printf("物联网专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[2],grades[2],ave[2]);
printf("\n学生成绩输出如下:\n");
for(i=0;i<5;i++)
printf("姓名:%5s\t专业:%5s\t英语成绩:%d\t数学成绩:%d\tC语言成绩:%d\t总成绩:%d\n",stu[i].name,stu[i].major,stu[i].English,stu[i].Maths,stu[i].C,stu[i].score);
}
怎么修改呢~ 展开
struct Student
{char name[20];
char major[20];
int English;
int Maths;
int C;
int score;
}stu[5]={{"zhang","通信",98,88,79,0},{"li","网络",67,78,89,0},{"lin","物联网",93,68,85,0},{"sun","网络",98,93,95,0},{"min","通信",88,79,69,0}};
void main()
{
struct Student *p=stu;
struct Student count(struct Student stu[5]);
count(p);
}
struct Student count (struct Student stu[5])
{
int ave[2];
int num1[3];
int grades[3];
int i;
int socre[5];
for(i=0;i<5;i++)
{
stu[i].score=stu[i].English+stu[i].Maths+stu[i].C;
if(stu[i].major=="通信")
{num1[0]++;
grades[0]=grades[0]+stu[i].score;}
else if
(stu[i].major=="网络")
{num1[1]++;
grades[1]=grades[1]+stu[i].score;}
else if
(stu[i].major=="物联网")
{num1[2]++;
grades[2]=grades[2]+stu[i].score;}
ave[i]=grades[i]/num1[i];}
printf("通信专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[0],grades[0],ave[0]);
printf("网络专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[1],grades[1],ave[1]);
printf("物联网专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[2],grades[2],ave[2]);
printf("\n学生成绩输出如下:\n");
for(i=0;i<5;i++)
printf("姓名:%5s\t专业:%5s\t英语成绩:%d\t数学成绩:%d\tC语言成绩:%d\t总成绩:%d\n",stu[i].name,stu[i].major,stu[i].English,stu[i].Maths,stu[i].C,stu[i].score);
}
怎么修改呢~ 展开
3个回答
展开全部
c语言中判断两个字符串是否相同,用到字符串比较函数strcmp(),不能用==
所以把你程序中所用判断字符串相等的if函数改了即可,如:
struct Student count (struct Student stu[5])
{
int ave[3]; //改后
int num1[3];
int grades[3];
int i;
int socre[5];
for(i=0;i<5;i++)
{
stu[i].score=stu[i].English+stu[i].Maths+stu[i].C;
if(strcmp(stu[i].major,"通信")==0) // 改后是
{num1[0]++;
grades[0]=grades[0]+stu[i].score;}
else if (strcmp(stu[i].major,"网络")==0) //改后
{num1[1]++;
grades[1]=grades[1]+stu[i].score;}
else if (strcmp(stu[i].major,"物联网")==0) //改后
{num1[2]++;
grades[2]=grades[2]+stu[i].score;}
ave[i]=grades[i]/num1[i];}
printf("通信专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[0],grades[0],ave[0]);
printf("网络专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[1],grades[1],ave[1]);
printf("物联网专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[2],grades[2],ave[2]);
printf("\n学生成绩输出如下:\n");
for(i=0;i<5;i++)
printf("姓名:%5s\t专业:%5s\t英语成绩:%d\t数学成绩:%d\tC语言成绩:%d\t总成绩:%d\n",stu[i].name,stu[i].major,stu[i].English,stu[i].Maths,stu[i].C,stu[i].score)
所以把你程序中所用判断字符串相等的if函数改了即可,如:
struct Student count (struct Student stu[5])
{
int ave[3]; //改后
int num1[3];
int grades[3];
int i;
int socre[5];
for(i=0;i<5;i++)
{
stu[i].score=stu[i].English+stu[i].Maths+stu[i].C;
if(strcmp(stu[i].major,"通信")==0) // 改后是
{num1[0]++;
grades[0]=grades[0]+stu[i].score;}
else if (strcmp(stu[i].major,"网络")==0) //改后
{num1[1]++;
grades[1]=grades[1]+stu[i].score;}
else if (strcmp(stu[i].major,"物联网")==0) //改后
{num1[2]++;
grades[2]=grades[2]+stu[i].score;}
ave[i]=grades[i]/num1[i];}
printf("通信专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[0],grades[0],ave[0]);
printf("网络专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[1],grades[1],ave[1]);
printf("物联网专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[2],grades[2],ave[2]);
printf("\n学生成绩输出如下:\n");
for(i=0;i<5;i++)
printf("姓名:%5s\t专业:%5s\t英语成绩:%d\t数学成绩:%d\tC语言成绩:%d\t总成绩:%d\n",stu[i].name,stu[i].major,stu[i].English,stu[i].Maths,stu[i].C,stu[i].score)
追问
这种我试过,什么结果都显示不出来- -
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
优化后的代码
#include<stdio.h>
#include <string.h>
struct Student
{
char name[20];
char major[20];
int English;
int Maths;
int C;
int score;
}stu[5]={
{"zhang","通信",98,88,79,0},
{"li","网络",67,78,89,0},
{"lin","物联网",93,68,85,0},
{"sun","网络",98,93,95,0},
{"min","通信",88,79,69,0} };
void count (struct Student stu[5])
{
int ave[3]={0};//初始化为0
int num1[3]={0};//
int grades[3]={0};//
int i;
for(i=0;i<5;i++)
{
stu[i].score=stu[i].English+stu[i].Maths+stu[i].C;
if( !strcmp(stu[i].major,"通信" ) )//字符串比较
{
num1[0]++;
grades[0]=grades[0]+stu[i].score;
}
else if (!strcmp(stu[i].major,"网络"))//
{
num1[1]++;
grades[1]=grades[1]+stu[i].score;
}
else if (!strcmp(stu[i].major,"物联网"))//
{
num1[2]++;
grades[2]=grades[2]+stu[i].score;
}
else
{
printf("------------\n");
}
//ave[i]=grades[i]/num1[i];
}
for( i=0;i<3;i++ ) //单独计算平均值
ave[i]=grades[i]/num1[i];
printf("通信专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[0],grades[0],ave[0]);
printf("网络专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[1],grades[1],ave[1]);
printf("物联网专业情况如下:\n");
printf("专业人数:%d\t专业总成绩:%d\t专业平均分:%d\n",num1[2],grades[2],ave[2]);
printf("\n学生成绩输出如下:\n");
for(i=0;i<5;i++)
printf("姓名:%5s\t专业:%5s\t英语成绩:%d\t数学成绩:%d\tC语言成绩:%d\t总成绩:%d\n",
stu[i].name,stu[i].major,stu[i].English,stu[i].Maths,stu[i].C,stu[i].score);
}
void main()
{
count(stu);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
struct Student count (struct Student stu[5])
{
int ave[2];
int num1[3];
int grades[3];
为什么第一个数组是ave[2],后面的是[3]????
应该是地址越界了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询