#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 20
struct time
{
unsigned int year;
unsigned int month;
unsigned int day;
int hour;
};
typedef struct time Time;
struct park_store
{
char lisence[10];
int state;
Time arrive_time;
Time leave_time;
float cost;
}store[max];
struct wait_store
{
char lisence[10];
struct wait_store *next;
};
enum park_state{park_no=0,park_yes=1};
enum return_result{empty=0,full=1};
typedef struct park_store Park;
typedef struct wait_store Wait_store;
typedef struct wait_store * Wait;
void menu();
void rechoice_menu();
int park_ok(int n);
void malloc_park(Park store[]);
void malloc_ok(Wait *new_node);
void record_park(Wait head);
void record_leave(Wait head);
void creat_link(Wait *head);
void insert_wait(Wait head,Wait node);
void list_park();
void list_wait(Wait head);
void wait_leave(Wait head);
void save_file(char *filename,int n);
void read_file(char *filename);
int daynum(Time time);
float cost_park(Park store[],int i);
int match(char a[],char b[]);
int main()
{
int menu_i;
Park store[max];
Wait head = NULL;
malloc_park(store);
creat_link(&head);
while(1)
{
menu();
printf("\t\t请输入您选择的编号:");
scanf("%d",&menu_i);
switch(menu_i)
{
case 1: record_park(head);
getchar();
getchar();
system("cls");
break;
case 2: record_leave(head);
getchar();
getchar();
system("cls");
break;
case 3: list_park();
getchar();
getchar();
system("cls");
break;
case 4: read_file("park_store.txt");
getchar();
getchar();
system("cls");
break;
case 5: list_wait(head);
getchar();
getchar();
system("cls");
break;
case 6: wait_leave(head);
getchar();
getchar();
system("cls");
break;
case 0: printf("\t\t谢谢使用,欢迎再次使用!\n");
exit(0);
break;
default :printf("\t\t输入有误!请输入正确的编号:\n");
continue;
}
}
return 0;
}
void malloc_park(Park store[])
{
store = (Park*)calloc(max,sizeof(Park));
if(store == NULL)
{
printf("申请空间失败!\n");
exit(-1);
}
}
void menu()
{
printf("\t\t***********************************************\n\n");
printf("\t\t******欢 迎 使 用 冰 羽 停 车 管 理 系 统******\n\n");
printf("\t\t 1、登记停车 2、登记离开\n\n");
printf("\t\t 3、停泊列表 4、文件读出\n\n");
printf("\t\t 5、显示便道 6、从便道离开\n\n");
printf("\t\t 0、退出\n\n");
printf("\t\t***********************************************\n\n");
}
void read_file(char *filename)
{
FILE * fp;
char ch;
char buf[20];
int i;
if ((fp = fopen(filename, "r")) == NULL)
{
printf("打开文件失败!\n");
exit(-1);
}
while ((ch = fgetc(fp)) != EOF)
{
if (ch == ' ')
{
fscanf(fp, "%d", &i);
i--;
fscanf(fp, "%s", buf);
strcpy(store[i].lisence, buf);
fscanf(fp, "%d", &store[i].arrive_time.year);
fscanf(fp, "%d", &store[i].arrive_time.month);
fscanf(fp, "%d", &store[i].arrive_time.day);
fscanf(fp, "%d", &store[i].arrive_time.hour);
store[i].state = park_yes;
}
}
fclose(fp);
printf("读取成功!\n");
}
int park_ok(int n)
{
if(store[n - 1].state == park_yes)
{
return full;
}
else
{
return empty;
}
}
void record_park(Wait head)
{
int i;
char buf[10];
printf("请输入车辆牌号:");
scanf("%s",buf);
for(i = max;i > 0;i--)
{
if(park_ok(i) == empty)
{
break;
}
}
if(i == 0)
{
Wait new_node;
printf("由于停车场没有空位,进入便道等待。\n");
malloc_ok(&new_node);
strcpy(new_node->lisence,buf);
insert_wait(head,new_node);
}
else
{
printf("%d号位置为空,停到该位置。\n",i);
i--;
strcpy(store[i].lisence,buf);
store[i].state = park_yes;
printf("请输入此刻时间(yearmonthdayhour:2016 11 25 17):");
scanf("%d%d%d%d",&store[i].arrive_time.year,&store[i].arrive_time.month,&store[i].arrive_time.day,&store[i].arrive_time.hour);
printf("停车成功!\n");
store[i].cost = cost_park(store,i);
}
}
void list_park()
{
int i;
int flag = 0;
for(i = 0;i < max;i++)
{
if(store[i].state == park_yes)
{
if(flag == 0)
{
printf("\t\t停车位\t车牌号\t\t到达时间\n");
}
printf("\t\t%-d\t%-s\t\t%-d/%d/%d/%d\n",i+1,store[i].lisence,store[i].arrive_time.year,store[i].arrive_time.month,store[i].arrive_time.day,store[i].arrive_time.hour);
flag = 1;
}
}
if(flag == 0)
{
printf("\t\t停车场内没有车辆停泊!\n");
}
}
void malloc_ok(Wait *new_node)
{
*new_node = (Wait)malloc(sizeof(Wait_store));
if(*new_node == NULL)
{
printf("空间申请失败!\n");
exit(-1);
}
}
void creat_link(Wait *head)
{
malloc_ok(head);
(*head)->next = NULL;
}
void insert_wait(Wait head,Wait node)
{
node->next = head->next;
head->next = node;
}
void list_wait(Wait head)
{
Wait p = head->next;
if(p != NULL)
{
printf("\t\t在等待的车辆有:\n");
}
else
{
printf("\t\t此刻没有车辆在便道等待!\n");
return;
}
while(p != NULL)
{
printf("\t\t%s\n",p->lisence);
p = p->next;
}
//printf("\t\t%s\n",p->lisence);
}
void wait_leave(Wait head)
{
char buf[10];
Wait p,q;
p = head->next;
if(p == NULL)
{
printf("\t\t便道上没有车辆,请确认!\n");
return;
}
list_wait(head);
printf("\t\t请输入想要离开的车牌号:");
scanf("%s",buf);
while(match(p->lisence,buf) != 1&&p != NULL)
{
q = p;
p = p->next;
}
if(p != NULL)
{
q->next = p->next;
free(p);
printf("\t\t删除成功!\n");
}
else
{
printf("\t\t没有这样的车牌,请确认!\n");
}
}
int match(char a[],char b[])
{
char *p1=a;
char *p2=b;
while(*p1 != '\0')
{
if(*p1 == *p2)
{
while(*p1 == *p2 && *p2 != '\0')
{
p1++;
p2++;
}
}
else
p1++;
if(*p2 == '\0')
{
return 1;
}
p2 = b;
}
return 0;
}
int daynum(Time time)
{
int i,days=0;
int day_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<time.month;i++)
{
days+=day_tab[i-1];
}
days+=time.day;
if((time.year%4==0&&time.year%100!=0||time.year%400==0)&&time.month>2)
days++;
return days;
}
float cost_park(Park store[],int i)
{
int num,yearsub;
float cost;
if(store[i].leave_time.year == 0)//没有离开,暂不计算费用
{
return 0;
}
yearsub=store[i].leave_time.year-store[i].arrive_time.year;
num=yearsub*365+daynum(store[i].leave_time)-daynum(store[i].arrive_time);
num+=yearsub/4;
cost = 0.5*((num-1)*24+store[i].leave_time.hour+24-store[i].arrive_time.hour);//假定每小时单价为0.5元
return cost;
}
void record_leave(Wait head)
{
int i;
int choice;
list_park();
printf("\t\t请输入登记离开的停车位置:");
scanf("%d",&i);
i--;
while(store[i].state == park_no)
{
printf("\t\t该停车位没有车辆,请选择重新输入或返回上一页!");
rechoice_menu();
scanf("%d",&choice);
choice%=2;
if(choice == 0)
{
printf("\t\t请输入登记离开车辆的停车位置:");
scanf("%d",&i);
i--;
}
else
{
return;
}
}
printf("\t\t请输入离开时间(年月日时:2016 11 25 19):");
scanf("%d%d%d%d",&store[i].leave_time.year,&store[i].leave_time.month,&store[i].leave_time.day,&store[i].leave_time.hour);
store[i].cost = cost_park(store,i);
printf("\t\t停车费用为%.2f元,登记离开成功!\n",store[i].cost);
store[i].state = park_no;
save_file("park_leave.txt",i);
Wait p = head;
Wait q;
if(p->next == NULL)
{
printf("\t\t此时便车车道没有车辆。\n");
}
else
{
while(p->next != NULL)//找到最先进入便道的车辆
{
q = p;
p = p->next;
}
printf("\t\t便道车辆进入该车位。车牌号为:%s\n",p->lisence);
strcpy(store[i].lisence,p->lisence);
store[i].arrive_time.year = store[i].leave_time.year;
store[i].leave_time.year = 0;
store[i].arrive_time.month = store[i].leave_time.month;
store[i].leave_time.month = 0;
store[i].arrive_time.day = store[i].leave_time.day;
store[i].leave_time.day = 0;
store[i].arrive_time.hour = store[i].leave_time.hour;
store[i].leave_time.hour = 0;
store[i].state = park_yes;
q->next == NULL;
free(p);
}
}
void save_file(char *filename,int n)
{
FILE * fp;
char buf[max];
if ((fp = fopen(filename, "a")) == NULL)
{
printf("%s open error!\n",filename);
exit(-1);
}
fputc(' ', fp);
fprintf(fp,"%d",n+1);
fputc(' ', fp);
strcpy(buf, store[n].lisence);
fprintf(fp, "%s", buf);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.year);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.month);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.day);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.hour);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.year);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.month);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.day);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.hour);
fputc(' ', fp);
fprintf(fp,"%.2f",store[n].cost);
fputc('\n', fp);
fclose(fp);
printf("\t\t存入文件成功!\n");
return;
}
void rechoice_menu()
{
printf("\t\t***********************************************\n\n");
printf("\t\t******欢 迎 使 用 冰 羽 停 车 管 理 系 统******\n\n");
printf("\t\t 1、重新输入 2、返回上一页\n\n");
printf("\t\t**************************************************\n\n");
printf("\t\t请输入您的选择:");
}
#include <stdlib.h>
#include <string.h>
#define max 20
struct time
{
unsigned int year;
unsigned int month;
unsigned int day;
int hour;
};
typedef struct time Time;
struct park_store
{
char lisence[10];
int state;
Time arrive_time;
Time leave_time;
float cost;
}store[max];
struct wait_store
{
char lisence[10];
struct wait_store *next;
};
enum park_state{park_no=0,park_yes=1};
enum return_result{empty=0,full=1};
typedef struct park_store Park;
typedef struct wait_store Wait_store;
typedef struct wait_store * Wait;
void menu();
void rechoice_menu();
int park_ok(int n);
void malloc_park(Park store[]);
void malloc_ok(Wait *new_node);
void record_park(Wait head);
void record_leave(Wait head);
void creat_link(Wait *head);
void insert_wait(Wait head,Wait node);
void list_park();
void list_wait(Wait head);
void wait_leave(Wait head);
void save_file(char *filename,int n);
void read_file(char *filename);
int daynum(Time time);
float cost_park(Park store[],int i);
int match(char a[],char b[]);
int main()
{
int menu_i;
Park store[max];
Wait head = NULL;
malloc_park(store);
creat_link(&head);
while(1)
{
menu();
printf("\t\t请输入您选择的编号:");
scanf("%d",&menu_i);
switch(menu_i)
{
case 1: record_park(head);
getchar();
getchar();
system("cls");
break;
case 2: record_leave(head);
getchar();
getchar();
system("cls");
break;
case 3: list_park();
getchar();
getchar();
system("cls");
break;
case 4: read_file("park_store.txt");
getchar();
getchar();
system("cls");
break;
case 5: list_wait(head);
getchar();
getchar();
system("cls");
break;
case 6: wait_leave(head);
getchar();
getchar();
system("cls");
break;
case 0: printf("\t\t谢谢使用,欢迎再次使用!\n");
exit(0);
break;
default :printf("\t\t输入有误!请输入正确的编号:\n");
continue;
}
}
return 0;
}
void malloc_park(Park store[])
{
store = (Park*)calloc(max,sizeof(Park));
if(store == NULL)
{
printf("申请空间失败!\n");
exit(-1);
}
}
void menu()
{
printf("\t\t***********************************************\n\n");
printf("\t\t******欢 迎 使 用 冰 羽 停 车 管 理 系 统******\n\n");
printf("\t\t 1、登记停车 2、登记离开\n\n");
printf("\t\t 3、停泊列表 4、文件读出\n\n");
printf("\t\t 5、显示便道 6、从便道离开\n\n");
printf("\t\t 0、退出\n\n");
printf("\t\t***********************************************\n\n");
}
void read_file(char *filename)
{
FILE * fp;
char ch;
char buf[20];
int i;
if ((fp = fopen(filename, "r")) == NULL)
{
printf("打开文件失败!\n");
exit(-1);
}
while ((ch = fgetc(fp)) != EOF)
{
if (ch == ' ')
{
fscanf(fp, "%d", &i);
i--;
fscanf(fp, "%s", buf);
strcpy(store[i].lisence, buf);
fscanf(fp, "%d", &store[i].arrive_time.year);
fscanf(fp, "%d", &store[i].arrive_time.month);
fscanf(fp, "%d", &store[i].arrive_time.day);
fscanf(fp, "%d", &store[i].arrive_time.hour);
store[i].state = park_yes;
}
}
fclose(fp);
printf("读取成功!\n");
}
int park_ok(int n)
{
if(store[n - 1].state == park_yes)
{
return full;
}
else
{
return empty;
}
}
void record_park(Wait head)
{
int i;
char buf[10];
printf("请输入车辆牌号:");
scanf("%s",buf);
for(i = max;i > 0;i--)
{
if(park_ok(i) == empty)
{
break;
}
}
if(i == 0)
{
Wait new_node;
printf("由于停车场没有空位,进入便道等待。\n");
malloc_ok(&new_node);
strcpy(new_node->lisence,buf);
insert_wait(head,new_node);
}
else
{
printf("%d号位置为空,停到该位置。\n",i);
i--;
strcpy(store[i].lisence,buf);
store[i].state = park_yes;
printf("请输入此刻时间(yearmonthdayhour:2016 11 25 17):");
scanf("%d%d%d%d",&store[i].arrive_time.year,&store[i].arrive_time.month,&store[i].arrive_time.day,&store[i].arrive_time.hour);
printf("停车成功!\n");
store[i].cost = cost_park(store,i);
}
}
void list_park()
{
int i;
int flag = 0;
for(i = 0;i < max;i++)
{
if(store[i].state == park_yes)
{
if(flag == 0)
{
printf("\t\t停车位\t车牌号\t\t到达时间\n");
}
printf("\t\t%-d\t%-s\t\t%-d/%d/%d/%d\n",i+1,store[i].lisence,store[i].arrive_time.year,store[i].arrive_time.month,store[i].arrive_time.day,store[i].arrive_time.hour);
flag = 1;
}
}
if(flag == 0)
{
printf("\t\t停车场内没有车辆停泊!\n");
}
}
void malloc_ok(Wait *new_node)
{
*new_node = (Wait)malloc(sizeof(Wait_store));
if(*new_node == NULL)
{
printf("空间申请失败!\n");
exit(-1);
}
}
void creat_link(Wait *head)
{
malloc_ok(head);
(*head)->next = NULL;
}
void insert_wait(Wait head,Wait node)
{
node->next = head->next;
head->next = node;
}
void list_wait(Wait head)
{
Wait p = head->next;
if(p != NULL)
{
printf("\t\t在等待的车辆有:\n");
}
else
{
printf("\t\t此刻没有车辆在便道等待!\n");
return;
}
while(p != NULL)
{
printf("\t\t%s\n",p->lisence);
p = p->next;
}
//printf("\t\t%s\n",p->lisence);
}
void wait_leave(Wait head)
{
char buf[10];
Wait p,q;
p = head->next;
if(p == NULL)
{
printf("\t\t便道上没有车辆,请确认!\n");
return;
}
list_wait(head);
printf("\t\t请输入想要离开的车牌号:");
scanf("%s",buf);
while(match(p->lisence,buf) != 1&&p != NULL)
{
q = p;
p = p->next;
}
if(p != NULL)
{
q->next = p->next;
free(p);
printf("\t\t删除成功!\n");
}
else
{
printf("\t\t没有这样的车牌,请确认!\n");
}
}
int match(char a[],char b[])
{
char *p1=a;
char *p2=b;
while(*p1 != '\0')
{
if(*p1 == *p2)
{
while(*p1 == *p2 && *p2 != '\0')
{
p1++;
p2++;
}
}
else
p1++;
if(*p2 == '\0')
{
return 1;
}
p2 = b;
}
return 0;
}
int daynum(Time time)
{
int i,days=0;
int day_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<time.month;i++)
{
days+=day_tab[i-1];
}
days+=time.day;
if((time.year%4==0&&time.year%100!=0||time.year%400==0)&&time.month>2)
days++;
return days;
}
float cost_park(Park store[],int i)
{
int num,yearsub;
float cost;
if(store[i].leave_time.year == 0)//没有离开,暂不计算费用
{
return 0;
}
yearsub=store[i].leave_time.year-store[i].arrive_time.year;
num=yearsub*365+daynum(store[i].leave_time)-daynum(store[i].arrive_time);
num+=yearsub/4;
cost = 0.5*((num-1)*24+store[i].leave_time.hour+24-store[i].arrive_time.hour);//假定每小时单价为0.5元
return cost;
}
void record_leave(Wait head)
{
int i;
int choice;
list_park();
printf("\t\t请输入登记离开的停车位置:");
scanf("%d",&i);
i--;
while(store[i].state == park_no)
{
printf("\t\t该停车位没有车辆,请选择重新输入或返回上一页!");
rechoice_menu();
scanf("%d",&choice);
choice%=2;
if(choice == 0)
{
printf("\t\t请输入登记离开车辆的停车位置:");
scanf("%d",&i);
i--;
}
else
{
return;
}
}
printf("\t\t请输入离开时间(年月日时:2016 11 25 19):");
scanf("%d%d%d%d",&store[i].leave_time.year,&store[i].leave_time.month,&store[i].leave_time.day,&store[i].leave_time.hour);
store[i].cost = cost_park(store,i);
printf("\t\t停车费用为%.2f元,登记离开成功!\n",store[i].cost);
store[i].state = park_no;
save_file("park_leave.txt",i);
Wait p = head;
Wait q;
if(p->next == NULL)
{
printf("\t\t此时便车车道没有车辆。\n");
}
else
{
while(p->next != NULL)//找到最先进入便道的车辆
{
q = p;
p = p->next;
}
printf("\t\t便道车辆进入该车位。车牌号为:%s\n",p->lisence);
strcpy(store[i].lisence,p->lisence);
store[i].arrive_time.year = store[i].leave_time.year;
store[i].leave_time.year = 0;
store[i].arrive_time.month = store[i].leave_time.month;
store[i].leave_time.month = 0;
store[i].arrive_time.day = store[i].leave_time.day;
store[i].leave_time.day = 0;
store[i].arrive_time.hour = store[i].leave_time.hour;
store[i].leave_time.hour = 0;
store[i].state = park_yes;
q->next == NULL;
free(p);
}
}
void save_file(char *filename,int n)
{
FILE * fp;
char buf[max];
if ((fp = fopen(filename, "a")) == NULL)
{
printf("%s open error!\n",filename);
exit(-1);
}
fputc(' ', fp);
fprintf(fp,"%d",n+1);
fputc(' ', fp);
strcpy(buf, store[n].lisence);
fprintf(fp, "%s", buf);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.year);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.month);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.day);
fputc(' ', fp);
fprintf(fp,"%d",store[n].arrive_time.hour);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.year);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.month);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.day);
fputc(' ', fp);
fprintf(fp,"%d",store[n].leave_time.hour);
fputc(' ', fp);
fprintf(fp,"%.2f",store[n].cost);
fputc('\n', fp);
fclose(fp);
printf("\t\t存入文件成功!\n");
return;
}
void rechoice_menu()
{
printf("\t\t***********************************************\n\n");
printf("\t\t******欢 迎 使 用 冰 羽 停 车 管 理 系 统******\n\n");
printf("\t\t 1、重新输入 2、返回上一页\n\n");
printf("\t\t**************************************************\n\n");
printf("\t\t请输入您的选择:");
}