
大神求解!~ C++ 栈类表达式的运算总是错>_<
C++学的不扎实...出错的地方有很多,但是我改不出来~~~~(>_<)~~~~所以求有耐心的大神帮忙看一下....#include<iostream>usingname...
C++学的不扎实...出错的地方有很多,但是我改不出来~~~~(>_<)~~~~
所以求有耐心的大神帮忙看一下....
#include <iostream>
using namespace std;
template <class T, int MaxSize = 100>
class SeqStack()
{
T data[MaxSize]; //存放栈元素的数组
int top; //栈顶指针
public:
SeqStack();
void Push(T &x); // 入栈
T Pop; //出栈
T &Top const; //取栈顶元素
bool Empty() const; //判断栈是否为空
};
//顺序栈的初始化
template<class T, int MaxSize>
SeqStack<T, MaxSize>::SeqStack():top(-1){}
//入栈
template<class T, int MaxSize>
void SeqStack<T, MaxSize>::Push(T &x)
{
if (top == MaxSize - 1)
{
cerr<<"上溢!"<<endl;
exit(-1);
}
top++;
data[top] = x;
}
//出栈
template<class T, int MaxSize>
T SeqStack <T, MaxSize>::Pop()
{
if (top == MaxSize - 1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
x = data[top];
top--;
return x;
}
//取栈顶元素
template<class T, int MaxSize>
const T &SeqStack<T,MaxSize>::Top() const
{
if (top == -1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
return data[top];
}
template<class T, int MaxSize>
bool SeqStack<T,MaxSize>::Empty() const
{
return top == -1;
}
bool InOPTR(char ch)
{
if (ch >= 0 && ch <=9)
{
return false;
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/'
|| ch == '#' || ch == '(' || ch == ')')
{
return true;
}
else
{
cout<<"错误的输入!";
exit(0);
}
}
char Precede(char pre_op, char ch)
{
int grade_top =0, grade_c = 0;
switch(pre_op)
{
case '#':
grade_top = 0;
break;
case ')':
grade_top = 1;
break;
case '+':
grade_top = 2;
break;
case '-':
grade_top = 2;
break;
case '*':
grade_top = 3;
break;
case '/':
grade_top = 3;
break;
case '(':
grade_top = 4;
break;
}
switch(ch)
{
case '#':
grade_c = 0;
break;
case ')':
grade_c = 1;
break;
case '+':
grade_c = 2;
break;
case '-':
grade_c = 2;
break;
case '*':
grade_c = 3;
break;
case '/':
grade_c = 3;
break;
case '(':
grade_c = 4;
break;
}
if (grade_top > grade_c)
{
return '>';
}
else if (grade_c == grade_top)
{
return '=';
}
else
{
return '<';
}
}
double Operate(double a, char op, char b)
{
switch(op)
{
case '+':
return (a+b);
break;
case '-':
return (a - b);
break;
case '*':
return (a*b);
break;
case '/':
return (a/b);
break;
default:
cout<<"ERROR!"<<endl;
return 0;
}
} 展开
所以求有耐心的大神帮忙看一下....
#include <iostream>
using namespace std;
template <class T, int MaxSize = 100>
class SeqStack()
{
T data[MaxSize]; //存放栈元素的数组
int top; //栈顶指针
public:
SeqStack();
void Push(T &x); // 入栈
T Pop; //出栈
T &Top const; //取栈顶元素
bool Empty() const; //判断栈是否为空
};
//顺序栈的初始化
template<class T, int MaxSize>
SeqStack<T, MaxSize>::SeqStack():top(-1){}
//入栈
template<class T, int MaxSize>
void SeqStack<T, MaxSize>::Push(T &x)
{
if (top == MaxSize - 1)
{
cerr<<"上溢!"<<endl;
exit(-1);
}
top++;
data[top] = x;
}
//出栈
template<class T, int MaxSize>
T SeqStack <T, MaxSize>::Pop()
{
if (top == MaxSize - 1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
x = data[top];
top--;
return x;
}
//取栈顶元素
template<class T, int MaxSize>
const T &SeqStack<T,MaxSize>::Top() const
{
if (top == -1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
return data[top];
}
template<class T, int MaxSize>
bool SeqStack<T,MaxSize>::Empty() const
{
return top == -1;
}
bool InOPTR(char ch)
{
if (ch >= 0 && ch <=9)
{
return false;
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/'
|| ch == '#' || ch == '(' || ch == ')')
{
return true;
}
else
{
cout<<"错误的输入!";
exit(0);
}
}
char Precede(char pre_op, char ch)
{
int grade_top =0, grade_c = 0;
switch(pre_op)
{
case '#':
grade_top = 0;
break;
case ')':
grade_top = 1;
break;
case '+':
grade_top = 2;
break;
case '-':
grade_top = 2;
break;
case '*':
grade_top = 3;
break;
case '/':
grade_top = 3;
break;
case '(':
grade_top = 4;
break;
}
switch(ch)
{
case '#':
grade_c = 0;
break;
case ')':
grade_c = 1;
break;
case '+':
grade_c = 2;
break;
case '-':
grade_c = 2;
break;
case '*':
grade_c = 3;
break;
case '/':
grade_c = 3;
break;
case '(':
grade_c = 4;
break;
}
if (grade_top > grade_c)
{
return '>';
}
else if (grade_c == grade_top)
{
return '=';
}
else
{
return '<';
}
}
double Operate(double a, char op, char b)
{
switch(op)
{
case '+':
return (a+b);
break;
case '-':
return (a - b);
break;
case '*':
return (a*b);
break;
case '/':
return (a/b);
break;
default:
cout<<"ERROR!"<<endl;
return 0;
}
} 展开
1个回答
展开全部
其实就错了3个地方:
class SeqStack//去掉括号
T Pop(); //出栈--加括号
const T &Top() const; //取栈顶元素--加括号+const,因为后面的top加了const
完整代码:
#include <iostream>
using namespace std;
template <class T, int MaxSize = 100>
class SeqStack//去掉括号
{
T data[MaxSize]; //存放栈元素的数组
int top; //栈顶指针
public:
SeqStack();
void Push(T &x); // 入栈
T Pop(); //出栈--加括号
const T &Top() const; //取栈顶元素--加括号+const,因为后面的top加了const
bool Empty() const; //判断栈是否为空
};
//顺序栈的初始化
template<class T, int MaxSize>
SeqStack<T, MaxSize>::SeqStack():top(-1){}
//入栈
template<class T, int MaxSize>
void SeqStack<T, MaxSize>::Push(T &x)
{
if (top == MaxSize - 1)
{
cerr<<"上溢!"<<endl;
exit(-1);
}
top++;
data[top] = x;
}
//出栈
template<class T, int MaxSize>
T SeqStack <T, MaxSize>::Pop()
{
if (top == MaxSize - 1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
x = data[top];
top--;
return x;
}
//取栈顶元素
template<class T, int MaxSize>
const T &SeqStack<T,MaxSize>::Top() const
{
if (top == -1)
{
cerr<<"下溢!"<<endl;
exit(-1);
}
return data[top];
}
template<class T, int MaxSize>
bool SeqStack<T,MaxSize>::Empty() const
{
return top == -1;
}
bool InOPTR(char ch)
{
if (ch >= 0 && ch <=9)
{
return false;
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/'
|| ch == '#' || ch == '(' || ch == ')')
{
return true;
}
else
{
cout<<"错误的输入!";
exit(0);
}
}
char Precede(char pre_op, char ch)
{
int grade_top =0, grade_c = 0;
switch(pre_op)
{
case '#':
grade_top = 0;
break;
case ')':
grade_top = 1;
break;
case '+':
grade_top = 2;
break;
case '-':
grade_top = 2;
break;
case '*':
grade_top = 3;
break;
case '/':
grade_top = 3;
break;
case '(':
grade_top = 4;
break;
}
switch(ch)
{
case '#':
grade_c = 0;
break;
case ')':
grade_c = 1;
break;
case '+':
grade_c = 2;
break;
case '-':
grade_c = 2;
break;
case '*':
grade_c = 3;
break;
case '/':
grade_c = 3;
break;
case '(':
grade_c = 4;
break;
}
if (grade_top > grade_c)
{
return '>';
}
else if (grade_c == grade_top)
{
return '=';
}
else
{
return '<';
}
}
double Operate(double a, char op, char b)
{
switch(op)
{
case '+':
return (a+b);
break;
case '-':
return (a - b);
break;
case '*':
return (a*b);
break;
case '/':
return (a/b);
break;
default:
cout<<"ERROR!"<<endl;
return 0;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询