【数据结构】停车场问题

简介: 【数据结构】停车场问题

实验二 线性表的应用(二)


【实验类别】设计型实验


【实验目的】


1.熟悉线性表的顺序存储和链式存储各自的特点及运算;

2.熟练掌握线性表的基本操作在不同存储结构中实现算法;

3.通过本次实验帮助学生加深对C语言的使用(特别是函数的参数调用、指针类型的应用和链表的建立等各种基本操作)

4.对一个实际的问题能够进行合理的需求分析,选择合适的存储结构,设计完成符合实际需要的功能。


【实验学时】4学时


【实验组人数】1人。


【实验设备环境】计算机,VC++6.0,C-Free等


【实验内容】


1、停车场的管理(4学时)

【问题描述】设有一个可以停放n辆汽车的停车场,它有二个大门可以供车辆进出,其中一个进,一个出。车辆到达停车场后任意选择空闲停车位停放,每个停车位按顺序编号。如果停车场已放满n辆车,则后来的车辆只能停在停车场大门外的便道上等待,一旦停车场里有车开走,则排在便道上的第一辆车就进入停车场。每辆车离开停车场时,都应根据其在停车场的逗留时间交费。如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆顺序。编制一程序模拟停车场的管理。


13f11334dbe03953b3a5c6fd3ebef636_e632f76e0fe9dabdda3cd67af31f0e75.png

[基本要求] 1、要求程序输出每辆车到达后的停车位置(停车场或便道上);

2、某辆车离开停车场时应交纳的费用和停留时间;

3、可以随时查看停车场及便道的状态。

4、可以随时查看空闲停车位。4、可以随时查看空闲停车位。

【实现提示】

1.本题可以用静态链表作为存储结构

2.汽车模拟输入格式为:(到达\ 离去, 汽车牌照号码,到达\离去的时刻), 例如: (‘A’,1,5) 表示1号车在5时刻到达;(‘D’, 5,20) 表示5号车在20时刻离开;结束标志为: (‘E’,0,0)。

说明:以上题目除了要求的基本功能以外,可以根据实际调研的需求自由发挥,增加可行功能,使系统的功能应用更加完善。


#include "stdio.h"
#include "malloc.h"
#define MAX 3     //停车站所能容纳的最大车数量
#define SIZE_INIT_LANE 100
#define INCREASE  10    //没次增量
#define PRICE 10;   //单价
typedef int Elemtype;//汽车号变量类型
typedef struct
{
    Elemtype Car_license;        //汽车号码
    int Car_Inbound;             //入站时刻
    int Car_Outbound;           //出站时刻
    int  flag;                  //是否有车标志
    struct Stop_car* next;
}Stop_car;//停车场用静态链表
typedef struct
{
    Elemtype* Car_license;
    int num;
    int size;
}Lane;//便车道动态数组
//全局变量
int Stop_car_Num;//停车站车数量
//函数声明
int Init_Stop_car(Stop_car* s);
int Init_Lane(Lane* L);
int push_car(Elemtype carNum, int time, Stop_car* s, Lane* L);
int pull_car(Elemtype carNum, int time, Stop_car* s, Lane* L);
Stop_car* find_INSTOP(Stop_car* s, Elemtype carNum);
int find_Lane(Lane* L, Elemtype carNum);
void meau(Stop_car* s, Lane* L);···
int main()
{
    Stop_car* s;
  Lane* L;
    s = (Stop_car*)malloc(sizeof(Stop_car));
    L = (Lane*)malloc(sizeof(Lane));
    Init_Stop_car(s);
    Init_Lane(L);
    meau(s, L);
    return 0;
}
/*---停车场初始化---*/
int Init_Stop_car(Stop_car* s)
{
    if (s == NULL)
    {
        return 0;
    }
    // s->Car_license = "";        //汽车号码置空
   //   s->Car_Inbound = 0;
   //  s->Car_Outbound = 0;
    s->next = NULL;            //头插法式初始化
 //   s->flag = 0;
    Stop_car_Num = 0;       //停车场初始车数量为零
    return 1;
}
/*---便车道初始化---*/
int Init_Lane(Lane* L)
{
    L->Car_license = (Elemtype*)malloc(SIZE_INIT_LANE * sizeof(Elemtype));
    if (L->Car_license == NULL)
        return 0;
    L->num = 0;
    L->size = SIZE_INIT_LANE;
    return 1;
}
/*---车入站(停车站/便车站)---*/
int push_car(Elemtype carNum, int time, Stop_car* s, Lane* L)
{
    //当停车场还能容纳车
    if (Stop_car_Num < MAX)
    {
        Stop_car* node = (Stop_car*)malloc(sizeof(Stop_car));
        if (node == NULL)
        {
            return 0;
        }
        node->Car_license = carNum;
        node->Car_Inbound = time;        //到达时刻
        node->flag = 1;
        node->next = s->next;
        s->next = node;
        Stop_car_Num++;
        return 1;
    }
    else
    {
        if (L->num < SIZE_INIT_LANE)
            L->Car_license[L->num++] = carNum;
        else
        {
            L->Car_license[L->num++] = carNum;
            L->Car_license = (char*)realloc(L->Car_license, (L->size + INCREASE) * sizeof(char));
            if (L->Car_license == NULL)
                exit(0);
            L->size += INCREASE;
        }
        return 1;
    }
}
/*---车出站(停车站/便车道)---*/
int pull_car(Elemtype carNum, int time, Stop_car* s, Lane* L)
{
    float Price;                                            //这里(计算费用)可以另写一个函数 ,有点让出站函数功能不单一了
    Stop_car* ss = find_INSTOP(s, carNum);
    if (ss != NULL)
    {
        Stop_car* p = ss->next;
        p->Car_Outbound = time;
        Price = (p->Car_Outbound - p->Car_Inbound) * PRICE;
        ss->next = p->next;
        free(p);
        printf("\n出站成功,本次费用为%.3f", Price);
    Stop_car_Num--;
        if(L->num>=1)
        {
            push_car(L->Car_license[0],time,s,L);
            L->Car_license++;
            L->num--;
        }
        else
        {
            return 1;
        }
    }
    else if (ss == NULL)
    {
    int i;
        int f = find_Lane(L, carNum);
        if (f >= 0)
        {
            for (i = f; i < L->num; i++)
            {
                L->Car_license[i] = L->Car_license[i + 1];
            }
            L->num--;
            return 1;
        }
        else
        {
            printf("暂无此车");
            return 0;
        }
    }
    else
    {
        printf("暂无此车");
        return 0;
    }
}
/*---判断某辆车是否在停车场---*/
Stop_car* find_INSTOP(Stop_car* s, Elemtype carNum)
{
    Stop_car* ss = s;
    Stop_car* p = ss->next;
    while (p != NULL)
    {
        if (p->Car_license == carNum)
            return ss;
        ss = p;
        p = p->next;
    }
    return NULL;
}
/*---判断车是否在便车道---*/
int find_Lane(Lane* L, Elemtype carNum)
{
  int i;
    Lane* LL = L;
    for (i = 0; i < LL->num; i++)
    {
        if (LL->Car_license[i] == carNum)
            return i;
    }
    return -1;
}
/*---车站管理菜单---*/
void meau(Stop_car* s, Lane* L)
{
    int flag;
    char ch,ch1;
    Elemtype carNum;
    int time;
    printf("---------------------停车站模拟---------------------\n");
    printf("输入格式(到达/离去,汽车牌照号码,达到/离去的时刻)\n");
    printf("请输入(A:代表进站 D:代表出站 P:代表结束(结束后显示状态))\n");
    flag = 1;
    while (flag)
    {
        printf("输入格式(到达/离去,汽车牌照号码,达到/离去的时刻)\n");
        scanf("%c", &ch);
        ch1 = getchar();
        switch (ch)
        {
        case 'A': {
            scanf("%c,%d",&carNum,&time);
            ch1 = getchar();
                     if(find_INSTOP(s,carNum)||find_Lane(L,carNum)>=0)
                    {
                        printf("该车已近入站\n");
                    }
                    else
                    {
                        push_car(carNum,time,s,L);
                        printf("入站成功\n");
                    }
        }; break;
        case 'D': {
            scanf("%c,%d",&carNum,&time);
            ch1 = getchar();
            pull_car(carNum, time, s, L);
            printf("出站成功\n");
        }; break;
        case 'P': {
            printf("停车站还剩%d个位置,变车道还有%d个人排队中\n",MAX-Stop_car_Num,L->num);
      ch1=getchar();
        }; break;
        }
    }
}

朋友完整版

更适合题目所需,思路一样。

点击查看代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define n 3//可以停车的总数;
#define m 3//便道的最大停车数
int Stop_car_Num;
typedef int Elemtype;
typedef struct {
  char zt;//进入或者出去
  int cp;//车牌
  int rushtime;//进入时间
  int leavetime;//离开时间
  struct Car *next;
}Car,*car;
typedef struct
{
    int Car_license;        //汽车号码
    int Car_Inbound;        //入站时刻
    int Car_Outbound;       //出站时刻
    int  flag;              //是满车标志
    struct Stop_car* next;
}Stop_car;
typedef struct{
  Elemtype * car_lincese;
  int sum;
  int length;//便道的长度
}load;//便道
void Initlist(load *x)//链表初始化
{
  x->car_lincese=(Elemtype *)malloc(m *sizeof(Elemtype));
  if(x->car_lincese==NULL)
    exit(0);
  x->sum=0;
  x->length=m;
}
int Init_Stop_car(Stop_car* s)//停车场初始化
{
    if (s == NULL)
    {
        return 0;
    }
    s->Car_license = 0;       //汽车号码置空
    s->Car_Inbound = 0;
    s->Car_Outbound = 0;
    s->next = NULL;
    s->flag = 0;
    Stop_car_Num = 0;       //停车场初始车数量为零
    return 1;
}
void push(int carnum,int time,Stop_car *s,load *x)
{
  if(Stop_car_Num< n)
  {
    Stop_car* node = (Stop_car*)malloc(sizeof(Stop_car));
    if(node==NULL)
      exit(0);
    node->Car_license=carnum;
    node->Car_Inbound=time;
    node->next=s->next;
    s->next=node;
    Stop_car_Num++;
    printf("车牌为%d的车辆在%d时刻已经停入停车场",carnum,time);
    printf("\n");
  }
  else
  {
    if(x->sum<x->length)
    {
      printf("停车场已满,进入便道\n");
      x->sum++;
      x->car_lincese[x->sum]=carnum;
      printf("当前便道已经有%d辆车等待\n",x->sum);
    }
    else
    {
      printf("当前停车场与便道车满,禁止停车\n");
    }
  }
}
Stop_car * find_insort(Stop_car * s,int carnum)
{
  Stop_car*ss=s;
  Stop_car * p = ss->next;
  while(p!=NULL)
  {
    if(p->Car_license == carnum)
      return ss;
    ss=p;
    p = p->next;
  }
  return NULL;
}
int find_load(load *x,int carnum)
{
  int i;
  load *xx=x;
  for(i=0;i<=xx->sum;i++)
  {
    if(xx->car_lincese[i] == carnum)
      return i;
  }
  return -1;
}
void leave_car(int carnum,int time,Stop_car *s,load *x)
{
  int price;
  Stop_car * ss = find_insort(s,carnum);
  if(ss!=NULL)
  {
    Stop_car *p=ss->next;
    p->Car_Outbound = time;
    price=(p->Car_Outbound-p->Car_Inbound) * 3;
    ss->next=p->next;
    free(p);
    printf("出站成功,本次费用%d元,请及时缴纳\n",price);
    Stop_car_Num--;
    if(x->sum>=1)
    {
      push(x->car_lincese[1],time,s,x);
      x->car_lincese++;
      x->sum--;
    }
    else
    {
      printf("便道中没有车进入停车场\n");
    }
  }
  else
  {
    int i;
    int f;
    f=find_load(x,carnum);
    if(f>=1)
    {
      for(i=f;i<x->sum;i++)
      {
        x->car_lincese[i]=x->car_lincese[i+1];
      }
      x->sum--;
      printf("该车位于便道中,离开不需要交付费用\n");
    }
    else
    {
      printf("查无此车\n");
    }
  }
}
int main()
{
  int i,num,time;
  char y;
  Stop_car *a;
  load *b;
  a = (Stop_car*)malloc(sizeof(Stop_car));
  Init_Stop_car(a);
  b = (load*)malloc(sizeof(load));
  Initlist(b);
  printf("*************************************\n");
  printf("           A.车辆到达                \n");
  printf("           D.车辆离开                \n");
  printf("           K.车辆情况                \n");
  printf("           E.退出                    \n");
  printf("当前停车场里有3辆空位,便道中有0辆车 \n");
  for(i=0;;i++)
  {
    scanf("%c",&y);
    if(y=='A')
    {
      printf("请输入车牌号和进入时间\n");
      scanf("%d %d",&num,&time);
      if(find_insort(a,num)!=NULL)
      {
        printf("该车已经进入停车场\n");
      }
      else if(find_load(b,num)>=0)
      {
        printf("该车已经进入便道\n");
      }
      else
        push(num,time,a,b);
    }
    if(y=='D')
    {
      printf("请输入车牌号和离开时间\n");
      scanf("%d %d",&num,&time);
      leave_car(num,time,a,b);
    }
    if(y=='K')
    {
      printf("停车站有%d辆车,便道中有%d辆车在排队\n",Stop_car_Num,b->sum);
    }
    if(y=='E')
    {
      break;
    }
  }
}

## 思路: 初次看题,便车道想用队列做,但是难于删除便车道的某一个车,故用了一个动态顺序表; 其他的看代码吧,对了,那个车站随意选位置 我没给位置赋值(赋值很容易实现,但是随意选位置不太好实现,姑且不给停车站赋值吧) 这个道题,算是对简单的顺序表和链表做了个综合练习; ## 下面贴一些出现的错误和解释

b49bb803d047cd7b7daa393f14c59ecd_aa9e3b37106655887b22bc0d43da2227.png

0125ee4d3bc477cc5808c6412ac518b8_d8c2ab80a6368fe80531740658cb3f65.png

c0fd20425b3a55f3e468de766655cebb_decf26d51093721991cf7a7132e41420.png

906e00af75d068df9398847abcb850ab_02423e7679abb15993029361b17bc719.png

52b988f9bc0576cb10b94c07fbb41f91_b5351f45a405a7429e2cc31ecc516374.png


相关文章
|
移动开发 弹性计算 缓存
阿里云服务器上如何部署 H5 游戏?
在自学游戏开发的路上,最有成就感的时刻就是将自己的小游戏做出来分享给朋友试玩,原生的游戏开可以打包分享,小游戏上线流程又长,那 H5 小游戏该怎么分享呢?本文就带大家通过 nginx 将构建好的 H5 游戏托管的阿里云上。
阿里云服务器上如何部署 H5 游戏?
|
5月前
|
缓存 运维 前端开发
|
11月前
|
算法 安全 量子技术
量子计算与金融风险管理:提升市场预测能力
【10月更文挑战第8天】量子计算作为一种前沿技术,正在逐步改变金融风险管理的格局。通过利用其独特的计算能力和优化算法,量子计算可以显著提高市场预测的准确性和及时性,为金融机构提供更精准的风险管理工具。尽管目前仍面临一些挑战和限制,但随着技术的不断进步和完善,相信量子计算将在未来的金融风险管理领域发挥更加重要的作用。
|
存储 人工智能 自然语言处理
阿里云大降价后,再谈“降本增效”
2024年2月29日,阿里云宣布史上最大力度降价,引发行业对用云成本的热议。
2847 4
阿里云大降价后,再谈“降本增效”
|
域名解析 缓存 网络协议
阿里云DNS常见问题之阿里云DNS的操作日志查不到如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
敏捷开发 程序员 定位技术
敏捷开发
敏捷开发
187 0
|
数据库 UED 索引
构建高效的数据库索引:提升查询性能的关键技巧
本文将深入探讨数据库索引的设计和优化,介绍如何构建高效的数据库索引以提升查询性能。通过学习本文,读者将掌握数据库索引的原理、常见类型以及优化策略,从而在实际应用中提升数据库查询效率。
|
程序员
程序员必知:原码、反码、补码和移码详解
程序员必知:原码、反码、补码和移码详解
667 0
|
JSON API 定位技术
批量解析地址的API
批量解析地址的API
639 1
|
安全 Linux 网络安全
【分享】非常全面的CentOS7系统安全检测和加固脚本
【分享】非常全面的CentOS7系统安全检测和加固脚本
1612 0
【分享】非常全面的CentOS7系统安全检测和加固脚本