#include
#include
#define ARRAY_LEN 100 /*数组长度*/
typedef struct {
int no; /*学号*/
char name[ARRAY_LEN]; /*姓名*/
float score1; /*平时成绩*/
float score2; /*期中成绩*/
float score3; /*期末成绩*/
float totalScore; /*学期成绩*/
} student;
/*初始化*/
void initInfo (student stu[], int *stuNum) {
*stuNum = 2; /*学生人数设定*/
}
/*输入学生信息*/
void inputInfo (student stu[], int stuIndex) {
int i;
printf ("第%d名学生↓\n", stuIndex+1);
printf ("学号:");
scanf ("%d",&stu[stuIndex].no);
printf ("姓名:");
scanf ("%s",&stu[stuIndex].name);
printf ("平时成绩:");
scanf ("%f",&stu[stuIndex].score1);
printf ("期中成绩:");
scanf ("%f",&stu[stuIndex].score2);
printf ("期末成绩:");
scanf ("%f",&stu[stuIndex].score3);
putchar ('\n');
}
/*计算学期成绩*/
void calculationScore (student stu[], int stuIndex) {
float sco1Ratio = 0.1; /*平时成绩比例*/
float sco2Ratio = 0.2; /*期中成绩比例*/
float sco3Ratio = 0.7; /*期末成绩比例*/
stu[stuIndex].totalScore = stu[stuIndex].score1 * sco1Ratio +
stu[stuIndex].score2 * sco2Ratio +
stu[stuIndex].score3 * sco3Ratio;
}
/*输出学生成绩*/
void printInfo (student stu[], int stuIndex) {
int i;
printf ("%d\t",stu[stuIndex].no);
printf ("%s\t",stu[stuIndex].name);
printf ("%.2f\t\t",stu[stuIndex].score1);
printf ("%.2f\t\t",stu[stuIndex].score2);
printf ("%.2f\t\t",stu[stuIndex].score3);
printf ("%.2f",stu[stuIndex].totalScore);
putchar ('\n');
}
int main (void) {
int stuNum,i;
student stu[ARRAY_LEN];
initInfo (stu,&stuNum);
/*输入、计算*/
puts ("请输入学生信息:");
putchar ('\n');
for (i=0; iinputInfo (stu,i);
calculationScore (stu,i);
}
putchar ('\n');
printf ("%d名学生成绩输入完毕!", stuNum);
putchar ('\n');
puts ("================================================================\n");
/*输出*/
puts ("学号\t姓名\t平时成绩\t期中成绩\t期末成绩\t学期成绩");
for (i=0; iprintInfo (stu,i);
getch (); /*屏幕暂留*/
return 0;
}
运行结果
以下图示改为2名学生,上方源代码为题主要求的5名学生
#include
struct Student
{
int num;
char name[16];
float regularGrade;
float midtermGrade;
float finalGrade;
float totalMark;
};
int main()
{
int i;
struct Student student[5];
for(i=0;i<5;i++)
{
printf("please enter student[%d]`s num:",i+1);
scanf("%d",&student[i].num);
printf("please enter student[%d]`s name:",i+1);
scanf("%s",student[i].name);
printf("please enter student[%d]`s regularGrade:",i+1);
scanf("%f",&student[i].regularGrade);
printf("please enter student[%d]`s midtermGrade:",i+1);
scanf("%f",&student[i].midtermGrade);
printf("please enter student[%d]`s finalGrade:",i+1);
scanf("%f",&student[i].finalGrade);
student[i].totalMark = student[i].regularGrade*0.1+student[i].midtermGrade*0.2+student[i].finalGrade*0.7;
}
for(i=0;i<5;i++)
{
printf("student[%d] num:%d name:%s tatalMark:%f\n",i+1,student[i].num,student[i].name,student[i].totalMark);
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
}
#include
struct Student
{
int num;
char name[16];
float regularGrade;
float midtermGrade;
float finalGrade;
float totalMark;
};
int main()
{
int i;
struct Student student[5];
for(i=0;i<5;i++)
{
printf("please enter student[%d]`s num:",i+1);
scanf("%d",&student[i].num);
printf("please enter student[%d]`s name:",i+1);
scanf("%s",student[i].name);
printf("please enter student[%d]`s regularGrade:",i+1);
scanf("%f",&student[i].regularGrade);
printf("please enter student[%d]`s midtermGrade:",i+1);
scanf("%f",&student[i].midtermGrade);
printf("please enter student[%d]`s finalGrade:",i+1);
scanf("%f",&student[i].finalGrade);
student[i].totalMark = student[i].regularGrade*0.1+student[i].midtermGrade*0.2+student[i].finalGrade*0.7;
}
for(i=0;i<5;i++)
{
printf("student[%d] num:%d name:%s tatalMark:%f\n",i+1,student[i].num,student[i].name,student[i].totalMark);
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
}
#include
#include
//C语言 用结构体类型数组编程实现输入5个学生的学号姓名平时成绩期中成绩和期末成绩然后输出每位同学的学期成绩,(平时成绩占10%期中成绩占20%期末成绩占70%)
struct Student
{
int id;
char name[20];
double norScore;
double midScore;
double finScore;
}student[5];
struct StudentScore
{
int id;
char name[20];
double Score;
}studentScore[5];
int main(int argc, const char * argv[]) {
int i;
struct Student student[5];
struct StudentScore studentScore[5];
for (i = 0; i < 5; i++) {
scanf("%d %s %lf %lf %lf", &student[i].id, student[i].name, &student[i].norScore, &student[i].midScore,&student[i].finScore);
}
for (i = 0; i < 5; i++) {
studentScore[i].Score = student[i].norScore * 0.1 + student[i].midScore * 0.2 + student[i].finScore * 0.7;
studentScore[i].id = student[i].id;
strcpy(studentScore[i].name, student[i].name);
}
for (i = 0; i < 5; i++) {
printf("num = %d\nname = %s\nscore = %.2lf\n\n", studentScore[i].id, studentScore[i].name, studentScore[i].Score);
}
return 0;
}
这就是一个结构体数组的遍历问题,代码如下:
#include
#define N 5
typedef struct
{
int sno;
char name[32];
double daily;
double middle;
double final;
}Student;
int main(void)
{
int i;
Student stus[N];
for(i=0; i{
scanf("%d%s%lf%lf%lf", &stus[i].sno, stus[i].name,
&stus[i].daily, &stus[i].middle, &stus[i].final);
printf("%d,%s,%.2lf\n", stus[i].sno, stus[i].name,
stus[i].daily*0.1+stus[i].middle*0.2+stus[i].final*0.7);
}
return 0;
}