#include
#include
#include
#include
int cars[12][4]={{1234,1,1,20},{2345,1,2,15},{3456,1,3,10},
{4567,1,4,5},{0,1,5,0},{0,1,6,0},{0,2,1,0},{0,2,2,0},{0,2,3,0},
{0,2,4,0},{0,2,5,0},{0,2,6,0}};/*二维数组代表停车信息*/
void save()
{FILE *fp;
int i,j;
if((fp=fopen("car.dat","w"))==NULL)
{printf("cannot open file\n");
return;
}
for(i=0;i<12;i++)
for(j=0;j<4;j++)
if(fwrite(cars,2,1,fp)!=1)
printf("file write error\n");
fclose(fp);
getchar();
}
void car_park(void)/*停车操作*/
{int x,i,j;
printf("\n ^-^ Welcome To Our Stop! ^-^\n ");
printf(" \n\n Please,input your car number:\n\n");
printf("\n NOTICE:car number is a digit between 1000 and 9999\n Input wrong number may back to menu\n\n");
scanf("%d",&x);/*输入要停车的车牌号*/
for (i=0;i<12;i++)
if(cars[i][0]==x||x<1000||x>9999)break;
if(i!=12)
{printf("\nWrong number or it's parked !!!\n");
getchar();}/*如果此车号以在,打印此车已停*/
else if(i==12&&x>=1000&&x<=9999)
{for (i=0;i<12;i++)
if(cars[i][0]==0) {cars[i][0]=x;save();
printf("\n\nSUCCESS\n\n");
printf("Floor=%d,position=%d\n",cars[i][1],cars[i][2]);
printf("\n\n\nTwo times 'Enter' to end...");break;
}/*如果此车号不在,则进行停车操作*/
for (i=0;i<12;i++)
if(cars[i][0]!=0) cars[i][3]+=5;/*所有停车时间+5*/
save();/*保存以上信息到文件*/
}
}
void car_get(void)/*取车操作*/
{
int i,y;float paid;int a;
printf("\n Get Car\n\n\n Input your car number:\n\n\n\n");
printf("\n NOTICE:car number is a digit between 1000 and 9999\n Wrong load would have no cue\n\n");
scanf("%d",&y);/*输入要取车的车牌号*/
for(i=0;i<12;i++)
{
for(i=0;i<12;i++)
if(cars[i][0]==y)
{
cars[i][0]=0;/*取车后车牌号清零*/
paid=0.2*cars[i][3]/5;/*计算停车费用*/
printf("\n Printf out the paid?(1--YES 2 or any key--NO)\n\n\n");
scanf("%d",&a);
{
switch(a)
{
case 1:
printf("\n\n\nThe paid is %8.2fyuan\n",paid);/*打印停车费用*/
cars[i][3]=0;/*时间清零*/
save();
break;
case 2:
printf("Good bye");
cars[i][3]=0;/*时间清零*/
save();
break;
default: break;
}
}
}else;break;
}
if(i==12)printf("\nThe number is not in the park!!!\n");/*如果此车不在,打印号码不在*/
}
void printfdata()/*停车信息*/
{int i,j;
FILE *fp;
fp=fopen("car.dat","r");/*打开文件"car.dat"*/
printf(" \n Number Floor Position Time\n");
for(i=0;i<12;i++)
{for(j=0;j<4;j++)
{fread(cars,2,1,fp);/*读文件*/
printf(" %6d",cars[i][j]);
}printf("\n");
}
fclose(fp);/*关闭文件"car.dat"*/
}
void save();
void car_park(void);
void car_get(void);
void printfdata();
char readcommand();
void initialization();
void main()
{
char c;
while(1)
{
initialization(); /*初始化界面*/
c=readcommand(); /*读取停车场状况*/
clrscr();
switch(c)
{
case 'p': car_park(); break;/*停车操作*/
case 'P': car_park(); break;/*停车操作*/
case 'g': car_get(); break;/*取车操作*/
case 'G': car_get(); break;/*取车操作*/
case 'd': printfdata();
printf("\n\n please press 'Enter' to continue....\n");
scanf("%c",&c); break;/*停车信息*/
case 'D': printfdata(); /*停车信息*/
printf("\n\n rreupklfdkplease press 'Enter' to continue....\n");
scanf("%c",&c); break;
case 'e': printf("\n\n\n\n Press 'Enter' to continue...");exit(0); break;
case 'E': printf("\n\n\n\n Press 'Enter' to continue...");exit(0); break;
default : printf("ERROR! Press 'Enter' to continue..."); getchar(); break;
}
}
}
/********************************************************************************/
void initialization() /*初始函数*/
{
int i;
getchar();
clrscr();
gotoxy(0,0);
for(i=1;i<=80;i++) printf("\1\3");
for(i=1;i<=80;i++) printf("\2");
gotoxy(15,6);
printf("THIS IS OUR CAR PART MANAGE SYSTEM!");
gotoxy(15,9);
printf("NAME: GuanYoufu");
gotoxy(15,10);
printf("NUM: 1111111327");
gotoxy(15,11);
printf("GRADE: 2006");
gotoxy(15,12);
printf("CLASS: caiyan0603");
gotoxy(1,14);
printf("\n********************************************************************************\n");
printf(" 1. Park car--p 2. Get car--g 3. Date of parking--d 4.Exit--E");
printf("\n\n********************************************************************************\n");
}
char readcommand() /*选择函数*/
{
char c;
while((c!='p')&&(c!='P')&&(c!='g')&&(c!='G')&&(c!='d')&&(c!='D')&&(c!='e')&&(c!='E'))
{
printf("Input p,g,d,e choose!!\n");
c=getchar();
printf("\n");break;
}
return c;
}
根据自己得需要修改一下吧
呵呵,找到了,应该就是你要的了
http://www.pudn.com/downloads116/sourcecode/math/detail489511.html
这个,下载地址是
http://www.pudn.com/downloads116/sourcecode/math/21840280tingchechang.rar
如果不行的话
http://www.pudn.com/search_db.asp?keyword=%CD%A3%B3%B5%B3%A1+%D5%BB&p=C%2B%2B
这里还有几个,我再帮你下载。
楼上的没看到人家要C++的
要是n值不定,必须用链表了(栈也可以)
首先要建立一个停车位的结点(我这样写便于理解,但不是最优的方法):
struct point {
int no(车号),time(进入时刻),n(停车位),key(该位是否有车0为无,1有);
No *next,*top;
}a,*p;
然后读入n=i;建立链表:
a.next=NULL;
p=&a;
for(k=2;k<=n;k++){
p->next=new point;
p->next->next=NULL;
p->next->n=k;
p->next->key=0;
p->next->top=p;
p=p->next;}
然后让p指向表头,进一辆车将数据写入p指向,并将p指向p->next;
初一辆车搜索链表,找到位置,计算时间并cout,再将后面的车辆数据向前挪1位.
具体的还是你自己写吧,思路其实也不难
栈以顺序结构实现,队列以链表结构实现 如果把这个要求取消 会简单和直观很多
如果能接受C 的话,我倒能试试. ryw12403
用C的话是不是要用上指向指针的指针和结构体才可以?
我的思路是要两个都用上才行.