简单的表达式计算(中缀、前缀、后缀)

利用中缀表达是现实计算

中缀表达式入一下形式:
3+4 * 5 * (2+3)
((3+5 * 2)+3)/5+6/4 * 2+3

所谓的中缀表达是就是我们正常计算时候使用的表达式
代码如下:

#include
#include
#include
using namespace std;
const int Min = 1e-6;
const int maxsize =  1e3;
int opt(float a, float b, char type, float &result)
{
    if(type == '+'){

            result = a + b;
            return 1;
    }
    else if(type == '-'){
            result = a - b;
            return 1;
    }
    else if(type == '*'){
            result = a * b;
            return 1;
    }
    else if(type == '/')
    {
        if(fabs(b) < Min){
               // cout<<"-----------b"<
                return 0;
        }
        else
        {
            result = a / b;
            return 1;
        }
    }
}
int GetPriority(char type)
{
    if(type == '+' || type == '-')return 0;
    else return 1;
}
float Cal(char exp[])
{
    int i = 0, top1 = -1, top2 = -1;//s1用来存储数字,s2用来存储符号
    float s1[maxsize];
    char s2[maxsize];
    while(exp[i] != '\0')
    {

        if(exp[i] >= '0' && exp[i] <= '9' )
        {
            //cout<
            s1[++top1] = exp[i] - '0';
            i++;

        }
        else if(exp[i] == '(')
        {
            //cout<
            s2[++top2] = exp[i];
            i++;

        }
        else if(exp[i] == '+'||exp[i] == '-'||exp[i] == '*'||exp[i] == '/')
        {
            if(top2 == -1 || s2[top2] == '(' || GetPriority(exp[i]) > GetPriority(s2[top2]))
            {
                s2[++top2] = exp[i];
                //cout<
                i++;
            }
            else
            {
                float a, b, res;
                char type;
                b = s1[top1--];
                a = s1[top1--];

                type = s2[top2--];

                int flag = opt(a, b, type, res);
                // cout<<"a   b"<
                //cout<<" res "<
                if(!flag)printf("计算出现问题\n");
                s1[++top1] = res;

            }
        }
        else if(exp[i] == ')')
        {
            while(s2[top2] != '(')
            {
                float a, b, res;
                char type;
                b = s1[top1--];
                a = s1[top1--];
                type = s2[top2--];

                int flag = opt(a, b, type, res);
                // cout<<"a   b"<
                //cout<<" res "<
                if(!flag)printf("计算出现问题\n");
                s1[++top1] = res;
            }
            --top2;
            i++;
        }



    }
    while(top2 != -1)
    {
        float a, b, res;
        char type;
        b = s1[top1--];
        a = s1[top1--];
        type = s2[top2--];
        //cout<<"a   b"<
        int flag = opt(a, b, type, res);
        //cout<<" res "<
        if(flag == 0)printf("计算出现问题\n");
        s1[++top1] = res;
    }
    return s1[top1];


}

int main()
{
    char a[100];
    int n;
    scanf("%s", a);
    //cout<
    float res = Cal(a);
    printf("%.5f\n",res );


}
/*
11
3+4*5*(2+3)

((3+5*2)+3)/5+6/4*2+3
*/


利用后缀表达是现实计算

后缀表达式入一下形式:
345 * 23+ * +
352 * +3+5/64/2 * 3++

所谓的后缀表达是就是我们正常计算时候使用的表达式
代码如下:

#include
#include
#include
using namespace std;
const int Min = 1e-6;
const int maxsize =  1e3;
int opt(float a, float b, char type, float &result)
{
    if(type == '+')
    {

        result = a + b;
        return 1;
    }
    else if(type == '-')
    {
        result = a - b;
        return 1;
    }
    else if(type == '*')
    {
        result = a * b;
        return 1;
    }
    else if(type == '/')
    {
        if(fabs(b) < Min)
        {
            // cout<<"-----------b"<
            return 0;
        }
        else
        {
            result = a / b;
            return 1;
        }
    }
}

float Cal(char exp[])
{
    float s[maxsize];
    int top = -1;
    for(int i = 0; exp[i] != '\0'; i++){
        if(exp[i] >= '0' && exp[i] <= '9'){
            s[++top] = exp[i] - '0';
        }
        else {
            float a, b, res;
            char type;
            type = exp[i];
            b = s[top--];
            a = s[top--];
            int flag = opt(a, b, type, res);
            if(!flag)puts("出现问题!");
            s[++top] = res;
            //printf("%.6f\n", res);
        }
    }
    return s[top];

}
int main()
{
    char a[maxsize];
    scanf("%s", a);
    float res = Cal(a);
    printf("%.5f\n", res);
    return 0;
}

/*

345*23+*+
352*+3+5/64/2*3++

*/

利用前缀表达是现实计算

前缀表达式入一下形式:
+3 * 4 * 5+23
所谓的前缀表达是就是我们正常计算时候使用的表达式
代码如下:

#include
#include
#include
#include
using namespace std;
const int Min = 1e-6;
const int maxsize =  1e3;
int opt(float a, float b, char type, float &result)
{
    if(type == '+')
    {

        result = a + b;
        return 1;
    }
    else if(type == '-')
    {
        result = a - b;
        return 1;
    }
    else if(type == '*')
    {
        result = a * b;
        return 1;
    }
    else if(type == '/')
    {
        if(fabs(b) < Min)
        {
            // cout<<"-----------b"<
            return 0;
        }
        else
        {
            result = a / b;
            return 1;
        }
    }
}

float Cal(char exp[])
{
    float s[maxsize];
    int top = -1;
    for(int i = strlen(exp)-1; i >= 0; i--){
        if(exp[i] >= '0' && exp[i] <= '9'){
            s[++top] = exp[i] - '0';
        }
        else {
            float a, b, res;
            char type;
            type = exp[i];
            a = s[top--];
            b = s[top--];
            int flag = opt(a, b, type, res);
            if(!flag)puts("³öÏÖÎÊÌâ!");
            s[++top] = res;
            //printf("%.6f\n", res);
        }
    }
    return s[top];

}
int main()
{
    char a[maxsize];
    scanf("%s", a);
    float res = Cal(a);
    printf("%.5f\n", res);
    return 0;
}

/*

+3*4*5+23
*/


你可能感兴趣的:(其他)