學生資訊管理系統
一、開發語言:C語言
二、開發要求:每人提交一份系統研發報告和原始碼,基於課程設計報告格式,可以建立小組討論。2020年08月10日前上交。
三、開發主要功能如下
系統功能包括:使用者登陸、資訊管理、使用者退出,資訊管理要求實現以下功能:
1.錄入學生資訊:
使用者可以自由輸入學生資訊到系統中。
資訊包括:個人姓名、性別、年齡、學號、成績(這學期所有科目的平均成績)
要求錄入宿舍所有人資訊,成績他人填爲100
2.列印學生資訊:展示系統中的學生資訊。
3.儲存學生資訊:將系統中的學生資訊儲存到本地文件。
4.讀取學生資訊:讀取本地文件中的學生資訊並顯示。
5.統計所有學生人數:計算表中有的數量人數
6.查詢學生資訊:根據使用者給定的資訊(學號)在系統中查詢該學生的資訊
7.修改學生資訊:根據使用者給定的資訊(學號)在系統中查詢該學生的資訊
8.刪除學生資訊:根據使用者給定的資訊(學號)在系統中查詢該學生的資訊
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define LEN 15//賬號密碼,姓名學號長度
#define N 200//人數基數
int n = 0, k = 0;//全域性變數,n學生人數,k賬戶數目
struct User {
char account[LEN];
char password[LEN];
}user1 = {"juzhuohan","zuoye"};
void login() {//使用者登錄
char u_account[LEN], u_password[LEN];
printf("Welcome, enter a valid account to enter the system.\n");
L1: printf("ACCOUNT : ");
scanf_s("%s", u_account, LEN);
if (strcmp(u_account, user1.account) != 0) {
printf("Account error !\n");
printf("Try again !\n");
goto L1;
}
printf("PASSWORD : ");
scanf_s("%s", u_password, LEN);
if (strcmp(u_password, user1.password) == 0) {
printf("welcome . . .");
}
else {
printf("wrong password . ");
printf("Try again !\n");
goto L1;
}
}
struct Student_Info {
char name[LEN];//姓名
char sex[LEN];//性別
int age;//年齡
char student_ID[11];//學號
double grade_average;//平均成績
}SI[N];
void input_info() {//輸入學生資訊
int flag = 0;//選擇
char m[LEN];//字元中轉站
L2: printf("Please enter the student ID to join:");
scanf_s("%s", m, LEN);//要輸入的ID m
if (n == 0) {
strcpy(SI[n].student_ID, m);
printf("Please enter the name of the student to join:");
scanf_s("%s", SI[n].name, LEN);
printf("Please enter the gender of the student to join:");
scanf_s("%s", SI[n].sex, LEN);
printf("Please enter the age of the student to join:");
scanf_s("%d", &SI[n].age);
printf("Please enter the average score of the student name to join:");
scanf_s("%lf", &SI[n].grade_average);
n++;
}
else {
for (int i = 0; i < n; i++) {
if (strcmp(m, SI[i].student_ID) == 0) {
printf("Already have the same ID\n1. Back 2. Cancel input");
scanf("%d", &flag);
if (flag == 1) {
goto L2;
}
else {
break;
}
}
else if (i == n - 1) {//學號不重複
L: strcpy(SI[n].student_ID, m);
printf("Please enter the name of the student to join:");
scanf_s("%s", SI[n].name, LEN);
printf("Please enter the gender of the student to join:");
scanf_s("%s", SI[n].sex, LEN);
printf("Please enter the age of the student to join:");
scanf_s("%d", &SI[n].age);
printf("Please enter the average score of the student name to join:");
scanf_s("%lf", &SI[n].grade_average);
n++;
}
}
}
}
void display_info() {//列印學生資訊
printf("have %d people \n", n);
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%d\t%s\t%.2lf\n", SI[i].name, SI[i].sex, SI[i].age, SI[i].student_ID, SI[i].grade_average);
}
}
void save_file() {//儲存檔案
FILE* write = fopen("01.txt", "w");
for (int i = 0; i < n; i++) {
fprintf_s(write, "%s\t%s\t%d\t%s\t%lf\n", SI[i].name, SI[i].sex, SI[i].age, SI[i].student_ID, SI[i].grade_average);
}
fclose(write);
printf("Saved successfully !\n");
}
void read_file() {//讀取檔案
struct Student_Info buf;//用於儲存讀取的資訊
FILE* read = fopen("01.txt", "r");//只讀開啓檔案
if (read == NULL) {//判斷檔案是否存在
printf("Reading failed!\n");
return;
}
while (fscanf(read, "%s\t%s\t%d\t%s\t%lf\n", buf.name, buf.sex, &buf.age, buf.student_ID, &buf.grade_average) != EOF) {
strcpy(SI[n].name, buf.name);
strcpy(SI[n].sex, buf.sex);
strcpy(SI[n].student_ID, buf.student_ID);
SI[n].age = buf.age;
SI[n].grade_average = buf.grade_average;
n++;
}
printf("Read successfully ! \n");
fclose(read);
}
void find_info() {//查詢學生資訊
int i = 0;
char id[LEN];
printf("What is the student ID to find? ");
scanf_s("%s", id, LEN);
for (i = 0; i < n; i++) {
if (strcmp(id, SI[i].student_ID) == 0) {
printf("This is the student information you are looking for : \n");
printf("%s\t%s\t%d\t%s\t%.2lf\n", SI[i].name, SI[i].sex, SI[i].age, SI[i].student_ID, SI[i].grade_average);
break;
}
else if (i == n - 1) {
printf("There is no student with student ID %s in the list .\n", id);
break;
}
}
}
void change_info() {//修改資訊
int i = 0, j = 0;//回圈 i 和 選擇 j
char id[LEN], m[LEN];//目標id 和 修後的字串值 m
L4: printf("What is the student ID that needs to modify the information ");
scanf_s("%s", id, LEN);//輸入id
for ( i = 0; i < n; i++) {//找序列i
if (strcmp(id, SI[i].student_ID) == 0) {
break;
}
}
L3: printf("What information do you want to change\n1.name 2.sex 3.age 4.student ID 5.grade");
printf("Your choice is ");
scanf_s("%d", &j);
switch (j) {
case 1:
printf("new name :"); scanf_s("%s", m, LEN); strcpy(SI[i].name, m); break;
case 2:
printf("new sex :"); scanf_s("%s", m, LEN); strcpy(SI[i].sex, m); break;
case 3:
printf("new age :"); scanf_s("%d", &SI[i].age); break;
case 4:
printf("new student id :"); scanf_s("%s", SI[i].student_ID, LEN); break;
case 5:
printf("new grade :"); scanf_s("%lf", SI[i].grade_average, LEN); break;
}
printf("Do you still need to change this classmate’s information?\n1.yes 2.others 3.no one ");
scanf("%d", &j);
if (j == 1 ) {
goto L3;
}
else if (j == 2 ) {
goto L4;
}
}
void delete_info() {
int i = 0, j = 0;
char id[LEN];
printf("What is the student ID to delete student information ");
scanf_s("%s", id, LEN);
for (i = 0; i < n; i++) {
if (strcmp(id, SI[i].student_ID) == 0) {
for (j = i; j < n - 1; j++) {
strcpy(SI[j].name, SI[j + 1].name);
strcpy(SI[j].sex, SI[j + 1].sex);
strcpy(SI[j].student_ID, SI[j + 1].student_ID);
SI[j].grade_average = SI[j + 1].grade_average;
SI[j].age = SI[j + 1].age;
}
printf("delete well");
n--;
break;
}
else if (strcmp(id, SI[i].student_ID) != 0 && i == n - 1) {
printf("not find !\n");
}
}
}
void menu() {
printf("0.召喚選單\n");
printf("1.新增資訊\n");
printf("2.列印資訊\n");
printf("3.儲存資訊\n");
printf("4.檢視人數\n");
printf("5.查詢資訊\n");
printf("6.更改資訊\n");
printf("7.刪除資訊\n");
printf("8.退出系統\n");
}
int main() {
L5: login();
read_file();
menu();
int f = 0;
while (f>=0) {
scanf("%d", &f);
switch (f) {
case 0:
menu(); break;
case 1:
input_info(); break;
case 2:
display_info(); break;
case 3:
save_file(); break;
case 4:
printf("There are %d student information", n); break;
case 5:
find_info(); break;
case 6:
change_info(); break;
case 7:
delete_info(); break;
case 8:
save_file(); n = 0; goto L5; break;
}
}
return 0;
}