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

📄 okc9.cpp

📁 设计一个易于理解的界面
💻 CPP
字号:

 #include <iostream.h>
 #include <malloc.h>
 #include <stdio.h> 
 #define max 10
 #define  n 4
 void operation();
 typedef struct{
	int GoingUp,GoingDown;
} wait;
       class dianti {
         private :
			// int ran,value;
			 int toopen[5];
			 int noindianti,flag,id,pos;
           public:
        int  countup(int floor,int &sta);
		int countdown(int floor,int &sta);
		int open(wait m[5]);
		int inandout(wait m[5]);
		int pressbutton();
        void thestatusup(int floor,int sta);
        void thestatusdown(int floor,int sta);
		void testopen(wait m[5]);
           // int random () {
           //    ++ran;
		//	   value=(ran %3);
			//   return value;
		//	}
			void create () {
			  //ran=0;
              noindianti=0;
			  flag=0;
			  for (int i=0;i<=n;i++) {
              toopen[i]=0; }
			  pos=0;};
			  void printid() {cout<<id;};
              void printfpos() {cout<<pos;};
              int theid(int m) {
               id=m;
			   return id;}; }; 
       void  choliftup(dianti &a,dianti &b,dianti &c,int floor); 
	   void  choliftdown(dianti &a,dianti &b,dianti &c,int floor);
       void oper(dianti &a,dianti &b,dianti &c,wait m[5]);
      
		int dianti::inandout(wait m[5]) {
			int num,leave=0,count=0;
			if (noindianti>0) {
							do {
            cout<<"你们有多少人想在"<<pos<<"楼离开"<<id<<"号电梯?\n";
			cin>>num;
				} while ((num<0)||(num>noindianti));
			noindianti=noindianti-num;
			};
			
		if ((flag==1)||(flag==2)) {
           testopen(m);
		};

		//离开
			if (pos==n) {
				if (flag==1) {flag=0;m[pos].GoingUp=0;};
			};
            if (pos==0) {
				if (flag==2) {flag=0;m[pos].GoingDown=0;};
			};
			if (flag==0) {
				do{
				cout<<"上请按1,下请按2,不用请按0\n";
				cout<<"如果你在"<<n<<"楼请不要按1;如果你在0楼请不要按2\n";
				cin>>flag;
				if ((flag==1)&&(pos==n)) {
                     flag=3;
					 cout<<"Input Error!"<<n<<"是最顶层!\n";
				}else if ((flag==2)&&(pos==0)) {
                      flag=3;
					  cout<<"Input Error!0是最低层!\n";
				}

				} while ((flag<0)||(flag>2));
				if (flag==0) {m[pos].GoingUp=0;m[pos].GoingDown=0;return 0;};
			};
				
              
			

		 if (flag==1) {
		     cout<<"本升降机是上去的!\n";
			 do {
             cout<<"你们有多少人想在"<<pos<<"楼进入"<<id<<"号电梯?\n"; 
			 cin>>num;
			 } while (num<0);
              noindianti=noindianti+num;
			  while (noindianti>max) {
				  cout<<"超载!"<<endl;
				  cout<<"请问你们有多少人想离开"<<endl;
				  cin>>leave;
				  
			  };
              noindianti=noindianti-leave;
			  m[pos].GoingUp=0;
			} else if (flag==2) {
		     cout<<"本升降机是下去的!\n";
			 do {

             cout<<"你们有多少人想在"<<pos<<"楼进入"<<id<<"号电梯?\n"; 
			 cin>>num;
			 } while (num<0);
              noindianti=noindianti+num;
			  while (noindianti>max) {
				  cout<<"超载!"<<endl;
				  cout<<"请问你们有多少人想离开"<<endl;
				  cin>>leave;
				  
			  };
                noindianti=noindianti-leave;
			  m[pos].GoingDown=0;
			} 
			 return 1;
			 

		}

	void main(){//OK
	int bo;
	
	cout<<"电梯管理人员你要启动本大厦所有的电梯吗?"<<endl;
	cout<<"启动请按1,不开启请按0"<<endl;
	cin>>bo;
	if (bo) {
		cout<<"*******************************\n";
		cout<<"等电梯时,上楼请按1,下楼请按2;\n";
		cout<<"查看3台电梯的位置按3,运行按4,离开按e\n";
        cout<<"*******************************\n";
    operation();
};
}
	//ok
void operation() {
			wait *m;
			int floor;
			char choose;
             m=new(wait[n+1]);
			dianti a,b,c;
			a.theid(1);
			b.theid(2);
			c.theid(3);
			a.create();
			b.create();
			c.create();
			for (int i=0;i<=n;i++) {
                m[i].GoingUp=0;
				m[i].GoingDown=0;
			}
			do {
			 cout<<"***************************************\n";
             cout<<"***    上-1;下-2;电梯位置-3;      ***\n";
			 cout<<"***    运行-4;关闭所有电梯请按e     ***"<<endl;
             cout<<"***************************************\n";
             cin>>choose;
			 if (choose=='1'||choose=='2') {
				 do {
                 cout<<"请问你/你们现在在第几层?";
				 cin>>floor;
				 } while((floor<0)||(floor>n)); 
                   if (choose=='1') {
					   
					   if (floor!=n) {
						   if (!m[floor].GoingUp) {
                       choliftup(a,b,c,floor);
						   };
						   m[floor].GoingUp=1;
				   } else {
				   cout<<n<<"楼是最顶层不可以上去!"<<endl;
				   };
				   }
				   else if (choose=='2') {
                        if (floor!=0) {
						   if (!m[floor].GoingDown) {
                       choliftdown(a,b,c,floor);
						   };
						   m[floor].GoingDown=1;
				   } else {
				   cout<<"0楼是最底层不可以下去!"<<endl;
				   };
			 }}
		else if (choose=='3') {
               a.printfpos();
			   cout<<"楼.\n";
               b.printid();
			   cout<<"号电梯在第";
			   b.printfpos();
			   cout<<"楼.\n";
               c.printid();
			   cout<<"号电梯在第";
			   c.printfpos();
			   cout<<"楼.\n";

		} else if (choose=='4')  {
               oper(a,b,c,m); 
			   a.printid();
			   cout<<"号电梯在第";
			   a.printfpos();
			   cout<<"楼.\n";
               b.printid();
			   cout<<"号电梯在第";
			   b.printfpos();
			   cout<<"楼.\n";
               c.printid();
			   cout<<"号电梯在第";
			   c.printfpos();
			   cout<<"楼.\n";
			   
		} 
		else if (choose=='e') {break;};
			} while(choose!='e');

		}
//定义的状态
		void dianti::thestatusup(int floor,int sta) {
              toopen[floor]=sta;
			  if ((sta==2)||(sta==1)) {
			  flag=1;} 
			  else if (sta==3) {
				  flag=2;}
		}
       void  choliftup(dianti &a,dianti &b,dianti &c,int floor) {
			int x,y,z,min;
            int sta1,sta2,sta3;
			x=a.countup(floor,sta1);
			y=b.countup(floor,sta2);
			z=c.countup(floor,sta3);
		    min=x;
					if (min>y) {min=y;}
					if (min>z) { min=z;};
				if (min==x) {
				a.thestatusup(floor,sta1);
				}
                   else if (min==y) {
				b.thestatusup(floor,sta2);
				   }  else {
					   c.thestatusup(floor,sta3);
}}
	   
int  dianti::countup(int floor,int &sta) {
			int min,maximum;
			//if (pos==floor) return 0;
			min=maximum=pos;
			int count=0;
			int num=0;
              for (int j=pos+1;j<=n;j++) {
					if (toopen[j]>0) {
						maximum=j;};}
               for (j=pos-1;j>=0;j--) {
					if (toopen[j]>0) {
						min=j;};}
			if ((!flag)&&(pos<=floor)) {
				sta=1;
                 num=floor-pos;}
			else  if ((!flag)&&(pos>floor)) {
                        sta=3;
						num=pos-floor;	}
			else if ((flag==1)&&(pos<=floor)) {
               sta=1;
			   num=floor-pos;}
			else if ((flag==1)&&(pos>floor)) {
				sta=2;
				num=maximum-pos+maximum-min+floor;
			} else if ((flag==2)&&(pos<=floor)) {
				sta=3;
					num=pos-min+floor;
			} else if ((flag==2)&&(pos>floor)) {
				sta=3;
				num=pos-min+floor-min;}
			return num;
	}
	int  dianti::countdown (int floor,int &sta) {
			int min,maxi,num;
		//	if (pos==floor)  return 0;
			    min=maxi=pos;
			    num=0;
               for (int j=pos+1;j<=n;j++) {
					if (toopen[j]>0) {
						maxi=j;};};
               for (j=pos-1;j>=0;j--) {
					if (toopen[j]>0) {
						min=j;};};
			 if ((!flag)&&(pos>=floor)) {
				sta=1;
                 num=pos-floor; }
			else  if ((!flag)&&(pos<floor)) {
                        sta=5;
						num=floor-pos;	}
             if ((flag==2)&&(pos>=floor)) {
				sta=1;
				num=pos-floor;} 
			 if ((flag==2)&&(pos<floor)) {
                 sta=4;
	             num=pos-min+maxi-min+maxi-floor;
			} else if ((flag==1)&&(pos>=floor)) {
				sta=5;
             num=maxi-pos+maxi-floor;
			} else if ((flag==1)&&(pos<floor)) {
				sta=5;
				num=maxi-pos+maxi-floor;
			};return num;		
	}
         void dianti::thestatusdown(int floor,int sta) {
              toopen[floor]=sta;
			  if ((sta==4)||(sta==1)) {
			  flag=2;
			  } else if (sta==5) {
				  flag=1;};
}
         void  choliftdown(dianti &a,dianti &b,dianti &c,int floor) {
			int x,y,z,min,sta1,sta2,sta3;
			x=a.countdown(floor,sta1);
			y=b.countdown(floor,sta2);
			z=c.countdown(floor,sta3);
	         	min=x;
			if (min>y) min=y;
			if (min>z) min=z;
			
				if (min==x) {
				a.thestatusdown(floor,sta1);
				}
                   else if (min==y) {
				b.thestatusdown(floor,sta2);
				   }  else {
					   c.thestatusdown(floor,sta3);
				   };}	
	//电梯门开
		
		void dianti::testopen(wait m[5]) {
				int count=0;
				int i=0,tr=0;
			//flag是1的状态,什么电梯需要开门!
				if(flag==1){
					if (m[pos].GoingUp==1) {
                          tr=1;	}
	             for (i=(pos+1);i<=n;i++) {
                	if (toopen[i]) {tr=1;};};
				 if (!tr) {
						for (i=pos-1;i>=0;i--) {
							if (toopen[i]>0) {
								tr=1;
							if (toopen[i]==2) {
									flag=2;
									tr=1;
									toopen[i]=3;
								} else if (toopen[i]==5) {
									toopen[i]=1;
									flag=2;
									tr=1;
								};	};};};	}
                    if(flag==2){
	             	if (m[pos].GoingDown==1) {
                          tr=1;	}
                for (i=(pos-1);i>=0;i--) {
                	if (toopen[i]) {tr=1;};	};
				if (!tr) {
                          for (i=pos+1;i<=n;i++) {
							if (toopen[i]>0) {
								tr=1;
								if (toopen[i]==4) {
									flag=1;
									tr=1;
									toopen[i]=3;
								} else if (toopen[i]==5) {
									toopen[i]=1;
									flag=2;
									tr=1;
								};};};};};
                	if (toopen[pos]==3) {flag=1;tr=1;count=1;}
				    else if (toopen[pos]==5) {
						count=1;
					tr=1;
					flag=2;
				};
							if (!tr) {flag=0;}
				}  

	int dianti::open(wait m[5]) {
				int count=0;
				int i=0,tr=0;
				if ((m[pos].GoingUp==1)&&(flag==1)) {
					count=1;}
                  else if ((m[pos].GoingUp==1)&&(flag==2)) {
					count=1;}
				  else if (flag==1) {

					if (pos<4) {++pos;};
					if (m[pos].GoingUp==1) {	count=1;};
                    if (toopen[pos]>0) {
						count=1;
					for (int i=(pos+1);i<=4;i++) {
                	if (toopen[i]) {tr=1;count=1;};
					};	};
				      if (!tr) {
					for (i=pos-1;i>=0;i--) {
							if (toopen[i]>0) {
								tr=1;								
								if (toopen[i]==2) {
									flag=2;
									tr=1;
									toopen[i]=3;
								} else if (toopen[i]==5) {
									toopen[i]=1;
									flag=2;
									tr=1;	};	};	};};}
               else	if(flag==2){
				 if (pos>0) {--pos;};
					if (m[pos].GoingDown==1) {
					count=1;
					};
					if (toopen[pos]>0) {
						count=1;
					for (int i=(pos-1);i>=0;i--) {
                	if (toopen[i]) {tr=1;count=1;};
					};};
				      if (!tr) {
					for (i=pos+1;i<=4;i++) {
							if (toopen[i]>0) {
								tr=1;								
								if (toopen[i]==3) {
									flag=1;
									tr=1;
								toopen[i]=1;
								} else if (toopen[i]==4) {
									toopen[i]=5;
									flag=2;
									tr=1;};	};	};};};
                     if (toopen[pos]==3) {flag=1;count=1;}
				else if (toopen[pos]==5) {
					count=1;	flag=2;};
                     if ((count)||(toopen[pos])) {
					toopen[pos]=0;
					 return 1;};
				     return 0;
              }  
void oper(dianti &a,dianti &b,dianti &c,wait m[5]) {
		
			if (a.open(m)) {
				a.printid();
           cout<<"号电梯在";
		   a.printfpos();
		   cout<<"楼已经打开\n";
               if (a.inandout(m)) {
			        a.pressbutton();
					};};
			if (b.open(m)) {
           b.printid();
           cout<<"号电梯在";
		   b.printfpos();
		   cout<<"楼已经打开\n";
               if (b.inandout(m)) {
			        b.pressbutton();
					};}
			if (c.open(m)) {
            c.printid();
           cout<<"号电梯在";
		   c.printfpos();
		   cout<<"楼已经打开\n";
               if (c.inandout(m)) {
			        c.pressbutton();
					};	}}
 int dianti::pressbutton()
	   {
		   int num,tr=0,tr1=0;
		   int lift[n+1];
		   for (int j=0;j<=n;j++) {
			   lift[j]=0; };
  
		do {
			cout<<"请输入你们要去的层数,不想输入请按5\n";
			cin>>num;
			if (num==5) {cout<<"电梯"<<id<<"号已关\n"; break;};
			if (flag==1) {
			if ((num<=pos)&&(num>n)) {
            cout<<"输入错误,本电梯是GoingUp的最高是"<<n<<"楼\n";
			} else {if (num>=pos) {
				toopen[num]=1;
				tr=1;
			} else if (pos>num) { lift[num]=1;tr1=1;};
			};
			
			};
			if (flag==2) {
            	if ((num>=pos)&&(num<0)) {
            cout<<"输入错误,本电梯是GoingDown的最底层是ground floor\n";
				} else {if (num<=pos) {
				toopen[num]=1;
				tr=1;
				} else if (pos>num) 
			{lift[num]=1;tr1=1;};
			};

			}
		}  while (flag<5);
        if (!tr) {
			
		 if(!tr1) {
if (flag==1) {flag=0;
				for (int j=pos+1;j<=n;j++) {
					if (toopen[j]==1) {
						flag=1;
					};
				};} else if (flag==2) {
					flag=0;
                    for (int k=pos-1;k>=0;k--) {
						if (toopen[j]==1) {
						flag=1;
					};
					}

				}
				else {
              flag=0;
				};
			} 
		 if (tr1) {
				if (flag==1) {
					flag=2;
					for (int i=pos-1;i>=0;i--) {
						if (lift[i]==1) {toopen[i]=1;}
					}
				}
			else if (flag==2) {
						flag=1;
						for (int i=pos+1;i<=n;i++) {
                        if (lift[i]==1) {toopen[i]=1;}
						;}
	   }}}
	
	
	
return 1; }

    

 

⌨️ 快捷键说明

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