#include <stdio.h>
#include <string.h>
#include <conio.h>
struct BUY
{
char sort[10];
char season[10];
char brand[10];
int
q;
char location[20];
};
struct QUERY
{
char sort[10];
char season[10];
};
void purchase(struct BUY buy1);
void query(struct QUERY query1);
main()
{
int option=10;
struct BUY buy1;
struct QUERY query1;
int con=1;
while(con)
{
printf("service type as follows\n");
printf("1:add new purchasing\t 2:inquire about present storage\t 0:exit\n");
scanf("%1d",&option);
clrsrc();——————————————————————在这
switch(option)
{
case 0:
exit(1);
case 1:
printf("input things newly purchased\n");
printf("directions:\n");
printf("input list as follows:clothes summer nike 1 room1\n");
printf("sorted
as: clothes\t shoesandhats \n");
printf("seasoned as: summer\t winter\t others \n");
printf("nike represents the brand; 1 represents quanity \n");
printf("room1cabinet represents the location \n");
scanf("%s %s %s %d %s",buy1.sort,buy1.season,buy1.brand,&buy1.q,buy1.location);
purchase(buy1);
break;
case 2:
printf("input the list for inquiry\n");
printf("inquiry list formation is as follows:");
printf("clothes summer \n");
scanf("%s %s",&query1.sort,&query1.season);
query(query1);
break;
default:
;
}
printf("continue:1 exit:0\n");
scanf("%d",&con);
}
}
void purchase(struct BUY buy1)
{
FILE *fp;
fp=fopen("d:\\purchase.txt","a+");
fprintf(fp,"%s ",buy1.sort);
fprintf(fp,"%s ",buy1.season);
fprintf(fp,"%s ",buy1.brand);
fprintf(fp,"%d ",buy1.q);
fprintf(fp,"%s\n",buy1.location);
fclose(fp);
}
void query(struct QUERY query1)
{
struct QUERY query2;
struct BUY
buy2;
long number;
FILE *fp;
fp=fopen("d:\\purchase.txt","r");
if((fp=fopen("d:\\purchase.txt","r"))==NULL)
{
printf("file do not exist\n");
exit(1);
}
while(getc(fp)!=EOF)
{
fseek(fp,-1L,SEEK_CUR);
number=ftell(fp);
fscanf(fp,"%s %s ",query2.sort,query2.season);
if(!strcmp(query2.sort,query1.sort)&&!strcmp(query2.season,query1.season))
{
fseek(fp,number,SEEK_SET);
fscanf(fp,"%s %s %s %d %s",buy2.sort,buy2.season,buy2.brand,&buy2.q,buy2.location);
fprintf(stdout,"%s %s %s %d %s\n",buy2.sort,buy2.season,buy2.brand,buy2.q,buy2.location);
}
while(getc(fp)!='\n')
{
fseek(fp,1L,SEEK_CUR);
}
}
fclose(fp);
}
[
本帖最后由 rainybear 于 2009-11-24 00:24 编辑 ]