zoukankan      html  css  js  c++  java
  • 数据结构实验6

    题目:某汽车轮渡口,过江渡船每次能载 10 辆车过江。过江车辆分别为客车类和 货车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上 4 辆客 车,才允许上一辆货车;若等待客车不足 4 辆则以货车代替;若无货车等待则允许客 车都上船。设计一个算法模拟渡口管理。

    test.h

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    #define MaxSize 50
    typedef int elemtype;
    typedef struct SequenQueue
    {
        elemtype sequ[MaxSize];
        int front;
        int Len;        //内含元素的个数
        int Rear;        //队尾元素的位置
    }SequenQueue;
    
    SequenQueue *Init_SequenQueue()
    {
        SequenQueue * Q;
        Q = (SequenQueue *)malloc(sizeof(SequenQueue));
        if (Q == NULL)
        {
            printf("申请空间失败
    ");
            exit(0);
        }
    
        if (Q != NULL)
        {
            Q->front = 0;
            Q->Rear = 0;
            Q->Len = 0;
        }
        return Q;
    }
    void Print(SequenQueue *Q)
    {
        int a, i = 0;
        a = Q->Len;
        while (a != 0)
        {
            printf("%d  ", Q->sequ[(Q->front + i) % MaxSize]);
            a--;
            i++;
        }
    }
    
    void Printexit(SequenQueue *Q)
    {
        int a, i = 0;
        a = Q->Len;
        while (a != 0)
        {
            printf("%d	", Q->sequ[(Q->front + i) % MaxSize]);
            a--;
            i++;
            Q->Len--;                    //长度减一
            Q->front = (Q->front + 1) % MaxSize;    //移动头指针
    
        }
        system("pause");
    }
    void menu()
    {
        printf("---菜单栏---
    ");
        printf("1.客车入队
    ");
        printf("2.货车入队
    ");
        printf("3.查看当前客车队列
    ");
        printf("4.查看当前货车队列
    ");
        printf("5.汽车装载渡船
    ");
        printf("6.查看渡船队列
    ");
        printf("0.退出
    ");
        printf("提示:请输入正确指令进行操作
    ");
    }
    void entryQueue(SequenQueue *Q)//入队
    {
        int x;
        if (Q->Len == MaxSize)
        {
            printf("队列已满,不能入列。
    ");
            return;
        }
        printf("输入x并以0结束:");
        scanf("%d", &x);
        while (x != 0)
        {
            Q->sequ[Q->Rear] = x;    //入队
            Q->Rear = (Q->Rear + 1) % MaxSize;        //移动队尾指针
            Q->Len++;
            printf("已入队
    ");
            printf("尾部位置%d
    ", Q->Rear);
            printf("头部位置%d
    ", Q->front);
            scanf("%d", &x);
        }
    
        return;
    }
    
    int exitQueue(SequenQueue *Q)//出队
    {
        int x, First;//队头元素的下标
        if (Q->Len == 0)
        {
            printf("队列已空,不能出队
    ");
            return -1;
        }
        First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize;        //计算队头元素的下标
        x = Q->sequ[Q->front];            //获得队头元素
        Q->Len--;                    //长度减一
        Q->front = (Q->front + 1) % MaxSize;    //移动头指针
        printf("已将%d出队
    ", x);
        printf("尾部位置%d
    ", Q->Rear);
        printf("头部位置%d
    ", Q->front);
        return x;
    }
    
    int exit1(SequenQueue *Q)
    {
        int x, First;//队头元素的下标
        First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize;        //计算队头元素的下标
        x = Q->sequ[Q->front];            //获得队头元素
        Q->Len--;                    //长度减一
        Q->front = (Q->front + 1) % MaxSize;    //移动头指针
        return x;
    }
    void entry1(SequenQueue *Q, int x)
    {
        if (Q->Len == MaxSize)
        {
            printf("队列已满,不能入列。
    ");
            return;
        }
        Q->sequ[Q->Rear] = x;    //入队
        Q->Rear = (Q->Rear + 1) % MaxSize;        //移动队尾指针
        Q->Len++;
        return;
    }
    
    void Output(SequenQueue *b, SequenQueue *t, SequenQueue *f)
    {
        int totalNum = 0, busNum = 0, truckNum = 0;
        int x;
        while (totalNum < 10)
        {
            if (busNum < 4 && b->Len != 0)
            {
                x = exitQueue(b);//出队
                entry1(f, x);//入队
                totalNum++;
                busNum++;
            }
            else if (busNum < 4 && b->Len == 0 && t->Len != 0)
            {
                x = exitQueue(t);//出队
                entry1(f, x);//入队
                totalNum++;
                truckNum++;
                busNum = 0;
            }
            else if (busNum >= 4 && t->Len != 0)
            {
                x = exitQueue(t);//出队
                entry1(f, x);//入队
                totalNum++;
                truckNum++;
                busNum = 0;
            }
            else if (busNum >= 4 && t->Len == 0 && b->Len != 0)
            {
                x = exitQueue(b);//出队
                entry1(f, x);//入队
                totalNum++;
                truckNum = 0;
                busNum++;
            }
            else
            {
                return;
            }
        }
    }

    test.c

    #include "test.h"
    
    void main()
    {
        int i;
        SequenQueue *bus;
        SequenQueue *truck;
        SequenQueue *ferry;
        truck = Init_SequenQueue();
        bus = Init_SequenQueue();
        ferry = Init_SequenQueue();
        menu();
        i = _getch();
        while (i)
        {
    
            switch (i)
            {
            case '1':    
                entryQueue(bus); 
                system("pause");
                break;
            case '2':     
                entryQueue(truck);
                system("pause");
                break;
            case '3':    
                Print(bus);  
                printf("
    ");
                system("pause");
                break;
            case '4':     
                Print(truck);  
                printf("
    ");
                system("pause");
                break;
            case '5':
                Output(bus, truck, ferry);
                system("pause");
                break;
            case '6':
                printf("渡船顺序:");
                Print(ferry);  
                printf("
    ");
                system("pause");
                break;
            case '0':     
                exit(0);    
                break;
            default:
                printf("请正确输入
    ");
                system("pause");
                break;
            }
            system("cls");
            menu();
            i = _getch();
        }
    }
  • 相关阅读:
    无法直接启动带有类库输出类型的项目
    2个页面传值方法
    vs2005 无法附加 绑定句柄无效 解决办法
    认识serializable,序列化
    jsp 连接sql 2008
    有进步,嘎嘎....
    找不到存储过程'dbo.aspnet_CheckSchemaVersion'
    BackOffice Common中实现的相关功能
    MVC中Action相关方法的建议
    mysql的数据库相关维护操作:重启、修改连接数、删除连接
  • 原文地址:https://www.cnblogs.com/wuyibb/p/6930630.html
Copyright © 2011-2022 走看看