数据结构之算术表达式

#include<iostream>
#include<cstdio>
#include<stack>
#include<math.h>
using namespace std;

int charToInt(char str[], int n)
{
    int count = 0;
    for (int i = n - 1; i >= 0; i--)
        count += (str[i] - '0')*pow(10, n - 1 - i);
    return count;
}

int Compare(stack<char>ope, char ch)//比较运算符的优先级
{
    //判断当前优先级是否比栈顶操作符优先级高,操作符压入栈
    char top_ope;
    if (ope.empty())
        top_ope = '&';
    else
        top_ope = ope.top();
    if ((top_ope == '-' || top_ope == '+') && (ch == '*' || ch == '/'))
    {
        return 0;
    }

    else if (ope.empty() || ch == '(' || (top_ope == '(' && ch != ')'))
    {
        return 0;
    }
    //括号内的表达式计算完毕
    else if (top_ope == '(' && ch == ')')
    {
        
        return 1;
    }
    //其他情况则计算 
    else
    {
        return -1;
    }

}

int Calculate(char a[])
{
    stack<int>num;
    stack<char>ope;
    int i = 0, j = 0, op_num, flag;
    char temp[20];
    while (a[i] != '\0')
    {
        if (a[i] >= '0'&&a[i] <= '9')
        {
            j = i + 1;
            temp[0] = a[i];
            while (a[j] >= '0'&&a[j] <= '9'&&a[j] != '\0')
            {
                temp[j - i] = a[j];
                j++;
            }
            op_num = charToInt(temp, j - i);
            //操作数入栈 
            num.push(op_num);
            i = j;
        }
        while (1)
        {
            flag = Compare(ope, a[i]);      //判断操作符优先级

            if (flag == 0)
            {
                if (a[i] == '\0')//结束了避免i越界
                {
                    i--;
                    break;
                }
                ope.push(a[i]);   //压入操作符
                break;
            }

            else if (flag == 1)     //判断括号内的表达式是否结束
            {
                ope.pop();
                i++;
            }

            else if (flag == -1)   //进行数据处理
            {
                //取出数据栈中两个数据
                float num_1 = num.top();
                num.pop();
                float num_2 = num.top();
                num.pop();

                float value = 0;
                char ch = ope.top();
                ope.pop();

                if (ch == '+') //加法操作
                {
                    value = num_1 + num_2;
                }

                else if (ch == '-')  //减法操作
                {
                    value = num_2 - num_1;
                }

                else if (ch == '*')  //乘法操作
                {
                    value = num_2 * num_1;
                }

                else if (ch == '/')   //除法操作
                {
                    value = num_2 / num_1;
                }

                num.push(value); //将得到的值压入数据栈                         
            }
        }
        i++;
    }
    return num.top();
}

int main()
{
    char a[20];
    cout << "输入给定的表达式:\n";
    cin >> a;
    cout << Calculate(a) << endl;
    getchar();
    getchar();
    return 0;
}

/*
(10+2)*6-12/3
*/

 

转载于:https://www.cnblogs.com/BetterThanEver_Victor/p/8195700.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值