#include<stdio.h>
#define max 20
struct magazine
{
char name[11];
char sex[6];
char addr[11];
char phone_number[12];
char unit_price[5];
char amount[4];
char time_limit[11];
}order[max];
void save(int n);
int load_record();
void search();
void printf_n(int n);
void printf_one(int i);
void input(int i);
statitic();
void add();
void menu();
main()
{
FILE *fp;
fp=fopen("record.txt","w");/*在C:\Documents and Settings\Administrator里建立一个"record.txt"记事本*/
fclose(fp);
menu();
}
void menu()
{
int w,n;
do
{
clrscr();
printf("\n");
printf("\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\t\t \n");
printf("\t\t \n");
printf("\t\t Welcome to the magazine order system !^_^ \n");
printf("\t\t \n");
printf("\t\t \n");
printf("\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n\n\t\t *********************************************\n\n");
printf("\t\t 1:add_new_subscriber\n");
printf("\t\t 2:browse\n");
printf("\t\t 3:statistic\n");
printf("\t\t 4:exit\n\n");
printf("\t\t *********************************************\n\n");
printf(" Choice your number(1-4):[ ]\b\b");
scanf("%d",&n);
if(n<1||n>4) w=1;
else w=0;
}
while (w==1);
/***************选择功能****************/
switch(n)
{
case 1:add();break;/*增加新订户记录*/
case 2:search();break;/*查找过期的订户记录并删除*/
case 3:statistic();break;/*统计*/
case 4:exit();break;/*退出*/
}
}
/*******************************************添加模块***************************************************/
void add()
{
int t,i,m;
clrscr();
t=load_record();
printf("How many groups information do you want to add?\n");
printf("Enter the number:[ ]\b\b\b");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nInput %dth subscriber record.\n",i+1);
input(t+i);/*调用输入函数*/
}
save(t+m);/*调用保存函数 保存t+m组数据*/
clrscr();/**********清屏************/
menu();/*返回登陆界面*/
}
void input(int i)
{
printf("Please input:\nName sex address phone_number unit_price amount time_ imit \n");
scanf("%s%s%s%s%s%s%s",&order[i].name,&order[i].sex,&order[i].addr,&order[i].phone_number,&order[i].unit_price,&order[i].amount,&order[i].time_limit);
}
/*****************************************统计模块**************************************************/
statistic()
{
int t;
t=load_record();
printf("The amount of subscriber is[%d]\b\b\b",t);
printf_n(t);
printf("\n\n\nPress any key to go back...\n\n");
getch();
menu();
}
void printf_one(int i)/*显示一个仪器记录的函数*/
{
printf("%-10s %-5s %-10s %-11s %-4s %-3s %-10s\n\n",order[i].name,order[i].sex,order[i].addr,order[i].phone_number,order[i].unit_price,order[i].amount,&order[i].time_limit);
}
void printf_n(int n)/*显示n个仪器记录的函数*/
{
int j;
clrscr();/**********清屏************/
printf("Name sex address phone_number price amount time_ imit\n\n");
for(j=0;j<n;j++)
{
if((j+1)%10==0)/*控制显示,每屏显示10行*/
{
printf("\n\nPass any key to continue ...");
getch();
puts("\n\n");
}
printf_one(j);/*调用显示一个仪器记录的函数*/
}
}
/****************************查询模块********************************************/
void search()
{
FILE *fp;
char date[11],str[2];
int t,i,sum=0;
t=load_record();
clrscr();
printf("Please input the date:[ ]\b\b\b\b\b\b\b\b\b\b\b");
scanf("%s",date);
printf("\n");
if((fp=fopen("record.txt","w"))==NULL)/*以只写方式打开*/
{
printf("\nCannot open file\n");
exit(0);
}
clrscr();
printf("\n");
for(i=0;i<t;i++)
{
if(strcmp(order[i].time_limit,date)<0)
{
sum++;
printf("\n");
printf_one(i);/**********把符合条件的记录显示出来************************/
printf("\n\nThe record is out of date!");
printf("\n\nDo you want to delete it's information?('Y'or'N'):[ ]\b\b");
scanf("%s",str);;
if(strcmp(str,"Y")==0||strcmp(str,"y")==0)
continue;
}
fwrite(&order[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
if(sum==0)
{
printf("***************************************************\n\n");
printf("Sorry! Cannot find the equipment which you want to.\n\n");
printf("***************************************************\n\n");
}
printf("\n\n\nPress any key to go back the former step...\n");
getch();
menu();
}
/****************************公共函数********************************************/
int load_record()/***加载仪器信息并计算记录仪器个数的函数***/
{
FILE *fp;
int i;
if((fp=fopen("record.txt","r"))==NULL)/*以只读方式打开*/
{
printf("\nCannot open file\n");
exit(0);
}
for(i=0;!feof(fp);i++)/*从文件中读入数据并保存在结构体数组中*/
fread(&order[i],sizeof(struct magazine),1,fp);
fclose(fp);
return(i-1);
}
void save(int n)/*n表示保存n组数据,m表示保存在哪个文件夹里*/
{
FILE *fp;
int i;
fp=fopen("record.txt","w");/*以只写方式打开*/
if(fp==NULL)
{
printf("\nCannot open file\n");
exit(0);
}
for(i=0;i<n;i++)
{
fwrite(&order[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
}