北京邮电大学程序设计课程设计第二次实验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;
}
#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;
}
相关文章推荐
- 北京邮电大学程序设计课程设计第二次实验Problem 4
- C程序设计课程-2012电子信息 第二次上机实验
- 【8086】中国电力出版社 微机原理实验与课程设计指导书 实验二 数据处理实验程序设计 答案
- C程序设计课程-2012电子信息 第二次上机实验
- 山东大学Java程序设计课程实验报告5设计和使用类
- 20135213 20135231 信息安全系统设计基础课程第二次实验报告
- C程序设计课程-2012电子信息 第八次实验任务
- “C++程序设计”课程综合设计题目
- java课程实验设计——数据库连接 问题
- C程序设计课程第八次实验任务
- C程序设计课程-第八次实验任务
- 实验及课程设计
- Unix课程设计实验四 编制模拟“五个哲学家”问题的程序(进程同步)
- 2017-2018-1 20155314《信息安全系统设计基础》实验二 固件程序设计
- 20135213 20135231 信息安全系统设计基础课程第五次实验报告
- 现代程序设计 (课程设计中, 征求意见稿)
- c程序语言设计第二次上机实验报告
- C程序设计课程-2012电子信息 第三次上机实验
- 课程设计之第二次冲刺—(11.29-12.1总结)
- 课程设计之第二次冲刺----第九天