使用時需要用戶自己在d盤根目錄創建文件“學生信息.txt”
學生信息管理系統開發
學生信息至少包括學號,姓名,性別,計算機分數,數學分數,英語分數等(學號不得相等)。該系統需要能夠提供下列功能:
(1)系統以菜單方式工作(必做)
(2)學生信息錄入功能(學生信息用文件保存)(必做)
(3)學生信息瀏覽功能 (能查看所有同學的記錄)(必做)
(4)學生信息查詢功能,查詢方式:(至少完成一項)
1)按學號查詢
2)按姓名查詢
(5)成績排序統計功能:(至少完成一項)
1)按照指定的要求對學生記錄進行排序
2)按照指定的學科對成績進行統計,需要提供該門成績的:最高分、最低分、平均分、及格率及在五個分數段的學生人數比率([0,59],[60,69],[70,79],[80,89],[90,100])
(6)學生信息刪除,修改功能(可選項)。
其中:學號系統隨機生成,範圍在2016000—2016999內。
源代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 1000
int Total = 0; // 記錄當前學生人數
struct Students
{
int studentID;
char name[20];
char sex;
int computerScore, mathScore, englishScore;
} info[MAX_SIZE];
struct Students t; // 做臨時變量
// 退出
void quit()
{
system("cls");
printf("歡迎使用~再見~!\n");
}
// 導入文件
void loadInformation()
{
FILE *fp;
int i = 0;
fp = fopen("d:\\學生信息.txt", "r"); // r: 只讀
if (fp == NULL)
{
printf("Can not open the file !\n");
exit(0);
}
while (!feof(fp))
{
fscanf(fp, "%d\t%s\t%c\t%d\t%d\t%d", &info[i].studentID, &info[i].name, &info[i].sex, &info[i].computerScore, &info[i].mathScore, &info[i].englishScore);
if (info[i].studentID >= 2016000)
i++;
}
fclose(fp);
Total = i;
}
// 保存到文件
void saveInformation()
{
FILE *fp;
fp = fopen("d:\\學生信息.txt", "wt"); // wt : 清空原有文本並寫入
if (fp == NULL)
{
printf("Connot open file!\n");
exit(0);
}
for (int i = 0; i < Total; i++)
{
fprintf(fp, "%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}
fclose(fp);
}
// 瀏覽所有學生信息
void displayAll()
{
printf("\n學生信息:\n學號\t姓名\t性別\t計算機\t數學\t英語\n\n");
for (int i = 0; i < Total; i++)
{
printf("%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}
}
// 判斷是否爲空!
bool isEmpty()
{
if (Total != 0) return false;
return true;
}
// 判斷學生信息是否存在
int isExist(int studentID)
{
for (int i = 0; i < Total; i++)
{
if (studentID == info[i].studentID) return i;
}
return -1;
}
// 判斷分數是否輸入正確
bool isRightScore(int score)
{
if ((score < 0) || (score > 100)) return false;
else return true;
}
// 產生隨機數
int random()
{
srand((unsigned)time(NULL));
return rand() % 1000 + 2016000;
}
// 產生隨機學號
void getStudentID(int i)
{
int studentID = random();
while (isExist(studentID) != -1)
studentID = random();
info[i].studentID = studentID;
}
// 輸入學生信息
void inputInfomation(int i)
{
printf("請輸入第 %d 位學生信息: \n", i+1);
printf("學 號: %d\n", info[i].studentID);
printf("姓 名: ");
scanf("%s", info[i].name);
printf("性 別: ");
getchar();
scanf("%c", &info[i].sex);
while (true)
{
printf("計算機分數: ");
scanf("%d", &info[i].computerScore);
if (!isRightScore(info[i].computerScore))
printf("請輸入正確的分數,正確的分數範圍:[0,100]\n");
else break;
}
while (true)
{
printf("數 學 分數: ");
scanf("%d", &info[i].mathScore);
if (!isRightScore(info[i].mathScore))
printf("請輸入正確的分數,正確的分數範圍:[0,100]\n");
else break;
}
while (true)
{
printf("英 語 分數: ");
scanf("%d", &info[i].englishScore);
if (!isRightScore(info[i].englishScore))
printf("請輸入正確的分數,正確的分數範圍:[0,100]\n");
else break;
}
}
// 做排序交換
void doExchange(int i, int j)
{
t = info[i]; info[i] = info[j]; info[j] = t;
}
// 按姓名的首字母排序
void sortInformationByName()
{
int i, j;
for (i = 0; i < Total; i++)
for (j = 1; j < Total - i; j++)
{
if (strcmp(info[j].name, info[j - 1].name) < 0)
doExchange(j, j - 1);
}
displayAll();
}
// 刪除學生信息
void deleteInformationByID()
{
int studentID, index;
printf("請輸入您要刪除學生的學號: ");
scanf("%d", &studentID);
if ((index = isExist(studentID)) != -1)
{
for (index; index < Total - 1; index++)
{
info[index] = info[index+1];
}
saveInformation();
printf("刪除成功!");
--Total;
}
else
printf("該學生信息不存在!請檢查是否輸入正確!\n");
}
// 添加學生信息
void addInformation()
{
int add_num;
printf("請輸入要添加信息的條數: ");
scanf("%d", &add_num);
for (int i = Total; i < Total+add_num; i++)
{
getStudentID(i);
inputInfomation(i);
}
Total += add_num;
saveInformation();
printf("添加成功!\n");
}
// 修改學生信息
void modifyInfomationByID()
{
int studentID, index;
printf("請輸入被修改學生的學號或姓名: ");
scanf("%d", &studentID);
if ((index = isExist(studentID)) != -1)
{
printf("您將修改此學生的信息: %d(學號)\t%s(姓名)\n", info[index].studentID, info[index].name);
inputInfomation(index);
saveInformation();
printf("修改完成!");
}
else
printf("此學生信息不存在!請檢查輸入是否正確!");
}
// 每個分數段的人數比率
void computerScore_EachRate()
{
int number_60 = 0, number_70 = 0, number_80 = 0, number_90 = 0, number_100 = 0; // 低於各分值的人數
double rate;
for (int i = 0; i < Total; i++)
{
if ((info[i].computerScore >= 0) && info[i].computerScore < 60)
++number_60;
else if (info[i].computerScore < 70)
++number_70;
else if (info[i].computerScore < 80)
++number_80;
else if (info[i].computerScore < 90)
++number_90;
else if (info[i].computerScore <= 100)
++number_100;
}
rate = (number_60*1.0) / Total;
printf("[0,60): %4lf\%\n", rate * 100);
rate = (number_70*1.0) / Total;
printf("[60,70): %4lf\%\n", rate * 100);
rate = (number_80*1.0) / Total;
printf("[70,80): %4lf\%\n", rate * 100);
rate = (number_90*1.0) / Total;
printf("[80,90): %4lf\%\n", rate * 100);
rate = (number_100*1.0) / Total;
printf("[90,100): %4lf\%\n", rate * 100);
}
// 計算機的及格率
double computerScore_Rate()
{
int number = 0; // 及格人數
double rate;
for (int i = 0; i < Total; i++)
{
if ( info[i].computerScore >= 60)
{
++number;
}
}
rate = (number*1.0) / Total;
return rate; // 用百分號(%)可以乘以一百再返回
}
// 計算機的平均分
double computerScore_Average()
{
int sum = 0;
double average;
for (int i = 0; i < Total; i++)
sum += info[i].computerScore;
average = (sum*1.0) / Total;
return average;
}
// 計算機的最低分
int computerScore_MIN()
{
int minComputerScore = 100;
for (int i = 0; i < Total; i++)
{
if (info[i].computerScore <= minComputerScore)
minComputerScore = info[i].computerScore;
}
return minComputerScore;
}
// 計算機的最高分
int computerScore_MAX()
{
int maxComputerScore = 0;
for (int i = 0; i < Total; i++)
{
if (info[i].computerScore >= maxComputerScore )
maxComputerScore = info[i].computerScore;
}
return maxComputerScore;
}
// 顯示某學生的信息
void displayOne(int i)
{
printf("\n學生信息:\n學號\t姓名\t性別\t計算機\t數學\t英語\n\n");
printf("%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}
// 查找學生信息(按學號查找)
void searchInformationByID()
{
int studentID;
int index;
printf("請輸入學號: ");
scanf("%d", &studentID);
index = isExist(studentID);
if (index != -1)
displayOne(index);
else
printf("此學生信息不存在!請檢查輸入是否有誤!\n");
}
// 錄入學生信息
void getInformation()
{
int num;
char choose;
if ( !isEmpty() )
{
printf("是否清空所有信息重新錄入:y 是\tn 否\n");
getchar();
choose = getchar();
if (choose == 'Y' || choose == 'y')
{
Total = 0;
printf("請輸入您要錄入的人數: ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
getStudentID(i);
inputInfomation(i);
}
saveInformation();
Total += num;
printf("重新錄入成功!\n");
}
else
printf("已取消重新錄入!\n");
}
else
{
printf("請輸入您要錄入的人數: ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
getStudentID(i);
inputInfomation(i);
}
saveInformation();
Total += num;
printf("錄入成功!\n");
}
}
// 菜單
int menu()
{
int select = 0;
printf("_______________________________________________________________________________\n");
printf("*************************歡迎使用學生信息管理系統******************************\n");
printf(" \t╔════════════════════╗\n");
printf(" \t║1.學生信息錄入 ");
printf(" 2.學生信息添加 ║\n");
printf(" \t║3.學生信息瀏覽 ");
printf(" 4.學生信息查詢 ║\n");
printf(" \t║5.成績排序統計 ");
printf(" 6.刪除學生信息 ║\n");
printf(" \t║7.修改信息 ");
printf(" 0.退出系統 ║\n");
printf(" \t╚════════════════════╝\n");
printf("*******************************************************************************\n");
printf("_______________________________________________________________________________\n");
printf("請選擇:");
scanf("%d", &select);
return select;
}
int main()
{
loadInformation();
for (;;)
{
system("color a");
system("pause");
system("cls");
printf("_______________________________________________________________________________\n");
//printf("現有學生信息條數:%d\n",w);
switch (menu())
{
case 1:getInformation(); break;
case 2:addInformation(); break;
case 3:displayAll(); break;
case 4:searchInformationByID(); break;
case 5:sortInformationByName(); break;
case 6:deleteInformationByID(); break;
case 7:modifyInfomationByID(); break;
//case 8:rekey(); break;
case 0:quit(); exit(0);
default:
printf("您輸入有誤!\n");
}
}
return 0;
}