69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
/*商品属性结构体*/
typedef struct Commodity
{
char name[10];
long date;
}Commodity;
/*货架堆栈*/
typedef struct node
{
Commodity sp;
struct node *next;
}LinkStackNode, *LinkStack;
void Welcome(LinkStack *top, LinkStack *tmp);
/*初始化堆栈*/
void InitStack(LinkStack *top)
{
*top = (LinkStack)malloc(sizeof(LinkStackNode));
(*top) -> next = NULL;
}
/*打印货架商品货架状态*/
void State(LinkStack *top, LinkStack *tmp)
{
int a, i = 0;
if ((*top) -> next == NULL)
{
printf("商品货架无商品!退出请输入0,返回上一层请输入1:");
scanf("%d", &a);
switch (a)
{
case 0: exit(0);
case 1: Welcome(top, tmp);
}
}
else
{
while ((*top) -> next == NULL)
{
printf("%d.商品名称:%s,生产日期:%", i, (*top) -> sp.name);
i++;
(*top) = (*top) -> next;
}
}
}
/*将商品压入堆栈中*/
void Push1(LinkStack *top, LinkStack *s)
{
(*s) -> next = (*top) -> next;
(*top) -> next = (*s);
}
/*将商品从商品堆栈中压入临时堆栈中*/
void Push2(LinkStack *top, LinkStack *tmp)
{
LinkStackNode *p, *q;
p = (*top) -> next;
q = (*tmp) -> next;
(*top) -> next = p -> next;
p -> next = q;
(*tmp) -> next = p;
}
/*添加商品到商品货架上*/
void AddCommodity(LinkStack *top, LinkStack *tmp)
{
int x = 0, i = 1;
LinkStackNode *s;
while (i == 1)
{
s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
printf("请输入商品名称:");
scanf("%s", s -> sp.name);
printf("请输入商品生产日期:");
scanf("%d", &s -> sp.date);
if ((*top) -> next != NULL)
{
while (s -> sp.date > (*top) -> sp.date)
{
Push2(top, tmp);
}
Push1(top, &s);
while ((*tmp) -> next != NULL)
{
Push2(tmp, top);
}
i = 0;
}
else
{
Push1(top, &s);
i = 0;
}
printf("添加完成,是否需要继续添加?是请输入1,否则请输入0:");
scanf("%d", &i);
}
printf("操作完成,退出请输入0,返回上一层请输入1:");
scanf("%d", &x);
if (x == 1) Welcome(top, tmp);
else exit(0);
}
/*打印关于*/
void PrintAbout(LinkStack *top, LinkStack *tmp)
{
int i;
printf("计算机科学技术1003班 田世磊\n退出请输入0,返回上一层请输入1:");
scanf("%d", &i);
if (i == 1) Welcome(top, tmp);
else exit(0);
}
/*打印欢迎屏幕*/
void Welcome(LinkStack *top, LinkStack *tmp)
{
int i;
printf("欢迎进入XXX商品管理系统,请根据下面数字提示选择进入不同功能\n1.查看货架商品状态\n2.添加商品\n3.关于\n4.退出\n请输入数字:");
scanf("%d", &i);
switch (i)
{
case 1: State(top, tmp);
case 2: AddCommodity(top, tmp);
case 3: PrintAbout(top, tmp);
case 4: exit(0);
}
}
void main()
{
LinkStack tmp;
InitStack(&tmp);
LinkStack top;
InitStack(&top);
Welcome(&top, &tmp);
system("PAUSE");
}
#include <stdio.h>
#include <stdlib.h>
/*商品属性结构体*/
typedef struct Commodity
{
char name[10];
long date;
}Commodity;
/*货架堆栈*/
typedef struct node
{
Commodity sp;
struct node *next;
}LinkStackNode, *LinkStack;
void Welcome(LinkStack *top, LinkStack *tmp);
/*初始化堆栈*/
void InitStack(LinkStack *top)
{
*top = (LinkStack)malloc(sizeof(LinkStackNode));
(*top) -> next = NULL;
}
/*打印货架商品货架状态*/
void State(LinkStack *top, LinkStack *tmp)
{
int a, i = 0;
LinkStackNode *node;
if ((*top) -> next == NULL)
{
printf("商品货架无商品!退出请输入0,返回上一层请输入1:");
scanf("%d", &a);
switch (a)
{
case 0: exit(0);
case 1: Welcome(top, tmp);
}
}
else
{
node = (*top)->next;
while (node != NULL)
{
printf("%d.商品名称:%s,生产日期:%d\r\n", i, node->sp.name, node->sp.date);
i++;
node = node -> next;
}
}
}
// 压栈
void Push(LinkStack top, LinkStackNode *node)
{
node->next = top->next;
top->next = node;
}
// 出栈
LinkStackNode* Pop(LinkStack top)
{
LinkStackNode *node;
if (top->next == NULL)
return NULL;
node = top->next;
top->next = top->next->next;
return node;
}
bool IsEmpty(LinkStack top)
{
if (top->next == NULL)
return true;
else
return false;
}
/*添加商品到商品货架上*/
void AddCommodity(LinkStack *top, LinkStack *tmp)
{
int x = 0, i = 1;
LinkStackNode *s;
LinkStackNode *tempNode;
while (i == 1)
{
s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
printf("请输入商品名称:");
scanf("%s", s -> sp.name);
printf("请输入商品生产日期:");
scanf("%d", &s -> sp.date);
s->next =NULL;
while (!IsEmpty(*top))
{
if (s->sp.date > (*top)->next->sp.date)
{
tempNode = Pop(*top);
Push(*tmp, tempNode);
}
else
{
break;
}
}
Push(*top, s);
while (!IsEmpty(*tmp))
{
tempNode = Pop(*tmp);
Push(*top, tempNode);
}
i = 0;
printf("添加完成,是否需要继续添加?是请输入1,否则请输入0:");
scanf("%d", &i);
}
printf("操作完成,退出请输入0,返回上一层请输入1:");
scanf("%d", &x);
if (x == 1) Welcome(top, tmp);
else exit(0);
}
/*打印关于*/
void PrintAbout(LinkStack *top, LinkStack *tmp)
{
int i;
printf("计算机科学技术1003班 田世磊\n退出请输入0,返回上一层请输入1:");
scanf("%d", &i);
if (i == 1) Welcome(top, tmp);
else exit(0);
}
/*打印欢迎屏幕*/
void Welcome(LinkStack *top, LinkStack *tmp)
{
int i;
printf("欢迎进入XXX商品管理系统,请根据下面数字提示选择进入不同功能\n1.查看货架商品状态\n2.添加商品\n3.关于\n4.退出\n请输入数字:");
scanf("%d", &i);
switch (i)
{
case 1: State(top, tmp); break;
case 2: AddCommodity(top, tmp); break;
case 3: PrintAbout(top, tmp); break;
case 4: exit(0);break;
}
}
void main()
{
LinkStack tmp;
InitStack(&tmp);
LinkStack top;
InitStack(&top);
Welcome(&top, &tmp);
system("PAUSE");
}