學生資訊管理系統(C語言版)自我改動篇
原版鏈接
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 15
#define N 50
int k = 1, n = 0, m = 0;
char filename1[2 * LEN + 1];
int flush();
void seek();
void modify();
void insert();
void del();
void display();
void save();
void menu();
void transform();
struct student {
char StudentId[LEN + 1];
char StudentName[LEN + 1];
int StudentAge;
char StudentSex[LEN + 1];
float score[3];
}stu[N];
int main()
{
while(k) {
menu();
}
system("pause");
return 0;
}
void help()
{
char *message = {
"\n0.歡迎使用系統幫助!\n"
"\n1.初次進入系統後,請先選擇增加學生資訊;\n"
"\n2.按照選單提示鍵入數位代號;\n"
"\n3.增加學生資訊後,切記儲存;\n"
"\n4.謝謝您的使用!\n"
};
printf(message);
}
int flush()
{
char filename[LEN + 1];
int i = 0;
int j = 0;
FILE *fp;
printf("請輸入要重新整理學生資訊的檔名:\n");
scanf("%s",filename);
transform(filename);
if((fp = fopen(filename1,"r+")) != NULL) {
printf("檔案開啓成功!!!\n");
}
while(fscanf(fp,"%s %s %d %s %f %f %f\n",stu[j].StudentId,stu[j].StudentName,&stu[j].StudentAge,stu[j].StudentSex,
&stu[j].score[0],&stu[j].score[1],&stu[j].score[2]) == 7) {
j++;
}
n = j;
printf("重新整理成功!!!\n");
return n;
}
void seek()
{
int i,item,flag;
char s1[LEN + 1];
char *message = {
"----------------------\n"
"-----1.按學號查詢-----\n"
"-----2.按姓名查詢-----\n"
"-----3.退出本選單-----\n"
"----------------------\n"
};
printf(message);
while(1) {
printf("請選擇子選單編號:");
scanf("%d",&item);
flag = 0;
switch(item)
{
case 1:
printf("請輸入要查詢的學生的學號:\n");
scanf("%s",s1);
for(i = 0; i < n; i++) {
if(strcmp(s1,stu[i].StudentId) == 0) {
flag = 1;
printf("學生學號\t學生姓名\t年齡\t性別\tC語言成績\t高等數學\t大學英語成績\n");
printf("--------------------------------------------------------------------\n");
printf("%s\t%s\t%d\t%s\t%.1f\t%.1f\t%.1f\n", stu[i].StudentId, stu[i].StudentName, stu[i].StudentAge,
stu[i].StudentSex, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
}
if(flag == 0) {
printf("該學號不存在!!\n");
}
break;
case 2:
printf("請輸入要查詢的學生的姓名:\n");
scanf("%s",s1);
for(i = 0; i < n; i++) {
if(strcmp(stu[i].StudentName,s1) == 0) {
flag = 1;
printf("學生學號\t學生姓名\t年齡\t性別\tC語言成績\t高等數學\t大學英語成績\n");
printf("--------------------------------------------------------------------\n");
printf("%s\t%s\t%d\t%s\t%.1f\t%.1f\t%.1f\n", stu[i].StudentId, stu[i].StudentName, stu[i].StudentAge,
stu[i].StudentSex, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
}
if(flag == 0) {
printf("該姓名不存在!!\n");
}
break;
case 3:
return ;
default:
printf("請在1-3之間選擇\n");
}
}
}
void modify()
{
int i, item, num = -1;
char sex1[LEN + 1], s1[LEN + 1], s2[LEN + 1];
float score1;
printf("請輸入要修改的學生的學號");
scanf("%s",s1);
for(i = 0; i < n; i++) {
if(strcmp(stu[i].StudentId,s1) == 0) {
num = i;
printf("------------------\n");
printf("1.修改姓名\n");
printf("2.修改年齡\n");
printf("3.修改性別\n");
printf("4.修改C語言成績\n");
printf("5.修改高等數學成績\n");
printf("6.修改大學英語成績\n");
printf("7.退出本選單\n");
printf("------------------\n");
while(1) {
printf("請選擇子選單編號:");
scanf("%d",&item);
switch(item)
{
case 1:
printf("請輸入新的姓名:\n");
scanf("%s", s2);
strcpy(stu[num].StudentName, s2);
break;
case 2:
printf("請輸入新的年齡:\n");
scanf("%d", &stu[num].StudentAge);
break;
case 3:
printf("請輸入新的性別:\n");
scanf("%s", sex1);
strcpy(stu[num].StudentSex, sex1);
break;
case 4:
printf("請輸入新的C語言成績:\n");
scanf("%f", &score1);
stu[num].score[0] = score1;
break;
case 5:
printf("請輸入新的高等數學成績:\n");
scanf("%f", &score1);
stu[num].score[1] = score1;
break;
case 6:
printf("請輸入新的大學英語成績:\n");
scanf("%f", &score1);
stu[num].score[2] = score1;
break;
case 7:
printf("若修改完畢,請及時儲存!");
return;
default:
printf("請在1-7之間選擇\n");
}
}
break;
}
if(n == i) {
printf("沒有該生學號!!!!");
}
}
}
void sort()
{
int i, j, k, *p, *q, s;
char temp[LEN + 1], ctemp[LEN + 1];
float ftemp;
for(i = 0; i < n - 1; i++) {
for(j = n - 1; j > i; j--) {
if(strcmp(stu[j - 1].StudentId, stu[j].StudentId) > 0) {
strcpy(temp, stu[j - 1].StudentId);
strcpy(stu[j - 1].StudentId, stu[j].StudentId);
strcpy(stu[j].StudentId, temp);
strcpy(temp, stu[j - 1].StudentName);
strcpy(stu[j - 1].StudentName, stu[j].StudentName);
strcpy(stu[j].StudentName, temp);
strcpy(ctemp,stu[j - 1].StudentSex);
strcpy(stu[j - 1].StudentSex,stu[j].StudentSex);
strcpy(stu[j].StudentSex,ctemp);
p = &stu[j - 1].StudentAge;
q = &stu[j].StudentAge;
s = *q;
*q = *p;
*p = s;
for(k = 0; k < 3; k++) {
ftemp = stu[j - 1].score[k];
stu[j - 1].score[k] = stu[j].score[k];
stu[j].score[k] = ftemp;
}
}
}
}
}
void insert()
{
int i = n, j, flag;
printf("請輸入待增加的學生數:\n");
scanf("%d",&m);
if(m > 0) {
do
{
flag = 1;
while(flag) {
flag = 0;
printf("請輸入第%d位學生的學號:\n",i + 1);
scanf("%s",stu[i].StudentId);
for(j = 0; j < i; j++) {
if(strcmp(stu[i].StudentId, stu[j].StudentId) == 0) {
printf("該學號已存在,請重新輸入!!\n");
flag = 1;
break;
}
}
}
printf("請輸入第%d位學生的姓名:\n",i + 1);
scanf("%s",stu[i].StudentName);
printf("請輸入第%d位學生的年齡:\n",i + 1);
scanf("%d",&stu[i].StudentAge);
printf("請輸入第%d位學生的性別:\n",i + 1);
scanf("%s",stu[i].StudentSex);
printf("請輸入第%d 個學生的C語言成績\n",i+1);
scanf("%f", &stu[i].score[0]);
printf("請輸入第%d 個學生的高等數學成績:\n", i+1);
scanf("%f", &stu[i].score[1]);
printf("請輸入第%d 個學生的大學英語成績:\n", i+1);
scanf("%f", &stu[i].score[2]);
if(flag == 0) {
i++;
}
} while(i < n + m);
}
n += m;
printf("學生資訊增加完畢!!請及時儲存");
system("pause");
}
void del()
{
int i, j, flag = 0;
char s1[LEN + 1];
printf("請輸入你要刪出學生的學號:");
scanf("%s",s1);
for(i = 0; i < n; i++) {
if(strcmp(stu[i].StudentId, s1) == 0) {
flag = 1;
for(j = i;j < n -1;j++) {
stu[j] = stu[j + 1];
}
}
}
if(0 == flag) {
printf("沒有該生學號!!!\n");
}
if(1 == flag) {
printf("刪除成功!!!");
n--;
}
system("pause");
}
void display()
{
int i;
printf("共有%d位學生的資訊\n",n);
if(0 != n) {
printf("學生學號 學生姓名 年齡 性別 C語言成績 高等數學 大學英語成績\n");
printf("---------------------------------------------------------------------\n");
for(i = 0; i < n; i++) {
printf("%s \t%s \t%d \t%s \t%.1f \t%.1f \t%.1f\n", stu[i].StudentId, stu[i].StudentName,
stu[i].StudentAge, stu[i].StudentSex, stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
}
system("pause");
}
void save()
{
int i;
FILE *fp;
char filename[LEN + 1];
printf("請輸入要儲存的檔名:");
scanf("%s",filename);
transform(filename);
fp = fopen(filename1, "w+");
for(i = 0; i < n; i++) {
fprintf(fp, "%s \t%s \t%d \t%s \t%.lf \t%.lf \t%.lf \n", stu[i].StudentId, stu[i].StudentName, stu[i].StudentAge,
stu[i].StudentSex, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
printf("儲存成功!!\n");
fclose(fp);
system("pause");
}
void menu()
{
int num;
char *message = {
" \n\n \n\n"
" ******************************************************\n\n"
" * 學生資訊管理系統 *\n \n"
" ******************************************************\n\n"
"*********************系統功能選單*************************\n"
" ---------------------- ---------------------- \n"
" ********************************************* \n"
" * 0.系統幫助及說明 * * 1.重新整理學生資訊 * \n"
" ********************************************* \n"
" * 2.查詢學生資訊 * * 3.修改學生資訊 * \n"
" ********************************************* \n"
" * 4.增加學生資訊 * * 5.按學號刪除資訊 * \n"
" ********************************************* \n"
" * 6.顯示當前資訊 * * 7.儲存當前學生資訊* \n"
" ********************** ********************** \n"
" * 8.按照學號排列 * * 9.退出系統 * \n"
" ********************** ********************** \n"
" ---------------------- ---------------------- \n"
};
printf(message);
printf("請選擇選單編號:");
scanf("%d",&num);
switch(num)
{
case 0:
help();break;
case 1:
flush();break;
case 2:
seek();break;
case 3:
modify();break;
case 4:
insert();break;
case 5:
del();break;
case 6:
display();break;
case 7:
save();break;
case 8:
sort();break;
case 9:
k = 0;
printf("即將退出程式!\n");
break;
default:printf("請在0-7中選擇\n");
}
}
void transform(char filename[])
{
int i;
char *s = filename, *p = filename1;
for(;s < filename + strlen(filename); s++) {
*p = *s;
if(*s == '\\') {
*(p + 1) = '\\';
p++;
}
p++;
}
*p = '\0';
}