#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct things{
int id;
char name[40];
int num;
double value;
};
int compare(const void *a,const void *b)
{
return ((struct things*)a)->id-((struct things*)b)->id;
}
int main(int argc, char *argv[])
{
FILE *fp=NULL;
int num;
char st;
int loop1,loop2,loop3,loop4,loop5;
struct things store[100];
int storenum=0;
printf("Please input an integer:");
while(scanf("%d",&num)!=EOF)
{
int mark=0;
struct things temp;
temp.name[0]='\0';
if(num<0)
break;
if(num==0)
{fp=fopen("commodity.dat","wb+");
}
for(loop1=0;loop1<num;loop1++)
{
printf("Please input the name:");
getchar();
gets(temp.name);
printf("Please input three properties:");
scanf("%d%d",&temp.id,&temp.num);
scanf("%lf",&temp.value);
for(loop3=0;loop3<storenum;loop3++)
{
if(temp.id==store[loop3].id)
break;
}
if(loop3!=storenum)
{
store[loop3].id=temp.id;
strcpy(store[loop3].name,temp.name);
store[loop3].num=temp.num;
store[loop3].value=temp.value;
}
else
{
store[storenum].id=temp.id;
strcpy(store[storenum].name,temp.name);
store[storenum].num=temp.num;
store[storenum++].value=temp.value;
}
}
qsort(store,storenum,sizeof(struct things),compare);
if(fp==NULL)
{
printf("The record is not existent.\n");
storenum=0;
}
else
{
fwrite(store,sizeof(struct things),storenum,fp);
fclose(fp);
printf("Continue?y/n\n");
getchar();
scanf("%c",&st);
if(st=='n')break;
fopen("commodity.dat","wb+");
}
printf("please input an integer:");
}
fclose(fp);
return 0;
}
#include <string.h>
#include <stdlib.h>
struct things{
int id;
char name[40];
int num;
double value;
};
int compare(const void *a,const void *b)
{
return ((struct things*)a)->id-((struct things*)b)->id;
}
int main(int argc, char *argv[])
{
FILE *fp=NULL;
int num;
char st;
int loop1,loop2,loop3,loop4,loop5;
struct things store[100];
int storenum=0;
printf("Please input an integer:");
while(scanf("%d",&num)!=EOF)
{
int mark=0;
struct things temp;
temp.name[0]='\0';
if(num<0)
break;
if(num==0)
{fp=fopen("commodity.dat","wb+");
}
for(loop1=0;loop1<num;loop1++)
{
printf("Please input the name:");
getchar();
gets(temp.name);
printf("Please input three properties:");
scanf("%d%d",&temp.id,&temp.num);
scanf("%lf",&temp.value);
for(loop3=0;loop3<storenum;loop3++)
{
if(temp.id==store[loop3].id)
break;
}
if(loop3!=storenum)
{
store[loop3].id=temp.id;
strcpy(store[loop3].name,temp.name);
store[loop3].num=temp.num;
store[loop3].value=temp.value;
}
else
{
store[storenum].id=temp.id;
strcpy(store[storenum].name,temp.name);
store[storenum].num=temp.num;
store[storenum++].value=temp.value;
}
}
qsort(store,storenum,sizeof(struct things),compare);
if(fp==NULL)
{
printf("The record is not existent.\n");
storenum=0;
}
else
{
fwrite(store,sizeof(struct things),storenum,fp);
fclose(fp);
printf("Continue?y/n\n");
getchar();
scanf("%c",&st);
if(st=='n')break;
fopen("commodity.dat","wb+");
}
printf("please input an integer:");
}
fclose(fp);
return 0;
}