1、定义学生
可以使用一个结构体来表示学生信息,并使用一个数组来存储所有学生的信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
float scores[3];
float average;
} Student;
int main() {
printf("Hello, World!");
return 0;
}
2、添加学生成绩
添加学生及其成绩过定义一个结构体Student
来存储学生的姓名和成绩,并使用一个数组students
来存储所有学生的信息。通过一个函数addStudent
来实现添加学生的功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
float scores[3];
float average;
} Student;
void addStudent(Student students[], int *studentCount) {
if (*studentCount >= MAX_STUDENTS) {
printf("学生人数已达到上限,无法添加更多学生。\n");
return;
}
Student *student = &students[*studentCount];
printf("请输入学生ID: ");
scanf("%d", &student->id);
printf("请输入学生姓名: ");
scanf("%s", student->name);
printf("请输入三门课的成绩:\n");
for (int i = 0; i < 3; i++) {
printf("成绩 %d: ", i + 1);
scanf("%f", &student->scores[i]);
}
// 计算平均成绩
student->average = (student->scores[0] + student->scores[1] + student->scores[2]) / 3.0;
(*studentCount)++;
printf("学生信息已添加成功!\n");
}
int main() {
printf("Hello, World!");
return 0;
}
3、删除学生成绩
用户输入要删除学生的学号,遍历学生数组,找到要删除的学生,将找到的学生后面的所有学生往前移动一位,覆盖被删除的学生,将学生总数减一。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
float scores[3];
float average;
} Student;
void deleteStudent(Student students[], int *studentCount) {
int id;
printf("请输入要删除的学生ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < *studentCount; i++) {
if (students[i].id == id) {
found = 1;
// 将后面的元素向前移动一位
for (int j = i; j < *studentCount - 1; j++) {
students[j] = students[j + 1];
}
(*studentCount)--;
printf("学生ID为 %d 的信息已删除。\n", id);
break;
}
}
if (!found) {
printf("未找到ID为 %d 的学生。\n", id);
}
}
int main() {
printf("Hello, World!");
return 0;
}
4、显示学生成绩
定义了一个变量studentCount
来跟踪当前存储的学生数量,以及一个函数displayStudentScores
用于显示所有学生的成绩。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
float scores[3];
float average;
} Student;
void displayStudents(Student students[], int studentCount) {
printf("\n学生信息:\n");
for (int i = 0; i < studentCount; i++) {
printf("学生ID: %d\n", students[i].id);
printf("姓名: %s\n", students[i].name);
printf("成绩: %.2f, %.2f, %.2f\n", students[i].scores[0], students[i].scores[1], students[i].scores[2]);
printf("平均成绩: %.2f\n", students[i].average);
printf("----------------------------\n");
}
}
int main() {
printf("Hello, World!");
return 0;
}
5、完整代码
一个简易的学生成绩管理系统,可以根据自己的需求进行扩展和完善。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 50
typedef struct {
int id;
char name[NAME_LENGTH];
float scores[3];
float average;
} Student;
void addStudent(Student students[], int *studentCount) {
if (*studentCount >= MAX_STUDENTS) {
printf("学生人数已达到上限,无法添加更多学生。\n");
return;
}
Student *student = &students[*studentCount];
printf("请输入学生ID: ");
scanf("%d", &student->id);
printf("请输入学生姓名: ");
scanf("%s", student->name);
printf("请输入三门课的成绩:\n");
for (int i = 0; i < 3; i++) {
printf("成绩 %d: ", i + 1);
scanf("%f", &student->scores[i]);
}
// 计算平均成绩
student->average = (student->scores[0] + student->scores[1] + student->scores[2]) / 3.0;
(*studentCount)++;
printf("学生信息已添加成功!\n");
}
void displayStudents(Student students[], int studentCount) {
printf("\n学生信息:\n");
for (int i = 0; i < studentCount; i++) {
printf("学生ID: %d\n", students[i].id);
printf("姓名: %s\n", students[i].name);
printf("成绩: %.2f, %.2f, %.2f\n", students[i].scores[0], students[i].scores[1], students[i].scores[2]);
printf("平均成绩: %.2f\n", students[i].average);
printf("----------------------------\n");
}
}
void deleteStudent(Student students[], int *studentCount) {
int id;
printf("请输入要删除的学生ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < *studentCount; i++) {
if (students[i].id == id) {
found = 1;
// 将后面的元素向前移动一位
for (int j = i; j < *studentCount - 1; j++) {
students[j] = students[j + 1];
}
(*studentCount)--;
printf("学生ID为 %d 的信息已删除。\n", id);
break;
}
}
if (!found) {
printf("未找到ID为 %d 的学生。\n", id);
}
}
int main() {
Student students[MAX_STUDENTS];
int studentCount = 0;
int choice;
while (1) {
printf("\n学生成绩管理系统菜单:\n");
printf("1. 添加学生\n");
printf("2. 显示学生信息\n");
printf("3. 删除学生\n");
printf("4. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &studentCount);
break;
case 2:
displayStudents(students, studentCount);
break;
case 3:
deleteStudent(students, &studentCount);
break;
case 4:
printf("退出系统。\n");
exit(0);
default:
printf("无效的选择,请重试。\n");
}
}
return 0;
}