您的位置:首页 > 其它

北京邮电大学程序设计课程设计第二次实验problem 6

2013-03-17 14:49 239 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: