⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 出租汽车管理系统.cpp

📁 出租汽车管理系统
💻 CPP
字号:
#include<iostream>
using namespace std;
#include<time.h>
#include<string.h>
#include<errno.h>
/*=============时间结构 ====================*/
struct TIME
{
    int year;
    int month;  	
    int day; 
};
/*---输出时间----*/
void OUTPUT_TIME( TIME T )
{
    cout << T.year << ' ' << T.month << ' ' << T.day << endl; 
}
/*================汽车的三种状态分别用三种类型的链表来存储================*/
struct RENT_AVAIBLE
{
    char licence_number[16]; // 存储汽车的牌号   
    int distance; //汽车所走了路程
    RENT_AVAIBLE * next; //链表指针
};
typedef RENT_AVAIBLE * p1;
/*===可租用的汽车=========================*/
struct RENTED
{
    char licence_number[16]; // 存储汽车的牌号
    char name[16];     //用户姓名  
	char ID_number[32];  //用户证件号
	int distance;      //汽车行程    	
    TIME time_out;        //汽车租出时间 
    TIME time_back;       //预计归还时间	
    double benefit;      //获利
    RENTED * next; //链表指针
};
typedef RENTED * p2;
/*==========租出去的汽车=======================*/
struct IN_REPAIR
{
    char licence_number[32]; //汽车车牌号 
    int distance;    //行程
	TIME in;      //进场时间   
    //TIME out;    //出场时间
    IN_REPAIR * next; 
};
typedef IN_REPAIR * p3; 
/*=======处于维修状态的汽车====================*/
p1 rent; p2 rented; p3 repair; //设置变量
/*===============子函数部分===================*/
/*=================信息表=====================*/
void show_menu()
{ 
	cout<<"------------------------------------------------"<<endl; 
	cout<<"-----------------*出租车管理系统*-----------------"<<endl; 
	cout<<"------------------------------------------------"<<endl; 
    cout<<"-------------请选择以下选项来执行操作-------------"<<endl; 
    cout<<"1.引入新汽车" << '\n' << endl;
    cout<<"2.租汽车" << '\n' << endl;
    cout<<"3.归还汽车" << '\n' << endl;
    cout<<"4.查询汽车" << '\n' << endl;
    cout<<"5.打印全部信息" << '\n' << endl;
    cout<<"6.显示收益状况" << '\n' << endl; 
    cout<<"0.exit" << '\n' << endl; 	
}
/*-------------*由于租出去的汽车相关资料较多,故用一个独立函数来执行其输出操作*--------------*/
void OUTPUT2( p2 & p )
{
    cout << "汽车车牌号:" << p -> licence_number << endl; 			
	cout << "客户的姓名:" << p -> name << endl;
	cout << "客户的ID号:" << p -> ID_number << endl;			
	cout << "该车的行程:" << p -> distance << endl; 
	cout << "租用该车应付的钱:" <<  p -> benefit << endl;
	cout << "该车借出时间:" ; OUTPUT_TIME( p -> time_out );
	cout << "应当归还时间:" ; OUTPUT_TIME( p -> time_back );  
}
int COMPARE( TIME t1, TIME t2 )
{
    if( t1.year > t2.year ) return 1;
    else if( t1.year == t2.year && t1.month > t2.month ) return 1;
    else if( t1.year == t2.year && t1.month == t2.month && t1.day > t2.day ) return 1;
    else return 0;  	
}
/*===============引入新车相关函数=================*/
void ADD1( p1 & new_point )
{
    p1 p;
    p = rent; 	
    while( p -> next != NULL)   //按要求排序插入新结点
	{	
	    if( p -> next -> distance > new_point -> distance )  
		    break;
	    p = p -> next;  
	}		
	new_point -> next = p -> next ;
	p -> next  = new_point;
}
void ADD_CAR()
{
	p1 new_point;
	new_point = new RENT_AVAIBLE; //申请新结点
    cout << "请输入汽车的牌号:";  //输入车牌号
    cin >> new_point ->  licence_number;	
    cout << "汽车行驶的路程:";  //用户输入汽车行程
	cin >> new_point -> distance; 
    ADD1( new_point );
}
/*==============实现租车相关函数================*/
void ADD2( p2 & new_point  )
{
	p2 p;
	p = rented;
	while( p -> next != NULL )
	{
		if( COMPARE( new_point -> time_back, p -> time_back  ) == 1 ) 
			break; 
		p = p -> next; 
	}
	new_point -> next = p -> next;
	p -> next = new_point; 	
}
void RENT_CAR()
{
    p2 new_point;
	p1 q;
	int len, dis, i;
	double benf;
	if( rent -> next != NULL )
	{		
		cout << "欢迎您租用汽车" << endl;
		new_point = new RENTED; 	
		cout << "您租用的汽车号为:" << rent -> next -> licence_number << endl; //输出用户租借车的牌号
		/*------------------登记用户相关信息----------------------*/
		len = strlen( rent -> next -> licence_number );
		for( i = 0; i < len; i++ )
			new_point -> licence_number[i] = rent -> next -> licence_number[i];		
		cout << "请输入你的名字:";
		cin >> new_point -> name;
		cout << "请输入您的证件号:"; 
		cin >> new_point -> ID_number;    	
		cout << "请输入您的行程:";    
		cin >> dis;
		cout << "请输入你借出的时间( year month day ):";
		cin >> new_point -> time_out.year >> new_point -> time_out.month >> new_point -> time_out.day;
		cout << "请输入你借出的时间( year month day ):";
		cin >> new_point -> time_back.year >> new_point -> time_back.month >> new_point -> time_back.day;    	
		/*-----------------------------------------------------------*/
		new_point -> distance = dis + rent -> next -> distance;
		if( dis < 1000) benf = 20; //计算用户应付的钱
		else benf = 20 + ( dis - 100 ) * 0.15;
		cout << "您应付的钱为:" << benf << endl;
		new_point -> benefit = benf;
		ADD2( new_point );
		q = rent -> next;
		rent -> next = rent -> next -> next;
		delete q; 
	}
	else cout << "很抱歉目前车库中没有车,欢迎下次光临!" << endl ;  
}
/*======================*还车相关函数*============================*/
void ADD3( p3 & new_point  )
{
	p3 q;
	q = repair;
	while( q -> next !=NULL  ) //按送入维修的时间添加新结点到主链
		q = q -> next;
	new_point -> next = q -> next;
	q -> next = new_point;
}
void BACK_CAR()
{
	char ID[32]; //存储用户ID
	int len, i; //len 为licence 的长度 
	p2 p, fl;    
	p3 q, new_point; //新结点
	p = rented; q = repair;
	new_point = new IN_REPAIR;
    cout << "有借有还再借不难,欢迎能够再次光临" << endl;    	
    cout << "请输入您的ID号:";
	cin >> ID;
	while( p -> next != NULL )//查询用户
	{
	    if( strcmp( ID, p -> next -> ID_number) == 0 )
		{
		 /*----------------输出用户信息以便确认-------------*/   
			cout << "该车基本信息如下,请确认无误" << endl;
            OUTPUT2( p -> next  );
		/*-----------------将汽车送入维修站--------------------*/
			cout << "该车已到维护时间,系统将其自动送入维修站" << endl;
			len = strlen( p -> next -> licence_number );
			cout << "len = " << len << endl;
			for( i = 0; i < len; i++)
			new_point -> licence_number[i] = p -> next -> licence_number[i];  
			new_point ->  distance = p -> next -> distance;
			new_point -> in.year = p -> next -> time_out.year;
			new_point -> in.month = p -> next -> time_out.month;
            new_point -> in.day = p -> next -> time_out.day;
			//int add = 0; //进位
			ADD3( new_point );
			/*---------------------------------------------------*/
			fl = p -> next;
			p -> next = p -> next -> next; 
			delete( fl );
            break;  			
		}			 
        p = p -> next; 
	}
    if( p -> next == NULL )
        cout << "您是入的信息有误,请重新填写" << endl;   		
}
/*=========================汽车查询========================*/
void CHECK_CAR()
{
	p1 p; p2 q; p3 r;
	p = rent; q = rented; r = repair;
	char licence[16];
	int flag = 0;
	cout << "请输入汽车的licence_number:";
    cin >> licence;
    while( flag == 0 && p -> next != NULL )
	{
	    if( strcmp( p -> next -> licence_number, licence) == 0 )
		{
		    cout << "你查询的车现在处于空闲状态,可以租用,以下是该车的详细信息:" << endl;  
		    cout << "licence_number:"<< p -> next -> licence_number << endl; 
		    cout << "汽车的行程:" << p -> next -> distance << endl;
		    flag = 1; break;
		}			
	    p = p -> next; 
	}
	while( flag == 0 && q -> next != NULL )
	{
	    if( strcmp( q -> next -> licence_number, licence) == 0 )
		{
		    cout << "您要查询的车已被租出,以下是其详细信息:" << endl;
		    OUTPUT2( q -> next );
			flag = 1; break;
		}			
	    q = q -> next; 
	}
	while( flag == 0 && r -> next != NULL )
	{
	    if( strcmp( r -> next -> licence_number, licence ) == 0 )
		{
		    cout << "您查询的车处于维修状态,以下是其详细信息:" << endl;
            cout << "licence_number:" << r -> next -> licence_number << endl;  			
		    cout << "汽车行程:" << r -> next -> distance << endl;
		    cout << "该车进厂时间:"; OUTPUT_TIME( r -> next -> in );
	        //cout << "预计出场时间:"; OUTPUT_TIME( r -> next -> out );
			flag = 1; break;    
		}			
	    r = r -> next; 
	}		
    if( flag == 0 ) cout << "很抱歉,该车不存在!!" << endl;
}
void OUTPUT_ALL() //查看所有车辆
{
    p1 p; p2 q; p3 r;
	p = rent; q = rented; r = repair;
	int i;
	cout << "I.rent available " << endl;
    i = 1;
	while( p -> next != NULL ) //待租的汽车
	{    
		cout << i << endl;
	    cout << "车牌号:" << p -> next -> licence_number << endl;  
	    cout << "行程:" << p -> next -> distance << endl;
	    p = p-> next;  i++;
	}
	cout << "II.rented" << endl; 
	i = 1;
    while( q -> next != NULL ) //已出租的车
	{
		cout << i << endl; i++;
	    OUTPUT2( q -> next );   
	    q = q -> next; 
	}
	cout << "III.in_repair" << endl;
	i = 1;
    while( r -> next != NULL )  //在维修的车
	{
		cout << i << endl; i++;
	    cout << "车牌号:" << r -> next -> licence_number << endl;  
	    cout << "行程:" << r -> next -> distance << endl;
        cout << "进厂时间:"; OUTPUT_TIME( r -> next -> in );
        //cout << "出场时间:"; OUTPUT_TIME( r -> next -> out ); 
        r = r -> next;  		
	}		
}
void OUTPUT_BF()
{
	

}
void MAKENULL( )
{
    rent = new RENT_AVAIBLE;  
    rent -> next = NULL;
    rented = new RENTED;
    rented -> next = NULL;
    repair = new IN_REPAIR;
    repair -> next = NULL;   	
}
/*---------从文档中读出所含信息-------------------*/
void OPENFILE( )
{
    FILE * Ptr;
	p1 new_point1; p2 new_point2; p3 new_point3;
	/*--I.可租用的车----------------------------------- */
    if( ( Ptr = fopen("car_garage1.text", "r" ) ) != NULL )
	{
		while( ! feof( Ptr) )
		{
			new_point1 = new RENT_AVAIBLE;
			fread( new_point1,  sizeof( RENT_AVAIBLE), 1, Ptr ); 
		    ADD1( new_point1 );
		}
		fclose( Ptr );
	}
    /*---II。已租出的车----------------------------------*/	
    if( ( Ptr = fopen( "car_garage2.text", "r")) != NULL )
	{
	    while( ! feof( Ptr ) )
		{
		    new_point2 = new RENTED;  
			fread( new_point2, sizeof( RENTED ), 1, Ptr);
            ADD2( new_point2 );  			
		}
        fclose( Ptr ); 		
	}  
	/*-----III.处于维修状态的车--------------------------*/
    if( ( Ptr = fopen( "car_garage3.text", "r")) != NULL )
	{
	    while( ! feof( Ptr ) )
		{
		    new_point3 = new IN_REPAIR;
			fread( new_point3, sizeof( IN_REPAIR), 1, Ptr ); 			
		    ADD3( new_point3 ); 
		}
        fclose( Ptr );		
	}		
}
/*-----------------储存所有信息--------------------------*/
void SAVE_ALL()
{
    FILE * Ptr;
	if( ( Ptr = fopen("car_garage1.text", "w") ) == NULL )
		cout << "Can not open the file!" << endl;
    else {
		while( rent -> next != NULL )
	    {
		    fwrite( rent -> next, sizeof( RENT_AVAIBLE ), 1, Ptr );   
		    rent = rent -> next; 
		}
	}	fclose( Ptr );
    if( ( Ptr = fopen("car_garage2.text", "w") ) == NULL )
		cout << "Can not open the file!" << endl;
    else {
		while( rented -> next != NULL )
	    {
		    fwrite( rented -> next, sizeof(RENTED ), 1, Ptr );   
		    rented = rented -> next; 
		}
	}	fclose( Ptr );
	if( ( Ptr = fopen("car_garage3.text", "w") ) == NULL )
		cout << "Can not open the file!" << endl;
    else {
		while( repair -> next != NULL )
	    {
		    fwrite( repair -> next, sizeof( IN_REPAIR ), 1, Ptr );   
		    repair = repair -> next; 
		}
	}	fclose( Ptr );
}
/*^================子函数部分=====================================^*/
/*v=================主函数=======================================v*/
int main()
{
	int choice, num, i;
	MAKENULL();
	show_menu();
	OPENFILE();
	while ( cin >> choice  )
	{
		system("cls");
		show_menu();
		cout << "===========================================" << endl;
		switch ( choice )
		{
			case 1:
				cout << "您选择的是引入新车,请填写相关信息:" << endl; //提示信息
				cout << "请输入您要添加的汽车数:";
				cin >> num;
                for( i = 0; i < num; i++)
				{   cout << "请输入第" << i + 1 << "辆车的信息";             
					ADD_CAR();	
				}					
				break;
			case 2:
				cout << "您选择的是租车操祝,请根据提示信息填写相关材料" << endl;
				RENT_CAR();			
				break;
			case 3:  
				BACK_CAR();				
				break;
			case 4:
                CHECK_CAR();				
                break;
			case 5:
				OUTPUT_ALL();
                break;
		    case 6:
                //OUTPUT_BF();
                break;
		    case 0: 
				SAVE_ALL();
				return 0;				
		}
		cout << "------------------------*操作成功*--------------------" << endl;
        cout <<"=====================欢迎继续使用=====================" << endl; 		
	}		
	return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -