是靖芸啊~ 2021-06-11 14:16 采纳率: 83.3%
浏览 230
已结题

书店存货清单系统(简洁明了)

用结构体自行定义几本书籍,要求包括书籍名称,作者,出版社,出版日期,价格,在目录中的位置;当从键盘输入某本书的名称和作者姓名,如果能查询到则显示该书籍的所有信息,并提示“请输入所需数量”,如果所需数量在库存范围内,则显示总价,否则,显示“所需数量不在库存范围内”,接着继续提示顾客是否还想买其他书籍,如果输入“y”或“Y”表示继续输入下一本书的名称和作者姓名,进行下一轮查询;如果不能查询到该书籍,则显示“谢谢,再见!”,依次类推,不断循环。

 

c语言刚刚入门 希望有大神能帮我设计一个简洁明了的代码 最起码能看懂…

  • 写回答

3条回答 默认 最新

  • CSDN专家-Fay 2021-06-11 14:19
    关注

     看看这个是否符合你的要求:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #include<windows.h>
    //书店存货结构体
    typedef struct BookStore 
    {
         char name[100];         
        //书籍的名称 
         char author[100];       
        // 作者 
         char publish[100];      
        //出版社
         float date;              
        //出版日期
         double price;             
        //价格
         int location;           
        //在目录中的位置
         int count;              
        //当前图书的数量     
    }books;
    void display();
    void menu();
    void buy(); 
    void add();
    int main()
    {
        char ch; 
        system("title 书店存货清单4.0版本");
         display();
         menu();
         ch=getchar();
         switch (ch)
         {
             case '0':
                 printf("            谢谢使用,再见!\n");
                 return 0; 
             case '1':
                 buy();
                 break;
             case '2':
                 add();
                 system("cls");
                 buy();
                 break;
         }
    return 0;
    }
    void display()
    {
        printf("\n\n\n\n\n\n"); 
        system("color 7A");
        printf("                    欢");
        Sleep(250);
        system("color 7B");
        printf("迎");
        Sleep(250); 
        system("color 7C");
        printf("使");
        Sleep(250); 
        system("color 7D");
        printf("用");
        Sleep(250);
        system("color 7E");
        printf("书");
        Sleep(250);
        system("color 7F");
        printf("店");
        Sleep(250);
        system("color 7A");
        printf("存");
        Sleep(250);
        system("color 7B");
        printf("货");
        Sleep(250);
        system("color 7C");
        printf("清");
        Sleep(250);    
        system("color 7D");
        printf("单\n\n"); 
        Sleep(500);
        printf("                                ——计算机1812班***\n\n");
        Sleep(500);
        printf("                                  (请按任意键继续)");
        getch(); 
        system("cls"); 
         system("color 70");
    }
    void menu()
    {
        printf("请选择功能:");
        printf("(1)购买书籍;(2)添加书籍;(0)退出程序\n"); 
    }
    void buy()
    {
        FILE *fp;
        if((fp=fopen("books.txt","r+"))==NULL)
            {
                printf("文件打开失败!\n");
                system("pause");
                return;
            }
        books a[100];
        system("cls");
        printf("                ————购买书籍————\n");
        int i=0,n=0,flag=0,j=0,sum=0;
         char c,name[100];
         while(!feof(fp))
        {
            fscanf(fp,"%s%s%s%f%lf%d%d",a[i].name,a[i].author,a[i].publish,&a[i].date,&a[i].price,&a[i].location,&a[i].count);
            i++;
        }
          printf("\n");
         printf("            欢迎光临本书店,");
         flag1: 
         printf("请输入你所需要的书籍名字:");
         fflush(stdin);     
        //清空输入缓冲区,为了确保不影响后面的数据读取    
        scanf("%s",name);
        while(!feof(fp))
         {
              j++;           
            //j加到数组尾部说明未找到该书籍 
              if(strcmp(a[i].name,name)==0)
              {
                   printf("            书名:");
                   puts(a[i].name); 
                   printf("            作者:");
                   puts(a[i].author);
                   printf("            出版社:");
                   puts(a[i].publish);
                   printf("            出版日期:%.2f\n",a[i].date);
                   printf("            价格:%.2lf\n",a[i].price);
                printf("            位置:%d\n",a[i].location);
                   printf("            库存:%d\n",a[i].count);
                   printf("            请输入所需数量:");
                   fflush(stdin); 
                   scanf("%d",&n);
                   if(n<=a[i].count){
                       printf("            总价为:%.2lf\n",n*a[i].price);
                   }
                    
                   else
                    printf("            所需数量不在库存范围内\n");
                   printf("            是否还想买其他书籍?(Y/N)\n");
                   fflush(stdin);
                   printf("            ");
                   scanf("%c",&c);
                   if(c=='y'||c=='Y')
                goto flag1;
                   else if(c=='n'||c=='N')
                {
                     j=0;     
                      //当要退出是使j清0,防止当所找书籍位置为5是跳出for循环而误入下一个if语句 
                     printf("            谢谢使用,再见!\n");
                     system("pause");
                     exit(0);
                } 
              }
              i++;
         }
         if(j==100)
         {
              j=0;
              printf("            未查找到该书籍,请重新输入\n");
              goto flag1;
         }
        else
        {
            j=0;
              printf("            未查找到该书籍,请重新输入\n");
              goto flag1;
        } 
            fclose(fp); 
    }
    void add()
    {
        FILE *fp;
        if((fp=fopen("books.txt","a+"))==NULL)
        {
            printf("文件打开失败!");
            system("pause");  
            return;   
        } 
        books a[100];
        int i=0,n=0;
        char ch;
        system("cls");
        printf("                ————书籍记录添加————\n");
        while(1){
            flag2:
            printf("                请输入该书的书名:");
            scanf("%s",a[i].name);
            printf("                请输入该书的作者:");
            scanf("%s",a[i].author);
            printf("                请输入该书的出版社:");
            scanf("%s",a[i].publish); 
            printf("                请输入该书的出版日期:");
            scanf("%f",&a[i].date);
            printf("                请输入该书的价格:");
            scanf("%lf",&a[i].price); 
            printf("                请输入该书放置的位置:");
            scanf("%d",&a[i].location);
            printf("                请输入该书的进货量:");
            scanf("%d",&a[i].count);
            fprintf(fp,"\n%s %s %s %.2f %.2f %d %d",a[i].name,a[i].author,a[i].publish,a[i].date,a[i].price,a[i].location,a[i].count);
            i++;
            n=n+1;
            system("cls");
            printf("                是否继续添加(Y/N)\n");
            ch=getch();
            if(ch=='N'||ch=='n')
            break;
            else if(ch=='Y'||ch=='y')
            {
                system("cls");
                goto flag2;
            }
            system("pause");
            fclose(fp); 
        }
        printf("            谢谢使用,再见!\n");
        system("pause");  
        exit(0);  
    } 
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月4日
  • 已采纳回答 1月4日