数据结构实验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("申请空间失败\n");
        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\t", Q->sequ[(Q->front + i) % MaxSize]);
        a--;
        i++;
        Q->Len--;                    //长度减一
        Q->front = (Q->front + 1) % MaxSize;    //移动头指针

    }
    system("pause");
}
void menu()
{
    printf("---菜单栏---\n");
    printf("1.客车入队\n");
    printf("2.货车入队\n");
    printf("3.查看当前客车队列\n");
    printf("4.查看当前货车队列\n");
    printf("5.汽车装载渡船\n");
    printf("6.查看渡船队列\n");
    printf("0.退出\n");
    printf("提示:请输入正确指令进行操作\n");
}
void entryQueue(SequenQueue *Q)//入队
{
    int x;
    if (Q->Len == MaxSize)
    {
        printf("队列已满,不能入列。\n");
        return;
    }
    printf("输入x并以0结束:");
    scanf("%d", &x);
    while (x != 0)
    {
        Q->sequ[Q->Rear] = x;    //入队
        Q->Rear = (Q->Rear + 1) % MaxSize;        //移动队尾指针
        Q->Len++;
        printf("已入队\n");
        printf("尾部位置%d\n", Q->Rear);
        printf("头部位置%d\n", Q->front);
        scanf("%d", &x);
    }

    return;
}

int exitQueue(SequenQueue *Q)//出队
{
    int x, First;//队头元素的下标
    if (Q->Len == 0)
    {
        printf("队列已空,不能出队\n");
        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出队\n", x);
    printf("尾部位置%d\n", Q->Rear);
    printf("头部位置%d\n", 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("队列已满,不能入列。\n");
        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("\n");
            system("pause");
            break;
        case '4':     
            Print(truck);  
            printf("\n");
            system("pause");
            break;
        case '5':
            Output(bus, truck, ferry);
            system("pause");
            break;
        case '6':
            printf("渡船顺序:");
            Print(ferry);  
            printf("\n");
            system("pause");
            break;
        case '0':     
            exit(0);    
            break;
        default:
            printf("请正确输入\n");
            system("pause");
            break;
        }
        system("cls");
        menu();
        i = _getch();
    }
}

 

posted @ 2017-06-01 20:22  五邑bb  阅读(1631)  评论(0编辑  收藏  举报