C语言程序设计编程题
有N个学生,每个学生的数据包括学号(num),姓名(name[20]),性别(sex),年龄(age),三门课成绩(score[3])。要求在main函数中输入这几个学生...
有N个学生,每个学生的数据包括学号(num),姓名(name[20]),性别(sex),年龄(age),三门课成绩(score[3])。要求在main函数中输入这几个学生的数据,然后调用一个函数count,在该函数中计算出每个学生的总分和平均分,然后打印出所有各项数据(包括原有的和新求出的)。
展开
2个回答
展开全部
#include <stdio.h>
#define N 100
struct student
{
int num;
char name[20];
int sex;
int age;
float score[3];
};
void count(struct student *stu,int n,float *sum,float *avg)
{
int i=0,j=0;
for(i=0;i<n;i++)
{
sum[i]=0;
for(j=0;j<3;j++)
{
sum[i]+=stu[i].score[j];
}
avg[i]=sum[i]/3;
}
}
void main()
{
struct student stu[N];
int i,n;
float sum[N],avg[N];
printf("请输入学生个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("请输入第%d个学生信息:\n",i+1);
printf("学号:");
scanf("%d",&(stu[i].num));
printf("姓名:");
scanf("%s",&(stu[i].name));
printf("性别(男=0,女=1):");
scanf("%d",&(stu[i].sex));
printf("年龄:");
scanf("%d",&(stu[i].age));
printf("第一门课程成绩:");
scanf("%f",&(stu[i].score[0]));
printf("第二门课程成绩:");
scanf("%f",&(stu[i].score[1]));
printf("第三门课程成绩:");
scanf("%f",&(stu[i].score[2]));
}
count(stu,n,sum,avg);
printf("学号\t姓名\t性别\t年龄\t课程1\t课程2\t课程3\t总分\t平均分\n");
for(i=0;i<n;i++)
{
printf("%5d ",stu[i].num);
printf("%5s ",stu[i].name);
printf("%5s ",stu[i].sex==0?"男":"女");
printf("%6d ",stu[i].age);
printf("%6.2f ",stu[i].score[0]);
printf("%6.2f ",stu[i].score[1]);
printf("%6.2f ",stu[i].score[2]);
printf("%6.2f %6.2f\n",sum[i],avg[i]);
}
}
#define N 100
struct student
{
int num;
char name[20];
int sex;
int age;
float score[3];
};
void count(struct student *stu,int n,float *sum,float *avg)
{
int i=0,j=0;
for(i=0;i<n;i++)
{
sum[i]=0;
for(j=0;j<3;j++)
{
sum[i]+=stu[i].score[j];
}
avg[i]=sum[i]/3;
}
}
void main()
{
struct student stu[N];
int i,n;
float sum[N],avg[N];
printf("请输入学生个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("请输入第%d个学生信息:\n",i+1);
printf("学号:");
scanf("%d",&(stu[i].num));
printf("姓名:");
scanf("%s",&(stu[i].name));
printf("性别(男=0,女=1):");
scanf("%d",&(stu[i].sex));
printf("年龄:");
scanf("%d",&(stu[i].age));
printf("第一门课程成绩:");
scanf("%f",&(stu[i].score[0]));
printf("第二门课程成绩:");
scanf("%f",&(stu[i].score[1]));
printf("第三门课程成绩:");
scanf("%f",&(stu[i].score[2]));
}
count(stu,n,sum,avg);
printf("学号\t姓名\t性别\t年龄\t课程1\t课程2\t课程3\t总分\t平均分\n");
for(i=0;i<n;i++)
{
printf("%5d ",stu[i].num);
printf("%5s ",stu[i].name);
printf("%5s ",stu[i].sex==0?"男":"女");
printf("%6d ",stu[i].age);
printf("%6.2f ",stu[i].score[0]);
printf("%6.2f ",stu[i].score[1]);
printf("%6.2f ",stu[i].score[2]);
printf("%6.2f %6.2f\n",sum[i],avg[i]);
}
}
展开全部
#include<stdio.h>
#include<string.h>
struct student
{
int num;
char name[20];
char sex[3];
int age;
int score[3];
};
student stu[5];
int toalsc[5],avscore[5];
void count()
{
int i;
for(i=0;i<=5;i++)
{
toalsc[i]+=stu[i].score[0];
toalsc[i]+=stu[i].score[1];
toalsc[i]+=stu[i].score[2];
avscore[i]=toalsc[i]/3;
}
}
void main()
{
//char n[10]="王培军";
stu[0].num =111;
strcpy(stu[0].name,"王培军");
stu[0].age =18;
strcpy(stu[0].sex,"男");
stu[0].score[0]=88;
stu[0].score[1]=99;
stu[0].score[2]=91;
count();
printf("%s %d %s %d %d %d",stu[0].name,stu[0].num,stu[0].sex,stu[0].age,toalsc[0],avscore[0]);
//scanf("");
}
时间不够了,剩余你自己搞定,做个输入输出的循环即可
。
#include<string.h>
struct student
{
int num;
char name[20];
char sex[3];
int age;
int score[3];
};
student stu[5];
int toalsc[5],avscore[5];
void count()
{
int i;
for(i=0;i<=5;i++)
{
toalsc[i]+=stu[i].score[0];
toalsc[i]+=stu[i].score[1];
toalsc[i]+=stu[i].score[2];
avscore[i]=toalsc[i]/3;
}
}
void main()
{
//char n[10]="王培军";
stu[0].num =111;
strcpy(stu[0].name,"王培军");
stu[0].age =18;
strcpy(stu[0].sex,"男");
stu[0].score[0]=88;
stu[0].score[1]=99;
stu[0].score[2]=91;
count();
printf("%s %d %s %d %d %d",stu[0].name,stu[0].num,stu[0].sex,stu[0].age,toalsc[0],avscore[0]);
//scanf("");
}
时间不够了,剩余你自己搞定,做个输入输出的循环即可
。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询