根据学号查询成绩和根据姓名查询成绩两种方法 c语言
时间: 2023-09-06 07:14:14 浏览: 270
chengjichaxun.rar.rar_成绩查询
以下是根据学号查询成绩的 C 语言代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
int id;
float score;
};
int main() {
int n, i, search_id;
printf("Enter number of students: ");
scanf("%d", &n);
struct student students[n];
for (i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", students[i].name);
printf("Enter ID: ");
scanf("%d", &students[i].id);
printf("Enter score: ");
scanf("%f", &students[i].score);
}
printf("\nEnter the ID of the student to search: ");
scanf("%d", &search_id);
for (i = 0; i < n; i++) {
if (students[i].id == search_id) {
printf("\nName: %s\n", students[i].name);
printf("ID: %d\n", students[i].id);
printf("Score: %.2f\n", students[i].score);
break;
}
}
if (i == n) {
printf("\nNo student with ID %d found!\n", search_id);
}
return 0;
}
```
以下是根据姓名查询成绩的 C 语言代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
int id;
float score;
};
int main() {
int n, i, found = 0;
char search_name[50];
printf("Enter number of students: ");
scanf("%d", &n);
struct student students[n];
for (i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", students[i].name);
printf("Enter ID: ");
scanf("%d", &students[i].id);
printf("Enter score: ");
scanf("%f", &students[i].score);
}
printf("\nEnter the name of the student to search: ");
scanf("%s", search_name);
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, search_name) == 0) {
printf("\nName: %s\n", students[i].name);
printf("ID: %d\n", students[i].id);
printf("Score: %.2f\n", students[i].score);
found = 1;
}
}
if (!found) {
printf("\nNo student with name %s found!\n", search_name);
}
return 0;
}
```
阅读全文