我需要不使用结构体只使用数组的代码
时间: 2024-02-25 16:56:26 浏览: 22
好的,以下是不使用结构体,只使用数组实现学生成绩管理系统的示例代码,实现了输入、显示、按学号排序和按姓名查询的功能。需要注意的是,该代码同样并不完整,仅供参考。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 100
char id[MAX_STUDENT][10];
char name[MAX_STUDENT][20];
char course[MAX_STUDENT][20];
float score[MAX_STUDENT];
void input(int n);
void display(int n);
void sortById(int n);
void searchByName(int n, char name[]);
int main() {
int n;
char name[20];
printf("请输入学生人数:");
scanf("%d", &n);
input(n);
display(n);
sortById(n);
display(n);
printf("请输入要查询的学生姓名:");
scanf("%s", name);
searchByName(n, name);
return 0;
}
void input(int n) {
int i;
for (i = 0; i < n; i++) {
printf("请输入第 %d 个学生的学号、姓名、课程名和成绩:", i + 1);
scanf("%s%s%s%f", id[i], name[i], course[i], &score[i]);
}
}
void display(int n) {
int i;
printf("学号\t姓名\t课程名\t成绩\n");
for (i = 0; i < n; i++) {
printf("%s\t%s\t%s\t%.2f\n", id[i], name[i], course[i], score[i]);
}
}
void sortById(int n) {
int i, j;
char tempid[10];
char tempname[20];
char tempcourse[20];
float tempscore;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (strcmp(id[i], id[j]) > 0) {
strcpy(tempid, id[i]);
strcpy(id[i], id[j]);
strcpy(id[j], tempid);
strcpy(tempname, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], tempname);
strcpy(tempcourse, course[i]);
strcpy(course[i], course[j]);
strcpy(course[j], tempcourse);
tempscore = score[i];
score[i] = score[j];
score[j] = tempscore;
}
}
}
}
void searchByName(int n, char name[]) {
int i, found = 0;
printf("学号\t姓名\t课程名\t成绩\n");
for (i = 0; i < n; i++) {
if (strcmp(name[i], name) == 0) {
printf("%s\t%s\t%s\t%.2f\n", id[i], name[i], course[i], score[i]);
found = 1;
}
}
if (!found) {
printf("未找到该学生!\n");
}
}
```
该程序使用了四个数组,分别存储学生的学号、姓名、课程名和成绩。使用 `input` 函数实现了输入学生信息的功能,使用 `display` 函数实现了显示学生信息的功能,使用 `sortById` 函数实现了按学号排序的功能,使用 `searchByName` 函数实现了按姓名查询的功能。在 `main` 函数中,首先读入学生人数,然后依次调用四个函数实现各个功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)