(C语言课程设计)图书管理系统

   

学校开了C语言的课,学了很久C语言了,但是水平还是很垃圾。但还是得学。今天拿出了老师的课程设计报告要求。根据要求弄了半天,对着书上的例题,终于勉强弄出来一个小型图书管理系统。不管怎么说,这是我头一次自己写一个完整的有点功能的程序。也学到了不少的小知识。甚至在做完编译成功的时候还小有点成就感呢(刚写出来到程序,报出了N十来个错误,高手们不要拍我啊,哈哈)。

通过写了这一个程序,发现对于基础知识的理解真的很重要。比如函数参数及其传递,全局变量与局部变量,函数声明,数组的内存空间,还有对于各个变量的定义。没有真正弄懂这些东西写的程序会多很多错误而且浪费时间,而理解了就能避免很多错误。

确实,自己动手写程序很重要。即使你看的再明白,在动手的时候仍可能会出些错误,这些错误有时是很小的,比如将==写成赋值符号,或者少些了一个“;”,这些都是第一次编写程序时稍不注意便会出现的。一旦多写些程序,注意一下,这种错误便会很容易避免。而且编程序的过程也是综合利用自己所学知识的过程,在这之中可以发现自己的不足,加深对知识的理解。下面就是写的程序了。

**************************************************************************************************************

 

 

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
/*书的基本资料信息*/
struct book
{
char code[20];
char name[20];
char author[15];
char pub[20];
char pub_date[20];
float price;
long kucun;                                   /*库存*/
}book[100];
int a=0;
float cg_money=0,xs_money=0;
void input(int i)                              /*输入图书基本信息*/
{
printf("input information of books:/n");
fflush(stdin);
printf("book code:");
gets(book[i].code);
printf("name:");
gets(book[i].name);
printf("author:");
gets(book[i].author);
printf("pub:");
gets(book[i].pub);
printf("pub_date:");
gets(book[i].pub_date);
printf("price:");
scanf("%f",&book[i].price);
printf("kucun:");
scanf("%ld",&book[i].kucun);
fflush(stdin);
}

extern int a;
void search(char scode[20])                     /*查询书籍函数*/
{ int i;
for(i=0;i<=a;i++)
{
    if(strcmp(scode,book[i].code)==0)
    {
      printf("code:%s/n",book[i].code);
      printf("name:%s/n",book[i].name);
      printf("author:%s/n",book[i].author);
      printf("pub:%s/n",book[i].pub);
      printf("pub_date:%s/n",book[i].pub_date);
      printf("price:%6.1f/n",book[i].price);
      printf("kucun:%d/n",book[i].kucun);
      break;
    }
}
    if(i==a+1)
    {printf("Cannot find !/n");}

}

void cai(char code[20])                          /*采购书籍函数*/
{
float cg_price;
long cg_amount;
int i;
while(1)
{
for(i=0;i<=a;i++)
{
    if(strcmp(code,book[i].code)==0)
    {
      printf("code:%s/n",book[i].code);
      printf("name:%s/n",book[i].name);
      printf("author:%s/n",book[i].author);
      printf("pub:%s/n",book[i].pub);
      printf("pub_date:%s/n",book[i].pub_date);
      printf("price:%6.1f/n",book[i].price);
      printf("kucun:%d/n",book[i].kucun);
      break;
    }
}
    if(i==a+1)
    {printf("Cannot find !/nPress key to redial.../n");
     getch();
     }
     else break;
}
printf("cg_amount:");
scanf("%ld",&cg_amount);
printf("cg_price:");
scanf("%f",&cg_price);
cg_money=cg_amount*cg_price+cg_money;
book[i].kucun=book[i].kucun+cg_amount; fflush(stdin);
}

void sell(char code[20],long amount)                         /*出售书籍函数*/
{

int i=0;
if(strcmp(code,book[i].code)==0)
{
    printf("%s",book[i].name);
    if(amount<=book[i].kucun)
    {
      book[i].kucun=book[i].kucun-amount;
      xs_money=xs_money+amount*book[i].price;
    }
    else
    {
      printf("kucun is not enough,%ld is left/n",book[i].kucun);
    }

}
else {printf("Cannot find !/t Press any key to continue.../n");fflush(stdin);}
}


void main()                                               /*主函数*/
{ char c,code[20];
int s,i=0;
long amount;

while(1)
{ system("cls");                                   /*清屏*/
    printf("1.input information of books/n2.search books/n3.buy books/n4.sell books/n5.profit/n6.exit/n");
    printf("s=");
    scanf("%d",&s);
    switch(s)
    {
      case 1:i=a;
         fflush(stdin);
         input(i);
         printf("continue?/n");
         c=getchar();
         while(c=='y'||c=='Y')
         {
           i++;
           input(i);
           printf("continue?/n");
           c=getchar();

         }
         a=i+1;break;     /*a baoliu shu zu jin xing dao nayige shu*/
      case 2:fflush(stdin);printf("input searching code:");
         gets(code);
         search(code);
         printf("continue?");
         scanf("%c",&c);
         while(c=='y'||c=='Y')
         { fflush(stdin);printf("input searching code:");
           gets(code);
           search(code);
           printf("continue?");
           scanf("%c",&c);
         }
         break;
      case 3:fflush(stdin);
    printf("input code:");
         gets(code);
         cai(code);
         printf("continue?/n");
         c=getch();
         while(c=='y'||c=='Y')
         { fflush(stdin);
           printf("input code:");
           gets(code);
           cai(code);
           printf("continue?");
           c=getch();

         }
         break;
     case 4:fflush(stdin);printf("input code:");
        gets(code);
        printf("input amount:");
        scanf("%ld",&amount); fflush(stdin);
        sell(code,amount);
        printf("continue?/n");
        scanf("%c",&c);
        while(c=='y'||c=='Y')
        { fflush(stdin);
          printf("input code:");
          gets(code);
          printf("input amount:");
          scanf("%ld",&amount);
          sell(code,amount); fflush(stdin);
          printf("continue?");
          c=getchar();
        }
        break;
   case 5:printf("Profit:%.2f/n",xs_money-cg_money);
      getch();
      break;
   case 6:exit(0);

    }
}
}

/*本程序原来在Turbo C中能正常运行,但在VC中出现了几个错误;
进行了一下改正:1.将clrscr();去掉或者换成system(“cls”);
原因是,前者是TC中特有的。
2.增加了几个头文件,#include<string.h>是因为:strcmp;
3.增加了#include<conio.h>是因为:getch();
因为头文件没有加出现的错误是:undeclared identifier
4.主函数没有返回值,且没有声明为void,vc报错
5.本程序可以在vc下正常运行*/
/*功能:主菜单包括6个选项。录入,查找,买进,卖出,计算利润,退出程序。
录入:录入的数据用数组存放。主菜单选择1后,可以根据提示录入数据,录入
时需注意不要输错,因为本程序没有自动纠错和改错删除功能。
查找:本程序只能通过输入图书编号来查找图书基本信息。不能通过其他(如名字)
来查找。
买进:成功买进操作的结果是:该书库存数量增加,花钱增加。
卖出:成功卖出则:库存减少,销售得到的总金额增加。
计算利润:本程序中只有总利润,没有记下每次买进或卖出操作的账单。

亦可用链表来实现。
程序运行时产生的数据只限于本次使用,即再次编译运行程序后需重新输入数据;
要记录数据,可以加入文件。写入或者读出。
多次用到fflush(stdin);是因为在输入数字后不加无法达到目的。具体原因还
不清楚。不过可参考以下一种解释scanf("%d",&n);gets(str);中,读入str的字符
串是“/n”.
本程序除了功能不全外,还有很多缺点,需要进一步修改(代码太多)。*/

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值