c语言科研管理系统--源代码

c语言科研管理系统--源代码
c语言科研管理系统--源代码

#include

#include

#include

#include

#include//头文件

struct project

{

char pn[30];//项目名称

char name[30];//项目主持人的名字

char sex;//项目支持人的性别

int age;//项目主持人的年龄

char pnum[15];//项目主持人的电话号码

char pt[15];//项目编号

char time[30];//立项时间

char danwei[10];//项目所属单位

char pj[10];//项目级别

char qixian[40];//研究期限

char chengyuan[50];//研究人员

int jingfei;//下拨经费

int sjf;//学校配套经费

};

struct project a[100];

int r=0;//全局变量,用于计输入项目的个数

void shuru(struct project *p)//输入项目信息

{

int i;

char x;

FILE *file;

if((file=fopen("c:\\project.dat","rb+"))==NULL)

{

file = fopen("c:\\project.dat", "wb+");

}

do

{

printf("请输入第%d个的项目名称\n",r+1); fflush(stdin);

scanf("%s",(p+r)->pn);

printf("请输入项目主持人\n"); fflush(stdin);

gets((p+r)->name);

printf("请输入性别,男生:m 女生:w\n"); fflush(stdin);

scanf("%c",&(p+r)->sex);

printf("请输入年龄\n"); fflush(stdin);

scanf("%d",&(p+r)->age);

printf("请输入项目主持人的手机号码\n"); fflush(stdin);

gets((p+r)->pnum);

printf("请输入项目编号\n");fflush(stdin);

gets((p+r)->pt);

printf("请输入立项时间\n");fflush(stdin);

scanf("%s",(p+r)->time);

printf("请输入项目所属单位\n");fflush(stdin);

scanf("%s",(p+r)->danwei);

printf("请输入项目级别\n");fflush(stdin);

scanf("%s",(p+r)->pj);

printf("请输入研究期限\n");fflush(stdin);

scanf("%s",(p+r)->qixian);

printf("请输入研究人员\n");fflush(stdin);

gets((p+r)->chengyuan);

printf("请输入下拨经费\n");fflush(stdin);

scanf("%d",&(p+r)->jingfei);

printf("请输入学校配套经费\n");fflush(stdin);

scanf("%d",&(p+r)->sjf);

r++;

printf("是否继续输入?n/y\n"); fflush(stdin);

scanf("%c",&x);

}while(x=='y');//当输入为y时,继续输入,直到输入是n为止

for(i=0;i

fwrite((p+i),sizeof(struct project),1,file);

fclose(file); /* 关闭文件*/

fflush(stdin);

system("cls");

}

void shuchu(struct project *p)//输出项目信息

{

int i=0,s;

FILE *file;

if((file=fopen("c:\\project.dat","r"))==NULL)//打开文件

{

printf("Open error!!\n");

getch();

exit(0);

}

printf("项目名称项目主持人的姓名性别年龄手机号码项目编号立项时间项目单位项目级别项目期限研究人员下拨经费校配经费\n");

for(s=0;s

{

fread(p+s,sizeof(struct project),1,file);

printf("%-14s",p[s].pn);

printf("%-15s",p[s].name);

printf("%-5c",p[s].sex);

printf("%-6d",p[s].age);

printf("%-12s",p[s].pnum);

printf("%-11s",p[s].pt);

printf("%-12s",p[s].time);

printf("%-10s",p[s].danwei);

printf("%-8s",p[s].pj);

printf("%-12s",p[s].qixian);

printf("%-13s",p[s].chengyuan);

printf("%-10d",p[s].jingfei);

printf("%-10d",p[s].sjf);

printf("\n");

}

fclose(file);

}

void add(struct project *p)//添加项目信息

{

shuru(a);//调用输入函数

}

void xianshi(struct project *p)//根据要求输出信息

{

char i[30],c[15];

int j,s;

FILE *file;

if((file=fopen("c:\\project.dat","r"))==NULL)

{

printf("Open error!!\n");

getch();

exit(0);

}

printf("****************************\n");

printf("*********请输入类型*********\n");

printf("*1:按照老师姓名查找科研信息*\n");

printf("*2:按照手机号码查询老师信息*\n");

printf("****************************\n");

scanf("%d",&s);

if(s==1)

{

printf("请输入你想查询老师的姓名\n");fflush(stdin);

gets(i);

for(j=0;j

{

fread(p+j,sizeof(a),1,file);

if(strcmp(p[j].name,i)==0)

break;

printf("项目名称项目主持人的姓名性别年龄手机号码项目编号立项时间项目单位项目级别项目期限研究人员下拨经费校配经费\n");

fread(p+j,sizeof(a),1,file);

printf("%-14s",p[j].pn);

printf("%-15s",p[j].name);

printf("%-5c",p[j].sex);

printf("%-6d",p[j].age);

printf("%-12s",p[j].pnum);

printf("%-11s",p[j].pt);

printf("%-12s",p[j].time);

printf("%-10s",p[j].danwei);

printf("%-8s",p[j].pj);

printf("%-12s",p[j].qixian);

printf("%-13s",p[j].chengyuan);

printf("%-10d",p[j].jingfei);

printf("%-10d",p[j].sjf);

printf("\n");

fclose(file);

}

if(s==2)

{

printf("请输入手机号码\n");fflush(stdin);

gets(c);

for(j=0;j

{

fread(p+j,sizeof(a),1,file);

if(strcmp(p[j].pnum,c)==0)

break;

}

printf("老师的姓名为:%s\n",p[j].name);

fclose(file);

}

}

void xiugai(struct project *p)//修改信息

{

int b,c;

char i[15];

FILE *file;

if((file=fopen("c:\\project.dat","wb"))==NULL)

{

printf("Open error!!\n");

getch();

exit(0);

printf("请输入你想修改的项目编号\n");

fflush(stdin);

gets(i);

for(b=0;b

{

fread(p+b,sizeof(struct project),1,file);

if(strcmp(p[b].pt,i)==0)

break;

}

if(strcmp(p[b].pt,i)!=0)//判断上一个循环后是否找到与数组i相匹配的项目编号{ printf("无此项目编号!");

xiugai(a);//返回到修改函数重新输入

}

else

{

printf("请重新输入项目名称\n"); fflush(stdin);//重新输入项目信息

scanf("%s",(p+b)->pn);

printf("请输入项目主持人\n"); fflush(stdin);

gets((p+b)->name);

printf("请输入性别,男生:m 女生:w\n"); fflush(stdin);

scanf("%c",&(p+b)->sex);

printf("请输入年龄\n"); fflush(stdin);

scanf("%d",&(p+b)->age);

printf("请输入项目主持人的手机号码\n"); fflush(stdin);

gets((p+b)->pnum);

printf("请输入立项时间\n");fflush(stdin);

scanf("%s",(p+b)->time);

printf("请输入项目所属单位\n");fflush(stdin);

scanf("%s",(p+b)->danwei);

printf("请输入项目级别\n");fflush(stdin);

scanf("%s",(p+b)->pj);

printf("请输入研究期限\n");fflush(stdin);

scanf("%s",(p+b)->qixian);

printf("请输入研究人员\n");fflush(stdin);

gets((p+b)->chengyuan);

printf("请输入下拨经费\n");fflush(stdin);

scanf("%d",&(p+b)->jingfei);

printf("请输入学校配套经费\n");fflush(stdin);

scanf("%d",&(p+b)->sjf);

for(c=0;c

fwrite(p+c,sizeof(struct project),1,file);

fwrite(p+b,sizeof(struct project),1,file);

for(c=b+1;c

fwrite(p+c,sizeof(struct project),1,file);

fclose(file);

printf("修改成功!!");

}

}

void shanchu(struct project *p)//删除信息

{

int i,j;

char b[15];//保存输入的项目编号

FILE *file;

if((file=fopen("c:\\project.dat","wb"))==NULL)//打开文件

{

printf("打开错误!!\n");

getch();

exit(0);

}

printf("请输入想删除的项目编号\n");fflush(stdin);

gets(b);//输入字符串

for(j=0;j

{

fread(p+j,sizeof(struct project),1,file);

if(strcmp(p[j].pt,b)==0)

break;

}

if(strcmp(p[j].pt,b)!=0)//判断上一个循环后是否找到与数组i相匹配的项目编号{

printf("无此项目编号!!\n");

shanchu(a);//返回到删除函数重新输入

}

else if(j==r-1)

{

r--;

for(i=0;i

fwrite(p,sizeof(struct project),1,file);

fclose(file);//关闭文件

}

else

{

for(i=j;i

p[i]=p[i+1];

r--;//删除一个,计数的全局变量r自减一

for(i=0;i

fwrite(p,sizeof(struct project),1,file);//把信息写到文件中去

fclose(file);//关闭文件

printf("删除成功!");

}

}

void paixu(struct project *p)//进行排序

{

int i,j,t,sum[10],m;

struct project b;

FILE *file;

if((file=fopen("c:\\project.dat","r"))==NULL)//打开文件

{

printf("Open error!!\n");

getch();

exit(0);

}

for(i=0;i

{

fread(p+i,sizeof(struct project),1,file);//读取信息

sum[i]=p[i].sjf+p[i].jingfei; //总项目经费

}

for(i=0;i

{

for(j=0;j<=r-i;j++)

if(sum[j]

{

t=sum[j];

sum[j]=sum[j+1];

sum[j+1]=t;

b=p[j];

p[j]=p[j+1];

p[j+1]=b;

}

}

printf("项目名称项目主持人的姓名性别年龄手机号码项目编号立项时间项目单位项目级别项目期限研究人员下拨经费校配经费经费总额\n");

for(m=0;m

{

fread(p+m,sizeof(struct project),1,file);

printf("%-14s",p[m].pn);

printf("%-15s",p[m].name);

printf("%-5c",p[m].sex);

printf("%-6d",p[m].age);

printf("%-12s",p[m].pnum);

printf("%-11s",p[m].pt);

printf("%-12s",p[m].time);

printf("%-10s",p[m].danwei);

printf("%-8s",p[m].pj);

printf("%-12s",p[m].qixian);

printf("%-13s",p[m].chengyuan);

printf("%-10d",p[m].jingfei);

printf("%-10d",p[m].sjf);

printf("%-10d",sum[m]);

printf("\n");

}

fclose(file); //关闭文件

}

void caidan(struct project *p)//菜单

{

int n,t=0,s=0;

do

{

printf("\n\t\t*******************************************************");

printf("\n\t\t* 1:输出基本信息*");

printf("\n\t\t* 2:输出所需信息*");

printf("\n\t\t* 3:排序*");

printf("\n\t\t* 4:清屏*");

printf("\n\t\t* 0:退出*");

printf("\n\t\t* *");

printf("\n\t\t********************************************************");

printf("\n\n"); fflush(stdin);

scanf("%d",&n);

if(n==1)

shuchu(a);

if(n==2)

xianshi(a);

if(n==3)

paixu(a);

if(n==4)

system("cls");//清屏

if(n==0)

return;

}while(n!=0);//输入为0时退出

}

void main()//主函数

{

int w;

FILE*fp; //定义文件指针

FILE *file; //定义文件指针

if((fp=fopen("c:\\num.txt","r"))==NULL)//用于保存全局变量r的值

{

fp=fopen("c:\\num.txt","wb+");//如果没有文件就见一个

}

fscanf(fp,"%d",&r);

fclose(fp);

if((file=fopen("c:\\project.dat","rb+"))==NULL) //用于保存项目结构体的信息

{

file = fopen("c:\\project.dat", "wb+");

}

fscanf(file,"%s",a);//把文件中的信息调出来

fclose(file);

while(1)

{

printf("\n\t\t************************************************************"); printf("\n\t\t* 欢迎使用项目管理系统*"); printf("\n\t\t* *"); printf("\n\t\t* 请选择*"); printf("\n\t\t* 1. 录入项目信息*"); printf("\n\t\t* 2. 查询项目信息*"); printf("\n\t\t* 3. 删除信息*"); printf("\n\t\t* 4. 添加科研信息*"); printf("\n\t\t* 5. 修改信息*"); printf("\n\t\t* 6. 清屏*"); printf("\n\t\t* 0. 退出程序*"); printf("\n\t\t************************************************************"); printf("\n\n");

fflush(stdin);

scanf("%d",&w);//输入数字进行选择

switch(w)

{

case 1:shuru(a);break;

case 2:caidan(a);break;

case 3:shanchu(a);break;

case 4:add(a);break;

case 5:xiugai(a);break;

case 6: system("cls");break;

case 0: //终止程序,保存数据!

{

if((fp=fopen("c:\\num.txt","w"))==NULL)//用于保存全局变量r的值

{

printf("error");

exit(0);

}

fprintf(fp,"%d",r);

fclose(fp);

exit(0);break;

if((file=fopen("c:\\project.dat","rb+"))==NULL) //用于保存项目信息

{

printf("error");

exit(0);

}

fprintf(file,"%s",a);

fclose(file);

exit(0);break;

}default: printf("输入错误,请重新输入!\n");

}

}

}

相关主题
相关文档
最新文档