请高手帮忙找个错误

Legend258 2008-11-08 10:45:04
//"car.h"
#ifndef _CAR_H
#define _CAR_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class car;
istream &operator>>(istream &stream, car &e);
class car {
private:
string license;
string action;
int number;
public:
car ();
car (string license, string action,int number);
string getlicense() ;
string getaction() ;
int getnumber();
void plusnumber();
friend istream &operator>>(istream &stream, car &c);
};
#endif
//"car.cpp"
#include<iostream>
#include"car.h"
using namespace std;
car::car(string license,string action,int number){
this->license = license;
this->action = action;
this->number =number;
}
car::car(){
number = 0;
}
string car::getlicense (){
return license;
}

string car::getaction (){
return action;
}
int car::getnumber (){
return number;
}
void car::plusnumber (){
number= 10;
}
istream &operator>>(istream &stream, car &c) {
stream >> c.license;
stream >> c.action;
return stream;
}
//"main.cpp"
#include<iostream>
#include<fstream>
#include<stack>
#include <sstream>
#include"car.h"
using namespace std;
int main () {
stack<car*>file;
stack<car*> s;
ifstream in;
in.open("data.txt");
if (!in) {
cerr << "Couldn't open data file, data file empty" << endl;
return 0;
}
string line;
while(getline(in,line)){
stringstream strcin(line);
car a;
strcin>>a;
if(a.getaction() == "arrives"&&file.size () < 5){
file.push(&a);
}
else if (file.size () == 5&&a.getaction ()=="arrives"){
cout<<"Sorry "<<a.getlicense ()<<", the lot is full"<<endl;
}
else if (a.getaction() =="departs"){
int m=0;
while (file.top()->getlicense() !=a.getlicense() ){
cout<<"askd"<<endl;
s.push(file.top());
file.pop();
m++;
}
if(m!= 0){
while(!s.empty ()){
s.top()->plusnumber ();
cout<<s.top()->getnumber ();
file.push(s.top());
s.pop();
}
}


cout<<file.top()->getlicense()<<" was moved "<<file.top()->getnumber() <<" times while it is here"<<endl;
file.pop();
}
}

while(file.size()!= 0){
cout<<(file.top())->getlicense()<<" was moved "<<(file.top())->getnumber() <<" times while it is here"<<endl;
file.pop();
}
in.close ();
return 0;

}

//"data.txt"
COOLONE arrives
COOLONE departs
TKG-123 arrives
QWE-839 arrives
UTU-K90 arrives
QWE-839 departs
RRR-877 arrives
GHL-GII arrives
PROGRAM arrives
TKG-123 departs
HEAD-DR arrives
UTU-K90 departs
RRR-877 departs
DMS-RJS arrives
DMS-RJS departs
TUE-87B arrives
GHL-GII departs
WEW-GH1 arrives
THE-MAN arrives
PORSCHE arrives
HEAD-DR departs
ERU-883 arrives
TUE-87B departs
WEW-GH1 departs
APPLE-1 arrives
BKE-284 arrives
BKE-284 departs
APPLE-1 departs
THE-MAN departs

//要求最后的结果为
COOLONE was moved 0 times while it was here
QWE-839 was moved 0 times while it was here
TKG-123 was moved 0 times while it was here
UTU-K90 was moved 2 times while it was here
RRR-877 was moved 2 times while it was here
DMS-RJS was moved 0 times while it was here
GHL-GII was moved 3 times while it was here
Sorry PORSCHE, the lot is full
HEAD-DR was moved 3 times while it was here
TUE-87B was moved 2 times while it was here
WEW-GH1 was moved 2 times while it was here
BKE-284 was moved 0 times while it was here
APPLE-1 was moved 0 times while it was here
THE-MAN was moved 3 times while it was here
ERU-883 was moved 3 times while it was here
PROGRAM was moved 4 times while it was here


大部分都编好了,可就是结果有问题
看了半天,小弟认为出错的原因可能是"main.cpp"中
while(file.top()->getlicense()!=a.getlicense())
这个循环判断上出错了,可小弟不知道错在哪了
请各位大侠帮帮忙,小弟必有重谢。
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧