資源簡介
商品貨架可以看成一個棧,棧頂商品的生產日期最早,棧底商品的生產日期最近?!∩县洉r,需要倒貨架,以保證生產日期較近的商品在較下的位置。
代碼片段和文件信息
#include
#include
#define?size?3
struct?data/*定義商品信息*/
{
????char?name[6];?/*商品名稱*/
????long?int?date;?/*商品生產日期*/
};
typedef?struct/*定義順序棧*/
{
????struct?data?sp[size];
????int?top;
}seqstack;
int?push1(seqstack?*s1)
{
????int?i=0;
????for(s1->top=0;s1->toptop++)
????{
????????printf(“請輸入第%d件商品名稱:“s1->top+1);
????????scanf(“%s“s1->sp[s1->top].name);
????????printf(“請輸入生產日期:“);
????????scanf(“%d“&s1->sp[s1->top].date);
printf(“\n“);
????}
????return(--s1->top);
}
int?cmp(seqstack?*s1seqstack?*s2int?TOP)
{
????struct?data?new;
????s1->top=TOP;
????s2->top=-1;
????printf(“請輸入新商品名稱:“);
????scanf(“%s“new.name);
????printf(“請輸入生產日期:“);
????scanf(“%d“&new.date);
while(s1->top!=-1&&new.date-s1->sp[s1->top].date>0)
/*比較新舊商品的生產日期*/
????{
??????????s2->top++;
????????
評論
共有 條評論