C++实现数据结构作业——表达式求值

  

    /*用到了class, template,析构函数,构造函数,C++菜鸟一只,大牛无视。各种bug,待修改。。。*/
//My Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

const int N = 256;
char map[N][N];

template <class T>

class Node{
public:
T data;
Node * next;
Node(const T &e, Node<T> *t){
data = e;
next = t;
}
};

template <class T>

class LinkStack{
private:

Node<T> * top;
void init(){
top = NULL;
}
public:
void Clear(){
T tmp;
while(!Empty())
Pop(tmp);
}
LinkStack(){
init();
}
virtual ~LinkStack(){
Clear();
}
int Length() const
{
int count = 0;
for(Node<T> * i = top; i != NULL; i = i->next){
count++;
}
return count;
}
bool Empty() const{
return top == NULL;
}
int Push(const T &e){
Node<T> * new_top = new Node<T>(e, top);
if(new_top == NULL)
return 0;
else{
top = new_top;
return 1;
}
}
T GetTop(T &e) const{
if(Empty()){
return 0;
} else {
e = top->data;
return e;
}
}
int Pop(T &e){
if(Empty()){
return 0;
} else {
Node<T> * old_top = top;
e = old_top->data;
top = old_top->next;
delete old_top;
return 1;
}
}

};

char c[10];
int i;

void get_in(){

memset(c, 0, sizeof(c)); i = 0;
while(cin.get(c[i++])){
if(c[i-1] == ' ')
break;
}
}

void inite(){
map['+']['+'] = map['+']['-'] = map['+'][')'] = map['+']['#'] = '>';map['+']['*'] = map['+']['/'] = map['+']['('] = '<';
map['-']['+'] = map['-']['-'] = map['-'][')'] = map['-']['#'] = '>';map['-']['*'] = map['-']['/'] = map['-']['('] = '<';
map['*']['+'] = map['*']['-'] = map['*']['*'] = map['*']['/'] = map['*'][')'] = map['*']['#'] = '>'; map['*']['('] = '<';
map['/']['+'] = map['/']['-'] = map['/']['*'] = map['/']['/'] = map['/'][')'] = map['/']['#'] = '>'; map['/']['('] = '<';
map[')']['+'] = map[')']['-'] = map[')']['*'] = map[')']['/'] = map[')'][')'] = map[')']['#'] = '>';
map['(']['+'] = map['(']['-'] = map['(']['*'] = map['(']['/'] = map['(']['('] = '<'; map['('][')'] = '=';
map['#']['+'] = map['#']['-'] = map['#']['*'] = map['#']['/'] = map['#']['('] = '<'; map['#']['#'] = '=';
}

char Precede(char a, char b){
return map[(int)a][(int)b];
}

int Oprate(int a, char t, int b){
int x;
switch (t){
case '+':
x = a + b; break;
case '-':
x = a - b; break;
case '*':
x = a * b; break;
case '/':
x = a / b; break;
}
return x;
}

int In(char c){
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '#')
return 1;
return 0;
}

int solve(){
LinkStack<int> opnd;
LinkStack<char> optr; optr.Push('#');
char e, th;
int f, b, a;
get_in();
while(c[0] != '#' || (optr.GetTop(e) !='#')){
if(!In(c[0])){
f = 0;
for(int j = 0; j < i-1; j++){
f += (c[j]-'0') * pow(10.0, i-2-j);
}
opnd.Push(f);
get_in();
} else {
switch (Precede(optr.GetTop(e), c[0])){
case '<':
optr.Push(c[0]); get_in();
break;
case '=':
optr.Pop(e); get_in();
//cout << optr.GetTop(e) << endl;
break;
case '>':
optr.Pop(th);
opnd.Pop(b); opnd.Pop(a);
opnd.Push(Oprate(a, th, b));
//cout << a << " " << th << " " << b << endl;
//cout << opnd.GetTop(f) << endl;
break;
}
}
}
return opnd.GetTop(f);

}
int main(){
//freopen("data.in", "r", stdin);
inite();
cout << "Please input the Expresssion : " << endl;
cout << solve() << endl;
return 0;
}

posted @ 2011-10-17 22:05  AC_Von  阅读(784)  评论(0编辑  收藏  举报