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

此程序实现了一个简单易用的商品管理系统,允许用户输入商品名称、属性并存储到文件中。系统支持重复商品记录更新,并提供数据排序和文件持久化功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值