1. 程式人生 > >C++模擬電梯執行(模擬電梯顯示屏)

C++模擬電梯執行(模擬電梯顯示屏)

#include <stdlib.h>
//system("Cls");
#include<iostream>
#include <ctime> 
using namespace std;

void menu1()
{
	cout << "1.          /\           " << endl;
	cout << "           //\\          " << endl;
	cout << "          //  \\         " << endl;
	cout << "         //    \\        " << endl;
	cout << "        //      \\       " << endl;
	cout << "       //   ||   \\      " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "                         " << endl;
	cout << "2.          ||           " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "       \\   ||   //      " << endl;
	cout << "        \\      //       " << endl;
	cout << "         \\    //        " << endl;
	cout << "          \\  //         " << endl;
	cout << "           \\//          " << endl;
	cout << "            \/           " << endl;
}

void menu2()
{
	cout << " 5.  || || ||     6.||     ||     ||      " << endl;
	cout << "   |||| || ||||     ||||   ||   ||||      " << endl;
	cout << " |||||| || ||||||   |||||| || ||||||      " << endl;
	cout << "   |||| || ||||     ||||   ||   ||||      " << endl;
	cout << "     || || ||       ||     ||     ||      " << endl;
	cout << "                                          " << endl;
	cout << "       =||               =====            " << endl;
	cout << "        ||              ==   ||           " << endl;
	cout << "        ||                ====            " << endl;
	cout << "        ||              ==   ||           " << endl;
	cout << "       ====              =====            " << endl;
	cout << "                                          " << endl;
	cout << "      =====                ==             " << endl;
	cout << "     ==   ||              ===             " << endl;
	cout << "          ==             = ==             " << endl;
	cout << "        ==              =  ==             " << endl;
	cout << "      ==               ========           " << endl;
	cout << "     =======               ==             " << endl;
}

void shang()
{
	cout << "            /\           " << endl;
	cout << "           //\\          " << endl;
	cout << "          //  \\         " << endl;
	cout << "         //    \\        " << endl;
	cout << "        //      \\       " << endl;
	cout << "       //   ||   \\      " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "                           " << endl;
}

void xia()
{
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "            ||           " << endl;
	cout << "       \\   ||   //      " << endl;
	cout << "        \\      //       " << endl;
	cout << "         \\    //        " << endl;
	cout << "          \\  //         " << endl;
	cout << "           \\//          " << endl;
	cout << "            \/           " << endl;
	cout << "                            " << endl;
}

void number1()
{
	cout << "           =||                 " << endl;
	cout << "            ||                 " << endl;
	cout << "            ||                 " << endl;
	cout << "            ||                 " << endl;
	cout << "           ====                " << endl;
	cout << "                               " << endl;
	cout << "                               " << endl;
	cout << "                               " << endl;
}

void number2()
{
	cout << "          =====               " << endl;
	cout << "         ==   ||              " << endl;
	cout << "              ==              " << endl;
	cout << "            ==                " << endl;
	cout << "          ==                  " << endl;
	cout << "         =======              " << endl;
	cout << "                               " << endl;
	cout << "                               " << endl;
	cout << "                               " << endl;
}

void number3()
{
	cout << "           =====            " << endl;
	cout << "          ==   ||           " << endl;
	cout << "            ====            " << endl;
	cout << "          ==   ||           " << endl;
	cout << "           =====            " << endl;
	cout << "                            " << endl;
	cout << "                            " << endl;
	cout << "                            " << endl;
}

void number4()
{
	cout << "            ==             " << endl;
	cout << "           ===             " << endl;
	cout << "          = ==             " << endl;
	cout << "         =  ==             " << endl;
	cout << "        ========           " << endl;
	cout << "            ==             " << endl;
	cout << "                            " << endl;
	cout << "                            " << endl;
	cout << "                            " << endl;
}


void   Delay(int   time)//time*1000為秒數 
{
	clock_t   now = clock();

	while (clock() - now   <   time);
}

//open
void open()
{
	Delay(1000);
	system("Cls");
	cout << "(門已開)" << endl;
}

//close
void close()
{
	Delay(1000);
	system("Cls");
	cout << "(門已關)" << endl;
}

//go up
int goup(int nowfloor,int nextfloor)
{
	for (nowfloor;nowfloor <= nextfloor; nowfloor++)
	{
		shang();
		switch (nowfloor)
		{
		case 1:
		{
			number1();
			Delay(2000);
			break;
		}
		case 2:
		{
			number2();
			Delay(2000);
			break;
		}
		case 3:
		{
			number3();
			Delay(2000);
			break;
		}
		case 4:
		{
			number4();
			Delay(2000);
			break;
		}
		}
	}
	nowfloor--;
	Delay(1000);
	open();
	return nowfloor;
}

//decline
int decline(int nowfloor, int nextfloor)
{
	for (nextfloor; nextfloor>=nowfloor; nextfloor--)
	{
		xia();
		switch (nextfloor)
		{
		case 1:
		{
			number1();
			Delay(2000);
			break;
		}
		case 2:
		{
			number2();
			Delay(2000);
			break;
		}
		case 3:
		{
			number3();
			Delay(2000);
			break;
		}
		case 4:
		{
			number4();
			Delay(2000);
			break;
		}
		}
	}
		nowfloor=nextfloor++;
		open();
		return nowfloor;
}
////////////////////////////////////////////
//電梯執行
void Do(int k,int nowfloor )
{
	if (k > nowfloor)
	{
		decline(nowfloor, k);
	}
	else if (k < nowfloor)
	{
		goup(nowfloor, k);
	}
	else
		cout << "電梯正在開啟" << endl;
}

//中間函式
void fun()
{
	int m;
	menu2();
	cout << "(保持開門或者關門):" << endl;
	cin >> m;
	if (m == 5)
	{
		open();
		fun();
	}
	else
	{
		close();
	}

}

void start(int nowfloor,int k)
{	
	int n;
	int nextfloor;
	menu1();
	cout<<"(輸入 1.上 或者 2.下):"<<endl;
	cin >> n;
	cout<<"(電梯執行中)"<<endl;
	Delay(500);
		switch (n)
	{
		case 1:
		{
			Do(k, nowfloor);
			Delay(1500);
			fun();
			menu2();
			cout << "(輸入要到的樓層):" << endl;
			cin >> nextfloor;
			cout<<"(電梯執行中)"<<endl;
			goup(nowfloor, nextfloor);
			break;
		}
		case 2:
		{
			Do(k, nowfloor);
			Delay(1500);
			fun();
			menu2();
			cout << "(輸入要到的樓層):" << endl;
			cin >> nextfloor;
			cout<<"電梯執行中"<<endl;
			decline(nowfloor, nextfloor);
		}
	}
	start(nowfloor, k);
}


////////////////////////////////////////////////////
int main()
{
	int nowfloor;
	int k;
	cout << "現在樓層(1-4):";
	cin >> nowfloor;
	cout << "電梯所在樓層(1-4):";
	cin >> k;
	start(nowfloor, k);
	return 0;
}