c语言编写一个成绩管理系统,用一个一维数组存放学生成绩(为了测试方便,假定有10位学生,要求具有成绩录入、成绩删除、成绩查找、成绩排序、成绩插入的功能,设计文本菜单进行交互
时间: 2023-06-26 20:08:22 浏览: 191
好的,以下是一个简单的成绩管理系统:
```
#include <stdio.h>
#define MAX_SIZE 10
void print_menu();
void print_grades(int grades[], int size);
void insert_grade(int grades[], int *size);
void delete_grade(int grades[], int *size);
void search_grade(int grades[], int size);
void sort_grades(int grades[], int size);
int main() {
int grades[MAX_SIZE];
int size = 0;
int choice = 0;
while (1) {
print_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_grades(grades, size);
break;
case 2:
insert_grade(grades, &size);
break;
case 3:
delete_grade(grades, &size);
break;
case 4:
search_grade(grades, size);
break;
case 5:
sort_grades(grades, size);
break;
case 6:
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
void print_menu() {
printf("***************************\n");
printf("* MENU *\n");
printf("* 1. Print grades *\n");
printf("* 2. Insert a grade *\n");
printf("* 3. Delete a grade *\n");
printf("* 4. Search a grade *\n");
printf("* 5. Sort grades *\n");
printf("* 6. Exit *\n");
printf("***************************\n");
}
void print_grades(int grades[], int size) {
printf("Grades: ");
for (int i = 0; i < size; i++) {
printf("%d ", grades[i]);
}
printf("\n");
}
void insert_grade(int grades[], int *size) {
if (*size >= MAX_SIZE) {
printf("Array is full, cannot insert more grades!\n");
return;
}
int grade = 0;
printf("Enter the grade to insert: ");
scanf("%d", &grade);
grades[*size] = grade;
(*size)++;
printf("Grade inserted successfully!\n");
}
void delete_grade(int grades[], int *size) {
if (*size == 0) {
printf("Array is empty, cannot delete any grades!\n");
return;
}
int index = 0;
printf("Enter the index of the grade to delete (0-%d): ", *size - 1);
scanf("%d", &index);
if (index < 0 || index >= *size) {
printf("Invalid index, cannot delete the grade!\n");
return;
}
for (int i = index; i < *size - 1; i++) {
grades[i] = grades[i + 1];
}
(*size)--;
printf("Grade deleted successfully!\n");
}
void search_grade(int grades[], int size) {
if (size == 0) {
printf("Array is empty, cannot search for any grades!\n");
return;
}
int grade = 0;
printf("Enter the grade to search: ");
scanf("%d", &grade);
int found = 0;
for (int i = 0; i < size; i++) {
if (grades[i] == grade) {
printf("Grade found at index %d!\n", i);
found = 1;
break;
}
}
if (!found) {
printf("Grade not found!\n");
}
}
void sort_grades(int grades[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (grades[i] > grades[j]) {
int temp = grades[i];
grades[i] = grades[j];
grades[j] = temp;
}
}
}
printf("Grades sorted in ascending order!\n");
}
```
这个程序使用一个一维数组来存储学生成绩,最大大小为10。程序实现了成绩录入、成绩删除、成绩查找、成绩排序、成绩插入的功能,并且使用文本菜单进行交互。
阅读全文