實驗二 線性表的應用(二)
【實驗類別】設計型實驗
【實驗目的】
1.熟悉線性表的順序存儲和鏈式存儲各自的特點及運算;
2.熟練掌握線性表的基本操作在不同存儲結構中實現算法;
3.通過本次實驗幫助學生加深對C語言的使用(特別是函數的參數調用、指針類型的應用和鏈表的建立等各種基本操作)
4.對一個實際的問題能夠進行合理的需求分析,選擇合適的存儲結構,設計完成符合實際需要的功能。
【實驗學時】4學時
【實驗組人數】1人。
【實驗設備環境】計算機,VC++6.0,C-Free等
【實驗內容】
1、停車場的管理(4學時)
【問題描述】設有一個可以停放n輛汽車的停車場,它有二個大門可以供車輛進出,其中一個進,一個出。車輛到達停車場后任意選擇空閑停車位停放,每個停車位按順序編號。如果停車場已放滿n輛車,則后來的車輛只能停在停車場大門外的便道上等待,一旦停車場里有車開走,則排在便道上的第一輛車就進入停車場。每輛車離開停車場時,都應根據其在停車場的逗留時間交費。如果停留在便道上的車未進停車場就要離去,允許其離去,不收停車費,並且仍然保持在便道上等待的車輛順序。編制一程序模擬停車場的管理。
[基本要求] 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;
}
}
}