`
radovi
  • 浏览: 64262 次
  • 性别: Icon_minigender_1
  • 来自: 杭 州
社区版块
存档分类
最新评论

停车场管理———严蔚敏版数据结构实习题

阅读更多
*问题描述
* 设停车藏是一个可以停放n辆车的狭长的通道,且只有一个大门可供进出。汽车在停车场内按到达的先后顺序,一次由北向南停放,若场内停放已满,那么就先停放在便道中。
* 一旦由车从停车场内出来后。便道中的第一辆车马上那个听进去。当停车场内由车要出来时。在他后面的车要为他让路。然后再按原顺序进入停车场,收取费用。

parking.java
/**
 * ***********CopyRight**************
 *-------Powered by QianXunNet-----
 *-----Version 1.00   2009-01-17-----
 *-----   Design BY  NiChao    -----
 *=========description===========
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
 */

package yanweimin.Parking;
import java.util.Scanner;
import yanweimin.Parking.*;
import java.util.Scanner;
public class Park {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		        
		          //设定一个便道和两个车库
		System.out.println("the parking money is 10  yuan per hour");
		System.out.println("input a num for the maxsize of parking:");
		Scanner in = new Scanner(System.in);
		int maxsize = in.nextInt();
	 Queue biandao = new Queue();
	 StackArray main = new StackArray(maxsize,"主车库");
	 StackArray fuzhu = new StackArray(maxsize, "辅助车库");	 		
       loop: while(true)
        {
        	Scanner cin = new Scanner(System.in);
        	System.out.println("请输入当前时间:");
        	           //======当前时间=======
        	int time = cin.nextInt();
            System.out.println("select === 1: 到达 2.离去 3.结束程序");
                
                int select = cin.nextInt();
                switch(select)
                {
                         //车辆到站
                		case 1 :
                		{
                			 System.out.println("input the num of car");
                			 int num = cin.nextInt();
                			 Car mycar = new Car(time ,num);
                			 int into = main.push(mycar);
                			 if(into == -1)
                			 {
                				 biandao.addQueue(mycar);
                			 }
                			 break;
                			
                		}
                		     //出车库
                             case 2 :
                		{
                			System.out.println("input the num of the car you want to out:");
                		    int num = cin.nextInt();
                		    
                             while( main.stackvalue[main.p].num != num)
                             {
                            	 Car acar = main.stackvalue[main.p];
                            	 main.pop();
                            	 fuzhu.push(acar);
                             }
                             Car right_car = main.stackvalue[main.p]; 
                        	 main.pop();
                             int money = (time - right_car.time)*10;
                             System.out.println("该车应该付费"+money);

                             while( fuzhu.p != -1)
                            	 {
                            	    Car acar = fuzhu.stackvalue[fuzhu.p];
                            	    fuzhu.pop();
                            	    main.push(acar);
                            	 }
                              if(biandao.rear != -1)
                              {
                            	  Car acar = biandao.queuevalue[0];
                                  acar.time = time;
                                  biandao.delQueue();
                                  main.push(acar);
                              }
                              break;  
                		}
                		case 3 :
                		{
                			break loop;
                		}               			
                }
        }		      
	}
}



queue.class
package yanweimin.Parking;

            //优化过的数组模拟的队列类

import yanweimin.Parking.Car;
public class Queue {
   
		   /*
		    * 查看队列数组中的数
		    */
		//   public void coutQueue()
		//   {
			   //设定一根移动指针 用于指示当前的虚拟队头
          //     if(this.rear == -1)
          //  	   System.out.println("当前队列为空");
        //       else
        //       {
        //    	   int j = 0;
         //   	   for(int i =0; i<=rear;i++,j++)
    	//		   {
    	//			   System.out.print(queuevalue[i]+" ");
    	//		   }
       //     	   System.out.println("共有元素"+j+"个"); 
         //      }			   
		//   }
		   
		   		/*
		   		 * 向队列中增加元素
		   		 * @param num  欲增加的值
		   		 */
		   			public void addQueue(Car car)
		   			{
		   				 //队列已满
		   				if(this.rear == this.maxsize-1)
		   				{
		   					System.out.println("便道已满");		   					
		   				}
		   				else
		   				{
		   					this.rear ++;
		   					this.queuevalue[this.rear] = car;
		   					System.out.println(" 车牌号为 "+car.num+"进入便道了");
		   				}
		   			}
		   			
		   			/*
		   			 *  取出队列中的元素
		   			 *  @return num 取出的元素
		   			 */
		   				public void delQueue()
		   				{
		   					if(this.rear == -1)
		   					{
		   						System.out.println(" 便道已经空");
		   					}
		   					else
		   					{
		   						 Car car = this.queuevalue[0];
		   						 int temp = 0;
		   						 while ( temp != this.rear )
		   						 {
		   							 this.queuevalue[temp] = this.queuevalue[temp +1];
		   							 temp ++;
		   						 }
		   						 this.rear --;
		   						 System.out.println("车牌号为"+car.num+"出便道了");
		   					}
		   				}
		   				
		   				public int maxsize = 20;
		   				public Car[] queuevalue = new Car[maxsize];
		   				public int rear = -1;
		   	
	
}



stackarray.class
package yanweimin.Parking;


import yanweimin.Parking.Car;
public class StackArray {
  
	
	            //构造函数
			public StackArray(int maxsize,String name)
			{
				p = -1;
				this.maxsize = maxsize;
				stackvalue = new Car[maxsize];
				this.name = name;
			}
	
	
	
	
	     //打印数组内的数据
	   //	public void print()
	 //  	{
	   		   //指针指向当前栈的有效数组上
	   	//	if(this.p==-1)
	   		//	System.out.println("栈空");
	   	//	else
	   		//{
	   		//	int i = this.p;
	  	  // 	  while(i>=0)
	  	   //	  {
	  	   //		  System.out.println(this.stackvalue[p]);
	  	   //		 i--;
	  	  // 	  }
	   	//	}
	   			
	  // 	} //print
	   	         //进车库
			      //返回值 -1 没有成功进入车库
			      //返回值 1 成功进入车库
	   	public int push(Car value)
	   	{
	   		if(this.p == this.maxsize-1 ) 
	   			{
	   			System.out.println(name+"已满");
	   			return -1;
	   			}
	   		else 
	   			{
	   			this.p++;
	   			this.stackvalue[p] = value;
	   			System.out.println("车牌为 "+value.num+"进入了"+this.name);
	   			return 1;
	   			}
	   		  
	   	}  //push	   	
	   	
	   	
	   	public int pop()
	   	{
	   		// int temp = -1;
	   		if(this.p == -1)
	   			{
	   			System.out.println("车库中已经没有车了");
		   		return -1;
	   			}

	   		else
	   		{
	   			int temp;
                      temp = this.stackvalue[p].num;
	 	   		      System.out.println("车牌号为 "+temp+" 出"+this.name);
	 	   			  this.p--;

	   			return temp;
	   		}
	   	}
	
	
	
	
	 int p;       //栈内指针
	 int maxsize;       //栈的最大容量
   Car[] stackvalue;
	String name;
}



测试结果
--------------------------------------------
the parking money is 10  yuan per hour
input a num for the maxsize of parking:
2
请输入当前时间:
5
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
1
车牌为 1进入了主车库
请输入当前时间:
10
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
2
车牌为 2进入了主车库
请输入当前时间:
15
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
1
车牌号为 2 出主车库
车牌为 2进入了辅助车库
车牌号为 1 出主车库
该车应该付费100
车牌号为 2 出辅助车库
车牌为 2进入了主车库
请输入当前时间:
20
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
3
车牌为 3进入了主车库
请输入当前时间:
25
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
4
主车库已满
车牌号为 4进入便道了
请输入当前时间:
30
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
5
主车库已满
车牌号为 5进入便道了
请输入当前时间:
35
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
2
车牌号为 3 出主车库
车牌为 3进入了辅助车库
车牌号为 2 出主车库
该车应该付费250
车牌号为 3 出辅助车库
车牌为 3进入了主车库
车牌号为4出便道了
车牌为 4进入了主车库
请输入当前时间:
40
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
4
车牌号为 4 出主车库
该车应该付费50
车牌号为5出便道了
车牌为 5进入了主车库
请输入当前时间:
45
select === 1: 到达 2.离去 3.结束程序
3
分享到:
评论
3 楼 radovi 2009-02-20  
Saito 写道

面向过程式编程 + 重复造轮子. 有很明显的c痕迹.. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 还有.&nbsp; Java 的 collection里面有 Queue + Stack

是的 这些类是可以用的 我用数组模拟的
2 楼 hahalizx 2009-02-19  
错别字太多
1 楼 Saito 2009-02-19  
面向过程式编程 + 重复造轮子. 有很明显的c痕迹..


                    还有.  Java 的 collection里面有 Queue + Stack

相关推荐

Global site tag (gtag.js) - Google Analytics