首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问为什么编译器运行成功,但windows下运行失败

2012-03-25 
请教为什么编译器运行成功,但windows下运行失败?#includeiostream#includefstream#includestringusi

请教为什么编译器运行成功,但windows下运行失败?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
template <class type0>
class LinkStack;
template <class type0>
class LinkStackNode{
friend class LinkStack<type0>;
private:
type0 data;
LinkStackNode<type0>*pro;
public:
LinkStackNode(){pro=NULL;}
LinkStackNode(type0 t,LinkStackNode<type0>*p=NULL){data=t;pro=p;}
virtual ~LinkStackNode(){}
};
template <class type0>
class LinkStack{
private:
LinkStackNode<type0>*sp;
public:
LinkStack(){sp=new LinkStackNode<type0>;}
bool Empty()const{return sp->pro==NULL;}
void Pop();
void Pop(type0&);
void Push(type0);
void Top(type0&t){t=sp->data;}
virtual ~LinkStack();
};
template <class type0>
void LinkStack<type0>::Push(type0 t){
LinkStackNode<type0>*temptr=new LinkStackNode<type0>(t);
temptr->pro=sp;
sp=temptr;
}
template <class type0>
void LinkStack<type0>::Pop(){
if(!Empty()){
LinkStackNode<type0>*temptr=sp;
sp=temptr->pro;
delete temptr;
}
}
template <class type0>
void LinkStack<type0>::Pop(type0& t){
if(!Empty()){
LinkStackNode<type0>*temptr=sp;
sp=temptr->pro;
t=temptr->data;
delete temptr;
}
}
template <class type0>
LinkStack<type0>::~LinkStack(){
while(!Empty())Pop();
}
template <class type>
class BinaryTree;
template <class type>
class BinaryTreeNode{
friend class BinaryTree<type>;
private:
type data;
BinaryTreeNode<type>*LeftChild;
BinaryTreeNode<type>*RightChild;
public:
BinaryTreeNode(type t){data=t;LeftChild=RightChild=NULL;}
BinaryTreeNode(type t,BinaryTreeNode<type>*L,
BinaryTreeNode<type>*R):data(t),LeftChild(L),RightChild(R){;}
virtual ~BinaryTreeNode(){}
type Getdata()const{return data;}
BinaryTreeNode*GetLeftChild()const{return LeftChild;}
BinaryTreeNode*GetRightChild()const{return RightChild;}
void SetLeftChild(BinaryTreeNode<type>*p){LeftChild=p;}
void SetRightChild(BinaryTreeNode<type>*p){RightChild=p;}
void SetData(type t){data=t;}
};
template <class type>
class BinaryTree{
private:
BinaryTreeNode<type>*root;
void DestoryHelp(BinaryTreeNode<type>*r);
public:
BinaryTree(){root=NULL;}
BinaryTree(BinaryTreeNode<type>*r){root=r;}
void Destory(){DestoryHelp(root);}
virtual ~BinaryTree(){Destory();}
BinaryTreeNode<type>*GetRoot ()const{return root;}
};
template<class type>
void BinaryTree<type>::DestoryHelp(BinaryTreeNode<type>*r){
if(r!=NULL){
DestoryHelp(r->LeftChild);
DestoryHelp(r->RightChild);
delete r;
}
}
class GameOfAnimal{
private:
string NameOfKnowledgeBase;
BinaryTree<string>*pDTree;
void WriteHelp(BinaryTreeNode<string>*root,ofstream&outFile);
bool IsQuestion(string strSentence);
public:
GameOfAnimal();
virtual ~GameOfAnimal(){pDTree->~BinaryTree();};
void Run();
};
bool GameOfAnimal::IsQuestion(string strSentence){
return strSentence.find('?',0)!=strSentence.npos;
}
void GameOfAnimal::WriteHelp(BinaryTreeNode<string>*root,ofstream&outFile){
if(root!=NULL){
WriteHelp(root->GetLeftChild(),outFile);
WriteHelp(root->GetRightChild(),outFile);
outFile<<root->Getdata()<<endl;
}
}
bool UserSaysYes(){
cout<<" <y/n>";
char c;
cin>>c;
return c=='y';
}
GameOfAnimal::GameOfAnimal(){


NameOfKnowledgeBase="D:\\animal.dat";
ifstream inFile; 
inFile.open(NameOfKnowledgeBase,ios::in|ios::binary);
string strElem,strElems;
LinkStack<BinaryTreeNode<string>*>s;
BinaryTreeNode<string>*r,*r1,*r2;
inFile>>strElem;
while(!strElem.empty()){
if(IsQuestion(strElem)){
s.Pop(r2);s.Pop(r1);
r=new BinaryTreeNode<string>(strElem,r1,r2);
s.Push(r);
}
else{
r=new BinaryTreeNode<string>(strElem);
s.Push(r);
}
inFile>>strElems;
if(strElem==strElems)break;
else strElem=strElems;
}
s.Top(r);
pDTree=new BinaryTree<string>(r);
inFile.close();
}
void GameOfAnimal::Run(){
cout<<"欢迎来到动物游戏"<<endl;
do{
cout<<"想出一个动物,我将尽力猜它……"<<endl;
BinaryTreeNode<string>*p=pDTree->GetRoot();
string strElem;
strElem=p->Getdata();
while(IsQuestion(strElem)){
cout<<strElem;
cout<<"请回答";
if(UserSaysYes())p=p->GetLeftChild();
else p=p->GetRightChild();
strElem=p->Getdata();
}
cout<<"你想的动物是"<<strElem<<"吗?请回答";
if(UserSaysYes())cout<<"哈!一台计算机都打败你了……"<<endl;
else{
cout<<"你是幸运的……"<<endl;
cout<<"你想的动物是什么呢?"<<endl;
string strNewAnimal,strOldAnimal=strElem;
cin>>strNewAnimal;
cout<<"请输入一个疑问句(其中含有?),肯定回答为"<<strNewAnimal
<<",否定回答为"<<strOldAnimal<<endl;
cin>>strElem;
p->SetData(strElem);
p->SetLeftChild(new BinaryTreeNode<string>(strNewAnimal));
p->SetRightChild(new BinaryTreeNode<string>(strOldAnimal));
}
cout<<"想再玩一次吗?请回答";
}while(UserSaysYes());
ofstream outFile(NameOfKnowledgeBase,ios::out);
outFile<<"";outFile.close();
outFile.open(NameOfKnowledgeBase,ios::app|ios::binary);
WriteHelp(pDTree->GetRoot(),outFile);
outFile.close();
}
void InitialBaseOfKnowledge(string p){
ofstream outFile(p,ios::out|ios::binary|ios::_Noreplace);
if(outFile!=NULL)outFile<<"兔子\n"<<"鲸\n"<<"是陆生的吗?";
outFile.close();
}
int main(){
InitialBaseOfKnowledge("D:\\animal.dat");
GameOfAnimal A;
A.Run();
return 0;
}

[解决办法]
void InitialBaseOfKnowledge(string p){
ofstream outFile(p,ios::out|ios::binary|ios::_Noreplace);
if(outFile!=NULL)outFile<<"兔子\n"<<"鲸\n"<<"是陆生的吗?";
outFile.close();
}

outFile肯定不会是NULL。用is_open成员函数确认是否打开。

热点排行