看代码:
#include
typedef struct
{
char name[20];
char id[20];
float math_score;
float lang_score;
}student;
#define N 5
int main(int argc, char** argv)
{
int i;
student stu[N];
for (i = 0; i < N; ++i)
{
printf ("输入第 %d 个学生的信息(顺序为姓名,学号,数学成绩,语文成绩,并用空格分开):\n", i + 1);
scanf ("%s %s %f %f", stu[i].name, stu[i].id, &stu[i].math_score, &stu[i].lang_score);
}
printf ("\n");
for (i = 0; i < N; ++i)
{
printf ("第 %d 个学生的信息:\n", i + 1);
printf ("姓名:%s 学号:%s 数学成绩:%.1f 语文成绩:%.1f 总分:%.1f\n",
stu[i].name, stu[i].id, stu[i].math_score, stu[i].lang_score, stu[i].math_score + stu[i].lang_score);
}
return 0;
}
运行:
看代码:
#include
typedef struct
{
char name[20];
char id[20];
float math_score;
float lang_score;
}student;
#define N 5
int main(int argc, char** argv)
{
int i;
student stu[N];
for (i = 0; i < N; ++i)
{
printf ("输入第 %d 个学生的信息(顺序为姓名,学号,数学成绩,语文成绩,并用空格分开):\n", i + 1);
scanf ("%s %s %f %f", stu[i].name, stu[i].id, &stu[i].math_score, &stu[i].lang_score);
}
printf ("\n");
for (i = 0; i < N; ++i)
{
printf ("第 %d 个学生的信息:\n", i + 1);
printf ("姓名:%s 学号:%s 数学成绩:%.1f 语文成绩:%.1f 总分:%.1f\n",
stu[i].name, stu[i].id, stu[i].math_score, stu[i].lang_score, stu[i].math_score + stu[i].lang_score);
}
return 0;
}运行:
#include
struct student{
char name[50];
int id;
double math;
double chinese;
double sum;
}s[5];
int main()
{
for (int i=0;i<5;i++)
{
scanf("%s %d %lf %lf",s[i].name,&s[i].id,&s[i].math,&s[i].chinese);
s[i].sum=s[i].math+s[i].chinese;
}
for (int i=0;i<5;i++)
printf("%s sum=%.2lf\n",s[i].name,s[i].sum);
}