| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4791 人关注过本帖, 3 人收藏
标题:使用动态链表完成一个简单的商品库存信息管理系统。
只看楼主 加入收藏
faminxmu
Rank: 3Rank: 3
来 自:厦门
等 级:论坛游侠
帖 子:191
专家分:106
注 册:2008-4-23
结帖率:70%
收藏(3)
 问题点数:0 回复次数:15 
使用动态链表完成一个简单的商品库存信息管理系统。
商品信息包括如下字段
商品号
商品名称
商品库存

函数
create:接收用户输入的商品号和商品名称的信息,建立链表;库存初始化为0,没有进货之前不允许销售;商品号为0表示用户输入结束。本函数用于初始化,如果第二次被调用的时候,首先要执行destroy清除旧链表。
destroy:给定链表的头指针,删除链表的所有节点,并释放相应的空间。本函数在程序退出前应至少被调用一次。在调用此函数前,必须给予用户提示,使用户在删除前有反悔的机会。
sell:商品销售,由参数传入商品号和销售数量。如果不存在给定商品号的商品或销售数量大于相应商品的库存则出错;否则,从指定商品的库存中扣除相应的销售数量。当商品库存为0,则从链表中删除该商品。
stock:商品进货,由参数传入商品号和进货数量。如果不存在给定商品号的商品则在链表中插入新商品,并提示用户输入该商品名称;否则,增加指定商品的库存量。
list:列出所有商品的情况。

主程序
程序运行后,循环显示如下菜单: 1. 输入商品信息 2. 销售 3. 进货 4. 列举商品信息 5. 清除所有商品 6. 退出
根据用户的选择进一步提示用户输入并调用对应函数。
搜索更多相关主题的帖子: 链表 信息管理系统 库存 商品 动态 
2008-11-15 10:17
faminxmu
Rank: 3Rank: 3
来 自:厦门
等 级:论坛游侠
帖 子:191
专家分:106
注 册:2008-4-23
收藏
得分:0 
这个是我写的代码而且编译运行完全正确,大家写写看互相学习,顺便帮我看看有哪些不合理的地方
主要是排版问题!
/*******************************************
   作者:发哥
   版本历史:2008-11-15 V1.0
********************************************/

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct goods)
struct goods
{
       long number;
       char name[20];
       int count;
       struct goods* next;
       };
struct goods* creat(struct goods* head);//链表构造函数
struct goods* destory(struct goods* head);//清空物品函数
struct goods* sell(long number,int count,struct goods* head);//售货函数
struct goods* stock(long number,int count,struct goods* head);//进货函数
void list(struct goods* head);//打印函数
struct goods* del(struct goods* p1,struct goods* head);//删除一个接点函数
void main()
{
     struct goods* head=NULL;//让head初始值为0
     long number;
     int count,choose;
     do//菜单选择
     {
         printf("1. 输入商品信息\n2. 销售\n3. 进货\n4. 列举商品信息\n5. 清除所有商品\n6. 退出\n\n");
         printf("input choose:\n");
         scanf("%d",&choose);
         if(choose==1) head=creat(head);
         else if(choose==2)
         {
              printf("input number and count:\n");
              scanf("%ld%d",&number,&count);
              head=sell(number,count,head);
              }
         else if(choose==3)
         {
              printf("input number and count:\n");
              scanf("%ld%d",&number,&count);
              head=stock(number,count,head);
              }
         else if(choose==4) list(head);
         else if(choose==5) head=destory(head);
         }while(choose!=6);
         }
struct goods* creat(struct goods* head1)
{
       struct goods* head=NULL,*p1,*p2;
       int n=0;
       if(head1) destory(head1);//如果head有非0的值就先清空
       p2=p1=(struct goods*)malloc(LEN);//开辟存储单元
       printf("input number and name:\n");
       scanf("%ld%s",&p2->number,p2->name);
       p2->count=0;
       while(p2->number)//链表的创建
       {
                        if(n==0) head=p1;
                        else p1->next=p2;
                        p1=p2;
                        p2=(struct goods*)malloc(LEN);
                        printf("input number and name:\n");
                        scanf("%ld%s",&p2->number,p2->name);
                        p2->count=0;
                        n++;
                        }
                        p1->next=NULL;
                        return head;
                        }
struct goods*destory(struct goods* head)
{
     char word;
     struct goods *p1,*p2;
     p1=p2=head;
     do
     {
                getchar();//用来接收菜单选择时的回车键
                printf("Do you really want to destory the goods system y/n\n?");
                scanf("%c",&word);//对于是否清空系统进行确认
                }while(word!='y'&&word!='n');
     if(word=='y')
     {
                  if(p1)
                  do
                  {
                        p2=p1;
                        p1=p1->next;
                        free((void*)p2);
                        }while(p1);//清空系统并释放存储单元
                        printf("the goods system have been destoried!\n");
                        return NULL;
                        }
     else printf("the operation of destory have been canceled\n");//反悔就不清空原样返回
     return head;
     }
struct goods* sell(long number,int count,struct goods* head)
{
       struct goods *p1,*p2;
       if(head==NULL)
       {
                     printf("error! the system is null!\n");
                     system("pause");
                     return NULL;
                     }//如果系统什么也没有就提示错误
       for(p1=head;p1!=NULL;p1=p1->next)
       if(p1->number==number) break;//找要卖的货物
       if(p1)//如果找到了
       {
           if(p1->count<count)//如果货物不足就提示错误并且返回原样
           {
                              printf("The count is too less!\n");
                              return head;
                              }
           else p1->count-=count;//足够的话就卖出去
           }
           else  printf("error! cannot find %ld's name\n",number);//找不到就提示
           for(p1=head;p1!=NULL;p1=p1->next)
           if(p1->count==0) head=del(p1,head);//查找货物为0的并把它的接点删掉
           return head;
           }
struct goods* stock(long number,int count,struct goods* head)
{
       struct goods *p1,*p2;
       for(p1=head;p1;p1=p1->next)
       if(p1->number==number)//如果找到了对应的号就进货
       {
                             p1->count+=count;
                             break;
                             }
       if(p1==NULL)
       {
                   p2=(struct goods*)malloc(LEN);//找不到就建立一个接点接在最前面
                   printf("input name:\n");
                   getchar();//接收空格
                   gets(p2->name);//对新货物进行初始化
                   p2->number=number;
                   p2->count=count;
                   p2->next=head;
                   head=p2;
                   }
       return head;//把表头返回去
       }
void list(struct goods* head)
{
     if(head)
     do
     {
             printf("%ld %s %d\n",head->number,head->name,head->count);
             head=head->next;
             }while(head);//打印系统清单
             else printf("the system is null!\n");
             }
struct goods* del(struct goods* p1,struct goods* head)
{
       struct goods *p2;
       if(p1==head) head=head->next;//如果接点是表头就把表头移后
       else if(p1==NULL) return head;//如果找不到要删除的点就不删除
       else
       {
           for(p2=head;;p2=p2->next)
           if(p2->next==p1)//删除非表头的接点
           {
                           p2->next=p1->next;
                           break;
                           }
                           }
                           free((void*)p1);//删除并释放空间
                           return head;
                           }

[[it] 本帖最后由 faminxmu 于 2008-12-10 20:13 编辑 [/it]]
收到的鲜花
  • 永夜的极光2008-11-15 19:51 送鲜花  49朵   附言:好文章

在虚拟的世界中寻找目标。
2008-11-15 10:19
网易
Rank: 1
来 自:金星
等 级:禁止访问
帖 子:193
专家分:0
注 册:2008-6-10
收藏
得分:0 
不错

答案是:雨中飞燕!
2008-11-15 10:29
RogueBoy
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-11-15
收藏
得分:0 
支持楼主辛苦编辑代码。
支持原创。
2008-11-15 19:16
lil174
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-11-6
收藏
得分:0 
我看书上面说的每个代码块的花括号的首尾要对齐这是对格式的最基本的要求
LZ的格式非常有个性啊
2008-11-15 19:21
faminxmu
Rank: 3Rank: 3
来 自:厦门
等 级:论坛游侠
帖 子:191
专家分:106
注 册:2008-4-23
收藏
得分:0 
回复 5# 的帖子
有道理,由于我是用DEV-C++写的,所以花括号没有自动对齐,如果在VC下编写则花括号是自动对齐的

在虚拟的世界中寻找目标。
2008-11-15 21:59
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
收藏
得分:0 
dev的风格其实是
int main(void){
    return 0;
}

/images/2011/147787/2011051411021524.jpg" border="0" />
2008-11-15 22:17
faminxmu
Rank: 3Rank: 3
来 自:厦门
等 级:论坛游侠
帖 子:191
专家分:106
注 册:2008-4-23
收藏
得分:0 
关于风格我都不知道什么是比较好了,迷茫啊

在虚拟的世界中寻找目标。
2008-12-08 21:32
ying8501
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:1092
专家分:1446
注 册:2008-11-24
收藏
得分:0 
谢谢。学习了。
2008-12-08 21:36
OneMan
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2008-12-5
收藏
得分:0 
不错不错  学习了
2008-12-10 22:08
快速回复:使用动态链表完成一个简单的商品库存信息管理系统。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022420 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved