主题:[讨论]为什么编译通过,但运行的时候什么也没输入就出错了啊?
一运行就说Debug Assertion Failed!!
#include<vector>
#include<iostream>
using namespace std;
template<class T>class stack{
public:
stack();
void push(T item);
T top();
T pop();
bool empty();
private:
int count;
vector <T> data;
};
template<class T> stack<T>::stack():count(0){
}
template<class T> void stack<T>::push(T item){
data[count++] = item;
}
template<class T> T stack<T>::top(){
return data[count-1];
}
template<class T> T stack<T>::pop(){
return data[--count];
}
template<class T> bool stack<T>::empty(){
return count==0;
}
bool In(char c){
switch(c){
case'+':case'-':case'*':case'/':case'(':case'+)':case'#': return true;
break;
default :
return false;
}
}//判断c是不是运算符
template<class T> T operater(T a,T b,char c){
T outcome;
switch(c){
case'+':
outcome=a+b;
break;
case'-':
outcome=b-a;
break;
case'*':
outcome=a*b;
break;
case'/':
outcome=b/a;
break;
}
return outcome;
}//两个运算数进行运算
char compare(char c1,char c2){//c2为栈顶元素
char f;
switch(c1){
case'+':case'-':
if(c2=='+'||c2=='-'||c2==')'||c2=='#') f = '>';
else f = '<';
break;
case'*':case'/':
if(c2=='*'||c2=='/'||c2==')'||c2=='+'||c2=='-') f = '>';
else f = '<';
break;
case'(':
if(c2==')') f = '=';
else f = '<';
case')':
f = '>';
case'#':
if(c2=='#') f = '=';
else f = '<';
}
return f;
}//比较两个运算符的优先级
int experssion(){
stack<char> yusuanfu;
stack<int> yusuanshu;
yusuanfu.push('#');
char c;
char last;
c = getchar();
last = yusuanfu.top();
if(c >= '0'&&c<= '9')
yusuanshu.push(c);
else
if(In(c))
switch(compare(last,c)){
case'>':
yusuanfu.push(c);
c = getchar();
break;
case'<':
//int shu1 = yusuanshu.pop();
//int shu2 = yusuanshu.pop();
//char fuhao = yusuanshu.pop();;
yusuanshu.push(operater(yusuanshu.pop(),yusuanshu.pop(),yusuanshu.pop()));
break;
case'=':
yusuanfu.pop();
c=getchar();
break;
}
return yusuanshu.top();
}
int main(){
//stack<int> st;
cout<<"请输入算术表达式(中间值及最终结果要在0~9之间),并以#结束"<<endl;
cout<<experssion();
return 0;
}
#include<vector>
#include<iostream>
using namespace std;
template<class T>class stack{
public:
stack();
void push(T item);
T top();
T pop();
bool empty();
private:
int count;
vector <T> data;
};
template<class T> stack<T>::stack():count(0){
}
template<class T> void stack<T>::push(T item){
data[count++] = item;
}
template<class T> T stack<T>::top(){
return data[count-1];
}
template<class T> T stack<T>::pop(){
return data[--count];
}
template<class T> bool stack<T>::empty(){
return count==0;
}
bool In(char c){
switch(c){
case'+':case'-':case'*':case'/':case'(':case'+)':case'#': return true;
break;
default :
return false;
}
}//判断c是不是运算符
template<class T> T operater(T a,T b,char c){
T outcome;
switch(c){
case'+':
outcome=a+b;
break;
case'-':
outcome=b-a;
break;
case'*':
outcome=a*b;
break;
case'/':
outcome=b/a;
break;
}
return outcome;
}//两个运算数进行运算
char compare(char c1,char c2){//c2为栈顶元素
char f;
switch(c1){
case'+':case'-':
if(c2=='+'||c2=='-'||c2==')'||c2=='#') f = '>';
else f = '<';
break;
case'*':case'/':
if(c2=='*'||c2=='/'||c2==')'||c2=='+'||c2=='-') f = '>';
else f = '<';
break;
case'(':
if(c2==')') f = '=';
else f = '<';
case')':
f = '>';
case'#':
if(c2=='#') f = '=';
else f = '<';
}
return f;
}//比较两个运算符的优先级
int experssion(){
stack<char> yusuanfu;
stack<int> yusuanshu;
yusuanfu.push('#');
char c;
char last;
c = getchar();
last = yusuanfu.top();
if(c >= '0'&&c<= '9')
yusuanshu.push(c);
else
if(In(c))
switch(compare(last,c)){
case'>':
yusuanfu.push(c);
c = getchar();
break;
case'<':
//int shu1 = yusuanshu.pop();
//int shu2 = yusuanshu.pop();
//char fuhao = yusuanshu.pop();;
yusuanshu.push(operater(yusuanshu.pop(),yusuanshu.pop(),yusuanshu.pop()));
break;
case'=':
yusuanfu.pop();
c=getchar();
break;
}
return yusuanshu.top();
}
int main(){
//stack<int> st;
cout<<"请输入算术表达式(中间值及最终结果要在0~9之间),并以#结束"<<endl;
cout<<experssion();
return 0;
}