中缀转后缀, 主要是模拟过程吧。

1、 建立一个符号栈(存储运算符和括号);

2、 表达式中遇到'('入栈; 

3、 遇到')'将栈中所有元素出栈即括号和括号内所包含的运算符;

4、 数字字符直接存到数组中;

5、根据运算符优先级来入栈和出栈运算符, 即当前运算符优先级大于栈顶元素优先级直接入栈, 小于的话出栈栈顶元素直到当前运算符优先级大于栈顶元素优先级;

6、出栈栈中所有字符, 如 a+b。 

计算后缀表达式:

  一层层算吧, 代码给出, 模拟就会明白 。

中缀转后缀(纯手打, 累到不要不要的)

#include <stack>
#include <cmath> 
#include <cstdio>
#include <cstring>
const int MAXN = 600;
using namespace std;
char str[MAXN], result[MAXN];
bool Judge(char c){
    if(c == '+' || c == '-' || c == '*' || c == '/')
        return true;
    return false;
}
int HowLarge(char c){
    if(c == '+')
        return 1;
    if(c == '-')
        return 1;
    if(c == '*')
        return 2;
    if(c == '/')
        return 2;
}
int t;
void Addelem(char c){
    if(c == '.')
        result[t++] = c;
    else{
        if(t == 0)
            result[t++] = c; 
        else{
            int a = t;
            if(result[a-1] == '.')
                result[t++] = c;
            else{
                result[t++] = ' ';
                result[t++] = c;
            }
            
        }
    }
}
void Printfstring(char *str){
    for(int i = 0; i < strlen(str); i++)
        printf("%c", str[i]);
    printf("\n"); 
}
void Sposfixexpression(char *str){
//    printf("1\n");
    t = 0;
    stack<char> S;
    int len = strlen(str);
    for(int i = 0; i < len; i++){
        if(str[i] == ' ')
            continue;
        if(str[i] >= '0' && str[i] <= '9' || str[i] == '.')
            Addelem(str[i]);
        if(str[i] == '(')
            S.push(str[i]); 
        if(str[i] == ')' && !S.empty()){
            while(S.top() != '('){
                if(Judge(S.top()))
                    Addelem(S.top());                 
                S.pop();
            }
            S.pop();
        }
        if(Judge(str[i])){
            
            if(S.empty() || S.top() == '(')
                S.push(str[i]);
            else if(Judge(S.top())){
                int a = HowLarge(str[i]);
                int b = HowLarge(S.top());
                if(a > b)
                    S.push(str[i]);
                else{
                    while(str[i] > S.top()){
                        Addelem(S.top()); 
                        S.pop();
                    }
                    S.push(str[i]);
                }
            } 
        }
    }
    while(!S.empty()){
        Addelem(S.top());
        S.pop();
    }
    result[t] = '\0';
}
int main(){
    while(gets(str)){
        //puts(str);
        Sposfixexpression(str);
        Printfstring(result);
    }
    return 0;
} 

中缀转前缀:  

 

 

 

栈: 先入后出(******)

计算前缀表达式

#include <stack> 
#include <cmath> 
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 600;
char str[MAXN];
void Deal(char *str){
    stack<double> S;
    char num[MAXN];
    strrev(str);                      /*important*/
    double c;
    int t = 0, len = strlen(str);
    for(int i = 0; i < len; i++){
        if(str[i] >= '0' && str[i] <= '9' || str[i] == '.'){
            num[t++] = str[i];
        }
        else if(str[i] == ' ' || str[i] == '\0'){
                num[t] = '\0';
                strrev(num);
                if(atof(num) != 0)          //数和运算符数目是对应的。 
                    S.push(atof(num));
                printf("%d %f\n", i, atof(num)); 
                t = 0;
        }
        else{
            double a = S.top(); S.pop();
            double b = S.top(); S.pop();
            switch(str[i]){
                case '*': c = a * b; break;
                case '/': c = a / b; break;
                case '+': c = a + b; break;
                case '-': c = a - b; 
            }
            S.push(c); 
        }
    }
    printf("%.2lf\n", S.top());
}
int main(){
    while(gets(str)){
        Deal(str);
    }
    return 0;
}

计算后缀表达式;

#include <stack>
#include <cmath>
#include <cstdio>
#include <cstring>
const int MAXN = 600;
using namespace std;
char str[MAXN];
char Deal(char *str){
    stack<double> S;
    int t = 0, len = strlen(str);
    char num[MAXN];
    for(int i = 0; i < len; i++){
        if(str[i] >= '0' && str[i] <= '9' || str[i] == '.'){
            num[t++] = str[i];
        }
        else if(str[i] == ' '){
            num[t] = '\0';     //字符串结束标志; 
            if(atof(num))
                S.push(atof(num));
            t = 0; 
        }
        else{
            double a = S.top(); S.pop();
            double b = S.top(); S.pop(); 
            double c;
            switch(str[i]){
                case '*': c = a*b; break;
                case '/': c = b/a; break;
                case '+': c = a+b; break;
                case '-': c = b-a; break;
            }
            S.push(c);
        }
    }
    printf("%.2lf\n", S.top());
} 
int main(){
    while(gets(str)){
        Deal(str);
    }
    return 0;
}

 

posted on 2015-12-17 22:07  cleverbiger  阅读(164)  评论(0)    收藏  举报