「已注销」 2019-10-14 23:31 采纳率: 0%
浏览 745

C语言直接跳过了switch进行了结尾的语句怎么解决?

//该程序实现了简单的停车场以及计费功能,若停车场满了还有汽车要进停车场便要在便道等候直到停车场有空位
#include<stdio.h>
#include<stdlib.h>
int carPark = 0; 
struct Queue{
    int carId;
    int enterTime;
    int leftTime;
    struct Queue *next;
};
struct Park{
    int carId;
    int enterTime;
    int leftTime;
};
struct Pointer{
    struct Park *base;
    struct Park *top;
    struct Queue *front;
    struct Queue *rear;
};
void ParkingLot(struct Pointer *p,int n){       //停车场
    p->base = (struct Park *)malloc(n*sizeof(struct Park));
    p->top = p->base;
}
void Convenient(struct Pointer *p){     //便道
    p->front = (struct Queue *)malloc(sizeof(struct Queue));
    p->rear = p->front; 
    p->front->next = NULL;
}
void TempPark(struct Pointer *Tp,int n){    //临时停车场
    Tp->base = (struct Park*)malloc((n-1)*sizeof(struct Park));
    Tp->top = Tp->base;
}
void CarEnter(struct Pointer *p,int carId,int enterTime,int n){     //汽车停车
    if(p->top - p->base == n){  //判断停车场是否满了
        printf("停车场已满,请在便道等候!\n");
        struct Queue *q = (struct Queue *)malloc(sizeof(struct Queue));
        q->carId = carId;
        q->enterTime = 0;
        p->rear->next = q;
        p->rear = q;
        q->next = NULL;
    }else{
        p->top->carId = carId;
        p->top->enterTime = enterTime;
        p->top++;
        carPark++;
        printf("停车场里有%d辆车。\n",carPark);
    }
} 
void CarLeft(struct Pointer *p,struct Pointer *Tp,int carId,int leftTime,int n){//汽车离开停车场 
        p->top--;
        while(p->top->carId != carId){          //停车场出临时停车场 
            Tp->top->carId = p->top->carId;
            Tp->top->enterTime = p->top->enterTime;
            Tp->top++;
            p->top--;
        }
        printf("牌照号码:%d 的车已经离开停车场,停车费用为:%d \n",carId,leftTime-p->top->enterTime);
        carPark--;
        printf("停车场里有%d辆车。\n",carPark);
        while(Tp->top!=Tp->base){       //临时停车场返回停车场 
            Tp->top--;
            p->top->carId = Tp->top->carId;
            p->top->enterTime = Tp->top->enterTime;
            p->top++;   
        }
        if(p->front!=p->rear&&p->top-p->base <= n){     //便道进入停车场
            int enterTime;
            printf("便道汽车进入停车场,进入的时间(enterTime):");
            scanf("%d",&enterTime); 
            struct Queue *q = (struct Queue *)malloc(sizeof(struct Queue));
            q = p->front->next;
            printf("牌照:%d的汽车已进入停车场.\n",q->carId);
            p->top->carId = q->carId;
            p->top->enterTime = enterTime;
            p->top++;
            p->front->next = q->next;
            if(p->rear==q)
                p->front = p->rear;
            carPark++;
            printf("停车场里有%d辆车。\n",carPark);
        }   
}
int main(){
    struct Pointer *p;
    struct Pointer p1;
    int count = 1;
    p = &p1;
    struct Pointer *Tp;
    struct Pointer p2;
    Tp = &p2;
    int n; 
    char C;
    int carId;
    int enterTime,leftTime,tempTime;
    printf("输入要创建的停车场的长度n:");
    scanf("%d",&n);
    ParkingLot(p,n);
    Convenient(p);
    TempPark(Tp,n);
    printf("创建便道......\n");
    printf("请选择要进行的操作(到达/离去/结束:A/D/E  牌照:carId  进入/离开时间:time):");
    while(scanf("%c%d%d",&C,&carId,&tempTime)!=EOF&&C!='E'){
        switch(C){
            case 'A':
                enterTime = tempTime;
                CarEnter(p,carId,enterTime,n);
                break;
            case 'D':
                leftTime = tempTime;
                CarLeft(p,Tp,carId,leftTime,n);
                break;
            default:
                break;
         }
         if(count%2 == 1){
            count++;
        }else{
            printf("请选择要进行的操作(到达/离去/结束:A/D/E  牌照:carId  进入/离开时间:time):");
            count++;    
         }
     }  
    return 0;
}

 ![图片说明](https://img-ask.csdn.net/upload/201910/14/1571066750_441303.jpg)

 如果没有主函数最后面的if(count%2==1)语句就会多输出那个黄色的语句,那是多余的.
  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2019-10-15 08:59
    关注

    单步调试一下switch 分支接收到的 C 是什么值呢?跳到最后一步 default 说明没有匹配到分支值,可能跟类型有关。

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?