| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:关于c++新手
只看楼主 加入收藏
欧系哟西
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-12-10
收藏
 问题点数:0 回复次数:0 
关于c++新手
学了c++一年了~
写程序总是很乱、很乱,虽说写出了程序,可是很混乱,而且很多的不足~
就像最近这道程序~
题目是:
停车场管理。设有一个可以停放n辆汽车的狭长停车场(先进后出),它只有一个大门可以供车辆进出。车辆按到达停车场时间的先后依次从停车场最里面向大门口处停放(最先到达的第一辆车停放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车离开,则排在便道上的第一辆车就可以进入停车场。停车场内如有某辆车要离开,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车再按原来的次序进停车场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车没进停车场就要离开,允许其离开,不收停车费,并且仍然保持在便道上的车辆次序。试编程模拟停车场管理。


#include <iostream>
using namespace std;
const int Max=5;
struct CarData
{
 CarData *next;
 int carnum;
};
class Queue
{
public:
 Queue();
 ~Queue();
 void inqueue(int x);
 int outqueue();
 int queuesize();
 CarData *pqueue();
private:
 CarData *front,*rear,*head;
 int length;
};
Queue::Queue()
{
 head=new CarData;
 head->next=NULL;
 rear=front=head;
 length=0;
}
Queue::~Queue()
{
}
void Queue::inqueue(int x)
{
 CarData *s;
 s=new CarData;
 s->carnum=x;
 s->next=NULL;
 rear->next=s;
 rear=s;
 length++;
}
int Queue::outqueue()
{
 CarData *p;
 int x;
 p=front->next;
 if(p->next==NULL)
 {
  x=p->carnum;
  front=rear;
  delete p;
  length--;
  return x;
 }
 else
 {
  x=p->carnum;
  front->next=p->next;
  delete p;
  length--;
  return x;
 }
}
int Queue::queuesize()
{
 return length;
}
CarData *Queue::pqueue()
{
 return head;
}
class TempStack
{
public:
 TempStack();
 ~TempStack();
 void instack(int x);
 int outstack();
 int stacksize();
 int topstack();
private:
 int TempStation[Max];
 int top;
};
TempStack::TempStack()
{
 top=-1;
}
TempStack::~TempStack()
{
}
void TempStack::instack(int x)
{
 if(top==Max-1)
  cout<<"临时车场已满!"<<endl;
 else
 {
  top++;
  TempStation[top]=x;
 }
 
}
int TempStack::outstack()
{
 if(top==-1)
  cout<<"临时车场已空!"<<endl;
 else
 {
  top--;
  return TempStation[top+1];
 }
}
int TempStack::topstack()
{
 if(top==-1)
  return 0;
 else
  return TempStation[top];
}
int TempStack::stacksize()
{
 return top;
}
class Stack
{
public:
 Stack();
 ~Stack();
 void instack(int x);
 int topstack();
 int outstack();
 int stacksize();
 void outcar();
 void printstack();
private:
 int Station[Max];
 int top;
 Queue queue;
 TempStack tempstack;
};
Stack::Stack()
{
 top=-1;
}
Stack::~Stack()
{
}
void Stack::instack(int x)
{
 if(top>=Max-1)
 {
  cout<<"停车场已满!"<<endl;
  cout<<"接下来的车将被送入队列中!"<<endl;
  queue.inqueue(x);
 }
 else
 {
  top++;
  Station[top]=x;
 }
}
int Stack::outstack()
{
 if(top==-1)
  cout<<"停车场已空!"<<endl;
 else
 {
  top--;
  return Station[top+1];
 }
}
int Stack::topstack()
{
 if(top==-1)
  return -1;
 else
  return Station[top];
}
int Stack::stacksize()
{
 return top;
}
void Stack::outcar()
{
 int y;
 cout<<"输入离开车牌号(0退出):";
 cin>>y;
 while(y!=0)
 {
  while(topstack()!=y&&topstack()>-1)
  {
   tempstack.instack(outstack());
  }
  if(topstack()==-1)
   cout<<"停车场里没有该车牌号!"<<endl;
  else
  {
   cout<<outstack()<<endl;
  }
  while(tempstack.stacksize()>=0)
   instack(tempstack.outstack());
  if(queue.queuesize()>0&&top<Max-1&&tempstack.stacksize()==-1)
   instack(queue.outqueue());
  printstack();
  cout<<"输入离开车牌号(0退出):";
  cin>>y;
 }
}
void Stack::printstack()
{
 if(top==-1)
  cout<<"停车场空了!";
 else
 {
  cout<<"停车场的车牌号有:"<<endl;
  for(int i=top;i>-1;i--)
   cout<<Station[i]<<' ';
 }
 cout<<endl;
 CarData *i=queue.pqueue()->next;
 if(!queue.queuesize())
  cout<<"队列空了!";
 else
 {
  cout<<"队列里的车牌号有:"<<endl;
  while(i!=NULL)
  {
   cout<<i->carnum<<' ';
   i=i->next;
  }
 }
 cout<<endl;
}
void main()
{
 Stack car1;
 int x;
 cout<<"输入车牌号(0退出):";
 cin>>x;
 while(x!=0)
 {
  car1.instack(x);
  cout<<"输入车牌号(0退出):";
  cin>>x;
 }
 car1.printstack();
 car1.outcar();
}
求助高手~希望给点建议呀!!
谢谢啦~
搜索更多相关主题的帖子: 停车场 汽车 时间 
2010-12-10 10:12
快速回复:关于c++新手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026340 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved