用C语言写一个有1---学生信息录入2------学生信息浏览3---学生成绩排4---学生信息查询 5------学生信息修改 6---学生信息统计7---学生投票功能的代码
时间: 2023-12-13 17:33:06 浏览: 226
以下是一个简单的学生信息管理系统的C语言代码,包括1-学生信息录入、2-学生信息浏览、3-学生成绩排名、4-学生信息查询、5-学生信息修改、6-学生信息统计和7-学生投票功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数量
#define MAX_NAME_LEN 20 // 最大姓名长度
#define MAX_SUBJECT_NUM 5 // 最大科目数量
// 学生结构体
struct student {
char name[MAX_NAME_LEN]; // 姓名
int id; // 学号
int score[MAX_SUBJECT_NUM]; // 科目成绩
int total_score; // 总成绩
float average_score; // 平均成绩
};
struct student students[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 学生数量
// 函数声明
void input_student_info();
void browse_student_info();
void rank_student_score();
void search_student_info();
void modify_student_info();
void statistics_student_info();
void vote_student();
int main() {
int choice;
while (1) {
printf("|*菜单*|\n");
printf("1-录入/插入学生信息\n");
printf("2-浏览学生信息\n");
printf("3-查询学生信息\n");
printf("4-删除学生信息\n");
printf("5-修改学生信息\n");
printf("6-学生信息统计\n");
printf("7-学生投票功能\n");
printf("0-退出系统\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
input_student_info();
break;
case 2:
browse_student_info();
break;
case 3:
search_student_info();
break;
case 4:
// TODO: 删除学生信息
break;
case 5:
modify_student_info();
break;
case 6:
statistics_student_info();
break;
case 7:
vote_student();
break;
case 0:
printf("谢谢使用,再见!\n");
exit(0);
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
// 录入学生信息
void input_student_info() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数量已达到上限,无法继续录入!\n");
return;
}
printf("请输入学生姓名:");
scanf("%s", students[student_num].name);
printf("请输入学生学号:");
scanf("%d", &students[student_num].id);
printf("请输入学生各科成绩:\n");
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
printf("请输入第%d科成绩:", i + 1);
scanf("%d", &students[student_num].score[i]);
students[student_num].total_score += students[student_num].score[i];
}
students[student_num].average_score = (float)students[student_num].total_score / MAX_SUBJECT_NUM;
student_num++;
printf("学生信息录入成功!\n");
}
// 浏览学生信息
void browse_student_info() {
if (student_num == 0) {
printf("暂无学生信息!\n");
return;
}
printf("学生姓名\t学号\t科目1\t科目2\t科目3\t科目4\t科目5\t总成绩\t平均成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].average_score);
}
}
// 学生成绩排名
void rank_student_score() {
// TODO: 学生成绩排名
}
// 学生信息查询
void search_student_info() {
if (student_num == 0) {
printf("暂无学生信息!\n");
return;
}
int choice;
printf("请选择查询方式:\n");
printf("1-按姓名查询\n");
printf("2-按学号查询\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1: {
char name[MAX_NAME_LEN];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学生姓名\t学号\t科目1\t科目2\t科目3\t科目4\t科目5\t总成绩\t平均成绩\n");
printf("%s\t\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].average_score);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生信息!\n");
}
break;
}
case 2: {
int id;
printf("请输入要查询的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("学生姓名\t学号\t科目1\t科目2\t科目3\t科目4\t科目5\t总成绩\t平均成绩\n");
printf("%s\t\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].average_score);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生信息!\n");
}
break;
}
default:
printf("输入有误,请重新输入!\n");
break;
}
}
// 学生信息修改
void modify_student_info() {
if (student_num == 0) {
printf("暂无学生信息!\n");
return;
}
int id;
printf("请输入要修改的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("请输入学生姓名:");
scanf("%s", students[i].name);
printf("请输入学生各科成绩:\n");
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("请输入第%d科成绩:", j + 1);
scanf("%d", &students[i].score[j]);
students[i].total_score += students[i].score[j];
}
students[i].average_score = (float)students[i].total_score / MAX_SUBJECT_NUM;
printf("学生信息修改成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生信息!\n");
}
}
// 学生信息统计
void statistics_student_info() {
if (student_num == 0) {
printf("暂无学生信息!\n");
return;
}
int choice;
printf("请选择统计方式:\n");
printf("1-按总成绩排序\n");
printf("2-按平均成绩排序\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1: {
// 按总成绩排序
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].total_score < students[j + 1].total_score) {
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("学生姓名\t学号\t科目1\t科目2\t科目3\t科目4\t科目5\t总成绩\t平均成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].average_score);
}
break;
}
case 2: {
// 按平均成绩排序
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].average_score < students[j + 1].average_score) {
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("学生姓名\t学号\t科目1\t科目2\t科目3\t科目4\t科目5\t总成绩\t平均成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%d\t", students[i].score[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].average_score);
}
break;
}
default:
printf("输入有误,请重新输入!\n");
break;
}
}
// 学生投票
void vote_student() {
if (student_num == 0) {
printf("暂无学生信息!\n");
return;
}
int id;
printf("请输入要投票的学生学号:");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("您已成功为%s投票!\n", students[i].name);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生信息!\n");
}
}
```
阅读全文