一个C++程序作业,模拟电梯运行 C++程序:模拟三部电梯运行过程

\u6c42\u6559\u7528C/C++\u7f16\u7a0b\u7b80\u5355\u6a21\u62df\u7535\u68af\u8fd0\u884c

/**Elevator.h*ElevatorAssignment**CreatedbyIsaacHuangon08-6-23.*Copyright2008__MyCompanyName__.Allrightsreserved.**/classElevator{ :Elevator();voidChangeFloor(int);intGetFloor();voidWorking();};/**Elevator.cpp*ElevatorAssignment**CreatedbyIsaacHuangon08-6-23.*Copyright2008__MyCompanyName__.Allrightsreserved.**/#include"Elevator.h"#includeElevator::Elevator():WhichFloor(1){}voidElevator::ChangeFloor(intnum){cout#include#includeBuilding::Building():Floor1(1),Floor2(2),WorkTime(0){}voidBuilding::Running(intOrderTime){inti,iLater1=0,iLater2=0,Spent;for(i=1;i<=OrderTime;i+=Spent){Spent=1;srand(time(0));intNumber1=5+rand()%16;if((i-iLater1)==Number1){Floor1.CreatePeople();iLater1=i;}srand(time(0));intNumber2=5+rand()%16;if((i-iLater2)==Number2){Floor2.CreatePeople();iLater2=i;}if(Eleva.GetFloor()==1){if(Floor1.ReturnPeople()!=0){Floor1.GetInside();Eleva.ChangeFloor(2);WorkTime++;Spent=5;Print(i+Spent);}elseif(Floor2.ReturnPeople()!=0){Eleva.ChangeFloor(2);Floor2.GetInside();Eleva.ChangeFloor(1);WorkTime++;Spent=10;Print(i+Spent);}}elseif(Eleva.GetFloor()==2){if(Floor2.ReturnPeople()!=0){Floor2.GetInside();Eleva.ChangeFloor(1);WorkTime++;Spent=5;Print(i+Spent);}elseif(Floor1.ReturnPeople()!=0){Eleva.ChangeFloor(1);Floor1.GetInside();Eleva.ChangeFloor(2);WorkTime++;Spent=10;Print(i+Spent);}}}//Print(OrderTime);}voidBuilding::Print(intTime){cout<<"WorkTime:"<<Time<<"s"<<endl;cout<<WorkTime<<"PeopleHaveBeenTransported."<<endl;cout<<"TheNumberOfPeopleOnThe1stFloor:"<<Floor1.ReturnPeople()<<endl;cout<<"TheNumberOfPeopleOnThe2ndFloor:"<<Floor2.ReturnPeople()<<endl;}/**Floor.h*ElevatorAssignment**CreatedbyIsaacHuangon08-6-23.**/classFloor{ :Floor(int);voidCreatePeople();voidGetInside();intReturnNumber();intReturnPeople();};/**Floor.cpp*ElevatorAssignment**CreatedbyIsaacHuangon08-6-23.**/#include"Floor.h"Floor::Floor(intnum):FloorNumber(num){if(FloorNumber==1)PeopleOnThisFloor=100;elsePeopleOnThisFloor=100;}voidFloor::CreatePeople(){PeopleOnThisFloor++;}intFloor::ReturnNumber(){returnFloorNumber;}voidFloor::GetInside(){PeopleOnThisFloor--;}intFloor::ReturnPeople(){returnPeopleOnThisFloor;}#include"Building.h"intmain(){BuildingSample;Sample.Running(3600);return0;}

\u524d\u51e0\u5929\u4e4b\u524d\u505a\u7684 \u81ea\u5df1\u518d\u52a0\u4e0a\u6ee1\u8f7d\u5224\u65ad\u5c31\u884c\u4e86
#include
class lifta{
private:
int lift;
public:
void intit(int lift1)
{lift=lift1;}
void L1(int go);
void L2(int go);
void L3(int go);
};
void lifta::L1(int go)
{if(lift==go||go33) return;
else if(go>lift)
{cout<<"\nStarting at floor"<<lift<<endl;
while(go>lift)
{lift++;
cout<<"Going up now at floor"<<lift<<endl;
}
cout<<"Stopping at floor"<<lift<<endl;
}
else
{cout<<"\nStarting at floor"<<lift<<endl;
while(go<lift)
{lift--;
cout<<"Going up now at floor"<<lift<<endl;
}
cout<<"Stopping at floor"<<lift<<endl;
}
return;
}
void lifta::L2(int go)
{if(lift==go||go33||go%2==0) return;
else if(go>lift)
{cout<<"\nStarting at floor"<<lift<<endl;
while(go>lift)
{lift=lift+2;
cout<<"Going up now at floor"<<lift<<endl;
}
cout<<"Stopping at floor"<<lift<<endl;
}
else
{cout<<"\nStarting at floor"<<lift<<endl;
while(go<lift)
{lift=lift-2;
cout<<"Going up now at floor"<<lift<<endl;
}
cout<<"Stopping at floor"<<lift<<endl;
}
return;
}
void lifta::L3(int go)
{if(go!=-1&&(lift==go||go33||((go>-1&&go12)))) return ;
else if(go>lift)
{cout<<"\nStarting at floor"<<lift<<endl;
while(go>lift)
{if(lift==8||lift==4)
{lift=lift+4;cout<<"Going up now at floor"<<lift<<endl;}
else if(lift==-1)
{lift=lift+5;cout<<"Going up now at floor"<<lift<<endl;}
else
{lift=lift+2;cout<<"Going up now at floor"<<lift<<endl;}
}cout<<"Stopping at floor"<<lift<<endl;}
else
{cout<<"\nStarting at floor"<<lift<<endl;
while(go<lift)
{if(lift==8||lift==12)
{lift=lift-4;cout<<"Going up now at floor"<<lift<<endl;}
else if(lift==4)
{lift=lift-5;cout<<"Going up now at floor"<<lift<<endl;}
else {lift=lift-2;cout<<"Going up now at floor"<<lift<<endl;}
}cout<<"Stopping at floor"<<lift<<endl;}
return;
}
int main()
{lifta obj1,obj2,obj3;
int go,which,l1,l2,l3;char q;
cout<<"--------chu shi hua!---------"<<endl;
cout>l1;
cout>l2;
cout>l3;
obj1.intit(l1);
obj2.intit(l2);
obj3.intit(l3);
for(;;)
{ do{cout<<"input which lift you choose!(1/2/3)"<<endl;
cin>>which;
}while(which3);
cout<<"which floor are you go?"<<endl;
cin>>go;
if(which==1) obj1.L1(go);
else if(which==2)obj2.L2(go);
else if(which==3)obj3.L3(go);
cout<<"are you continue?(y/n)"<<endl;
cin>>q;
if(q=='n') break;
}}

为了简单,分配了1到9层.另外也没加入有几人同时按,不过那样也不难,再分配10个每层的变量,随机按.再处理,是先向上还是直接向下.不过那倒真的可以用到真的上了.

#include <stdio.h>
#include <dos.h>
#include <ctype.h>
#define ON 1
#define OFF 0
main(){

int power,i;
char oldkey,key ;
struct date today;
struct time now;
getdate(&today); /*把系统当前日期存入today所指向的date结构中*/
gettime(&now);
gotoxy(25,3);
printf("Today's date is %d-%d-%d\n",today.da_year,today.da_mon,today.da_day);
gotoxy(25,6);
printf("Current time is %02d:%02d:%02d\n",now.ti_hour,now.ti_min,now.ti_sec);

if( now.ti_hour>=8||(now.ti_hour<=23&&now.ti_min<59)){
power=ON;
printf("\n\n\nLift power is on.Lift stop in 1 floor.");

}
else {
power=OFF;
printf("Lift power is off.");
}
oldkey=0;

while(power==ON) {

printf("\n\n\nFloor");
for(i=1;i<10;i++)
printf("\n %d",i);
printf("\nPress floor number or q to shut down power :");

if( now.ti_hour>=8||(now.ti_hour<=23&&now.ti_min<59))
power=ON;
else
power=OFF;

key=bioskey(0);
key=key&0x00ff;

system("cls");
if(key=='0'){

power=OFF;
printf("\n\n Lift Power off");
sleep(1);
break;
}
if(key>oldkey)
printf(" %c Go up!...",24);
if(key<oldkey)
printf(" %c Go down!...",25);
if(key==oldkey)
printf(" %c ok you are here",22);
sleep(2);
system("cls");
printf("\n\n ");

switch(key){
case '1':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '2':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '3':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '4':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '5':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '6':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '7':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '8':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;
case '9':
if(key==oldkey)
printf(" %c ok you are here",22);
else if(key>oldkey)

printf("%c Go up!...stop in %c floor.",24,key);
else
printf("%c Go down!...stop in %c floor.",25,key);
oldkey=key;
break;

}
}
getch();
}

/*
* Elevator.h
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/

class Elevator
{
private:
int WhichFloor;
public:
Elevator();
void ChangeFloor(int);
int GetFloor();
void Working();
};
/*
* Elevator.cpp
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*
*/

#include "Elevator.h"
#include <iostream.h>
Elevator::Elevator():WhichFloor(1){}
void Elevator::ChangeFloor(int num)
{
cout<<"Elevator Goes From "<<WhichFloor<<" To "<<num<<endl;
WhichFloor=num;
}
int Elevator::GetFloor()
{
return WhichFloor;
}
/*
* Building.h
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
*
*/
#include "Floor.h"
#include "Elevator.h"
class Building
{
private:
Floor Floor1;
Floor Floor2;
Elevator Eleva;
int WorkTime;
public:
Building();
void Running(int);
void Print(int);
};
/*
* Building.cpp
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
*
*/

#include "Building.h"
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
Building::Building():Floor1(1),Floor2(2),WorkTime(0){}
void Building::Running(int OrderTime)
{
int i,iLater1=0,iLater2=0,Spent;
for(i=1;i<=OrderTime;i+=Spent)
{
Spent=1;
srand(time(0));
int Number1=5+rand()%16;
if((i-iLater1)==Number1)
{
Floor1.CreatePeople();
iLater1=i;
}
srand(time(0));
int Number2=5+rand()%16;
if((i-iLater2)==Number2)
{
Floor2.CreatePeople();
iLater2=i;
}
if(Eleva.GetFloor()==1)
{
if(Floor1.ReturnPeople()!=0)
{
Floor1.GetInside();
Eleva.ChangeFloor(2);
WorkTime++;
Spent=5;
Print(i+Spent);

}
else if(Floor2.ReturnPeople()!=0)
{
Eleva.ChangeFloor(2);
Floor2.GetInside();
Eleva.ChangeFloor(1);
WorkTime++;
Spent=10;
Print(i+Spent);
}
}
else if(Eleva.GetFloor()==2)
{
if(Floor2.ReturnPeople()!=0)
{
Floor2.GetInside();
Eleva.ChangeFloor(1);
WorkTime++;
Spent=5;
Print(i+Spent);
}
else if(Floor1.ReturnPeople()!=0)
{
Eleva.ChangeFloor(1);
Floor1.GetInside();
Eleva.ChangeFloor(2);
WorkTime++;
Spent=10;
Print(i+Spent);
}
}
}

//Print(OrderTime);
}

void Building::Print(int Time)
{
cout<<"Work Time: "<<Time<<"s"<<endl;
cout<<WorkTime<<" People Have Been Transported."<<endl;
cout<<"The Number Of People On The 1st Floor: "<<Floor1.ReturnPeople()<<endl;
cout<<"The Number Of People On The 2nd Floor: "<<Floor2.ReturnPeople()<<endl;
}
/*
* Floor.h
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
*
*/

class Floor
{
private:
int FloorNumber;
int PeopleOnThisFloor;
public:
Floor(int);
void CreatePeople();
void GetInside();
int ReturnNumber();
int ReturnPeople();
};
/*
* Floor.cpp
* ElevatorAssignment
*
* Created by Isaac Huang on 08-6-23.
*
*/

#include "Floor.h"

Floor::Floor(int num):FloorNumber(num)
{
if(FloorNumber==1)
PeopleOnThisFloor=100;
else
PeopleOnThisFloor=100;
}
void Floor::CreatePeople()
{
PeopleOnThisFloor++;
}
int Floor::ReturnNumber()
{
return FloorNumber;
}
void Floor::GetInside()
{
PeopleOnThisFloor--;
}
int Floor::ReturnPeople()
{
return PeopleOnThisFloor;
}

#include "Building.h"
int main()
{
Building Sample;
Sample.Running(3600);

return 0;
}

我估计你在开玩笑

  • 涓涓狢++绋嬪簭浣滀笟,妯℃嫙鐢垫杩愯
    绛旓細printf("%c Go up!...stop in %c floor.",24,key);else printf("%c Go down!...stop in %c floor.",25,key);oldkey=key;
  • 姹妯℃嫙10灞傜殑鐢垫鎺у埗绋嬪簭(C/C++)鍙互杩愯鐨
    绛旓細printf(" %c ok you are here",22);else if(key>oldkey)printf("%c Go up!...stop in %c floor.",24,key);else
  • 鎬ユ眰鐢c璇█瀹炵幇鐢垫鎺у埗
    绛旓細褰鐢垫杩愯鑷虫煇灞傛湁鎸囦护鍙戝嚭鏃讹紟鎸囩ず浣嶇疆鍙婃寚浠ゃ備互浜屽眰涓轰緥锛3锛2鐢垫涓嬭绋嬪簭璁捐 浠ョ數姊湪涓夊眰涓嬭鎯呭喌涓轰緥銆傚綋鐢垫鐨勪竴鎴栦簩灞傛湁鎸囦护鏃讹紝灏嗕笁灞備笅琛屼綅缃1锛鍚屾椂鏃犱笂琛岋紝椹卞姩鐢垫涓嬭銆傜▼搴忚鏄庡涓嬶細3锛3鐢垫涓婅绋嬪簭璁捐 浠ョ數姊湪浜屽眰涓婅鎯呭喌涓轰緥銆傜▼搴忚鏄庡涓嬶細3锛4鐢垫鍒拌揪鏃剁▼搴忚璁 鐢垫鍒拌揪...
  • 鎬ユ眰!鐢C鎴栬匔++绋嬪簭妯℃嫙鎿嶄綔绯荤粺鐨鐢垫绠楁硶
    绛旓細鎴戜滑绉佷笅浜ゆ祦鍚
  • 涓轰粈涔坢bolck鍦ㄧ嚎缂栫▼鍜岀绾跨紪绋嬩細浣縧ed鐏寒搴︿笉涓鏍
    绛旓細鍥3-1鐪嬭绯荤粺涓昏鐢变袱閮ㄧ粍鍏鐢垫鎺у埗閫昏緫閮ㄧ敱PLC瀹炵幇閫氭瀽鐮旂┒鐢垫瀹為檯杩愯鎯呭喌鎺у埗瑙勫緥璁捐鍙戠數姊仈鎺у埗绋嬪簭浣縋LC鑳藉鎺у埗鐢垫杩愯鎿嶄綔鐢垫璋冮熼儴鍒欓夌敤楂樻ц兘鐭㈤噺鎺у埗鍙橀鍣ㄩ厤鑴夊啿鍙戝櫒(缂栫爜鍣)娴嬮噺榧犵寮忔嫿寮曠數鏈鸿浆閫熷鐢垫満闂幆鐭㈤噺鎺у埗绯荤粺瀹炵幇榧犵寮忔嫿寮曟満鐢垫満浜ゆ祦鍙橀璋冮(Variable Voltage Variable Frequency,绠绉...
  • 鐗圭璁惧瀹夎鏀归犵淮淇鍙瘉鐢垫鎬庢牱浠C绾у崌鍒癇绾
    绛旓細6銆杩5骞寸疮璁″畨瑁呯敵璇疯寖鍥村唴鐨勭壒绉嶈澶囨暟閲忚嚦灏戜负锛鐢垫80鍙板锛涜捣閲嶆満姊40鍙板锛涘杩愮储閬撴垨澶у瀷娓镐箰璁炬柦12鍙板銆傚叾瀹冩寜锛歍SG Z0004-2007銆婄壒绉嶈澶囧埗閫犮佸畨瑁呫佹敼閫犮佺淮淇川閲忎繚璇佷綋绯诲熀鏈姹傘婽SG Z0005-2007銆婄壒绉嶈澶囧埗閫犮佸畨瑁呫佹敼閫犮佺淮淇鍙壌瀹氳瘎瀹$粏鍒欍嬪姙鐞绋嬪簭璺C绾т竴鏍 http://zhidao....
  • 褰涓涓浜哄拰浣犺鎴戝浣犲嵃璞℃繁鍒讳綘鎬庝箞鍥炵瓟浠?
    绛旓細C. 璧炵編鍙互涓嶅崟绾嫎娉ヤ簬鈥璇█鈥溿傚湪琛ㄨ揪瀵瑰鏂硅禐缇庣殑鏃跺,杩樺彲浠ラ『甯﹂檮璧涓涓灏忕ぜ鐗┿傜ぜ鐗╀笉鐢ㄧ壒鍒吹閲,浣嗚绮惧阀缁嗚嚧銆佹湁鍝佷綅銆 D. 璧炵編鐨勫満鍚堝緢閲嶈銆傛瘮濡,鍚戝じ璧炰竴涓汉鐨勬椂鍊,鑳岀潃褰撲簨浜虹殑闈㈠じ浼氭瘮褰撶潃闈㈠じ鏇存湁鏁堟灉;鍏紑鍦哄悎瀵圭潃澶氫釜浜哄じ,浼氭瘮瀵圭潃鍗曚釜浜哄じ,鏇磋兘璁╁綋浜嬩汉鎰熻鍒拌嚜璞 F. 鏄庤船鏆楄鐨...
  • 姹妯℃嫙10灞傜殑鍙互杩愯鐨鐢垫鎺у埗绋嬪簭(C/C++)
    绛旓細鐜板湪鍐欏ソ浜,涔熻皟璇曚簡,鑷垜鎰熻寰堝ソ,瑙夊緱鍙互绉绘鍒扮湡鐢垫涓婂幓浜嗗摝.鍝堝搱.涓轰簡绠鍗,鍒嗛厤浜1鍒9灞.鍙﹀涔熸病鍔犲叆鏈夊嚑浜哄悓鏃舵寜,涓嶈繃閭f牱涔熶笉闅,鍐嶅垎閰10涓瘡灞傜殑鍙橀噺,闅忔満鎸.鍐嶅鐞,鏄厛鍚戜笂杩樻槸鐩存帴鍚戜笅.涓嶈繃閭e掔湡鐨勫彲浠ョ敤鍒扮湡鐨勪笂浜.include <stdio.h> include <dos.h> include <ctype.h> defi...
  • 扩展阅读:考电梯证模拟考试软件 ... 电梯考试题库免费 ... 扫一扫题目出答案 ... skyscraper电梯模拟下载 ... c++搜题专用软件 ... 让我们的电梯下载安装 ... 大学搜题酱下载安装 ... 电梯模拟游戏 ... 手机仿真电路模拟器 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网