1、实验内容(参考以下功能,可增加) 对学生基本信息,包括学号、姓名、至少三门课成绩。 ① 学生基本信息录入; ② 学生基本信息的输出; ③ 按学号查询学生信息(学号、姓名、成绩); ④ 按学号修改某学生信息并输出; ⑤ 计算平均成绩(计算总分、平均分、最高分、最低分等); ⑥ 删除某学生信息; ⑦ 添加某学生信息 ⑧ 输出平均分大于80分的同学信息(按条件输出)使用C语言编辑一个程序实现上述目的
时间: 2023-12-16 14:17:12 浏览: 201
好的,下面是一个使用C语言编写的学生信息管理系统程序,包括了上述所有功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[20];
int scores[3];
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student()
{
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students!\n");
return;
}
struct student s;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter scores for three courses: ");
scanf("%d%d%d", &s.scores[0], &s.scores[1], &s.scores[2]);
students[num_students++] = s;
}
void print_student(struct student s)
{
printf("%d\t%s\t%d\t%d\t%d\n", s.id, s.name, s.scores[0], s.scores[1], s.scores[2]);
}
void list_students()
{
printf("ID\tName\tCourse1\tCourse2\tCourse3\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void find_student()
{
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
print_student(students[i]);
return;
}
}
printf("Error: student not found!\n");
}
void update_student()
{
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Enter new name: ");
scanf("%s", students[i].name);
printf("Enter new scores for three courses: ");
scanf("%d%d%d", &students[i].scores[0], &students[i].scores[1], &students[i].scores[2]);
print_student(students[i]);
return;
}
}
printf("Error: student not found!\n");
}
void delete_student()
{
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("Student deleted.\n");
return;
}
}
printf("Error: student not found!\n");
}
void calculate_average()
{
int total_scores[3] = {0};
int max_scores[3] = {0};
int min_scores[3] = {100};
double avg_scores[3] = {0};
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < 3; j++) {
total_scores[j] += students[i].scores[j];
if (students[i].scores[j] > max_scores[j]) {
max_scores[j] = students[i].scores[j];
}
if (students[i].scores[j] < min_scores[j]) {
min_scores[j] = students[i].scores[j];
}
}
}
for (int j = 0; j < 3; j++) {
avg_scores[j] = (double)total_scores[j] / num_students;
printf("Average score for course %d: %.2lf\n", j + 1, avg_scores[j]);
printf("Max score for course %d: %d\n", j + 1, max_scores[j]);
printf("Min score for course %d: %d\n", j + 1, min_scores[j]);
}
}
void find_excellent_students()
{
printf("Excellent students (>80): \n");
printf("ID\tName\tCourse1\tCourse2\tCourse3\n");
for (int i = 0; i < num_students; i++) {
double avg_score = (double)(students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3;
if (avg_score > 80) {
print_student(students[i]);
}
}
}
void menu()
{
printf("\n");
printf("1. Add student\n");
printf("2. List students\n");
printf("3. Find student\n");
printf("4. Update student\n");
printf("5. Delete student\n");
printf("6. Calculate average scores\n");
printf("7. Find excellent students\n");
printf("0. Exit\n");
printf("Enter your choice: ");
}
int main()
{
int choice;
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
list_students();
break;
case 3:
find_student();
break;
case 4:
update_student();
break;
case 5:
delete_student();
break;
case 6:
calculate_average();
break;
case 7:
find_excellent_students();
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Error: invalid choice!\n");
}
} while (choice != 0);
return 0;
}
```
这个程序使用了结构体来存储学生信息,使用了循环和条件语句来实现各种功能。
阅读全文