Sun May 28 09:50:00 UTC 2023 inscode

上级 fd6d1e15
#include <stdio.h> #include <stdio.h>
// 定义学生结构体
struct Student struct Student
{ {
char *name; char *name; // 姓名
int id; int id; // 学号
unsigned int age; unsigned int age; // 年龄
char group; char group; // 小组
float score; float score; // 成绩
} } cls[] = {
// 初始化学生信息
{.name = "张三", .group = 'A', .age = 16, .score = 95.50, .id = 1001},
{.age = 15, .score = 90.00, .group = 'A', .id = 1002, .name = "李四"},
{.group = 'B', .age = 16, .name = "王五", .score = 80.50, .id = 1003}
};
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
struct Student stu; size_t i, num_stu = sizeof(cls) / sizeof(struct Student); // 获取学生人数
stu.name = "张三";
stu.id = 1001;
stu.age = 16;
stu.group = 'A';
stu.score = 95.50;
printf("========== 学生基本信息 ==========\n");
printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n",
stu.name, stu.id, stu.age, stu.group, stu.score);
printf("==================================\n");
return 0;
}
#include <stdio.h>
// 定义结构体Student float total = 0, average = 0; // 定义总成绩和平均成绩变量
struct Student printf("=============== 学生基本信息 ===============\n");
{ printf("姓名\t学号\t年龄\t小组\t成绩\n");
char *name; // 姓名 printf("--------------------------------------------\n");
int id; // 学号
unsigned int age; // 年龄
char group; // 所在小组
float score; // 成绩
};
int main(int argc, char** argv) // 打印学生信息
{ for (i = 0; i < num_stu; i++) {
// 初始化一个Student类型的变量stu printf("%s\t%d\t%d\t%c\t%.2f\n",
struct Student stu; cls[i].name, cls[i].id, cls[i].age, cls[i].group, cls[i].score);
stu.name = "张三"; // 给stu的name成员赋值 total += cls[i].score; // 计算总成绩
stu.id = 1001; // 给stu的id成员赋值 }
stu.age = 16; // 给stu的age成员赋值 printf("============================================\n");
stu.group = 'A'; // 给stu的group成员赋值
stu.score = 95.50; // 给stu的score成员赋值
// 输出学生基本信息 average = total / num_stu; // 计算平均成绩
printf("========== 学生基本信息 ==========\n"); printf("班级平均成绩:%.2f", average);
printf("姓名:%s\n学号:%d\n年龄:%d\n所在小组:%c\n成绩:%.2f\n",
stu.name, stu.id, stu.age, stu.group, stu.score);
printf("==================================\n");
return 0; return 0;
} }
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册