用c语言编程设计一个学生成绩管理系统,进入系统之前要先输入密码
时间: 2023-12-17 16:05:30 浏览: 134
用c语言实现学生成绩管理系统
4星 · 用户满意度95%
好的,我可以为您提供一个简单的学生成绩管理系统的C语言代码,其中包含密码验证功能。代码如下:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int id;
float score;
};
void login(char password[]);
void add_student(struct student students[], int *count);
void display_students(struct student students[], int count);
void search_student(struct student students[], int count);
void save_students(struct student students[], int count);
int main() {
char password[20];
struct student students[MAX_STUDENTS];
int count = 0;
int choice;
login(password);
do {
printf("\n---- Student Grade Management System ----\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Search Student\n");
printf("4. Save Students\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &count);
break;
case 2:
display_students(students, count);
break;
case 3:
search_student(students, count);
break;
case 4:
save_students(students, count);
break;
case 0:
printf("\nExiting the program... Goodbye!\n");
break;
default:
printf("\nInvalid choice. Try again.\n");
}
} while (choice != 0);
return 0;
}
void login(char password[]) {
printf("Enter password: ");
scanf("%s", password);
// Check if password is correct
if (strcmp(password, "123456") != 0) {
printf("\nIncorrect password. Try again.\n");
login(password);
}
}
void add_student(struct student students[], int *count) {
struct student new_student;
printf("\nEnter student name: ");
scanf("%s", new_student.name);
printf("Enter student ID: ");
scanf("%d", &new_student.id);
printf("Enter student score: ");
scanf("%f", &new_student.score);
students[*count] = new_student;
(*count)++;
printf("\nStudent added successfully.\n");
}
void display_students(struct student students[], int count) {
if (count == 0) {
printf("\nNo students found.\n");
return;
}
printf("\n%-20s %-10s %-10s\n", "Name", "ID", "Score");
for (int i = 0; i < count; i++) {
printf("%-20s %-10d %-10.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void search_student(struct student students[], int count) {
int id;
int found = 0;
printf("\nEnter student ID to search: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
printf("\n%-20s %-10s %-10s\n", "Name", "ID", "Score");
printf("%-20s %-10d %-10.2f\n", students[i].name, students[i].id, students[i].score);
found = 1;
break;
}
}
if (!found) {
printf("\nStudent not found.\n");
}
}
void save_students(struct student students[], int count) {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("\nError saving students.\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(file, "%s,%d,%.2f\n", students[i].name, students[i].id, students[i].score);
}
fclose(file);
printf("\nStudents saved successfully.\n");
}
```
在此代码中,我们使用了一个简单的密码验证函数 `login()`,以确保只有已知密码的人才能访问系统。在主函数中,我们使用了一个循环来提供菜单选项,用户可以选择添加学生、显示所有学生、搜索学生、保存学生信息或退出程序。每个选项都有对应的函数,用来执行所需的操作。此代码还包含将学生信息保存到文件的功能,以便后续使用。
请注意,此代码仅提供了一个简单的框架,您可以根据自己的需要进行修改和扩展。
阅读全文