这个问题弄了很久不出来, 问了很多人, 不是不懂就是没回音,或者忙.
首先: 程序在DEV里通过没问题。
问题出在: VS.NET2003里, 以不调试的方式运行, 结果对,但是运行结束后弹出以个错误信息,是什么内存溢出。 以调试的方式根本不能正确运行。
想了N久,反反复复看了代码, 就是不知道哪里错, 跪求了。
// DEV 05.10.22
#include "iostream"
#include"string"
using namespace std;
template<class T>
class Stack
{
public:
Stack(int maxSize);
~Stack(){ delete [] base ;}
//创建一个空的堆栈
bool isEmpty();
//若堆栈为空,则返回true,否则返回false
bool isFull();
//若堆栈为满,则返回true,否则返回false
int Length();
//precondition:Stack exist
//后条件:reture the length of Stack
Stack<T> *push(T x);
//add element
//getTop();
//precondition:Stack exist
//end :
Stack<T> *pop(T& x);
//precondition:Stack exist
//return the top element and delete it
T getTop();
private:
T *base ;
int top ; //top指向栈顶元素
int maxSize ;//指定元素最大个数
};
template<class T>
Stack<T>::Stack(int maxSize)
{
this->maxSize = maxSize ;
base = new T[maxSize] ;
top = -1;
}
template<class T>
bool Stack<T>::isEmpty()
{
return top == -1;
}
template<class T>
bool Stack<T>::isFull()
{
return top == maxSize-1 ;
}
template<class T>
int Stack<T>::Length()
{
return top+1;
}
template<class T>
T Stack<T>::getTop()
{
if(isEmpty()) { cout<<"堆栈是空的"<<endl; return -1; }
else return base[top] ;
}
template<class T>
Stack<T> *Stack<T>::push(T x)
{
if(isFull())
{ cout<<"堆栈满了"<<endl; return this; }
else
base[++top] = x;
return this;
}
template<class T>
Stack<T> *Stack<T>::pop(T& x)
{
if(isEmpty()) { cout<<"堆栈是空的!!"; return this; }
else
{
x = base[top--];
return this;
}
}
template<class T>
class Queue
{
public:
Queue();
~Queue() { delete [] base; }
bool isEmpty() const { return front == rear; }
bool isFull() const { return ((rear + 1) % maxQueueSize == front) ? 1 : 0 ; }
Queue<T> *enQueue(T x);
Queue<T> *deQueue(T& x);
T first() const;
private:
int front;
int rear;
T *base;
int maxQueueSize;
};
template<class T>
Queue<T>::Queue()
{
maxQueueSize = 100;
this->maxQueueSize = maxQueueSize;
base = new T[maxQueueSize];
front = rear = 0;
}
template<class T>
Queue<T> *Queue<T>::enQueue(T x)
{
if(isFull()) { cout<<"队列满了"<<endl; return this; }
else
{
rear = (rear + 1) % maxQueueSize;
base[rear] = x;
return this;
}
}
template<class T>
Queue<T> *Queue<T>::deQueue(T& x)
{
if(isEmpty()) { cout<<"队列为空"<<endl; return this; }
else
{
front = (front + 1) % maxQueueSize;
x = base[front];
return this;
}
}
template<class T>
T Queue<T>::first() const
{
if(isEmpty()) { cout<<"队列为空"<<endl; return -1; }
else return base[(front + 1) % maxQueueSize] ;
}
class FiendLanguage{
public:
FiendLanguage();
~FiendLanguage(){ delete sentence; }
FiendLanguage *translateToHumanLanguage();
friend istream& operator >>(istream& ins,FiendLanguage& first);
friend ostream& operator<<(ostream& outs,FiendLanguage& first);
private:
string A;
char *sentence;
Queue<char> q1;
};
////////////////////////////////////////////
FiendLanguage::FiendLanguage()
{
sentence = new char;
}
istream& operator >>(istream& ins,FiendLanguage& first)
{
string _B;
string _sentence;
char *q;
q = first.sentence;
cout<<"大写字母只能输A和B"<<endl;
cout<<"魔王语言输入词汇A:(任意个小写字母)"<<endl;
ins>>first.A;
cout<<"输入魔王语言词汇B:(任意个小写字母混合任意个大写字符A)"<<endl;
cin>>_B;
cout<<"输入魔王说的话:"<<endl;
cin>>_sentence;
for(int k = 0; _sentence[k] != '\0' ; k++)
{
if(_sentence[k] == 'B')
{
for(int i = 0; i < _B.length(); i++)
{
if(_B[i] == 'A')
{
for(int g = 0; g < first.A.length(); g++)
*first.sentence++ = first.A[g];
}
else
*first.sentence++ = _B[i];
}
}
else
*first.sentence++ = _sentence[k];
}
*first.sentence = '\0';
first.sentence = q;
return ins;
}
FiendLanguage *FiendLanguage::translateToHumanLanguage()
{
Stack<char> stack1(50);
char temp;
char first;
for(int i = 0;i < strlen(sentence) + 1 ; i++)
{
q1.enQueue(sentence[i]);
}
int i = 0;
while(q1.first() != '\0')
{
if(q1.first() == '(')
{
q1.deQueue(temp);
temp = first = q1.first();
while( q1.first() != ')' )
{
q1.deQueue(temp);
stack1.push(temp);
}
q1.deQueue(temp);
while( !stack1.isEmpty() )
{
q1.enQueue(first);
stack1.pop(temp);
if(temp != first)
q1.enQueue(temp);
}
}
else
{
q1.deQueue(temp);
q1.enQueue(temp);
}
}
q1.deQueue(temp);
q1.enQueue(temp);
return this;
}
ostream& operator<<(ostream& outs,FiendLanguage& first)
{
typedef struct
{
char letter;
string cn_message;
}Table;
Table translate[26]=
{
{'a', "一只" } ,
{'b', "傻B" },
{'c', "CS" },
{'d', "地" },
{'e', "鹅" },
{'f', "负" },
{'g', "赶" },
{'h', "恨"},
{'i', "爱" },
{'j', "鸡" },
{'k', "打"},
{'l', "垃圾"},
{'n', "蛋" },
{'m', "吗?"},
{'o', "饿" },
{'p', "劈" },
{'q', "强" },
{'r', "软件学院" },
{'s', "上" },
{'t', "天"},
{'u', "输"},
{'v', "赢"},
{'w', "我"},
{'x', "下" },
{'y', "洋浦" },
{'z', "追"}
};
char temp;
char *p =new char;
char *q;
q = p;
string str[100];
int i = 0;
pair<char,string> result;
outs<<"解释成小写英文是:"<<endl;
while(!first.q1.isEmpty())
{
first.q1.deQueue(temp);
*p++ = temp;
outs<<temp;
}
*p = '\0';
p = q;
outs<<endl;
outs<<"翻译成人的语言是:"<<endl;
for(int i = 0; p[i] !='\0'; i++ )
for(int j = 0; j < 26; j++)
{
if( translate[j].letter == p[i] )
outs<<translate[j].cn_message;
}
outs<<endl;
return outs;
}
int main()
{
FiendLanguage f1;
cin>>f1;
f1.translateToHumanLanguage();
cout<<f1;
system("pause");
return 0;
}