?? main.cpp
字號:
#include <iostream>
#include<windows.h>
#include<time.h>
#include<conio.h> //用于getch()函數的調用
using namespace std;
class Time //time類的定義
{
public:
void waittime(); //電梯上升或下降等待時間
void timer(); //演示停頓時間
int m;
};
void Time::waittime()
{
int m=5;
while(m)
{
cout<<m;
Sleep(1000);
m--;
cout<<'\b';
}
}
void Time::timer()
{
Sleep(1000); //停頓1秒
}
class Person //persom類的定義
{
public:
void persons(); //產生一個人隨機到達一層或二層
void in(); //人進入電梯
void out(); //人走出電梯
int WhichOnStorey(); //輸出所在樓層
int WhichGoStorey(); //輸出要去樓層
private:
int OnStorey;//
int GoStorey; //
int PersonNum; // 編號
static int Num; // 已經產生的總人數
Time y;
};
int Person::Num=0;
void Person::persons()
{
srand( (unsigned)time( NULL ) );
OnStorey = rand()%2 + 1; //隨機產生到一層或二層的人
if (OnStorey==1) GoStorey = 2;
else GoStorey = 1;
PersonNum = ++Num;
cout << "一個人到來,編號:" << PersonNum << ",他將會在" << WhichOnStorey() << "層守候電梯,然后去" << WhichGoStorey() << "層。" <<endl;
y.timer();
}
inline int Person::WhichOnStorey()
{
return OnStorey;
}
inline int Person::WhichGoStorey()
{
return GoStorey;
}
inline void Person::in()
{
cout<<"此人進入電梯"<<endl;
y.timer();
}
inline void Person::out()
{
cout<<"此人走出電梯"<<endl;
y.timer();
}
class Floor //Floor類的定義
{
public:
Floor(){state=1;LeftTime=0;} //開始時,電梯停在1樓
void Up1to2(); //電梯上升
void Down2to1(); //電梯下降
void light(); //電梯中燈亮
void bell(); //鈴響
void Stop(); //電梯停止
void open(); //打開門
void close(); //關閉門
int onstate(); //輸出電梯所在樓層
private:
int state;
int LeftTime; // 剩余到達時間
Time x;
};
void Floor::Up1to2()
{
cout<<"電梯正在向2樓出發"<<endl;
state++;
x.waittime();
}
void Floor::Down2to1()
{
cout<<"電梯正在向1樓出發"<<endl;
state--;
x.waittime();
}
void Floor::light()
{
cout<<"電梯里的電燈亮了"<<endl;
x.timer();
}
void Floor::bell()
{
cout<<"發出鈴聲"<<endl;
x.timer();
}
void Floor::Stop()
{
cout<<"電梯停止,到達目的樓層"<<state<<endl;
x.timer();
}
inline int Floor::onstate()
{
return state;
}
inline void Floor::open()
{
cout<<"電梯門打開"<<endl;
x.timer();
}
inline void Floor::close()
{
cout<<"電梯門關閉"<<endl;
x.timer();
}
class Button //button類的定義
{
public:
Button(){buttons=0;}
void button1(); //按下按鈕1
void button2(); //按下按鈕2
void button0(); //按鈕歸零
private:
int buttons;
Time z;
};
inline void Button::button1()
{
cout<<"按下1層的按鈕"<<endl;
buttons=1;
z.timer();
}
inline void Button::button2()
{
cout<<"按下2層的按鈕"<<endl;
buttons=2;
z.timer();
}
inline void Button::button0()
{
buttons=0;
cout<<"電梯按鈕復位"<<endl;
}
class Elevator // elevator類的定義
{
public:
void elevators();
private:
int GoStorey;
int state;
int OnStorey;
int PersonNum; // 編號
static int Num; // 已經產生的總人數
Person m1;
Button m2;
Floor m3;
Time m4;
};
void Elevator::elevators()
{
m1.persons();
if(m1.WhichOnStorey()==1)
m2.button2();
else
m2.button1();
while (m1.WhichGoStorey()==m3.onstate())
{
while (m3.onstate()==1)
{
cout<<"電梯還在一樓,請等待..."<<endl;
m4.timer();
m3.Up1to2();
}
break;
if(m3.onstate()==2)
cout<<"電梯還在二樓,請等待..."<<endl;
m4.timer();
m3.Down2to1();
}
if(m1.WhichOnStorey()==1)
{
m3.open();
m1.in();
m3.Up1to2();
}
else
{
m3.open();
m1.in();
m3.Down2to1();
}
m3.light();
m3.bell();
m3.Stop();
m1.out();
m3.close();
}
void mulu() //界面函數
{
Elevator s1;
void impress();
system("cls");
system("color 2");
cout<<endl;
cout <<"\t 學生姓名: 文茂齊"<<" 指導教師:譚平"<<endl<<endl;
cout<<"\t ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇" <<endl
<<"\t ◇ ◇" <<endl
<<"\t ◇ 電梯系統模擬程序 ◇" <<endl
<<"\t ◇ ◇" <<endl
<<"\t ◇ 1. 程序功能描述 ◇" <<endl
<<"\t ◇ ◇" <<endl
<<"\t ◇ 2. 程序功能演示 ◇" <<endl
<<"\t ◇ ◇" <<endl
<<"\t ◇ 3. 退出電梯系統 ◇" <<endl
<<"\t ◇ ◇" <<endl
<<"\t ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇" <<endl <<endl <<"\t\t";
int n;
cin>>n;
switch (n)
{
case 1:
impress();
break;
case 2:
system("cls");
int a;
s1.elevators();
cout<<"演示結束"<<endl;
cout<<"是否還要進行其他操作?1.是;2.否"<<endl;
cin>>a;
if(a==1)
{void mulu();mulu();}
break;
case 3:
cout << endl << "\t\t謝謝使用!!!" << endl << endl << "\t\t";
exit(0);
break;
default:
break;
}
}
void impress() //功能描述函數
{
system("cls");
cout<<" 功能描述 "<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"程序自動生成一個人隨機來到電梯的一層或二層"<<endl
<<"如果他在二層要去一層而電梯在一層,則電梯自己上升到二層接他,反之亦然"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"任意鍵返回"<<endl;
getch();
void mulu();
mulu();
}
int main() //主函數
{
void mulu();
mulu();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -