c语言学生管理系统

更新时间:2023-05-11 11:15:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

学生信息的录入,查找,删除,修改等操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct pnode //结构体定义 用于存放学生信息的节点
{
char name [20]; //姓名
char xh[20]; //学号
char fh[30]; //房号
}personnode;
char filename[20]; //文件名
FILE *fp; //指向文件的指针
void creat() //创建新数据文件的函数 创建一个二进制文件 用于存放学生数据
{
personnode *person;
person=(personnode *)malloc(sizeof(personnode)); //为节点分配内存
printf("\n please enter the filename:\n");
scanf("%s",filename);
if((fp=fopen(filename,"w+"))==NULL)
{
printf("\n you have no enter the filename,can not font the file");
exit(0);
}

printf("\n please enter the name,studentnumbeer (the same length) and roomnumber,spare with space,endwith #\n");
scanf("%s",person->name);
while(strcmp(person->name,"#")) //该循环用于控制 学生信息的录入 当输入#时 学生信息录入完毕
{
scanf("%s %s",person->xh,person->fh);

fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
scanf("%s",person->name);
}
fclose(fp);
}
void readfile() //文件读取函数 用于打开已有的二进制数据文件
{printf("\n please enter the fileroad:\n");
scanf("%s",filename); //此处输入为文件的路径 如c:\zhang
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n can't open thefile:\n");
exit(0);
}
fclose(fp);
}

void output() //输出函数 用于输出文件的全部信息
{
personnode *person;
long offset1,offset2;
char name1[8],name2[8],name3[8];
char xh1[16],xh2[16],xh3[16];
char fh1[30],fh2[30],fh3[30];
person=(personnode *)malloc(sizeof(personnode));
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n can't open the file");
exit(0);
}
while(!feof(fp)) //此循环用于 对文件数据中关键字学号进行从小到大冒泡排序
{
int a,b,c,d; //a为循环结束判定变量 b,c,d用于起泡排序时学号交换的替换

if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n can't open the file");
exit(0);
}
while(!feof(fp)) // 从文件头开始遍历
{

while(!feof(fp)) //对两组数据进行排序
{
offset1=ftell(fp); //获取文件内部当前指针位置
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);

strcpy(name1,per
son->name);
strcpy (xh1,person->xh);
strcpy(fh1,person->fh);
if(feof(fp)) break; //文件结束 跳出循环
offset2=ftell(fp); //获取文件内部下一指针位置

学生信息的录入,查找,删除,修改等操作

fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy (name2,person->name);
strcpy(xh2,person->xh);
strcpy(fh2,person->fh);
b=strlen(xh1);
c=strlen(xh2);
d=strcmp(xh1,xh2);
if(b==c&&d>0) //如果 学号1大于等于学号2 怎交换全部数据
{
strcpy(name3,name1);
strcpy(name1,name2);
strcpy(name2,name3);
strcpy(xh3,xh1);
strcpy(xh1,xh2);
strcpy(xh2,xh3);
strcpy(fh3,fh1);
strcpy(fh1,fh2);
strcpy(fh2,fh3);

fseek(fp,offset1,SEEK_SET); //将文件指针移动offset1个字节
strcpy(person->name,name1);
strcpy(person->xh,xh1);
strcpy(person->fh,fh1);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);//排序后写入文件

strcpy(person->name,name2);
strcpy(person->xh,xh2);
strcpy( person->fh,fh2);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);//排序后写入文件

}
fseek(fp,offset2,SEEK_SET); ///将文件位置指针从文件头向前移动offset2个字节
}
rewind(fp); //指向头文件
while(!feof(fp)) //此循环用于判断学号是否有序
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy (xh1,person->xh);
if(feof(fp)) {a=1; break;}
offset2=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy(xh2,person->xh);
b=strlen(xh1);
c=strlen(xh2);
d=strcmp(xh1,xh2);

if(b==c&&d<=0) a=1; //若a为1 则有序
else{ a=0; //a为0怎无序
break;}

fseek(fp,offset2,SEEK_SET);
}

if(a) break; //a为1 说明学号已经有序 跳出循环
rewind(fp);
}
if(a) break; ////若a为1,则学号已有序排列,跳出总循环,输出文件
}
rewind(fp);

printf("\n******************************************************************\n");
printf("%35s\n","thefile(thefile hava printf as studentnumber)");
printf("%-10s%-20s%-50s\n","name","studentnumber","roomnumber");
while(!feof(fp)) //次循环用于输出文件
{
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
}

fclose(fp);
printf("********
***********************************************************\n\n");
}




void search1() // 按姓名搜索函数
{
int k=0;
char namekey[8];

学生信息的录入,查找,删除,修改等操作

personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n please enter the namehead you need :");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n can't open thefile");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s\n",person->name,person->xh,person->fh);
if(!strcmp(namekey,person->name)) //比较是否相同 相同则输出结果 否则输出未找到记录
{
printf("\n\n have serched,the courrent is:");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!k) printf("\n\n sorry,there is not the current of thiss people \n");
fclose(fp);
}


void search2() //按学号搜索函数
{
int k=0;
char xhkey[16];
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n please enter the roomnumber you want to serch:");
scanf("%s",xhkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n can't open the file ");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s\n",person->name,person->xh,person->fh);
if(!strcmp(xhkey,person->xh)) ////比较是否相同 相同则输出结果 否则输出未找到记录
{
printf("\n\n have got it,the current is:");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!k) printf("\n\n sorry,there is not the current of thiss people \n");
fclose(fp);
}


void search3() //按房号搜索函数
{
int k=0;
char fhkey[30];
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n please enter the roomnumber you want to serch:");
scanf("%s",fhkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n can't open thefile");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s\n",person->name,person->xh,person->fh);
if(!strcmp(fhkey,person->fh)) //比较是否相同 相同则输出结果 否则输出未找到记录
{
printf("\n\n have got it,the current is:");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!k) printf("\n\n sorry,there is not the current of thiss people\n");
fclose(fp);
}

void append() //插入函数 用于像已有文件插入一条新的学生信息记录
{
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n can't open the file");
exit(0);
}
printf("\n please enter the name,studentnumber and roomnumber \n");
scanf("%s %s %s",person->name,person->xh,pers
on->fh);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
fclose(fp);
}


void modify() //更新函数 用

学生信息的录入,查找,删除,修改等操作

于修改指定学生姓名的记录
{
int k=0;
long offset;
char namekey[8];
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n please enter the studentname you want to updata :");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n can't open thefile");
exit(0);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s %s %s\n",person->name,person->xh,person->fh);
if(!strcmp(namekey,person->name)) //比较是否相同 如
{
k=1;
break;
}
}
if(k) //相同 输出记录并进行修改
{
printf("\n hava got it,the current is:");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
printf("\n please enter the new studentname,studentnumber and roomnumber :");
scanf("%s %s %s",person->name,person->xh,person->fh);
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
}
else printf("\n sorry,there is not the current of this people \n");
fclose(fp);
}


void deleted() //删除函数 用于删除指定学生姓名的记录
{
int k=0;
char m;
long offset;
char namekey[8];
personnode *person;
person=(personnode *)malloc(sizeof(personnode));
printf("\n please enter the name you want to delete :");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n can't open the file ");
exit(0);
}
while(!feof(fp)) //此循环遍历整个文件 查找需要删除的记录
{
offset=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
if(!strcmp(namekey,person->name))
{
k=1;
break;
}
}
if(k)
{
printf("\n hava got it,the current is:");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
printf("\n are you sure to delete it?y/n?");
scanf("%s",&m);
if(m=='y') //删除确认按钮
{
fseek(fp,offset,SEEK_SET); //删除记录
fprintf(fp,"%-10s%-20s%-50s\n","","","");
}
else rewind(fp);

}

else printf("\n sorry,there is not the current of this people \n");
fclose(fp);
}


void main() //主函数
{
int m,flag=1; // m用于控制菜单的选择项 flag用于控制菜单弹出
while(flag)
{
printf("%33s\n","thefile");
printf("--------------------------------------------------------------------------------\n");
printf("\t\t0-------------creat a new datafile\n");
printf("\t\t1-------------read the oldfile\n");
printf("\t\t2------------ serch as
name\n");
printf("\t\t3------------ serch as studentnumber\n");
printf("\t\t4------------ serch as roomnumber\n");

学生信息的录入,查找,删除,修改等操作

printf("\t\t5-------------updata\n");
printf("\t\t6-------------delete\n");
printf("\t\t7-------------insert\n");
printf("\t\t8-------------printf\n");
printf("\t\t9-------------exit\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t please choice(0-----------9)\n");
scanf("%d",&m);
switch(m)
{ case 0:creat(); break;
case 1:readfile(); break;
case 2:search1(); break;
case 3:search2(); break;
case 4:search3();
break;
case 5:modify(); break;
case 6: deleted(); break;
case 7:append(); break;
case 8:output(); break;
case 9:exit(0);
}
}
}

本文来源:https://www.bwwdw.com/article/vtbe.html

Top