用c语言写出以下程序:登录:显示登录界面,提示用户选择 1.学生登录,学生菜单:显示学生登录界面,显示学生菜单 可以查询自己的排名(可以按照不同的条件)。显示自己的各科成绩。 2.管理员登录,管理员菜单:显示管理员登录界面,显示管理员菜单。 显示学生信息:遍历(链表),依次输出学生节点的各个信息(12) 删除学生信息:根据学生的姓名/学号进行删除,遍历链表找到需要删除的节点(2)。 添加学生信息:前插法插入节点,(2) 查询学生信息:遍历链表,根据对比学生姓名/学号来查询对应的节点,并输出该学生节点的信息,若未查到会提示用户。(12) 查看学生总人数:输出总人数。(12) 修改学生信息:遍历链表,根据ID来找到需要修改信息的学生(会对学号的存在性进行检测,也会对修改过后学号是否重复进行检查)再对节点的信息进行修改。(2) 排序:用户可以选择按照学号排序/单科成绩排序。 保存文件:将链表中的数据写入到文件中。
时间: 2024-01-24 12:17:44 浏览: 72
此处提供一个简单的示例程序,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char id[20];
char name[20];
float score1;
float score2;
float score3;
float total_score;
struct student *next;
} Student;
// 全局变量,存储学生链表头指针
Student *head = NULL;
// 显示学生登录菜单
void show_student_menu() {
printf("请选择操作:\n");
printf("1. 查询排名\n");
printf("2. 显示成绩\n");
printf("3. 返回上级菜单\n");
}
// 显示管理员菜单
void show_admin_menu() {
printf("请选择操作:\n");
printf("1. 显示学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 添加学生信息\n");
printf("4. 查询学生信息\n");
printf("5. 查看学生总人数\n");
printf("6. 修改学生信息\n");
printf("7. 排序\n");
printf("8. 保存文件\n");
printf("9. 返回上级菜单\n");
}
// 遍历学生链表,输出学生信息
void show_students() {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t总成绩\n");
Student *p = head;
while (p != NULL) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", p->id, p->name, p->score1, p->score2, p->score3, p->total_score);
p = p->next;
}
}
// 根据学生姓名或学号删除学生信息
void delete_student() {
char id[20];
printf("请输入要删除的学生姓名或学号:");
scanf("%s", id);
Student *p = head;
Student *prev = NULL;
while (p != NULL) {
if (strcmp(p->id, id) == 0 || strcmp(p->name, id) == 0) {
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该学生信息!\n");
}
// 添加学生信息
void add_student() {
Student *p = (Student *) malloc(sizeof(Student));
printf("请输入学号:");
scanf("%s", p->id);
printf("请输入姓名:");
scanf("%s", p->name);
printf("请输入成绩1:");
scanf("%f", &p->score1);
printf("请输入成绩2:");
scanf("%f", &p->score2);
printf("请输入成绩3:");
scanf("%f", &p->score3);
p->total_score = p->score1 + p->score2 + p->score3;
p->next = head;
head = p;
printf("添加成功!\n");
}
// 根据学生姓名或学号查询学生信息
void search_student() {
char id[20];
printf("请输入要查询的学生姓名或学号:");
scanf("%s", id);
Student *p = head;
while (p != NULL) {
if (strcmp(p->id, id) == 0 || strcmp(p->name, id) == 0) {
printf("学号:%s\n", p->id);
printf("姓名:%s\n", p->name);
printf("成绩1:%.2f\n", p->score1);
printf("成绩2:%.2f\n", p->score2);
printf("成绩3:%.2f\n", p->score3);
printf("总成绩:%.2f\n", p->total_score);
return;
}
p = p->next;
}
printf("未找到该学生信息!\n");
}
// 输出学生总人数
void show_student_count() {
int count = 0;
Student *p = head;
while (p != NULL) {
count++;
p = p->next;
}
printf("学生总人数:%d\n", count);
}
// 根据ID查找学生节点
Student *find_student_by_id(char *id) {
Student *p = head;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
// 修改学生信息
void modify_student() {
char id[20];
printf("请输入要修改的学生学号:");
scanf("%s", id);
Student *p = find_student_by_id(id);
if (p == NULL) {
printf("未找到该学生信息!\n");
return;
}
Student *temp = (Student *) malloc(sizeof(Student));
printf("请输入新的学号:");
scanf("%s", temp->id);
printf("请输入新的姓名:");
scanf("%s", temp->name);
printf("请输入新的成绩1:");
scanf("%f", &temp->score1);
printf("请输入新的成绩2:");
scanf("%f", &temp->score2);
printf("请输入新的成绩3:");
scanf("%f", &temp->score3);
temp->total_score = temp->score1 + temp->score2 + temp->score3;
// 检测学号是否存在
if (find_student_by_id(temp->id) != NULL) {
printf("学号已存在,请重新输入!\n");
return;
}
// 更新节点信息
strcpy(p->id, temp->id);
strcpy(p->name, temp->name);
p->score1 = temp->score1;
p->score2 = temp->score2;
p->score3 = temp->score3;
p->total_score = temp->total_score;
printf("修改成功!\n");
}
// 按照学号排序
void sort_by_id() {
Student *p = head;
while (p != NULL) {
Student *q = p->next;
while (q != NULL) {
if (strcmp(p->id, q->id) > 0) {
// 交换节点数据
char temp_id[20];
strcpy(temp_id, p->id);
strcpy(p->id, q->id);
strcpy(q->id, temp_id);
char temp_name[20];
strcpy(temp_name, p->name);
strcpy(p->name, q->name);
strcpy(q->name, temp_name);
float temp_score1 = p->score1;
p->score1 = q->score1;
q->score1 = temp_score1;
float temp_score2 = p->score2;
p->score2 = q->score2;
q->score2 = temp_score2;
float temp_score3 = p->score3;
p->score3 = q->score3;
q->score3 = temp_score3;
float temp_total_score = p->total_score;
p->total_score = q->total_score;
q->total_score = temp_total_score;
}
q = q->next;
}
p = p->next;
}
printf("按照学号排序成功!\n");
}
// 按照单科成绩排序
void sort_by_score() {
int choice;
printf("请选择排序的科目:\n");
printf("1. 成绩1\n");
printf("2. 成绩2\n");
printf("3. 成绩3\n");
scanf("%d", &choice);
Student *p = head;
while (p != NULL) {
Student *q = p->next;
while (q != NULL) {
float score_p, score_q;
switch (choice) {
case 1:
score_p = p->score1;
score_q = q->score1;
break;
case 2:
score_p = p->score2;
score_q = q->score2;
break;
case 3:
score_p = p->score3;
score_q = q->score3;
break;
default:
printf("输入错误!");
return;
}
if (score_p < score_q) {
// 交换节点数据
char temp_id[20];
strcpy(temp_id, p->id);
strcpy(p->id, q->id);
strcpy(q->id, temp_id);
char temp_name[20];
strcpy(temp_name, p->name);
strcpy(p->name, q->name);
strcpy(q->name, temp_name);
float temp_score1 = p->score1;
p->score1 = q->score1;
q->score1 = temp_score1;
float temp_score2 = p->score2;
p->score2 = q->score2;
q->score2 = temp_score2;
float temp_score3 = p->score3;
p->score3 = q->score3;
q->score3 = temp_score3;
float temp_total_score = p->total_score;
p->total_score = q->total_score;
q->total_score = temp_total_score;
}
q = q->next;
}
p = p->next;
}
printf("按照单科成绩排序成功!\n");
}
// 保存学生信息到文件
void save_students() {
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
Student *p = head;
while (p != NULL) {
fwrite(p, sizeof(Student), 1, fp);
p = p->next;
}
fclose(fp);
printf("保存成功!\n");
}
// 加载学生信息
void load_students() {
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
Student temp;
while (fread(&temp, sizeof(Student), 1, fp)) {
Student *p = (Student *) malloc(sizeof(Student));
strcpy(p->id, temp.id);
strcpy(p->name, temp.name);
p->score1 = temp.score1;
p->score2 = temp.score2;
p->score3 = temp.score3;
p->total_score = temp.total_score;
p->next = head;
head = p;
}
fclose(fp);
printf("加载成功!\n");
}
int main() {
int choice;
while (1) {
printf("请选择登录类型:\n");
printf("1. 学生\n");
printf("2. 管理员\n");
printf("3. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1: {
printf("欢迎进入学生登录界面!\n");
while (1) {
show_student_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
printf("查询排名!\n");
break;
case 2:
printf("显示成绩!\n");
break;
case 3:
printf("返回上级菜单!\n");
goto student_menu_exit;
default:
printf("输入错误!\n");
}
}
student_menu_exit:
break;
}
case 2: {
printf("欢迎进入管理员登录界面!\n");
while (1) {
show_admin_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
show_students();
break;
case 2:
delete_student();
break;
case 3:
add_student();
break;
case 4:
search_student();
break;
case 5:
show_student_count();
break;
case 6:
modify_student();
break;
case 7:
printf("请选择排序方式:\n");
printf("1. 按照学号排序\n");
printf("2. 按照单科成绩排序\n");
scanf("%d", &choice);
if (choice == 1) {
sort_by_id();
} else if (choice == 2) {
sort_by_score();
} else {
printf("输入错误!\n");
}
break;
case 8:
save_students();
break;
case 9:
printf("返回上级菜单!\n");
goto admin_menu_exit;
default:
printf("输入错误!\n");
}
}
admin_menu_exit:
break;
}
case 3:
printf("退出程序!\n");
exit(0);
default:
printf("输入错误!\n");
}
}
}
```
阅读全文