学生竞赛信息管理系统c语言代码
时间: 2023-06-30 10:10:28 浏览: 77
好的,下面是一个简单的学生竞赛信息管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 30
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
char gender;
int score;
} Student;
int readInt() {
int n;
scanf("%d", &n);
return n;
}
void clearInputBuffer() {
char c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
void printStudent(Student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("Gender: %c\n", s.gender);
printf("Score: %d\n", s.score);
}
void addStudent(Student students[], int *count) {
if (*count >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
printf("Enter student name: ");
fgets(students[*count].name, MAX_NAME_LENGTH, stdin);
students[*count].name[strcspn(students[*count].name, "\n")] = '\0';
printf("Enter student age: ");
students[*count].age = readInt();
printf("Enter student gender (M/F): ");
do {
scanf("%c", &students[*count].gender);
clearInputBuffer();
} while (students[*count].gender != 'M' && students[*count].gender != 'F');
printf("Enter student score: ");
students[*count].score = readInt();
printf("Student added successfully.\n");
(*count)++;
}
void listStudents(Student students[], int count) {
if (count == 0) {
printf("No students to display.\n");
return;
}
printf("List of students:\n");
for (int i = 0; i < count; i++) {
printf("%d. %s\n", i+1, students[i].name);
}
}
void viewStudent(Student students[], int count) {
if (count == 0) {
printf("No students to display.\n");
return;
}
printf("Enter student number to view: ");
int choice = readInt();
if (choice < 1 || choice > count) {
printf("Error: Invalid choice.\n");
return;
}
printf("Student information:\n");
printStudent(students[choice-1]);
}
int main() {
Student students[MAX_STUDENTS];
int count = 0;
int choice;
do {
printf("\nStudent Competition Information Management System\n");
printf("1. Add student\n");
printf("2. List students\n");
printf("3. View student\n");
printf("4. Quit\n");
printf("Enter your choice: ");
choice = readInt();
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
listStudents(students, count);
break;
case 3:
viewStudent(students, count);
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Error: Invalid choice.\n");
break;
}
} while (choice != 4);
return 0;
}
```
这个示例定义了一个 `Student` 结构体,包含学生的姓名、年龄、性别和得分。然后,它实现了添加学生、列出所有学生和查看单个学生的功能。
在 `main()` 函数中,它使用一个菜单来让用户选择要执行的操作。用户可以选择添加学生、列出所有学生、查看单个学生或退出程序。
希望这个示例能够帮助你理解如何创建一个简单的学生竞赛信息管理系统。如果你有更多问题,请随时问我!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)