4常量表达式计算器



#include <iostream>

#include <cstdlib>

#include <cctype> //字符串判定

 

using namespace std;

const int MAX = 1024;

double operation(char *str);

char * extract(char *str, int &index)

{

    char *pstr(nullptr);//处理字符串

    int num(0);//记录一下多少对括号

    int bufindex(index);//记录下标

 

    do

    {

        switch (*(str + index))

        {

        case ')':

            if (0 == num)

            {

                ++index;

                pstr = new char[index - bufindex];

                if (!pstr)

                {

                    throw  "malloc fail";

                }

                //拷贝字符串

                strncpy_s(pstr, index - bufindex, str + bufindex, index - bufindex - 1);

                return pstr;

            }

            else

            {

                num--;

            }

            break;

        case '(':

            num++;

            break;

        }

    } while (*(str + index++) != '\0');

    throw  "errorfail";

}

 

//获得计算表达是中字符串的数字

double getNum(char *str, int &index)

{

    double value(0.0);

 

    if (*(str + index) == '(')

    {

        char *substr(nullptr);

        substr = extract(str, ++index);

 

        value = operation(substr);

        delete[] substr;

 

        return value;

    }

 

    if (!isdigit(*(str + index)))

    {

        char error[30] = "geterror";

        throw error;

    }

 

    //判断数值是否是数值

    while (isdigit(*(str + index)))

    {

        value = 10 * value + (*(str + index++) - '0');

    }

    //带有小数点时不做处理

    if (*(str + index) != '.')

    {

        return value;

    }

    else

    {

        double decimals(1.0); //定义一个小数

        while (isdigit(*(str + (++index))))

        {

            decimals /= 10;

            value = value + (*(str + index) - '0') * decimals;

        }

        return value;

    }

}

 

double term(char *str, int & index)

{

    double value(0.0);

    value = getNum(str, index);//获取数据

    while (1)

    {

        if (*(str+index) == '*')

        {

            //乘除法

            value *= getNum(str, ++index);

        }

        else if (*(str + index) == '/')

        {

            value /= getNum(str, ++index);

        }

        else

        {

            break;

        }

    }

    return value;

}

 

double operation(char *str)

{

    double value(0.0);

    int index(0);

    value += term(str, index);

    for (;;)

    {

        switch (*(str + (index++)))

        {

        case '\0':

            return value;

            break;

        case '+':

            value += term(str,index);

            break;

        case '-':

            value -= term(str, index);

            break;

        default:

            break;

        }

    }

}

 

void removeBlankSpace(char *str)

{

    int i(0);

    int j(0);

    //*(str + i) = *(str + j++) :通过这种方式循环获得字符串中的每个字符

    //下面的意思是获得字符不是'\0',也就是说没有到达字符串末尾

    //下面的方式是把不是空格的字符赋值给*(str+i)

    while ((*(str + i) = *(str + j++))!= '\0')

    {

        if (*(str+i) != ' ')

        {

            i++;

        }

    }

    //两个下标轮替,往前移动,链表的算法一样,循环向前挖

}

 

void main()

{

    char str[MAX] = { 0 };

    cout << "请输入表达式";

    cin.getline(str, MAX);

    cout << "\n" << str << endl;

 

    //去除空格

    removeBlankSpace(str);

 

    cout << str << endl;

 

    cout << operation(str) << endl;

 

    cin.get();

}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值