#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 30
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct information
{
int number;
char name[10];
char place[10];
int people;
DATE time;
int money;
}INF;
void WritetoFile(INF stu[],int n);
void PrintformFile(INF stu[],int n);
void Findnumber(INF stu[],int n);
void Findname(INF stu[],int n);
void Xiugai(INF stu[],int n);
void Add(INF stu[],int n);
int main()
{
int a,n;
INF stu[N];
printf("\n");
printf("\n");
printf(" ╭══════════╮ \n");
printf(" ║ 软件信息管理系统V1.0 ║\n");
printf(" ╰═══════════╯\n");
printf("\n");
printf(" ╭═══════════════════╮ \n");
printf(" ║ 【1】录入软件信息到文件中 ║\n");
printf(" ║ ║\n");
printf(" ║ 【2】显示所有软件项目信息 ║\n");
printf(" ║ ║\n");
printf(" ║ 【3】按编号查询相关软件信息 ║\n");
printf(" ║ ║\n");
printf(" ║ 【4】按名称查询相关软件信息 ║\n");
printf(" ║ ║\n");
printf(" ║ 【5】修改一条软件项目信息 ║\n");
printf(" ║ ║\n");
printf(" ║ 【6】添加一条软件项目信息 ║\n");
printf(" ║ ║\n");
printf(" ║ 【7】退出系统 ║\n");
printf(" ╰════════════════════╯\n");
printf("\n");
printf("请输入你所录入的或文件里的项目数:");
scanf("%d",&n);
printf("\n");
printf("请输入数字(1-7):");
scanf("%d",&a);
switch(a)
{
case 1: WritetoFile(stu,n) ;
break;
case 2: PrintformFile(stu,n);
break;
case 3: Findnumber(stu,n);
break;
case 4: Findname(stu,n);
break;
case 5:Xiugai(stu,n);
break;
case 6: Add(stu,n);
break;
case 7:exit(0);
default :
printf("错误");
}
}
FILE*fp;
void WritetoFile(INF stu[],int n)//录入信息到文件
{
int i;
for(i=0;i<n;i++)
{
printf("输入软件信息 %d:\n",i+1);
scanf(" %s %s %d %d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
}
if((fp = fopen("D:\\information.txt","w")) == NULL)
{
printf("Failure to open file.txt!\n");
exit(0);
}
for(i=0;i<n;i++)
{
fprintf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
fclose(fp);
}
void PrintformFile(INF stu[],int n)//打印按编号排序好的软件信息
{
int i,j,k,temp1,temp4,temp5,temp6,temp7,temp8;
char temp2[10],temp3[10];
FILE * fp;
if((fp = fopen("D:\\information.txt","r")) == NULL)
{
printf("Failure to open file!\n");
exit(0);
}
for(i=0; !feof(fp);i++)
{
fscanf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
}
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(stu[j].number<stu[k].number)
{
k = j;
}
}
if (k!=i)
{
temp1=stu[k].number;
strcpy(temp2,stu[k].name);
strcpy(temp3,stu[k].place);
temp4=stu[k].people;
temp5=stu[k].time.year;
temp6=stu[k].time.month;
temp7=stu[k].time.day;
temp8=stu[k].money;
stu[k].number=stu[i].number;
strcpy(stu[k].name,stu[i].name);
strcpy(stu[k].place,stu[i].place);
stu[k].people=stu[i].people;
stu[k].time.year=stu[i].time.year;
stu[k].time.month=stu[i].time.month;
stu[k].time.day=stu[i].time.day;
stu[k].money=stu[i].money;
stu[i].number=temp1;
strcpy(stu[i].name,temp2);
strcpy(stu[i].place,temp3);
stu[i].people=temp4;
stu[i].time.year=temp5;
stu[i].time.month=temp6;
stu[i].time.day=temp7;
stu[i].money=temp8;
}
}
printf(" 编号 名称 地点 人数 日期 经费:万\n");
for(i=0;i<n;i++)
{
printf("%10d%10s%10s%10d%10d/%02d/%02d%10d\n",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
fclose(fp);
}
FILE*fp;
void Findnumber(INF stu[],int n)//按编号查询软件信息
{
int x,i;
printf("请输入您所要查询的软件编号信息:");
scanf("%d",&x);
if((fp = fopen("D:\\information.txt","r")) == NULL)
{
printf("Failure to open file.txt!\n");
exit(0);
}
for(i=0; !feof(fp);i++)
{
fscanf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
}
for(i=0;i<n;i++)
{
if(stu[i].number==x)
{
printf(" 编号 名称 地点 人数 日期 经费:万\n");
printf("%10d%10s%10s%10d%10d/%02d/%02d%10d\n",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
}
fclose(fp);
}FILE*fp;
void Findname(INF stu[],int n)//按名称查询软件信息
{
int i;
char a[10];
printf("请输入您所要查询的软件名称信息:");
getchar();
gets(a);
if((fp = fopen("D:\\information.txt","r")) == NULL)
{
printf("Failure to open file.txt!\n");
exit(0);
}
for(i=0; !feof(fp);i++)
{
fscanf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
}
for(i=0;i<n;i++)
{
if(strcmp(stu[i].name,a)==0)
{
printf(" 编号 名称 地点 人数 日期 经费:万\n");
printf("%10d%10s%10s%10d%10d/%02d/%02d%10d\n",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
}
fclose(fp);
}
void Xiugai(INF stu[],int n)//修改软件编号信息,c表序号,h表新的编号
{
int i,c;
int temp1,temp4,temp5,temp6,temp7,temp8;
char temp2[10],temp3[10];
FILE * fp;//读文件
if((fp = fopen("D:\\information.txt","r")) == NULL)
{
printf("Failure to open information.txt!\n");
exit(0);
}
for(i=0; !feof(fp);i++)
{
fscanf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
}
fclose(fp);
printf("请输入所要修改的选项:");
scanf("%d",&c);
for(i=0;i<n;i++)
{
if(i==c)
{
printf("第%d项的原有数据信息如下:\n",c+1);
printf(" 编号 名称 地点 人数 日期 经费:万\n");
printf("%10d%10s%10s%10d%10d/%02d/%02d%10d\n",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
}
printf("请输入修改后的编号:");
scanf("%d",&temp1);
printf("修改后的名称:");
getchar();
gets(temp2);
printf("修改后的地点:");
gets(temp3);
printf("修改后的人数:");
scanf("%d",&temp4);
printf("修改后的年:");
scanf("%d",&temp5) ;
printf("修改后的月:");
scanf("%d",&temp6) ;
printf("修改后的日:");
scanf("%d",&temp7) ;
printf("修改后的金额:");
scanf("%d",&temp8) ;
printf(" 编号 名称 地点 人数 日期 经费:万\n");
printf("%10d%10s%10s%10d%10d/%02d/%02d%10d\n",
temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8);
for(i=0;i<n;i++)
{
if(i==c)
{
stu[i].number=temp1;
strcpy(stu[i].name,temp2);
strcpy(stu[i].place,temp3);
stu[i].people=temp4;
stu[i].time.year=temp5;
stu[i].time.month=temp6;
stu[i].time.day=temp7;
stu[i].money=temp8;
}
}
if((fp = fopen("D:\\information.txt","w")) == NULL)//从文件中写取信息
{
printf("Failure to open information.txt!\n");
exit(0);
}
for(i=0;i<n;i++)
{
fprintf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
fclose(fp);
printf("已修改\n");
}
void Add(INF stu[],int n)//在末尾添加软件信息
{
int i,x;
FILE*fp;
if((fp = fopen("D:\\information.txt","a+")) == NULL)//向文件读信息
{
printf("Failure to open score.txt!\n");
exit(0);
}
printf("请输入你所要添加的项数:");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("Input add record %d:\n",i+1);
scanf("%d%s%s%d%d%d%d%d",
&stu[i].number,stu[i].name,
stu[i].place,&stu[i].people,
&stu[i].time.year,&stu[i].time.month,
&stu[i].time.day,&stu[i].money);
fprintf(fp,"%10d%10s%10s%10d%10d/%02d/%02d%10d",
stu[i].number,stu[i].name,
stu[i].place,stu[i].people,
stu[i].time.year,stu[i].time.month,
stu[i].time.day,stu[i].money);
}
printf("已添加\n");
fclose(fp);
}