| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 436 人关注过本帖
标题:大哥们帮忙改下错误吧(关于栈的)
只看楼主 加入收藏
暗夜精灵
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-7-3
收藏
 问题点数:0 回复次数:0 
大哥们帮忙改下错误吧(关于栈的)
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "constant.h"
#include "car.h"
#include "SqStack.h"
#include "LinkQueue.h"

void printtitle(){                                    //先清空屏幕再打印标题
    system("cls");                                    //Visual c++(头文件stdlib.h)
                                                    //clrscr(); Turboc(头文件conio.h)
    printf("\n\t\t\t/********************************\\");
    printf("\n\t\t\t<*\t\t\t\t*>");
    printf("\n\t\t\t<*");
    printf("\t  停车场管理系统\t*>");
    printf("\n\n\n                              欢迎使用停车场管理系统");
    printf("\n\t\t\t<*\t\t\t\t*>");
    printf("\n\t\t\t\\********************************/");
}

void printhead(int n,float fee){
                                          //先清空屏幕再打印标题、停车场大小和费用
    printf("\n\n\t\t=================================================");
    printf("\n\t\t||          停车场能停汽车的最大数量n: %d       ||",n);
    //printf("\n\t\t||---------------------------------------------||");
    printf("\n\t\t||        每辆车每小时的停车费用fee: %.2f$     ||",fee);
    printf("\n\t\t=================================================\n\n");

}
void printsave(SqStack *S,LinkQueue *Q){            //打印停放车辆的信息
    int i;
    SElemType *e=S->base;                            //开始时e指向栈底
    QueuePtr q;
    printf("\n\t-----------------------------------------------------------------");
    for(i=1;i<=SqStackLength(*S);i++,e++){            //若栈不空打印栈中各元素信息
        printf("\n\t|\t  车牌号: %-3d  停放位置: 停车场%-2d号  进场时刻: %-5d\t|",e->number,i,e->time);
        printf("\n\t-----------------------------------------------------------------");
    }
    q=Q->front->next;                                //开始时q指向首元结点
    for(i=1;i<=LinkQueueLength(*Q);i++,q=q->next){  //若队列不空打印队列中各元素信息
        printf("\n\t|\t  车牌号: %-3d  停放位置: 便道上%-2d号  进道时刻: %-5d\t|",q->data.number,i,q->data.time);
        printf("\n\t-----------------------------------------------------------------");
    }
}
void printsave2(SqStack *S){                        //只打印栈中停放车辆的信息
    int i;
    SElemType *e=S->top-1;                            //开始时e指向栈底
    for(i=SqStackLength(*S);i>=1;i--,e--){            //若栈不空打印栈中各元素信息
        printf("\n\t|\t  车牌号: %-3d  停放位置: 停车场%-2d号  进场时刻: %-5d\t|",e->number,S->stacksize-i+1,e->time);
        printf("\n\t-----------------------------------------------------------------");
    }
    /*q=Q->front->next;                                //开始时q指向首元结点
    for(i=1;i<=LinkQueueLength(*Q);i++,q=q->next){  //若队列不空打印队列中各元素信息
        printf("\n\t|\t  车牌号: %-3d  停放位置: 便道上%-2d号  进道时刻: %-5d\t|",q->data.number,i,q->data.time);
        printf("\n\t-----------------------------------------------------------------");
    }*/
}
void arrive(SqStack *S,LinkQueue *Q,SElemType car,float fee){                    //有车辆到达时调用
    if(SqStackFull(*S))                                //栈满时入队
        EnQueue(Q,car);
    else                                            //栈不满时进栈
        Push(S,car);
    printhead(S->stacksize,fee);                    //刷新屏幕
    printsave(S,Q);
}
void departure(SqStack *S1,SqStack *S2,LinkQueue *Q,SElemType car,float fee){    //有车辆离开时调用
    SElemType e1;
    if(SqStackEmpty(*S1)){                            //停车场内为空时
        printhead(S1->stacksize,fee);
        printf("\n\n\t\t\t     停车场内没有停放任何车辆!\n");
    }
    while(!SqStackEmpty(*S1)){                        //若栈S1不空逐个出栈(若遇break结束循环)
        
        Pop(S1,&e1);
        if(e1.number==car.number)
        {                    //与出栈的元素比较 相匹配时刷新屏幕
            printhead(S1->stacksize,fee);
            if(car.time>e1.time){
                printf("\n\t     -------------------------------------------------------");
                printf("\n\t     |    车牌号: %-3d停车场内停放时段: %d--%-5d费用: %.2f  |",car.number,e1.time,car.time,fee*(car.time-e1.time));
                printf("\n\t     -------------------------------------------------------");
                if(LinkQueueEmpty(*Q)){
                    printsave(S1,Q);
                    printsave2(S2);
                }
                break;        
            }
            else{
                printf("\n\t\t\t 输入错误! 出场时刻应大于入场时刻!\n");
                Push(S1,e1);                        //时刻不正确时不应出栈    故将元素放回
                printsave(S1,Q);
                printsave2(S2);
                break;
            }
        }
        Push(S2,e1);                                //将出栈元素逐个放入S2中
        if(SqStackEmpty(*S1)){                        //S1中元素全部出栈但没有匹配成功时
            printhead(S1->stacksize,fee);
            printf("\n\t-----------------------------------------------------------------");
            printsave2(S2);
            printsave(S1,Q);
            printf("\n\n\t\t\t    停车场内没有车牌号为%d的车辆!\n",car.number);
        }
    }
    while(!SqStackEmpty(*S2)){                        //S2中元素逐个出栈并放回S1中
        Pop(S2,&e1);
        Push(S1,e1);
    }
    if(SqStackLength(*S1)==(*S1).stacksize-1&&!LinkQueueEmpty(*Q)){                //满栈有元素出栈并且队列非空时 出队的元素进栈
        printf("\n\t     |    车牌号: %-3d的车从便道驶入停车场%-2d号\t\t   |",Q->front->next->data.number,(*S1).stacksize);
        printf("\n\t     -------------------------------------------------------\n");
        DeQueue(Q,&e1);
        e1.time=car.time;                            //记录进栈时刻
        Push(S1,e1);
        printsave(S1,Q);                            //刷新屏幕
    }   
}


void main(){



    float fee;
    char abc;
    SElemType car;
    SqStack S1,S2;
    LinkQueue Q;
   
                           
                    do{
                        printtitle();
                        printf("\n\n\n\t\t\t请输入停车场能停汽车的最大数量: n=");
                        scanf("%d",&S1.stacksize);
                        S2.stacksize=S1.stacksize;
                        printf("\t\t\t请输入每辆车每小时的停车费用: fee=");
                        scanf("%f",&fee);
                        if(S1.stacksize<=0||fee<=0){                                //输入不正确时输出
                        
                            printf("\n\n\n\n\t\t 输入错误!n应为大于0的整数,fee应为大于0的浮点数!\n\n");
                            printf("\n                                  请重新输入!");
                            getch();
                        }
                    }while(S1.stacksize<=0||fee<=0);                //若输入不正确则重新输入
                    printhead(S1.stacksize,fee);

                    InitSqStack(&S1);                                //建立S1,S2,Q
                    InitSqStack(&S2);
                    InitLinkQueue(&Q);
                    do{
                        printf("\n\n\t\t\t 请输入 A/D 车牌号 到达或离开时刻:");
                        fflush(stdin);
                        scanf("%c,%d,%d",&abc,&car.number,&car.time);
                        //getchar();                          读回回车符
                        if(abc=='A'||abc=='a')
                            arrive(&S1,&Q,car,fee);
                        if(abc=='D'||abc=='d')
                            departure(&S1,&S2,&Q,car,fee);
                    }while(abc!='E'&&abc!='e');                        //输入e或E结束循环
                    DestroySqStack(&S1);                            //销毁S1,S2,Q
                    DestroySqStack(&S2);
                    DestroyLinkQueue(&Q);
                    exit(OK);                                        //退出系统
                }
        
        

这是模拟停车场管理系统,在车入满时候出栈打印正确,但是车不满时候出栈打印错误,错误在printsave2中S->stacksize-i+1只有当刚开始车入栈满时候才满足条件,请教如何修改不满斩时候打印也正确?
搜索更多相关主题的帖子: include system 大哥 
2010-07-03 21:57
快速回复:大哥们帮忙改下错误吧(关于栈的)
数据加载中...
 
   



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

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