本关任务:从键盘输入两个学生的学号,姓名和成绩(整数),分别存入结构体中,输出成绩较高的学生的学号,姓名和成绩。 #include<stdio.h> /*********Begin*********/ /*********End*********/
时间: 2024-01-09 19:23:09 浏览: 60
```c
#include<stdio.h>
struct student{
long number;
char name[20];
int score;
};
int main() {
struct student student1, student2;
// 输入学生1的信息
scanf("%ld%s%d", &student1.number, student1.name, &student1.score);
// 输入学生2的信息
scanf("%ld%s%d", &student2.number, student2.name, &student2.score);
// 比较两个学生的成绩并输出较高的学生信息
if(student1.score > student2.score) {
printf("%ld %s %d\n", student1.number, student1.name, student1.score);
} else {
printf("%ld %s %d\n", student2.number, student2.name, student2.score);
}
return 0;
}
```
相关问题
C语言结构体本关任务:从键盘输入两个学生的学号,姓名和成绩(整数),分别存入结构体中,输出成绩较高的学生的学号,姓名和成绩。
好的,这道题需要定义一个结构体类型来存储学生的学号,姓名和成绩,然后从键盘输入两个学生的信息,比较他们的成绩,输出成绩较高的学生的学号,姓名和成绩。下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
struct student {
int id;
char name[MAX_NAME_LEN];
int score;
};
int main() {
struct student s1, s2;
printf("Enter student 1 id, name and score: ");
scanf("%d %s %d", &s1.id, s1.name, &s1.score);
printf("Enter student 2 id, name and score: ");
scanf("%d %s %d", &s2.id, s2.name, &s2.score);
if (s1.score > s2.score) {
printf("Student %d %s has a higher score of %d.\n", s1.id, s1.name, s1.score);
} else {
printf("Student %d %s has a higher score of %d.\n", s2.id, s2.name, s2.score);
}
return 0;
}
```
在这个程序中,我们首先定义了一个结构体类型 `student` 来存储学生的信息。然后在 `main` 函数中创建了两个 `student` 类型的变量 `s1` 和 `s2`。接着通过 `scanf` 函数从键盘输入两个学生的信息,分别存储在 `s1` 和 `s2` 中。最后比较他们的成绩,输出成绩较高的学生的信息。注意在输入学生姓名时使用 `%s` 格式化字符串,因为学生姓名是一个字符串类型。
从键盘输入两个学生的学号,姓名和成绩(整数),分别存入结构体中,输出成绩较高的学生的学号,姓名和成绩。
请使用以下代码段:
```
struct student {
int id;
char name[20];
int score;
};
int main() {
struct student stu1, stu2;
printf("请输入学生1的学号、姓名和成绩:\n");
scanf("%d%s%d", &stu1.id, stu1.name, &stu1.score);
printf("请输入学生2的学号、姓名和成绩:\n");
scanf("%d%s%d", &stu2.id, stu2.name, &stu2.score);
if (stu1.score > stu2.score) {
printf("成绩较高的学生是:\n学号:%d\n姓名:%s\n成绩:%d\n", stu1.id, stu1.name, stu1.score);
} else {
printf("成绩较高的学生是:\n学号:%d\n姓名:%s\n成绩:%d\n", stu2.id, stu2.name, stu2.score);
}
return 0;
}
```
这段代码将定义一个名为student的结构体,并在main函数中创建两个结构体变量stu1和stu2。通过scanf函数从键盘输入两个学生的学号、姓名和成绩,分别存入结构体中。接着,使用if-else语句比较两个学生的成绩,并输出成绩较高的学生的学号、姓名和成绩。