|

楼主 |
发表于 2011-6-28 16:18:54
|
显示全部楼层
仰望天上的光 发表于 2011-6-27 09:25
按楼上的做法,LZ要改很多代码,LZ也可以试试我的做法。
我这个写了个停车场管理系统的程序,想把程序中输入的char car_type; //汽车类型 int number; //汽车牌号 int ar_time; //汽车到达时间还有int time_d;//汽车离开时间 int money;//汽车停车费写到文件里,但实在改不过来了,请您帮个忙,改下我的程序,把我刚刚说的能写到文件里,谢谢了
- // 停车场管理.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<stdio.h>//定义输入、输出函数
- #include<iostream>//数据流输入、输出
- #include<string.h>//字符串处理
- #include<math.h>//定义数学函数
- #include<stdlib.h>//定义杂项函数
- using namespace std;
- #define size 3 //停车场位置数
- typedef struct car_information {
- char car_type; //汽车类型
- int number; //汽车车号
- int ar_time; //汽车到达时间
- }Scar_information;
- typedef struct{
- Scar_information *base; //停车场的栈底
- Scar_information *top; //停车场的栈顶
- int stacksize;
- // int car_type;
- }stackhead;
- void initstack(stackhead &L) //构造一个空栈
- {
- L.base=(Scar_information*)malloc(size*sizeof(Scar_information));
- if(!L.base)exit(0); //存储分配失败
- L.top=L.base;
- L.stacksize=size;
- }
- void push(stackhead &L,Scar_information e) //把元素e压入L栈成为新的栈顶元素
- {
- *L.top=e;
- L.top++;
- // L.stacksize++;
- }
- void pop(stackhead &L,Scar_information &e) //把元素e弹出L栈
- {
- if(L.top==L.base)
- {
- cout<<"停车场没有车辆!!"<<endl;
- return;
- }
- L.top-- ;
- e=*L.top;
- // L.stacksize--;
- }
- //便道队列的性质
- typedef struct vehicle_information{
- char car_type; //汽车类型
- int number; //汽车车牌号
- int ar_time; //汽车到达时间
- struct vehicle_information *next;
- }*Qvehicle_information;
- typedef struct{
- Qvehicle_information front; //便道队列的队头
- Qvehicle_information rear; //便道队列的队尾
- int length;
- }linkqueue;
- //队列的基本操作
- void initqueue(linkqueue &q)//构造一个空队列
- {
- q.front=q.rear=(Qvehicle_information)malloc(sizeof(vehicle_information));
- if(!q.front||!q.rear)
- exit(0);
- q.front->next=NULL;
- q.length=0;
- }
- void enqueue(linkqueue &q,int number,int ar_time,int car_type)
- {
- Qvehicle_information p;
- p=(Qvehicle_information)malloc(sizeof(vehicle_information));
- if(!p)exit(0);
- p->car_type;
- p->number=number;
- p->ar_time=ar_time;
- p->next=NULL;
- q.rear->next=p;
- q.rear=p;
- q.length++;
- }
- void popqueue(linkqueue &q,Qvehicle_information &w)
- //删除队列中的车辆
- {
- Qvehicle_information p;
- if(q.front==q.rear)
- {
- cout<<"停车场的便道没有车辆!!"<<endl;
- return;
- }
- p=q.front->next;
- w=p;
- q.front->next=p->next;
- q.length--;
- if(q.rear==p)q.front=q.rear;
- }
- Scar_information enter_park(stackhead &st,linkqueue &q) //对进入停车场的汽车的处理
- {
- int number, time_a;
- char car_type;
- Scar_information e;
- cout<<"汽车型号:";
- cin>>car_type;
- cout<<"车牌号为:";
- cin>>number;
- cout<<"进入停车场的时刻:";
- cin>>time_a;
- if(st.top-st.base<st.stacksize){
- e.car_type=car_type;
- e.number=number;
- e.ar_time=time_a;
- push(st,e);
- cout<<"该车已经进入停车场在:"<<st.top-st.base<<"号车道"<<endl<<endl;
- }
- else{
- enqueue(q,number,time_a,car_type);//插入元素time_a为q的新的队尾元素
- cout<<"停车场已满,该车先停在便道的第"<<q.length<<"个位置上"<<endl;
- }
- return e;
- }
- void leave_park(stackhead &st,stackhead &sl,linkqueue &q, Scar_information e[])//对离开停车场的汽车的处理
- {//st堆栈为停车场,sl堆栈为临时的停车场
- int number,time_d,flag=1,money,arrivaltime,unit_price;char car_type;//q为便道队列
- int i, t;
- Scar_information q_to_s;
- cout<<"汽车车牌为:";
- cin>>number;
- for(i=0; i<3; ++i)
- if(e[i].number==number)
- {
- cout<<"该车类型为:"<<e[i].car_type<<"请根据汽车类型输入停车费单价:";
- t=i;
- }
- cout<<"停车费单价:";
- cin>>unit_price;
- cout<<"离开停车场时刻:";
- cin>>time_d;
- Qvehicle_information w;
-
- while(flag){//找到要开出的车,并且弹出停车场栈
- pop(st,e[t]);
- push(sl,e[t]);
- if(e[t].number==number)
- {
- flag=0;
- money=(time_d-e[t].ar_time)*unit_price;
- arrivaltime=e[t].ar_time;
- }
- }
- pop(sl,e[i]);//把临时停车场的第一辆车(要离开的)去掉//这里的错误原因在于i的值为3,超出e数组原来的范围
- while(sl.top!=sl.base)//把临时停车场的车倒回停车场
- {
- pop(sl,e[t]);
- push(st,e[t]);
- }
- if(sl.top-sl.base<st.stacksize&&q.length!=0)//停车场有空位置,便道上的车开进停车场
- {
- popqueue(q,w);
- q_to_s.car_type=car_type;
- q_to_s.ar_time=time_d;
- q_to_s.number=w->number;
- push(st,q_to_s);
- cout<<"车牌号为"<<q_to_s.number<<"的车已经从便道进入停车场,所在停车位为:"<<st.top-st.base<<endl<<endl;
- }
- cout<<" 收 据"<<endl;
- cout<<" ========================= 车牌号:"<<number<<endl;
- cout<<"===================================================="<<endl;
- cout<<"|进停车场时刻|出停车场时刻|停留时间|应付(元)|"<<endl;
- cout<<"===================================================="<<endl;
- cout<<"| "<<arrivaltime<<" | "<<time_d<<" | "<<time_d-arrivaltime<<" | "<<money<<" |"<<endl;
- cout<<"--------------------------------------"<<endl<<endl;
- }
- int main()
- {
- int m=100;
- int i=0;
- char flag;//进入或者离开停车场的标识
- char order;
- //char Y;
- stackhead sting,slinshi; //停车场和临时停车场栈的定义
- Scar_information e[200]; //添加一个数组存放三辆车信息,尽可能讲数组的大小扩大,因为其存放的是车辆信息,程序可以重复100次,所以数组值相应扩大
- linkqueue line; //队列的定义
- initstack(sting); //构造停车场栈sting
- initstack(slinshi); //构造临时停车场栈slinshi
- initqueue(line); //构造便道队列line
- while(m)
- {
-
- cout<<" 欢迎使用"<<endl;
- cout<<"\n **停车场管理系统**"<<endl;
- cout<<"======================================================"<<endl;
- cout<<"** **"<<endl;
- cout<<"** A--汽车进入停车场 D--汽车离开停车场 **"<<endl;
- cout<<"** **"<<endl;
- cout<<"** E--退出 程序 **"<<endl;
- cout<<"** 附:汽车类型及停车费单价 **"<<endl;
- cout<<"** 货车T,10元--客车B,8元--小型车C,4元 **"<<endl;
- cout<<"======================================================"<<endl;
- cout<<"是否需要清屏?(Y/N)";
- cin>>order;
- if('Y'==order)
- {
- system ("cls");
- }
- cout<<"请选择:(A,D,E):";
- cin>>flag;
- switch(flag)
- {
- case'A':e[i]=enter_park(sting,line);i++;break; //汽车进入停车场
- case'D':leave_park(sting,slinshi,line,e);break; //汽车离开停车场
- case'E':exit(0);
- }
- m--;
-
- }
-
-
- return 0;
-
- }
-
复制代码
|
|