c语言学生信息管理系统

一整天搞完了这个系统,难点在于对一个流文件进行记录化,也就是按记录写文件以及按记录读到内存中。

整体思想就是,一切对信息的操作,都是先把所有记录读到内存中并拉成链表,一切操作都在链表上做。

这就进一步要求了链表操作的基本功,好在哥之前把链表基本操作都实现了,现在觉得受益匪浅。

代码如下,bug很多,懒得改了,核心思想已经实现了。


// SIMSwork.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct Account{
char usrname[20];
char passwd[20];
char ismger[20];
Account * nextAcnt;
}Account,*pAccount;
typedef struct Stu{
char name[30];
char ID[20];
float grade;
Stu * nextStu;
}Stu,*pStu;


//把链表里的内容写入文件
void writeFile(int i,pStu stuhead,pAccount head){
FILE *fp;
pStu stuw;
pAccount accw;
stuw=(pStu)malloc(sizeof(Stu));
accw=(pAccount)malloc(sizeof(Account));
stuw=stuhead;
accw=head;
if(i==0){ //写student文件
fp=fopen("e:\\student.txt","wb");
while ((*stuw).nextStu!=NULL){
fwrite(stuw,sizeof(Stu),1,fp);
stuw=(*stuw).nextStu;
}fwrite(stuw,sizeof(Stu),1,fp);
fclose(fp);
}
if(i==1){ // 写account文件
fp=fopen("e:\\usr_account.txt","wb");
while ((*accw).nextAcnt!=NULL){
fwrite(accw,sizeof(Account),1,fp);
accw=(*accw).nextAcnt;
}fwrite(accw,sizeof(Account),1,fp);
fclose(fp);
}
}


//*****************************************关于student的代码***************************************
//在内存的student链表中头插一个节点,并把其写入文件中(添加到文件末尾)
void AddStu(pStu head){
pStu p1,temp;
FILE *fp;
if((fp=fopen("e:\\student.txt", "ab+"))==NULL){
printf("cannot open file");/*出错返回*/
getchar();
exit(1);
    }
p1=(pStu)malloc(sizeof(Stu));
memset(p1,0,sizeof(Stu));


printf("\ninput student name: ");
scanf ("%s",(*p1).name);
printf("\ninput ID: ");
scanf ("%s",(*p1).ID);
printf("\ninput grade ");
scanf ("%f",&(*p1).grade);

(*p1).nextStu=(*head).nextStu;
(*head).nextStu=p1;


fwrite(p1,sizeof(Stu),1,fp);


fclose(fp);
}
//求内存中student链表长度
int StuListLength(pStu head){
pStu work;
work=head;
int i=0;
while ((*work).nextStu!=NULL){
i++;
work=(*work).nextStu;
}
return i;
}


//把student文件读出来创建student链表
void creatStuNode(pStu head,pStu work){ //创建一个节点,把work节点赋值给它,并把它连到head后面
pStu temp=(pStu)malloc(sizeof(Stu));
memset(temp,0,sizeof(Stu));
int i=0;
while ((*work).name[i]!=NULL){
(*temp).name[i]=(*work).name[i];
i++;
}
i=0;
while ((*work).ID[i]!=NULL){
(*temp).ID[i]=(*work).ID[i];
i++;
}
(*temp).grade=(*work).grade;
(*temp).nextStu=(*head).nextStu;
(*head).nextStu=temp;
}
void creatStuList(pStu head){
FILE *fp;
char ch;
if((fp=fopen("e:\\student.txt", "ab+"))==NULL){
printf("cannot open file");/*出错返回*/
getchar();
exit(1);
    }


pStu work;
work=(pStu)malloc(sizeof(Stu));
memset(work,0,sizeof(Stu));


while(fread(work,sizeof(Stu),1,fp))
creatStuNode(head,work);

fclose(fp);
}


//打印所有student(仅姓名)在系统中并不直接使用,只供测试链表用
void printStuList(pStu head){
pStu work;
work=(pStu)malloc(sizeof(Stu));
work =head;
while ((*work).nextStu!=NULL){
printf ("\n%20s ",(*work).name);
printf ("%20s ",(*work).ID);
printf ("%20f ",(*work).grade);
work=(*work).nextStu;
}
printf ("\n%20s ",(*work).name);
printf ("%20s  ",(*work).ID);
printf ("%20f ",(*work).grade);
}


//删除student,在内存中删除一个节点,将链表重新写入文件
void delStu(pStu head){
char check[20]={NULL};
pStu work=(pStu)malloc(sizeof(Stu));
memset(work,0,sizeof(Stu));
pStu temp=(pStu)malloc(sizeof(Stu));
memset(temp,0,sizeof(Stu));


work=(*head).nextStu;
temp=head;
printf("input the name you want to delete: ");
scanf("%s",check);
while ((*work).nextStu!=NULL){
if(!strcmp((*work).name,check)){
(*temp).nextStu=(*work).nextStu;
free(work);
goto w;
}
temp=work;
work=(*work).nextStu;
}if(!strcmp((*work).name,check)){
(*temp).nextStu=(*work).nextStu;
free(work);
}else
printf("\nThe one you want to delete doesn't exist ");


w:  writeFile(0,head,NULL);
}


//更新student,在内存中修改一个节点,将链表重新写入文件
void updStu(pStu head){
char check[20]={NULL};
pStu work=(pStu)malloc(sizeof(Stu));
memset(work,0,sizeof(Stu));


work=(*head).nextStu;


printf("input the name of student you want to update: ");
scanf("%s",check);
while ((*work).nextStu!=NULL){
if(!strcmp((*work).name,check)){
printf("\ninput student name: ");
scanf ("%s",(*work).name);
printf("\ninput ID: ");
scanf ("%s",(*work).ID);
printf("\ninput grade ");
scanf ("%f",&(*work).grade);
goto w;
}
work=(*work).nextStu;
}if(!strcmp((*work).name,check)){
printf("\ninput student name: ");
scanf ("%s",(*work).name);
printf("\ninput ID: ");
scanf ("%s",(*work).ID);
printf("\ninput grade ");
scanf ("%f",&(*work).grade);
}else
printf("\nThe one you want to update doesn't exist ");

w:
writeFile(0,head,NULL);
}
//查找student,按姓名查找,输出信息
void searchStu(pStu stuhead){
pStu work;
char check[20];
work=(pStu)malloc(sizeof(Stu));
memset(work,0,sizeof(Stu));
work=stuhead;
printf("\n Input the name you want to search :");
scanf("%s",check);
while ((*work).nextStu!=NULL){
if(!strcmp((*work).name,check)){
printf ("\n%20s ",(*work).name);
printf ("%20s ",(*work).ID);
printf ("%20f \n",(*work).grade);
goto w;
}
work=(*work).nextStu;
}if(!strcmp((*work).name,check)){
printf ("\n%20s ",(*work).name);
printf ("%20s ",(*work).ID);
printf ("%20f \n",(*work).grade);
}else
printf("\nThe one you searched doesn't exist ");
w: int i=0;
}


//*****************************************关于用户的代码********************************************
//在内存的account链表中头插一个节点,并把其写入文件中(添加到文件末尾)
void AddAccount(pAccount head){
pAccount p1,temp;
FILE *fp;
if((fp=fopen("e:\\usr_account.txt", "ab+"))==NULL){
printf("cannot open file");/*出错返回*/
getchar();
exit(1);
    }
p1=(pAccount)malloc(sizeof(Account));
memset(p1,0,sizeof(Account));


printf("\ninput user name: ");
scanf ("%s",(*p1).usrname);
printf("\ninput password: ");
scanf ("%s",(*p1).passwd);
printf("\ninput user type ");
scanf ("%s",(*p1).ismger);

(*p1).nextAcnt=(*head).nextAcnt;
(*head).nextAcnt=p1;


fwrite(p1,sizeof(Account),1,fp);


fclose(fp);
}


//求内存中account链表长度
int AccListLength(pAccount head){
pAccount work;
work=head;
int i=0;
while ((*work).nextAcnt!=NULL){
i++;
work=(*work).nextAcnt;
}
return i;
}


//把account文件读出来创建account链表
void creatNode(pAccount head,pAccount work){ //创建一个节点,把work节点赋值给它,并把它连到head后面
pAccount temp=(pAccount)malloc(sizeof(Account));
memset(temp,0,sizeof(Account));
int i=0;
while ((*work).usrname[i]!=NULL){
(*temp).usrname[i]=(*work).usrname[i];
i++;
}
i=0;
while ((*work).passwd[i]!=NULL){
(*temp).passwd[i]=(*work).passwd[i];
i++;
}
i=0;
while ((*work).ismger[i]!=NULL){
(*temp).ismger[i]=(*work).ismger[i];
i++;
}
(*temp).nextAcnt=(*head).nextAcnt;
(*head).nextAcnt=temp;
}
void creatAccList(pAccount head){
FILE *fp;
char ch;
if((fp=fopen("e:\\usr_account.txt", "ab+"))==NULL){
printf("cannot open file");/*出错返回*/
getchar();
exit(1);
    }


pAccount work;
work=(pAccount)malloc(sizeof(Account));
memset(work,0,sizeof(Account));


while(fread(work,sizeof(Account),1,fp))
creatNode(head,work);

fclose(fp);
}


//打印所有用户(仅用户名)在系统中并不直接使用,只供测试链表用
void printAccList(pAccount head){
pAccount work;
work=(pAccount)malloc(sizeof(Account));
work =head;
while ((*work).nextAcnt!=NULL){
printf ("\n%s",(*work).usrname);
work=(*work).nextAcnt;
}printf("\n%s",(*work).usrname);
}


//删除一个用户
void delAcc(pAccount head){
char check[20]={NULL};
pAccount work=(pAccount)malloc(sizeof(Account));
memset(work,0,sizeof(Account));
pAccount temp=(pAccount)malloc(sizeof(Account));
memset(temp,0,sizeof(Account));


work=(*head).nextAcnt;
temp=head;
printf("input the name you want to delete: ");
scanf("%s",check);
while ((*work).nextAcnt!=NULL){
if(!strcmp((*work).usrname,check)){
(*temp).nextAcnt=(*work).nextAcnt;
free(work);
goto w;
}
temp=work;
work=(*work).nextAcnt;
}if(!strcmp((*work).usrname,check)){
(*temp).nextAcnt=(*work).nextAcnt;
free(work);
}else
printf("\nThe one you want to delete doesn't exist ");
w: int i=0;
writeFile(1,NULL,head);
}


//修改一个用户
void updAcc(pAccount head){
char check[20]={NULL};
pAccount work=(pAccount)malloc(sizeof(Account));
memset(work,0,sizeof(Stu));


work=(*head).nextAcnt;


printf("input the name you want to update: ");
scanf("%s",check);
while ((*work).nextAcnt!=NULL){
if(!strcmp((*work).usrname,check)){
printf("\ninput user name: ");
scanf ("%s",(*work).usrname);
printf("\ninput password: ");
scanf ("%s",(*work).passwd);
printf("\ninput account type ");
scanf ("%s",(*work).ismger);
goto w;
}
work=(*work).nextAcnt;
}if(!strcmp((*work).usrname,check)){
printf("\ninput usr name: ");
scanf ("%s",(*work).usrname);
printf("\ninput password: ");
scanf ("%s",(*work).passwd);
printf("\ninput account type ");
scanf ("%s",(*work).ismger);
}else
printf("\nThe one you want to update doesn't exist ");
w: int i=0;
writeFile(1,NULL,head);
}


//判断是哪个类型的用户,在这个函数里实现最初登陆界面,如果是非法用户就爆炸
void delay(){
int i=0,j=0,k=0;
for (i;i<90000000;i++)
for (j;j<90000000;j++)
for (k;k<100000000;k++);
}
void Bomb(){
int i=0;
printf("\n用户名或密码错误,炸弹即将爆炸\n");
for (i;i<10;i++){
printf ("还有%d秒炸弹爆炸,非战斗人员请迅速撤离\n\n",10-i);
delay();
}
for (i=0;i<19999;i++)
printf("**********************");
exit(1);
}
void StuInterface(pStu stuhead){
int k=0;
printf("\n********************************");
printf("\n###---------Student----------###");
printf("\n********************************");
printf("\n1.Search \n0.Exit\n");
scanf ("%d",&k);
if(k)
searchStu(stuhead);
else 
exit(1);
}
void AdminInterface(pAccount head,pStu stuhead){
int k=0;
printf("\n********************************");
printf("\n###---------Administrator-------###");
printf("\n********************************");
printf("\n1.Search student \n2.add student\n3.update student\n4.delete student \n5.add account\n6.update account\n7.delete account \n8.search account\n9.display students \n0.Exit \n");
scanf ("%d",&k);
switch (k){
case 1:
searchStu(stuhead);
break;
case 2:
AddStu(stuhead);
break;
case 3:
updStu(stuhead);
break;
case 4:
delStu(stuhead);
break;
case 5:
AddAccount(head);
break;
case 6:
updAcc(head);
break;
case 7:
delAcc(head);
break;
case 8:
printAccList(head);
break;
case 9:
printStuList(stuhead);
break;
default:
exit(1);
break;
}

}
int isAccount(pAccount head,pStu stuhead){
pAccount work;
char k[20]={NULL};
char m[20]={NULL};
char n[20]="admin";
char ch;
int i=0;
work=(pAccount)malloc(sizeof(Account));
memset(work,0,sizeof(Account));
work=head;


printf("\n**************************************************************\n");
printf("||       Student Information Management System              || \n");
printf("||                                                          || \n");
printf("||         Designed and implemented by Chris Shu            || \n");
printf("************************************************************** \n");


printf("user name: ");
scanf("%s",k);
//在已经做好的链表里查,遍历所有节点,遇到用户名符合的,再让输入密码,密码符合的返回其用户类型
while((*work).nextAcnt!=NULL){
if(!strcmp(k,(*work).usrname)){
printf("\npassword: ");
i=0;
while ((ch=getch())!='\r'){
m[i]=ch;
i++;
putchar('*');
}
//scanf("%s",m);
if(!strcmp(m,(*work).passwd)){
if(!strcmp(n,(*work).ismger)){
AdminInterface(head,stuhead);
return 1;
}else {
StuInterface(stuhead);
return 0;
}
}else
goto w;
}
work=(*work).nextAcnt;
} if(!strcmp(k,(*work).usrname)){ //作为while 循环的补充,因为最后一个节点while无法遍历到
printf("\npassword: ");
i=0;
 scanf("%s",m);
if(!strcmp(m,(*work).passwd)){
if(!strcmp(n,(*work).ismger)){
AdminInterface(head,stuhead);
return 1;
}
else {
StuInterface(stuhead);
return 0;
}
}else
goto w;
}else
goto w;
w: Bomb();
}
 








int _tmain(int argc, _TCHAR* argv[])
{


pAccount head;
head=(pAccount)malloc(sizeof(Account));
memset(head,0,sizeof(Account));
creatAccList(head); //先把account文件导出来拉链,一切操作都在内存中针对链表做


pStu stuhead;
stuhead=(pStu)malloc(sizeof(Stu));
memset(stuhead,0,sizeof(Stu));
creatStuList(stuhead); //先把student文件导出来拉链,一切操作都在内存中针对链表做




/*printAccList(head);
printStuList(stuhead);
*/

isAccount(head,stuhead);





system("pause");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值