qq_38349395
qq_38349395
2017-11-05 03:33

C++用栈进行表达式运算,控制台无输出?求助0.0

  • c++
  • 数据结构

#include
#include
#include
#include
using namespace std;
typedef int Rank;
typedef enum { ADD, SUB, MUL, DIV, POW, FAC, L_P, R_P, EOE }Operator;
#define N_OPTR 9
#define default_capacity 10
templateclass Vector {
public:
Rank _size;
int _capacity;
T*_elem;

Vector(int s = 0, T v = 0, int c = default_capacity)
{
_elem = new T[_capacity = c];
for (_size = 0; _size < s; _size++) {
_elem[_size] = v;
}

}
T&operator[](Rank r)const
{
    return _elem[r];
}
Rank size()
{
    return _size;
}
int remove(Rank lo, Rank hi)
{
    if (lo == hi)
    {
        return 0;
    }
    while (hi < _size)
    {
        _elem[lo++] = _elem[hi++];
        _size = lo;
        return hi - lo;
    }
}
T remove(Rank r)
{
    T e = _elem[r];
    remove(r, r + 1);
    return e;
}
bool empty() const{
    return !_size;

}
void expand()
{
    if (_size < _capacity)
    {
        return;
    }
    if (_size < default_capacity)
    {
        _capacity = default_capacity;
    }
    T* oldelem = _elem;
    _elem = new T[_capacity <<= 1];
    for (int i = 0; i < _size; i++)
    {
        _elem[i] = oldelem[i];
    }
    delete [] oldelem;
}
Rank insert(Rank r, T const& e)
{
    expand();
    for (int i = _size; i > r; i--)
    {
        _elem[i] = _elem[i - 1];
    }
    _elem[r] = e;
    _size++;
    return r;
}

};
templateclass Stack :public Vector {
public:
void push(T const& e)
{
insert(size(), e);
}
T pop() {
return remove(size() - 1);
}
T& top()
{
return (*this)[size() - 1];
}
};
double calcu2(int a,char ch,int b)
{
switch (ch)
{
case'+':
return a + b;
case'-':
return a - b;
case'*':
return a*b;
case'/':
return a / b;
case'^':
return pow(a, b);

}

}
const char pri[N_OPTR][N_OPTR] = {
'>','>','<','<','<','<','<','>','>',
'>','>','<','<','<','<','<','>','>',
'>','>','>','>','<','<','<','>','>',
'>','>','>','>','<','<','<','>','>',
'>','>','>','>','>','<','<','>','>',
'>','>','>','>','>','>',' ','>','>',
'<','<','<','<','<','<','<','=',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',
'<','<','<','<','<','<','<',' ','=',
};
Operator getop(char ch)
{
switch (ch)
{
case'+':
return ADD;
case'-':
return SUB;
case'*':
return MUL;
case'/':
return DIV;
case'^':
return POW;
case'!':
return FAC;
case'(':
return L_P;
case')':
return R_P;
case'#':
return EOE;

}

}
char order(char op1, char op2)
{
return pri[getop(op1)][getop(op2)];
}
int step(int x)
{
int k = 1;
if (x == 1)
{
return 1;
}
k = x*step(x - 1);
return k;
}
int calcu(char ch, int x)
{
switch (ch)
{
case'!':
return step(x);
}
}

int evaluate(char* S)
{
Stackopnd;
Stackoptr;
optr.push('#');
while (!optr.empty())
{
if (isdigit(*S))
{
opnd.push(*S);
}
else
switch (order(optr.top(), S)) {
case'<':
optr.push(*S);
S++;
break;
case'=':
optr.pop();
S++;
break;
case'>':
char op = optr.pop();
if ('!' == op)
{
int popnd = opnd.pop();
opnd.push(calcu(op, popnd));
}
else {
int popnd2 = opnd.pop();
int popnd3 = opnd.pop();
opnd.push(calcu2(popnd2, op, popnd3));
}
break;

}
}

return opnd.pop();
}
int main()
{
char
S=“2+3^2";

cout << evaluate(S) << endl;
system("pause");
return 0;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答