wybwhf 发布留言 2008-9-6 16:04 真正的求助呀!!!有一个两层的停车场,每层有6个车位,当第一层车停满后才允许使用第二层,(停车场可用一个二维数组实现,每个数组元素存放一个车牌号)每辆车的信息包括车牌号,层号,车位号,停车时间共4项,其中停车时间按分钟计算 2,假设停车场初始状态为第一层已经停有4辆车,其车位号依次为1-4,停车时间依次为20,15,10,5。即先将这四辆车的信息存入文件“car.dat"中(数组的对应元素也要进行赋值) 3,停车操作:当一辆车进入停车场时,先输入其车牌号,再为它分配一个层号和一个车位号(分配前先查询车位的使用情况,如果第一层有空则必须停在第一层),停车时间设为5,最后将新停入的汽车的信息添加文件"car.dat"中,并将在此之前的所有车的停车时间加5 4,收费管理(取车):当有车离开时,输入其车牌号,先按其停车时间计算费用,每5分钟0.2元。(停车费用可设置一个变量进行保存),同时从文件"car.dat"中删除该车的信息,并将该车对应的车位设置为可使用状态(即二维数组对应元素清零)。按用户的选择来判断是否要输出停车收费的总计。 5,输出停车场中全部车辆的信息 6,退出系统。 wybwhf 发布留言 2008-9-6 16:05 以下是一部分 #include "Stdio.h" #include "Conio.h" struct carplace { /*这是一个停车位*/ int car_id; int floor; int position; int time; int n; }a[2][6]={234,1,1,25,1,356,1,2,15,1,478,1,3,10,1,899,1,4, 5,1};/*一个2层 每层有6位的停车场;初始化了4个位置*/ struct carplace *p=&a[0][0]; FILE *pf; if((pf=fopen("D:\\lianlian\\car","at+"))==0){ getch(); exit(1); } fwrite (p,sizeof(struct carplace),4,pf); /*把初始化的4个车位得数据写进文件*/ int addcar() /*停车*/ {int i=0; int j; printf("please input a car_id\n"); for(i=0;i<2;i++){ for(j=0;j<6;j++){ if ((a.n)!=1){ scanf("%d",a.car_id); a.n =1; a.floor = i+1 ; a.position = j+1 ; printf("the car is in floor%d,position%d\n",a.floor ,a.position); if((pf = fopen("D:\lianlian\car","at+"))==0){ printf("can't open file strike any key exit2!") ; getch(); //exit(1); } fscanf(pf,"%d%d%d%d%d",&a.car_id,&a.floor,&a.position, &a.n); return 0 ; } } } } main() { int k ; int a = addcar(); if (a==0) /*如果有车停进,把其余车俩的5存车时间加5*/ { printf("the car is placed in\n"); for (k=0;k<12;k++,p++) { if (p->n == 1 ) p->time +=5; fscanf(pf,"%d", p->time); } } rewind (pf); getch(); } 他说:if((pf=fopen("D:\\lianlian\\car","at+"))==0){这里有错误,怎么回事? wybwhf 发布留言 2008-9-6 16:06 还有,怎么把上面的和下面的合二为一,满足题目要求? #include #include #include #include #define OK 1 #define NULL 0 #define ERROR 0 #define OVERFLOW 0 #define STACK_INIT_SIZE 2 //停车场大小
typedef struct{ char AorD; //表示车到达或离去的信息 char num[10]; //表示车的车牌号码 int time; //表示车到达或离开的时间 }ElemType; typedef struct{ //定义栈的结构体 ElemType *base; //在栈构造之前和销毁之后,base的值为null ElemType *top; //栈顶指针 int stacksize; //当前已分配的存储空间 }SqStack; typedef struct QNode{ ElemType cardata; struct QNode *next; }QNode,*Queueptr; typedef struct{ //定义队的结构体 Queueptr front; //队头指针 Queueptr rear; //队尾指针 }LinkQueue;
unsigned InitStack(SqStack &s){//构造一个空栈 s.base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!s.base)exit(OVERFLOW); //存储分配失败 s.top=s.base; s.stacksize=STACK_INIT_SIZE; return OK; }//InitStack unsigned Push(SqStack &s,ElemType e){//插入元素e为新的栈顶元素 *s.top++=e; return OK; }//Push unsigned pop(SqStack &s,ElemType &e){//若栈不空,则删除S的栈顶元素,用e返回其值并返回OK;否则返回ERROR; if(s.top==s.base)return ERROR; e=*--s.top; return OK; }//pop void Delete(SqStack &s,ElemType e){//若栈不空,则删除S中元素为e的元素 ElemType m[20],n,g; int a,i=0; while(s.base!=s.top){ g=*--s.top; s.top++; if(strcmp(g.num,e.num)==0) pop(s,n); else pop(s,m); } for(a=0;a Push(s,m[a]); } unsigned FullStack(SqStack s){//判断栈是否满 if(s.top-s.base>=s.stacksize) return OK; else return ERROR; }//FullStack unsigned InitQueue(LinkQueue &Q){//构造一个空队列 Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode)); if(!Q.front)exit(OVERFLOW); Q.front->next=NULL; return OK; }//InitQueue unsigned EnQueue(LinkQueue &Q,ElemType e){//插入元素e为Q的新的队尾元素 Queueptr p; p=new QNode; if(!p)exit(OVERFLOW); p->cardata=e; p->next=NULL; Q.rear->next=p; Q.rear=p; return OK; }//EnQueue unsigned EmptyQueue(LinkQueue Q){//判断队列是否为空 if(Q.front==Q.rear) return OK; else return ERROR; }//EmptyQueue unsigned DeQueue(LinkQueue &Q,ElemType &e){//若队列不空,则删除Q的对头元素,并用e返回其值 Queueptr p; if(!EmptyQueue(Q)) { p=Q.front->next; e=p->cardata; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); return OK; } else return ERROR; }//DeQueue unsigned SeekStack(SqStack s,ElemType e,ElemType &m){//查找栈中是否和e有相同号码的这个元素,若有则返回OK,否则返回ERROR,并用m返回这个元素 while(s.top!=s.base){ m=*(--s.top); if(strcmp(m.num,e.num)==0) return OK; } return ERROR; }//SeekStack void StackTrverse(SqStack s){//输出栈中的元素 ElemType m; while(s.top!=s.base){ m=*(--s.top); printf(" %s %d\n",m.num,m.time); } } unsigned SeekQueue(LinkQueue &Q,ElemType e,ElemType &n){//查找队中是否有和e相同号码的这个元素,若有则返回OK,否则返回ERROR,并用n返回这个元素 Queueptr p,q; p=Q.front; while(p->next){ if(strcmp(p->next->cardata.num,e.num)==0){ n=p->next->cardata; q=p->next; p->next=q->next; free(q); return OK; } else p=p->next; } return ERROR; }//SeekQueue void QueueTrverse(LinkQueue Q){//输出队列中的元素 Queueptr p; p=Q.front; while(p->next){ printf(" %s %d\n",p->next->cardata.num,p->next->cardata.time); p=p->next; } } void main(){//主函数 int i=1,m=1,n=1,totaltime; ElemType e,f,g,k; SqStack s; LinkQueue Q; InitStack(s); InitQueue(Q); printf("*******************停车场 管理系统******************\n"); printf("*****车停在停车场内0.5元每分钟,停在便道上免费******\n\n"); printf("(A:表示ARRIVAL D:表示DEPARTURE E:表示END)\n"); for(i=1;;i++) { printf("\n请输入车辆信息:A/D/E 车牌号码 到达/离开时间\n\n"); fflush(stdin); scanf("%c%s%d",&e.AorD,&e.num,&e.time); if(e.AorD=='A') { if(SeekStack(s,e,f))//查找栈中是否有和e相同号码的这个元素(车) { printf("你输入的车牌号码有误(与停车场内的车有相同的),请重新输入:\n"); i--; } else{ if(!FullStack(s)) { Push(s,e); printf("请将车停在停车场的第%d个位置\n\n",m++); printf("\n现在停车场内的车有:\n"); printf("车牌号码 停入停车场的时间\n\n"); StackTrverse(s); printf("\n现在便道上的车有:\n"); printf("车牌号码 停入便道上的时间\n\n"); QueueTrverse(Q); } else{ if(SeekQueue(Q,e,k))//查找队中是否有和e相同号码的这个元素(车) { printf("你输入的车牌号码有误(与便道上的车有相同的),请重新输入:\n"); i--; } else { EnQueue(Q,e); printf("停车场已满,请将车停在便道的第%d个位置\n",n++); printf("\n现在停车场内的车有:\n"); printf("车牌号码 停入停车场的时间\n\n"); StackTrverse(s); printf("\n现在便道上的车有:\n"); printf("车牌号码 停入便道上的时间\n\n"); QueueTrverse(Q); } } } } if(e.AorD=='D') { if(SeekStack(s,e,f))//查找栈中是否有和e相同号码的这个元素(车) { Delete(s,f); m--; totaltime=e.time-f.time; printf("你的车在停车场内停留了%d分钟\n", totaltime ); printf("你应付停车费%.2f元\n\n",totaltime*0.5);
if(!EmptyQueue(Q)){ DeQueue(Q,g); g.time=e.time; Push(s,g); m++; n--; printf("\n现在停车场内的车有:\n"); printf("车牌号码 停入停车场的时间\n\n"); StackTrverse(s); printf("\n现在便道上的车有:\n"); printf("车牌号码 停入便道上的时间\n\n"); QueueTrverse(Q); } } else { if(SeekQueue(Q,e,k)){//查找队中是否有和e相同号码的这个元素(车) totaltime=e.time-k.time; printf("你的车在便道上停留了%d分钟\n",totaltime); printf("你不用缴纳停车费\n\n"); n--; printf("\n现在停车场内的车有:\n"); printf("车牌号码 停入停车场的时间\n\n"); StackTrverse(s); printf("\n现在便道上的车有:\n"); printf("车牌号码 停入便道上的时间\n\n"); QueueTrverse(Q); } else printf("你的输入有误(停车场和便道上都没有此车),请重新输入:\n"); } } if(e.AorD=='E') { printf("输入结束,请按任意键结束.\n"); break; } } }wybwhf 发布留言 2008-9-6 16:24 高手都进来看下呀,各位大侠们wybwhf 发布留言 2008-9-6 16:43 怎么人人都把我给忽视了呢?真的感到很悲伤wsllsg 发布留言 2008-9-6 19:05 看起来C语言好复杂啊,有点恐怖!~~还是学VB算了[tk12]
页: [1] 特别说明:如网页特效代码中有引用图片文件等,请自己下载到本地调试! |