📄 main.cpp
字号:
#include <iostream>
#include <cstring>
using namespace std;
struct inv
{
char item[40];
double cost;
double retail;
int on_hand;
int lead_time;
inv *next;
};
//建立商品信息库
inv *creat()
{
inv *head, *tail, *pro;
head = tail = NULL;
cout << "Please initialize first:" << endl;
while (1)
{
pro = new inv;
pro -> next = NULL;
cout << "Item: ";
cin >> pro -> item;
//如果输入商品名字为0,结束
if (pro -> item[0] == '0')
{
delete pro;
break;
}
cout << "Cost: ";
cin >> pro -> cost;
cout << "Retail: ";
cin >> pro -> retail;
cout << "on_hand: ";
cin >> pro -> on_hand;
cout << "Lead_time: ";
cin >> pro -> lead_time;
//如果链表为空,加入第一个结点
if (tail == NULL)
head = tail = pro;
else
{
//如果链表中已经有其他点,将新结点加入链表结尾
tail -> next = pro;
tail = pro;
}
}
cout << endl;
return head;
}
//选择菜单
char manu()
{
char str;
do
{
cout << "e: enter new information" << endl;
cout << "d: display all of them" << endl;
cout << "r: delete one of them" << endl;
cout << "u: update one of them" << endl;
cout << "v: view one of them" << endl;
cout << "q: quit" << endl << endl;
cout << "choose one: ";
cin >> str;
}
while (!strchr("edruvq", tolower(str)));
return str;
}
//输出所有库存信息
void display(inv *pro)
{
//如果链表为空,输出提示语句
if (pro == NULL)
{
cout << "Link is NULL!" << endl << endl;
return;
}
inv *p;
p = pro;
//如果链表不为空,输出库存信息
while (p != NULL)
{
cout << "Items: " << p -> item << endl;
cout << "Cost: " << p -> cost << endl;
cout << "Retail price: " << p -> retail << endl;
cout << "On hand: " << p -> on_hand << endl;
cout << "Lead time to resupply: " << p -> lead_time << endl;
cout << endl;
p = p -> next;
}
}
//输入新产品信息
inv *enter(inv *pro)
{
inv *tail, *p;
tail = pro;
//如果链表不为空,找到链表结尾
if (tail != NULL) //*********必须加这一步判断,否则链表为空时,输入产品信息会出错
{
while (tail -> next != NULL)
tail = tail -> next;
}
//申请结点,存入新产品信息
p = new inv;
p -> next = NULL;
cout << "Item: ";
cin >> p -> item;
cout << "Cost: ";
cin >> p -> cost;
cout << "Retail: ";
cin >> p -> retail;
cout << "on_hand: ";
cin >> p -> on_hand;
cout << "Lead_time: ";
cin >> p -> lead_time;
//如果链表为空,加入第一个结点 *********必须加这一步,否则链表为空时,输入产品信息会出错
if(tail == NULL)
tail = p;
//如果链表不为空,将新结点加到链表尾部
else
tail -> next = p;
cout << endl;
return pro;
}
//删除某个产品信息
inv *remove(inv *pro)
{
//如果链表为空,输出提示信息
if (pro == NULL)
{
cout << "Link is NULL" << endl << endl;
return pro;
}
inv *p, *q;
p = pro;
q = pro -> next;
char name[40];
cout << "Please input the item you want to remove: ";
cin >> name;
cout << endl;
//如果头结点为要删除的点,删除之
if (!strcmp(name, pro -> item))
{
p = pro;
pro = pro -> next;
delete p;
return pro;
}
//找到要删除结点
while (q != NULL)
{
if (strcmp(name, q -> item))
{
p = q;
q = q -> next;
}
}
if (q != NULL)
{
p -> next = q -> next;
delete q;
}
//如果未找到要删除的产品,输出提示信息
else
cout << "Item is not found!" << endl;
cout << endl;
return pro;
}
//更新某个产品信息
inv *update(inv *pro)
{
//如果链表为空,输出提示信息
if (pro == NULL)
{
cout << "Link is NLL!" << endl << endl;
return pro;
}
inv *p;
p = pro;
char name[40];
cout << "Please input the item you want to update: ";
cin >> name;
cout << endl;
//找到要更新的产品
while (p != NULL)
p = p -> next;
//如果未找到该产品,输出提示信息
if (p == NULL)
{
cout << "Item is not found!" << endl << endl;
return pro;
}
//更新产品信息
cout << "Cost: ";
cin >> p -> cost;
cout << "Retail: ";
cin >> p -> retail;
cout << "on_hand: ";
cin >> p -> on_hand;
cout << "Lead_time: ";
cin >> p -> lead_time;
cout << endl;
return pro;
}
//查看某种产品信息
void view(inv *pro)
{
//如果链表为空,输出提示信息
if (pro == NULL)
{
cout << "Link is NULL!" << endl << endl;
return;
}
char name[40];
cout << "Please input the item you want to view: ";
cin >> name;
cout << endl;
inv *p;
p = pro;
//找到要查看的产品
while (p != NULL)
p = p -> next;
//如果未找到该产品,输出提示信息
if (p == NULL)
{
cout << "Item is not found!" << endl << endl;
return;
}
//输出该产品信息
cout << "Items: " << p -> item << endl;
cout << "Cost: " << p -> cost << endl;
cout << "Retail price: " << p -> retail << endl;
cout << "On hand: " << p -> on_hand << endl;
cout << "Lead time to resupply: " << p -> lead_time << endl;
cout << endl;
}
int main()
{
inv *product;
product = new inv;
product = NULL;
//链表初始化
product = creat();
char choice;
while (1)
{
//读入要进行的操作
choice = manu();
switch (choice)
{
case 'e':
product = enter(product);
break;
case 'd':
display(product);
break;
case 'r':
product = remove(product);
break;
case 'u':
product = update(product);
break;
case 'v':
view(product);
break;
case 'q':
return 0;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -