用c语言写代码:题目要求: 一、定义结构体类型: 每个同学的信息:学号、姓名(英文、无重名)、性别(英文)、三门课程成绩(语文、数学、英语) 二、功能要求: 运行后,显示如下信息,提示输入数字,并运行数字所对应的功能,如:按1,执行“显示所有同学的信息”;按6,执行“删除一个同学”。 1.显示所有同学的信息 2.查找指定姓名的信息 (模糊查找:输入姓名中任何一个或多个文字,比如:输入“zhang”,就显示所有姓名中含“zhang”的信息) 3.显示有不及格同学的信息 4.按指定课程排序输出 5.增加一个新同学 6.修改指定姓名的信息 7.删除指定姓名的信息 0.退出 说明: 1、学生信息不要增加额外的限制,比如:学号不要限制必须要8位。 3、学生信息显示在一行。 4、预先初始化几个学生的基本信息,在此基础上增删改。学生初始化信息,放在程序的第一页,便于修改。 5、菜单能循环显示,即完成一次功能选择后,显示相应结果,再显示菜单。
时间: 2024-02-04 17:02:17 浏览: 146
下面是用C语言实现的代码,实现了题目要求的各种功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
typedef struct {
int id; // 学号
char name[20]; // 姓名
char gender[10]; // 性别
int score[3]; // 三门课程成绩
} Student;
Student students[MAX_STUDENT_NUM] = {
{1001, "Tom", "Male", {85, 90, 80}},
{1002, "Jerry", "Male", {70, 75, 85}},
{1003, "Mary", "Female", {80, 65, 90}},
{1004, "Lisa", "Female", {90, 85, 80}},
{0, "", "", {0, 0, 0}} // 结束标志
};
int student_num = 4; // 当前学生数量
// 显示所有同学的信息
void show_all_students() {
printf("id\tname\tgender\tchinese\tmath\tenglish\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < student_num; i++) {
if (students[i].id == 0) break;
printf("%d\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].gender, students[i].score[0],
students[i].score[1], students[i].score[2]);
}
}
// 模糊查找指定姓名的信息
void search_student(char *name) {
printf("id\tname\tgender\tchinese\tmath\tenglish\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < student_num; i++) {
if (students[i].id == 0) break;
if (strstr(students[i].name, name) != NULL) {
printf("%d\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].gender, students[i].score[0],
students[i].score[1], students[i].score[2]);
}
}
}
// 显示有不及格同学的信息
void show_failed_students() {
printf("id\tname\tgender\tchinese\tmath\tenglish\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < student_num; i++) {
if (students[i].id == 0) break;
if (students[i].score[0] < 60 || students[i].score[1] < 60 || students[i].score[2] < 60) {
printf("%d\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].gender, students[i].score[0],
students[i].score[1], students[i].score[2]);
}
}
}
// 按指定课程排序输出
void sort_students(int course) {
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].score[course] < students[j + 1].score[course]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("id\tname\tgender\tchinese\tmath\tenglish\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < student_num; i++) {
if (students[i].id == 0) break;
printf("%d\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].gender, students[i].score[0],
students[i].score[1], students[i].score[2]);
}
}
// 增加一个新同学
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Too many students!\n");
return;
}
printf("Please input the information of the new student:\n");
printf("id: ");
scanf("%d", &students[student_num].id);
printf("name: ");
scanf("%s", students[student_num].name);
printf("gender: ");
scanf("%s", students[student_num].gender);
printf("chinese score: ");
scanf("%d", &students[student_num].score[0]);
printf("math score: ");
scanf("%d", &students[student_num].score[1]);
printf("english score: ");
scanf("%d", &students[student_num].score[2]);
student_num++;
printf("New student added successfully.\n");
}
// 修改指定姓名的信息
void modify_student(char *name) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Please input the new information of the student:\n");
printf("id: ");
scanf("%d", &students[i].id);
printf("name: ");
scanf("%s", students[i].name);
printf("gender: ");
scanf("%s", students[i].gender);
printf("chinese score: ");
scanf("%d", &students[i].score[0]);
printf("math score: ");
scanf("%d", &students[i].score[1]);
printf("english score: ");
scanf("%d", &students[i].score[2]);
printf("Student information modified successfully.\n");
return;
}
}
printf("No such student.\n");
}
// 删除指定姓名的信息
void delete_student(char *name) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("Student information deleted successfully.\n");
return;
}
}
printf("No such student.\n");
}
// 主函数
int main() {
int choice;
char name[20];
while (1) {
printf("\nPlease input a number to choose a function:\n");
printf("1. Show all student information\n");
printf("2. Search student by name\n");
printf("3. Show failed students\n");
printf("4. Sort students by course\n");
printf("5. Add a new student\n");
printf("6. Modify student information\n");
printf("7. Delete student information\n");
printf("0. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
show_all_students();
break;
case 2:
printf("Please input name: ");
scanf("%s", name);
search_student(name);
break;
case 3:
show_failed_students();
break;
case 4:
printf("Please input course number (0 for Chinese, 1 for Math, 2 for English): ");
scanf("%d", &choice);
sort_students(choice);
break;
case 5:
add_student();
break;
case 6:
printf("Please input name: ");
scanf("%s", name);
modify_student(name);
break;
case 7:
printf("Please input name: ");
scanf("%s", name);
delete_student(name);
break;
case 0:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice. Please input again.\n");
}
}
return 0;
}
```
以上代码实现了题目要求的各种功能,可以根据需要进行修改和优化。
阅读全文