C语言课程设计——书店存货清单

书店通常使用个人电脑设置一个清单来管理书籍的存货情况,该清单记录每本书籍的书名,作者,出版社,在清单中的位置等信息。当顾客想要买某本书时,只要输入书籍的名称和作者,系统就会显示该书籍是否在清单中,如果书籍在清单中,系统会显示书籍的详细信息以及库存数量,如果不在清单中,也会显示相应的提示信息。如果顾客想买的书籍数量在库存的范围内,系统会计算总价格并显示出来,否则,会提示“所需数量不在库存范围内”。

根据以上的分析,编程要求:

  1. 用结构体自行定义几本书籍,要求包括书籍名称,作者,出版社,出版日期,价格,在目录中的位置;
  2. 当从键盘输入某本书的名称和作者姓名,如果能查询到则显示该书籍的所有信息,并提示“请输入所需数量”,如果所需数量在库存范围内,则显示总价,否则,显示“所需数量不在库存范围内”,接着继续提示顾客是否还想买其他书籍,如果输入“y”或“Y”表示继续输入下一本书的名称和作者姓名,进行下一轮查询;如果不能查询到该书籍,则显示“谢谢,再见!”,依次类推,不断循环。*/
#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);  
} 
/*stdin就是标准输入 std即standard(标准),in即input(输入),合起来就是标准输入。 一般就是指键盘输入到缓冲区里的东西。 
函数名: fflush
功能: 清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件
原型:int fflush(FILE *stream)*/
发布了25 篇原创文章 · 获赞 18 · 访问量 2133

猜你喜欢

转载自blog.csdn.net/weixin_43568110/article/details/85299363
今日推荐