所有实践项目的源程序可到我的Github上下载:https://github.com/liulizhi1996/C-Programming-Practice-in-short-semester
• 使用动态链表完成一个简单的商品库存信息管理系统。
• 商品信息包括如下字段:商品号、商品名称、商品库存
• 函数
create:接收用户输入的商品号和商品名称的信息,建立链表;库存初始化为0,没有进货之前不允许销售;商品号为0表示用户输入结束。本函数用于初始化,如果第二次被调用的时候,首先要执行destroy清除旧链表。
destroy:给定链表的头指针,删除链表的所有节点,并释放相应的空间。本函数在程序退出前应至少被调用一次。在调用此函数前,必须给予用户提示,使用户在删除前有反悔的机会。
sell:商品销售,由参数传入商品号和销售数量。如果不存在给定商品号的商品或销售数量大于相应商品的库存则出错;否则,从指定商品的库存中扣除相应的销售数量。当商品库存为0,则从链表中删除该商品。
stock:商品进货,由参数传入商品号和进货数量。如果不存在给定商品号的商品则在链表中插入新商品,并提示用户输入该商品名称;否则,增加指定商品的库存量。
list:列出所有商品的情况。
• 主程序
程序运行后,循环显示如下菜单:
1. 输入商品信息2. 销售3. 进货4. 列举商品信息5. 清除所有商品6.退出
根据用户的选择进一步提示用户输入并调用对应函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRODUCT_NO_SIZE 15
#define PRODUCT_NAME_SIZE 50
struct Product
{
char no[PRODUCT_NO_SIZE];
char name[PRODUCT_NAME_SIZE];
int quantity;
};
struct ListNode
{
struct Product product;
struct ListNode *next;
};
struct ListNode *list;
int Create();
int Destroy();
int Sell(char *no, int quantity);
void Stock(char *no, int quantity);
void List();
void interface();
int Create()
{
printf("*** CREATE ***\n");
static int call_times = 0;
call_times++;
if (call_times > 1)
{
printf("!!! Warning: Before creation, system will destroy the existed records !!!\n");
if (!Destroy())
{
printf("Exit creation.\n");
return 0;
}
getchar();
}
char no[PRODUCT_NO_SIZE];
char name[PRODUCT_NAME_SIZE];
struct ListNode *p;
list = (struct ListNode *)malloc(sizeof(struct ListNode));
list->next = NULL;
while (1)
{
printf("Please enter the no (quit if no input): ");
fgets(no, PRODUCT_NO_SIZE, stdin);
no[strlen(no) - 1] = 0;
if (strlen(no) == 0)
{
printf("Finish creating!\n");
break;
}
p = (struct ListNode *)malloc(sizeof(struct ListNode));
strcpy(p->product.no, no);
printf("Please enter the name: ");
fgets(name, PRODUCT_NAME_SIZE, stdin);
name[strlen(name) - 1] = 0;
strcpy(p->product.name, name);
p->product.quantity = 0;
p->next = list->next;
list->next = p;
}
return 1;
}
int Destroy()
{
printf("*** DESTROY ***\n");
int choice;
printf("Do you want to destroy existed records? (1 - Yes, 0 - No): ");
scanf("%d", &choice);
if (!choice)
return 0;
struct ListNode *p = list, *q;
while (p)
{
q = p->next;
free(q);
p = q;
}
list = NULL;
printf("Finish destroying!\n");
return 1;
}
int Sell(char *no, int quantity)
{
struct ListNode *p = list->next, *q = list;
while (p)
{
if (strcmp(p->product.no, no) == 0)
{
if (p->product.quantity >= quantity)
{
p->product.quantity -= quantity;
if (p->product.quantity == 0)
{
q->next = p->next;
free(p);
p = q->next;
continue;
}
printf("Finish to selling!\n");
return 1;
}
else
{
printf("No sufficient stock! Failed to sell!\n");
return 0;
}
}
q = p;
p = p->next;
}
printf("No such record! Failed to sell!\n");
return 0;
}
void Stock(char *no, int quantity)
{
struct ListNode *p = list->next, *q;
while (p)
{
if (strcmp(p->product.no, no) == 0)
{
p->product.quantity += quantity;
printf("Finish stocking!\n");
return;
}
p = p->next;
}
char name[PRODUCT_NAME_SIZE] = { 0 };
q = (struct ListNode *)malloc(sizeof(struct ListNode));
strcpy(q->product.no, no);
printf("Please enter the name: ");
fgets(name, PRODUCT_NAME_SIZE, stdin);
name[strlen(name) - 1] = 0;
strcpy(q->product.name, name);
q->product.quantity = quantity;
q->next = list->next;
list->next = q;
}
void List()
{
if (!list)
{
printf("Please create list first!\n");
return;
}
printf("*** LIST ***\n");
struct ListNode *p = list->next;
if (p)
{
int i = 0;
while (p)
{
++i;
printf("%-3d - No: %s\t\tName: %s\t\tQuantity: %d\n", i, p->product.no, p->product.name, p->product.quantity);
p = p->next;
}
}
else
{
printf("No products info!\n");
}
}
void interface()
{
int choice;
char no[PRODUCT_NO_SIZE] = { 0 };
int quantity;
while (1)
{
printf("-------- Products Management System --------\n");
printf("1. Create\n");
printf("2. Sell\n");
printf("3. Stock\n");
printf("4. List\n");
printf("5. Destroy\n");
printf("6. Exit\n");
printf("--------------------------------------------\n");
printf("Your choice: ");
scanf("%d", &choice);
getchar();
switch (choice)
{
case 1:
Create(); break;
case 2:
printf("*** SELL ***\n");
if (!list)
{
printf("Please create list first!\n");
break;
}
printf("Please enter the no: ");
fgets(no, PRODUCT_NO_SIZE, stdin);
no[strlen(no) - 1] = 0;
printf("Please enter the quantity: ");
scanf("%d", &quantity);
Sell(no, quantity);
break;
case 3:
printf("*** STOCK ***\n");
if (!list)
{
printf("Please create list first!\n");
break;
}
printf("Please enter the no: ");
fgets(no, PRODUCT_NO_SIZE, stdin);
no[strlen(no) - 1] = 0;
printf("Please enter the quantity: ");
scanf("%d", &quantity);
getchar();
Stock(no, quantity);
break;
case 4:
List();
break;
case 5:
Destroy();
break;
case 6:
printf("Thank you for your using!\n");
return;
default:
printf("Unrecognized command.\n");
}
}
}
int main()
{
list = NULL;
interface();
return 0;
}
测试数据:
Product no Product name
DR100 juice
DR209 coke cola
FOOD302 bread
VEG091B tomato
VEG091A potato
BK991M1 Intro. To Alg.