您的位置:首页 > 编程语言 > C语言/C++

小程序:学生信息管理系统--C语言版本(升级版)

2012-08-19 19:27 639 查看
这个升级版本很勉强,文件的读写那一块不是很满意,但功能上确实有所增加,所以算个升级版本了。

以下是版本一的链接:/article/11464668.html

/*
*************************************************************
**                学生信息管理系统_V2                      **
**********************V2 PK V1*******************************
**1.添加学生时,首先判断学号是否存在,如存在则提示用户;    **
**2.实现学生信息修改功能;                                  **
**3.实现学生信息删除功能;                                  **
*************************************************************

**这几天工作事情比较多,趁着周末做个升级版吧^-^
**因为看到有二百多人看过这个程序,我表示有惊喜也有压力.
**希望大家能看完我的程序后很不屑,然后写出比我好许多的,嘻嘻,上程序!!!
*/

#include"StuManageSys.h"

//用"a+"权限来打开“学生信息”记录表
//这里我这么写是为了以“追加”的方式(即不覆盖之前的数据信息),把用户添加的学生添加到“学生信息”记录表中
void aOpenFile()
{
fp=fopen(fileName,"a+");//fileName定义为全局变量,在整个程序运行过程中,保存着上次用户创建或正确打开文件的名称
if(NULL==fp)
{
printf("\n文件以'a+'模式打开失败\n");
}
}

//用"r+"权限来打开“学生信息”记录表
void rOpenFile()
{
fp=fopen(fileName,"r+");
if(NULL==fp)
{
printf("\n文件以'r+'模式打开失败\n");
}
}

//系统初始化
void initSys()
{
bool flag = true;

printf("请输入学生信息记录表的名称(filename.txt):");
scanf("%s",fileName);
//printf("用户输入的信息记录表名称是:%s",fileName);

//fopen 函数定义: FILE *fopen( const char *fname, const char *mode );
/*功能说明: fopen()函数打开由fname(文件名)指定的文件, 并返回一个关联该文件的流.
**如果发生错误, fopen()返回NULL. mode(方式)是用于决定文件的用途(例如 用于输入,输出,等等)
**Point 1:是"r",不是'',我一开始在这里犯错啦~~
**Point 2:和fclose配合使用哈^^
*/
fp=fopen(fileName,"r");

if(NULL==fp){
printf("\n文件打开失败\n");
printf("1.文件名输入有误,重新输入;\n");
printf("2.以该文件名创建新文件;\n");
printf("请键入符合您需要的功能编号:");

while(flag)
{
int choose = 0;
choose = getchar();
//printf("choose = %d",choose);
switch(choose)
{
case 10:
break;
case 49:
printf("请输入学生信息记录表的名称(filename.txt):");
scanf("%s",fileName);
fp = fopen(fileName,"r");
if(NULL!=fp)
flag = false;
else
{
printf("\n文件打开失败\n");
printf("1.文件名输入有误,重新输入;\n");
printf("2.以该文件名创建新文件;\n");
printf("请键入符合您需要的功能编号:");
}
break;
case 50:
fp = fopen(fileName,"a+");
flag = false;
break;
default:
printf("输入有误,请键入1或2重新选择\n");
break;
}//end of switch
}//end of while
}
else
{
printf("文件打开成功!\n");
}

}

//将学生信息写入文件
void saveStu(stu *stu)
{
fprintf(fp,"\n%-16s\n",stu->stuName);
fprintf(fp,"%-4d\n",stu->stuID);

for(int i=0;i<STU_SCORE_NUM;i++)
fprintf(fp,"%-4d\n",stu->stuScore[i]);

fprintf(fp,"%-4d\n",stu->totalScore);
fprintf(fp,"%-4d\n",stu->aveScore);

fclose(fp);
}

//true,需要保存; false,无需保存
bool setStuInfo(stu *p)
{
bool isExist = false;
int ID = 0;

printf("\n学号:");
scanf("%d",&ID);

isExist = getStuByID(p,ID);

if(isExist)
{
printf("\n操作失败,该学号已存在!!!\n");
return false;
}
else
{
p->stuID = ID;//设置ID

printf("姓名:");
scanf("%s",p->stuName);

p->totalScore = 0;//help to init it;
for(int j=0;j<STU_SCORE_NUM;j++)
{
printf("\n第%d门功课的成绩:",j+1);
scanf("%d",&(p->stuScore[j]));
p->totalScore += p->stuScore[j];
}

p->aveScore = p->totalScore/STU_SCORE_NUM;
}

return true;

}

void addStu()
{
int num = 0;
bool flag = true;

while(flag)
{
printf("\n请输入要添加的学生人数:\n");
scanf("%d",&num);

if(num <= 0)
printf("输入人数<0,无效,请重新输入\n");
else if(num > 50)
printf("输入人数>50,为保证有效,建议分批次录入学生信息\n");
else
flag = false;
}

printf("用户想要添加%d个学生",num);
stu *p = (stu *)malloc(sizeof(stu));

for(int i=0;i<num;i++)
{
printf("\n请输入第%d个学生的信息:\n",i+1);

if(setStuInfo(p))
{
aOpenFile();
saveStu(p);
//fclose(fp);
}
}
}

void modifyStu()
{
bool isExist = false;
int ID = 0;
long curpos = 0;
stu *p = (stu *)malloc(sizeof(stu));

printf("\n学号:");
scanf("%d",&ID);

rOpenFile();

while(getStuInfo(p)!=0)
{

if(p->stuID==ID)
{
printf("\n查询到学生信息如下:");
printStuInfo(p);
isExist = true;
break;
}

curpos = ftell(fp);
}

if(isExist)
{
printf("\n请重新输入该学生信息:\n");
p->stuID = ID;//设置ID

printf("姓名:");
scanf("%s",p->stuName);

p->totalScore = 0;//help to init it;
for(int j=0;j<STU_SCORE_NUM;j++)
{
printf("\n第%d门功课的成绩:",j+1);
scanf("%d",&(p->stuScore[j]));
p->totalScore += p->stuScore[j];
}

p->aveScore = p->totalScore/STU_SCORE_NUM;

rOpenFile();
fseek(fp,curpos,SEEK_SET);
saveStu(p);
return;
}
else
{
printf("\n查无此学号!!!\n");
}
}

void deleteStu()
{
bool isExist = false;
int ID = 0;
long curpos = 0;
stu *p = (stu *)malloc(sizeof(stu));

printf("\n学号:");
scanf("%d",&ID);

rOpenFile();

while(getStuInfo(p)!=0)
{
if(p->stuID==ID)
{
printf("\n查询到学生信息如下:");
printStuInfo(p);

isExist = true;
break;
}

curpos = ftell(fp);
}

if(isExist)
{
p->stuID = 0;//设置ID
ZeroMemory(p->stuName,sizeof(p->stuName));

for(int j=0;j<STU_SCORE_NUM;j++)
{
p->stuScore[j] = 0;
}
p->totalScore = 0;
p->aveScore = 0;

rOpenFile();
fseek(fp,curpos,SEEK_SET);
saveStu(p);
return;
}
else
{
printf("\n查无此学号!!!\n");
}

}

void printStuInfo(stu *stu)
{
if(stu->stuID!=0)
{
printf("\n姓名:%s",stu->stuName);
printf("\n学号:%d",stu->stuID);

for(int i=0;i<STU_SCORE_NUM;i++)
{
printf("\n第%d们课程的分数是%d",i+1,stu->stuScore[i]);
}

printf("\n总分数是%d",stu->totalScore);
printf("\n平均分是%d\n",stu->aveScore);
}
}

int getStuInfo(stu *p)
{
char buf[STU_SIZE_BUFF];

if(fscanf(fp,"%s",buf)!=1)
return 0;
strncpy(p->stuName,buf,STU_NAME_LEN);

fscanf(fp,"%d",&(p->stuID));

p->totalScore = 0;//Help to init it;
for(int i=0;i<STU_SCORE_NUM;i++)
{
fscanf(fp,"%d",&(p->stuScore[i]));
}

fscanf(fp,"%d",&(p->totalScore));
fscanf(fp,"%d",&(p->aveScore));

return 1;
}

void printAllStuInfo(stu *p)
{
rOpenFile();

printf("\n系统中所有学生信息如下:");
while(getStuInfo(p)!=0)
{
printStuInfo(p);
}

fclose(fp);

}

int getAllStuNum(stu *p)
{
int num =0;

while(getStuInfo(p)!=0)
{
num++;
}

return num;
}

bool getStuByName(stu *p,char *name)
{
bool flag = false;

rOpenFile();

while(getStuInfo(p)!=0)
{
//printf("\nCheck Loop times,p->stuName=%s,name=%s",p->stuName,name);
if(!strcmp(p->stuName,name))
{
//printf("\np->stuName=%s,name=%s",p->stuName,name);
printf("\n查询到学生信息如下:");
printStuInfo(p);
flag = true;
}
}

fclose(fp);
return flag;
}

bool getStuByID(stu *p,int id)
{
bool flag = false;

rOpenFile();

while(getStuInfo(p)!=0)
{
//printf("\nCheck Loop times,p->stuID=%d, id=%d",p->stuID,id);
if(p->stuID==id)
{
//printf("\np->stuID=%d,id=%d",p->stuID,id);
printf("\n查询到学生信息如下:");
printStuInfo(p);
flag = true;
}
}

fclose(fp);
return flag;
}

void mainMenu()
{
stu p;
int choose;
bool flag = true,result=false;

while(flag)
{
printf("\n\t\t****************************\n");
printf("\t\t“学生成绩管理系统”功能列表:\n");
printf("\t\t1.添加学生;\n");
printf("\t\t2.修改学生信息;\n");
printf("\t\t3.删除学生信息;\n");
printf("\t\t4.按学生姓名查找;\n");
printf("\t\t5.按学号查询;\n");
printf("\t\t6.打印所有学生成绩信息;\n");
printf("\t\t7.退出;\n");
printf("\t\t****************************\n");
printf("\t\t请键入功能编号:\n");

scanf("%d",&choose);

switch(choose)
{
case 1:
addStu();
break;
case 2:
modifyStu();
break;
case 3:
deleteStu();
break;
case 4:
char name[STU_NAME_LEN];

printf("\n请输入学生姓名:\n");
scanf("%s",name);
result = getStuByName(&p,name);

if(!result)
printf("\n未查询到该学生信息\n");
break;
case 5:
int id;
printf("\n请输入学生学号:\n");
scanf("%d",&id);
result = getStuByID(&p,id);
if(!result)
printf("\n未查询到该学生信息\n");
break;
case 6:
printAllStuInfo(&p);
break;
case 7:
exit(0);
flag = false;
break;
default:
printf("\n您的输入有误,请重新输入\n");
break;
}

}

}

int main(void)
{
initSys();

mainMenu();

if(NULL==fp)
fclose(fp);

return 0;
}


以下是StuManageSys.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STU_NAME_LEN 16
#define STU_SCORE_NUM 3
#define STU_ID_LEN 4
#define STU_SIZE_BUFF 40  //这个Buffer的大小需要和sizeof(stu)相同,用来从文件中读取学生信息,这里我处理的不好,有better method 吗?
#define ZeroMemory(Destination,Length) memset((Destination),0,(Length))
//用结构体定义学生信息(姓名,学号,各门学科的成绩)
typedef struct student{
char stuName[STU_NAME_LEN];
int  stuID;
int  stuScore[STU_SCORE_NUM];
int  totalScore;
int  aveScore;

}stu;

//FILE *stufilept;
FILE *fp;
char fileName[20];

void aOpenFile();//用"a+"权限来打开“学生信息”记录表
void rOpenFile();//用"r"权限来打开“学生信息”记录表
void initSys();//系统初始化
void saveStu(stu *stu);//将学生信息写入文件

void printStuInfo(stu *stu);
void printAllStuInfo(stu *p);

int getStuInfo(stu *p);
int getAllStuNum(stu *p);
void addStu();
void deleteStu();

bool getStuByName(stu *p,char *name);
bool getStuByID(stu *p,int id);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: