スタックとキューに基づく駐車場管理システム

15893 ワード

1、ヘッダファイル
#ifndef _HEAD_H_
#define _HEAD_H_

#include 
#include 
#include 
#include 
#define MAX 5
#define PRICE 16.8

enum m
{
	STACK_FULL,
	STACK_NOFULL,
	STACK_EMPTY,
	STACK_NOEMPTY,
	PUSH_OK,
	QUEUE_EMPTY,
	QUEUE_NOEMPTY,
	IN_OK,
	POP_NO,
	POP_OK,
	OUT_OK,
	OUT_NO
};
typedef struct car
{
	char ID[10];                                            //   
	time_t intime;                                          //      
	time_t outtime;                                         //    
	int length;                                             //    
	float expense;                                          //    
	int place;                                              //    
}CAR;

struct Stack
{
	CAR num[MAX];                                           //           
	int top;                                                //    
};

struct node
{
	CAR data;                                              
	struct node *next;
};

typedef struct Queue
{
	struct node *front;                                     //    
	struct node *rear;                                      //    
}Queue;  //  

typedef struct temporary                                    //   
{
	CAR num1[MAX-1];
	int top;
}tem;

int menu();                                                 //    

void init_stack(struct Stack *s);                           //    
int empty_stack(struct Stack *s);                           //    
int full_stack(struct Stack *s);                            //    
int push_stack(struct Stack *s,CAR *car,Queue *q);          //      
int push1_stack(struct Stack *s,CAR *car,Queue *q);          //      
int POP_stack(struct Stack *s,CAR *car);                    //      

void init_stack1(tem *s1);                                  //      
int empty_stack1(tem *s1);                                  //      
int full_stack1(tem *s1);                                   //      
int push_stack1(tem *s1,CAR *car);                          //      
CAR *pop_stack1(tem *s1,CAR *car);                          //      

void init_queue(Queue *q);                                  //     
int empty_queue(Queue *q);                                  //        
int in_queue(Queue *q,CAR *car);                            //      
int out_queue(struct Stack *s,CAR *car,Queue *q);           //          

int  display_stack(struct Stack *s);                        //          
int display_queue(Queue *p);                                //         

int printf_check(CAR *car);                                 //    
int look_check();                                           //    

#endif
2、駐車する
#include "head.h"

int push_stack(struct Stack *s,CAR *car,Queue *q)
{
	char relay;
	Queue *p = q;

	if(full_stack(s) == STACK_FULL)
	{
		printf("        ,        (y/n):
"); scanf(" %c",&relay); while(!(relay == 'y'||relay == 'Y'||relay == 'N'|| relay == 'n')) { while(getchar() != '
'); printf(" , (y/n):"); scanf(" %c",&relay); } if(relay == 'y' ||relay == 'Y') // y { in_queue(p,car); } if(relay == 'n'|| relay == 'N') { printf(" , , !
"); return 0; } } else { s->top++; s->num[s->top] = *car; s->num[s->top].place = s->top+1; time(&s->num[s->top].intime); return PUSH_OK; } } int push1_stack(struct Stack *s,CAR *car,Queue *q) { if(full_stack(s) != STACK_FULL) { s->top++; s->num[s->top] = *car; s->num[s->top].place = s->top+1; return PUSH_OK; } }
3、車を取りに行きます
void init_stack1(tem *s1)                                                        //      
{
	s1->top = -1;
}

int empty_stack1(tem *s1)                                                        //      
{
	if(s1->top == -1)
	{
		return STACK_EMPTY;
	}
	return STACK_NOEMPTY;
}

int full_stack1(tem *s1)                                                         //         
{
	if(s1->top = MAX-2)
	{
		return STACK_FULL;
	}
	return STACK_NOFULL;
}

int push_stack1(tem *s1,CAR *car)                                                 //     
{
	s1->top++;
	s1->num1[s1->top] = *car;
}

CAR *pop_stack1(tem *s1,CAR *car)                                                 //       
{
	if(empty_stack1(s1) == STACK_EMPTY)
	{
		printf("     ,    !
"); } else { *car = s1->num1[s1->top]; s1->top--; return car; } } int pop_stack(struct Stack *s,CAR *car,Queue *q) // { char ID[10]; // int i = 0; // int flag = 0; // int j = 0; int temp = 0; if(empty_stack(s) == STACK_EMPTY) { printf(" , !
"); return POP_NO; } printf(" :
"); scanf("%s",ID); while(i <= s->top) { temp = s->top; if((strcmp(ID,s->num[i].ID) == 0)&&s->num[i].place == i+1) { if(i == s->top) // , { time(&s->num[s->top].outtime); s->num[s->top].length = s->num[s->top].outtime - s->num[s->top].intime; // s->num[s->top].expense = ((int)((s->num[s->top].length)/3600+1))*16.8; *car = s->num[s->top]; s->top--; } else // { tem *s1 = (tem*)malloc(sizeof(tem)); init_stack1(s1); j = i; while(j < temp) // { CAR *car1 = (CAR*)malloc(sizeof(CAR)); *car1 = s->num[s->top]; s->top--; push_stack1(s1,car1); j++; } time(&s->num[s->top].outtime); s->num[s->top].length = s->num[s->top].outtime - s->num[s->top].intime; // s->num[s->top].expense = ((int)((s->num[s->top].length)/3600+1))*16.8; *car = s->num[s->top]; s->top--; j = s->top; while(j < temp-1) // { CAR *newcar = (CAR *)malloc(sizeof(CAR)); CAR *car2 = (CAR *)malloc(sizeof(CAR)); newcar = pop_stack1(s1,car2); push1_stack(s,newcar,q); j++; } } flag = 1; } i++; } if(flag == 1) { printf(" , , !
"); return POP_OK; } else { printf(" , !
"); } }
4、駐車場情報と車道情報を確認する
#include "head.h"

int  display_stack(struct Stack *s)
{
	int i = 0;
	time_t time_current;                                                         //      
	int length;                                                                  //                

	if(s->top == -1)
	{
		printf("         ,         !
"); return -1; } while(i <= s->top) { printf(" :%s
",s->num[i].ID); printf(" :%s",ctime(&s->num[i].intime)); printf(" : ...
"); printf(" : %d
",s->num[i].place); time(&time_current); length = time_current - s->num[i].intime; printf(" :%d
",(int)(length/60)+1); printf(" : %.1f ",((int)(length/3600)+1)*16.8); printf("

==================================================================

"); i++; } }
#include "head.h"

int display_queue(Queue *q)
{
	struct node *p = q->front;
	int i = 0;

	if(p == NULL)
	{
		printf("           !
"); return 0; } while(p != NULL) { printf(" :%s
",p->data.ID); printf(" :%d
",++i); printf("
======================================================
"); p = p->next; } }
5、領収書の印刷と領収書の確認
#include "head.h"

int printf_check(CAR *car)
{
	printf("
"); printf(" :%s
",car->ID); printf(" :%s",ctime(&car->intime)); printf(" :%s",ctime(&car->outtime)); printf(" :%d
",(int)(car->length/60+1)); printf(" :%.1f
",car->expense); printf(" , ...
"); FILE *fp; time_t print_time; if((fp = fopen("check.txt","w+")) == NULL) { printf(" !
"); return -1; } fprintf(fp,"
566
"); fprintf(fp," :%s
",car->ID); fprintf(fp," :%s",ctime(&car->intime)); fprintf(fp," :%s",ctime(&car->outtime)); fprintf(fp," :%d
",(int)(car->length/60+1)); fprintf(fp," :%.1f
",car->expense); sleep(4); time(&print_time); fprintf(fp," :%s",ctime(&print_time)); printf("
!
"); fclose(fp); return 0; }
#include "head.h"

int look_check()
{
	FILE *fp;
	char ch;

	if((fp = fopen("check.txt","r")) == NULL)
	{
		printf("    !
"); return -1; } while((ch = fgetc(fp)) != EOF) // { putchar(ch); // } fclose(fp); return 0; }
6、メインメニューとメイン関数
#include "head.h"

void init_stack(struct Stack *s)       //    
{
	s->top = -1;
}

int empty_stack(struct Stack *s)       //    
{
	if(s->top == -1)
	{
		return STACK_FULL;
	}
	return STACK_NOFULL;
}

int full_stack(struct Stack *s)        //    
{
	if(s->top == MAX-1)
	{
		return STACK_FULL;
	}
	return STACK_NOFULL;	
}
#include "head.h"

void init_queue(Queue *q)                                              //     
{
	q->front = NULL;
	q->rear = NULL;
}

int empty_queue(Queue *q)                                              //        
{
	if(q->rear == NULL)
	{
		return QUEUE_EMPTY;
	}
	else
	{
		return QUEUE_NOEMPTY;
	}
}

int in_queue(Queue *q,CAR *car)
{
	struct node *p = (struct node*)malloc(sizeof(struct node));         //      
	if(p == NULL)
	{
		printf("new p malloc error!
"); return -1; } p->data = *car; p->next = NULL; if(q->rear == NULL) { q->rear = p; q->front = p; printf(" "); } else { q->rear->next = p; q->rear = p; printf(" "); } return IN_OK; } int out_queue(struct Stack *s,CAR *car,Queue *q) // { if(empty_queue(q) == QUEUE_EMPTY) // , { return OUT_NO; } struct node *p = q->front; // p if(empty_stack(s) != STACK_FULL) { *car = p->data; q->front = p->next; if(q->front == NULL) { q->rear = NULL; } free(p); p = NULL; push_stack(s,car,q); // return OUT_OK; } }
#include "head.h"

int menu()
{
	int choice = 0;                        
	int ret = 0;                                                                   //        

	printf("                 ====================================================== 
"); printf(" | |
"); printf(" | 566 |
"); printf(" | |
"); printf(" | :1.0 |
"); printf(" |----------------------------------------------------|
"); printf(" | 1、 | 2、 |
"); printf(" |----------------------------------------------------|
"); printf(" | 3、 | 4、 |
"); printf(" |----------------------------------------------------|
"); printf(" | 5、 | 6、 |
"); printf(" |----------------------------------------------------|
"); printf(" | : %d , |
",MAX); printf(" | , |
"); printf(" | ! |
"); printf(" |----------------------------------------------------|
"); printf(" | : %.1f / , |
",PRICE); printf(" | , ! |
"); printf(" |____________________________________________________|
"); printf(" (1/2/3/4/5/6):"); ret = scanf("%d",&choice); while(ret != 1||(choice != 1&&choice != 2&&choice != 3&&choice != 4&&choice != 5&&choice != 6)) // { while(getchar() != '
'); printf(" , :"); ret = scanf("%d",&choice); } if(choice == 1||choice == 2||choice == 3||choice == 4 ||choice == 5 ||choice == 6) { return choice; } }
#include "head.h"

int main()
{
	int choice = 0;
	int push_ok = 0;
	int out_ok = 0;
	int pop_ok = 0;
	struct Stack *s = (struct Stack *)malloc(sizeof(struct Stack));  
	Queue *q = (Queue *)malloc(sizeof(Queue));
	if(s == NULL)                                                         //         
	{
		printf("stack malloc error!
"); return 0; } init_stack(s); if(q == NULL) // { printf("queue malloc error!
"); return 0; } init_queue(q); while(1) { system("reset"); choice = menu(); switch(choice) { case 1: { system("reset"); CAR *car = (CAR *)malloc(sizeof(CAR)); //car printf("
"); printf(" :"); scanf("%s",car->ID); push_ok = push_stack(s,car,q); if(push_ok == PUSH_OK) { printf(" ! :%d , %s",s->num[s->top].place,ctime(&s->num[s->top].intime)); } break; } case 2: { system("reset"); CAR *car1 = (CAR *)malloc(sizeof(CAR)); //car1 pop_ok = pop_stack(s,car1); if(pop_ok == POP_OK) { printf("
:
"); printf_check(car1); CAR *car2 = (CAR *)malloc(sizeof(CAR)); // 1 out_ok = out_queue(s,car2,q); if(out_ok == OUT_OK) { printf(" 1 !
"); } } break; } case 3: { system("reset"); display_stack(s); break; } case 4: { system("reset"); display_queue(q); break; } case 5: { printf("\t\t !^V^
"); exit(0); } case 6: { system("reset"); look_check(); break; } } printf("

........."); getchar(); getchar(); } return 0; }