/*经过两个深夜的战斗,终于把老师要求的作业做完了,其实没办法,后天还让交上,
本想在网上下一个算了,不过还是自己些吧,写完方知其中乐趣。*/
1 //都无头节点
2 #include<stdio.h>
3 #include<malloc.h>
4 #include<string.h>
5 #include<stdlib.h>
6 #define MAX_PARK 2
7 typedef struct{
8 char license_plate[10]; /*汽车牌照号码*/
9 char state; /*汽车当前状态,p表示在停车位上,q表示在便道上*/
10 float time; /*汽车进入的时刻*/
11 }Car;
12 //停车场栈或辅助栈
13 typedef struct{
14 Car Pack[MAX_PARK]; /*各汽车信息的存储空间*/
15 int top; /*用来指示栈顶位置的指针*/
16 }ParkStack,*Parklink;
17 //便道(为队列)
18 typedef struct qnode{
19 Car data; /*各汽车信息的存储空间*/
20 struct qnode *next;
21 }Qnode;
22 typedef struct{
23 Qnode *front, *rear; /*用来指示队头和队尾位置的指针*/
24 }RoadQueue;
25 //辅助栈
26 /*typedef struct{
27 Car BUFFER[MAX_PARK]; //各汽车信息的存储空间
28 int top; //用来指示栈顶位置的静态指针
29 }BUFFER;*/
30 void out_zhan(Parklink a){
31 int i=0;
32 while(i<=a->top){
33 printf("(%s,%c,%.2f)",a->Pack[i].license_plate,a->Pack[i].state,a->Pack[i].time);
34 i++;
35 }
36 }//out_zhan结束
37 void out_biandao(RoadQueue *frb){
38 Qnode *p=frb->front;
39 while(p){
40 Car t=p->data;
41 printf("(%s,%c,%.2f),",t.license_plate,t.state,t.time);
42 p=p->next;
43 }
44 }
45 void depart(Parklink ht,Parklink hf,char a[],float t2,RoadQueue *frb){
46 int i=0;
47 while(strcmp(ht->Pack[i].license_plate,a)!=0){
48 i++;
49 }
50 while(ht->top>i){
51 hf->top++;
52 strcpy(hf->Pack[hf->top].license_plate,ht->Pack[ht->top].license_plate);
53 hf->Pack[hf->top].state=ht->Pack[ht->top].state;
54 hf->Pack[hf->top].time=ht->Pack[ht->top].time;
55 ht->top--;
56 printf("%s车已移到辅助栈 ",hf->Pack[hf->top].license_plate);
57 }
58 printf("\n");
59 ht->top--;
60 printf("%s已移出停车场,",ht->Pack[i].license_plate);
61 printf("应收费:");
62 printf("%.2f元\n",(t2-ht->Pack[i].time)*5);
63 while(hf->top!=-1){//再移回停车场
64 strcpy(ht->Pack[++ht->top].license_plate,hf->Pack[hf->top].license_plate);
65 ht->Pack[ht->top].state=hf->Pack[ht->top].state;
66 ht->Pack[ht->top].time=hf->Pack[hf->top].time;
67 printf("%s又移回停车场",hf->Pack[hf->top].license_plate);
68 hf->top--;
69 }
70 printf("\n");
71 while(ht->top<MAX_PARK-1&&frb->front!=NULL&&frb->rear!=NULL){//便道上的车停到车场
72 float t;
73 strcpy(ht->Pack[++ht->top].license_plate,frb->front->data.license_plate);
74 frb->front=frb->front->next;
75 printf("%s要进站",ht->Pack[ht->top].license_plate);
76 printf("请输入现在时间以对该车进行收费:");
77 scanf("%f",&t);
78 ht->Pack[ht->top].time=t;
79 }
80 }//end depart
81 void in_che(Parklink ht,Parklink hf,RoadQueue *frb){
82 char a[10],b;
83 float c;
84 Qnode*sb;
85 while(1){
86 int i=0,j=0;
87 printf("请输入车辆的信息:\n");
88 scanf("%s %s %f",a,&b,&c);
89 if(*a=='0'&&b=='0'&&c==0)
90 break;
91 if(b=='a'){
92 while(i<=ht->top){
93 if(strcmp(ht->Pack[i].license_plate,a)==0){
94 printf("以存在\n");
95 i=10;
96 }
97 else
98 i++;
99 }
100 if(i==10)
101 continue;
102 else{
103 if(++ht->top<MAX_PARK){//停车场未满
104 strcpy(ht->Pack[ht->top].license_plate,a);
105 ht->Pack[ht->top].state=b;
106 ht->Pack[ht->top].time=c;
107 }
108 else{
109 sb=(Qnode*)malloc(sizeof(Qnode));
110 if(frb->rear==NULL){
111 frb->front=sb;
112 frb->rear=sb;
113 }
114 else {
115 frb->rear->next=sb;
116 frb->rear=sb;
117 }
118 strcpy(sb->data.license_plate,a);
119 sb->data.state=b;
120 sb->data.time=c;
121 sb->next=NULL;
122 }
123 if(ht->top==MAX_PARK)
124 ht->top=MAX_PARK-1;
125 }
126 } //end if(b=='a')
127 else if(b=='d'){//车要离开
128 while(j<=ht->top){//判断是否有该车
129 if(strcmp(ht->Pack[j].license_plate,a)==0){
130 j=10;
131 break;
132 }
133 else
134 j++;
135 }
136 if(j==10)
137 depart(ht,hf,a,c,frb);
138 else{
139 printf("车场中无该车");
140 }
141 }
142 else
143 printf("输入错误,");
144 }//end while
145 }//end in_che
146 void out_che(Parklink ht,Parklink hf,RoadQueue *frb){
147 printf("停车场里的车:");
148 if(ht->top>-1){
149 out_zhan(ht);printf("\n");
150 }
151 else
152 printf("无\n");
153 printf("便道上的车有:");
154 if(frb->front==frb->rear&&frb->rear==NULL)
155 printf("无\n");
156 else{
157 out_biandao(frb);printf("\n");
158 }
159 }
160 void main(){
161 int fs=0,bd=0,caoz;
162 ParkStack *ht,*hf; //hf代表辅助栈头指针
163 RoadQueue *frb; //frb便道头尾
164 printf("停车场模拟\n");
165 printf("车辆的信息以空格隔开如(f004 a 6.5)a代表到达,d代表离开,输入(0 0 0)结束\n收费每分钟5元\n");
166 ht=(ParkStack*)malloc(sizeof(ParkStack));//开辟停车场栈
167 ht->top=-1; //top该插的位置
168 hf=(ParkStack*)malloc(sizeof(ParkStack));//开辟辅助栈
169 hf->top=-1;
170 frb=(RoadQueue*)malloc(sizeof(RoadQueue));//开辟便道栈
171 frb->rear=NULL;frb->front=NULL;
172 start:
173 printf("你想进行的操作(1输入车信息,2查看车信息,3清屏)\n");
174 scanf("%d",&caoz);
175 if(caoz==1){
176 in_che(ht,hf,frb);
177 }
178 else if(caoz==2){
179 out_che(ht,hf,frb);
180 }
181 else if(caoz==3)
182 system("CLS");
183 printf("//------------------------------------------------//\n");
184 goto start;
185 }//main结束
186
2 #include<stdio.h>
3 #include<malloc.h>
4 #include<string.h>
5 #include<stdlib.h>
6 #define MAX_PARK 2
7 typedef struct{
8 char license_plate[10]; /*汽车牌照号码*/
9 char state; /*汽车当前状态,p表示在停车位上,q表示在便道上*/
10 float time; /*汽车进入的时刻*/
11 }Car;
12 //停车场栈或辅助栈
13 typedef struct{
14 Car Pack[MAX_PARK]; /*各汽车信息的存储空间*/
15 int top; /*用来指示栈顶位置的指针*/
16 }ParkStack,*Parklink;
17 //便道(为队列)
18 typedef struct qnode{
19 Car data; /*各汽车信息的存储空间*/
20 struct qnode *next;
21 }Qnode;
22 typedef struct{
23 Qnode *front, *rear; /*用来指示队头和队尾位置的指针*/
24 }RoadQueue;
25 //辅助栈
26 /*typedef struct{
27 Car BUFFER[MAX_PARK]; //各汽车信息的存储空间
28 int top; //用来指示栈顶位置的静态指针
29 }BUFFER;*/
30 void out_zhan(Parklink a){
31 int i=0;
32 while(i<=a->top){
33 printf("(%s,%c,%.2f)",a->Pack[i].license_plate,a->Pack[i].state,a->Pack[i].time);
34 i++;
35 }
36 }//out_zhan结束
37 void out_biandao(RoadQueue *frb){
38 Qnode *p=frb->front;
39 while(p){
40 Car t=p->data;
41 printf("(%s,%c,%.2f),",t.license_plate,t.state,t.time);
42 p=p->next;
43 }
44 }
45 void depart(Parklink ht,Parklink hf,char a[],float t2,RoadQueue *frb){
46 int i=0;
47 while(strcmp(ht->Pack[i].license_plate,a)!=0){
48 i++;
49 }
50 while(ht->top>i){
51 hf->top++;
52 strcpy(hf->Pack[hf->top].license_plate,ht->Pack[ht->top].license_plate);
53 hf->Pack[hf->top].state=ht->Pack[ht->top].state;
54 hf->Pack[hf->top].time=ht->Pack[ht->top].time;
55 ht->top--;
56 printf("%s车已移到辅助栈 ",hf->Pack[hf->top].license_plate);
57 }
58 printf("\n");
59 ht->top--;
60 printf("%s已移出停车场,",ht->Pack[i].license_plate);
61 printf("应收费:");
62 printf("%.2f元\n",(t2-ht->Pack[i].time)*5);
63 while(hf->top!=-1){//再移回停车场
64 strcpy(ht->Pack[++ht->top].license_plate,hf->Pack[hf->top].license_plate);
65 ht->Pack[ht->top].state=hf->Pack[ht->top].state;
66 ht->Pack[ht->top].time=hf->Pack[hf->top].time;
67 printf("%s又移回停车场",hf->Pack[hf->top].license_plate);
68 hf->top--;
69 }
70 printf("\n");
71 while(ht->top<MAX_PARK-1&&frb->front!=NULL&&frb->rear!=NULL){//便道上的车停到车场
72 float t;
73 strcpy(ht->Pack[++ht->top].license_plate,frb->front->data.license_plate);
74 frb->front=frb->front->next;
75 printf("%s要进站",ht->Pack[ht->top].license_plate);
76 printf("请输入现在时间以对该车进行收费:");
77 scanf("%f",&t);
78 ht->Pack[ht->top].time=t;
79 }
80 }//end depart
81 void in_che(Parklink ht,Parklink hf,RoadQueue *frb){
82 char a[10],b;
83 float c;
84 Qnode*sb;
85 while(1){
86 int i=0,j=0;
87 printf("请输入车辆的信息:\n");
88 scanf("%s %s %f",a,&b,&c);
89 if(*a=='0'&&b=='0'&&c==0)
90 break;
91 if(b=='a'){
92 while(i<=ht->top){
93 if(strcmp(ht->Pack[i].license_plate,a)==0){
94 printf("以存在\n");
95 i=10;
96 }
97 else
98 i++;
99 }
100 if(i==10)
101 continue;
102 else{
103 if(++ht->top<MAX_PARK){//停车场未满
104 strcpy(ht->Pack[ht->top].license_plate,a);
105 ht->Pack[ht->top].state=b;
106 ht->Pack[ht->top].time=c;
107 }
108 else{
109 sb=(Qnode*)malloc(sizeof(Qnode));
110 if(frb->rear==NULL){
111 frb->front=sb;
112 frb->rear=sb;
113 }
114 else {
115 frb->rear->next=sb;
116 frb->rear=sb;
117 }
118 strcpy(sb->data.license_plate,a);
119 sb->data.state=b;
120 sb->data.time=c;
121 sb->next=NULL;
122 }
123 if(ht->top==MAX_PARK)
124 ht->top=MAX_PARK-1;
125 }
126 } //end if(b=='a')
127 else if(b=='d'){//车要离开
128 while(j<=ht->top){//判断是否有该车
129 if(strcmp(ht->Pack[j].license_plate,a)==0){
130 j=10;
131 break;
132 }
133 else
134 j++;
135 }
136 if(j==10)
137 depart(ht,hf,a,c,frb);
138 else{
139 printf("车场中无该车");
140 }
141 }
142 else
143 printf("输入错误,");
144 }//end while
145 }//end in_che
146 void out_che(Parklink ht,Parklink hf,RoadQueue *frb){
147 printf("停车场里的车:");
148 if(ht->top>-1){
149 out_zhan(ht);printf("\n");
150 }
151 else
152 printf("无\n");
153 printf("便道上的车有:");
154 if(frb->front==frb->rear&&frb->rear==NULL)
155 printf("无\n");
156 else{
157 out_biandao(frb);printf("\n");
158 }
159 }
160 void main(){
161 int fs=0,bd=0,caoz;
162 ParkStack *ht,*hf; //hf代表辅助栈头指针
163 RoadQueue *frb; //frb便道头尾
164 printf("停车场模拟\n");
165 printf("车辆的信息以空格隔开如(f004 a 6.5)a代表到达,d代表离开,输入(0 0 0)结束\n收费每分钟5元\n");
166 ht=(ParkStack*)malloc(sizeof(ParkStack));//开辟停车场栈
167 ht->top=-1; //top该插的位置
168 hf=(ParkStack*)malloc(sizeof(ParkStack));//开辟辅助栈
169 hf->top=-1;
170 frb=(RoadQueue*)malloc(sizeof(RoadQueue));//开辟便道栈
171 frb->rear=NULL;frb->front=NULL;
172 start:
173 printf("你想进行的操作(1输入车信息,2查看车信息,3清屏)\n");
174 scanf("%d",&caoz);
175 if(caoz==1){
176 in_che(ht,hf,frb);
177 }
178 else if(caoz==2){
179 out_che(ht,hf,frb);
180 }
181 else if(caoz==3)
182 system("CLS");
183 printf("//------------------------------------------------//\n");
184 goto start;
185 }//main结束
186