c++ - 如何计算多项式 (x^2^2^2^2+x^2^2^2)

标签 c++ algorithm

我想计算 (x^2^2^2^2+x^2^2^2) 结果应该是 [x^256+x^16]..但我无法完全做到这一点..我也写了一个代码,它在前半部分(在“+”之前)工作,但在另一半却无法做到......

#include<iostream>
    #include<string>
    #include <algorithm>
    #include<sstream>

  using namespace std;
  int main()
 {
string a;
cin >> a;
string s1 = "^";
string::size_type foud;
foud = a.find(s1);
int i = foud;
int flag = 0;
i++;
while (foud != std::string::npos)
{
    flag = 0;
    cout << i <<"I"<< endl;

    while (flag != 1 && i < a.length())
    {
        if (a[i] == '(' || a[i] == '+' || a[i] == '-' || a[i] == ')')
        {
            flag++;
            cout << "terminator" << endl;
        }


        else if (a[i] == '^')
        {
            /*int j = (int)(a[i - 1]);
            j = j - 48;
            int k = (int)(a[i + 1]);
            k = k - 48;
            i = k + 1;
            int power =0;
            power = pow(j, k);
            ;*/
            int j = i;
            int k = i;
            k--;
            j++;
            string bcknumber;
            while (a[k] != '^' && a[k] != '(' && a[k] != '+' && a[k] != '-' && a[k] != ')')
            {
                bcknumber = bcknumber + a[k];
                k--;

            }
            cout << bcknumber << endl;
            reverse(bcknumber.begin(), bcknumber.end());
            cout << bcknumber << endl;

            int BK;
            BK = stoi(bcknumber);

            int FD;
            string frdnum;
            while (a[j] != '^'&&a[j] != '\0' && a[j] != '(' && a[j] != '+' && a[j] != '-' && a[j] != ')')
            {
                frdnum = frdnum + a[j];
                j++;

            }
            FD = stoi(frdnum);
            int resil = pow(BK, FD);
            frdnum.clear();
            stringstream s;
            string res;
            s << resil;
            res = s.str();
            if (i == 15)
            {
                a.replace(14, 15, res);
            }
            else
            {
                a.replace(i - bcknumber.length(), i + frdnum.length(), res);
            }

            i--;
            bcknumber.clear();


        }
        else
            i++;
    }
    foud = a.find("^", foud + 1);
    i = foud;
    i++;


}

cout << a << endl;
system("pause");

 }

最佳答案

这不是一个小问题。你想构建一个中缀计算器 (a + b)。前缀计算器 (+ a b) 或后缀计算器 (a b +) 更简单,因为根本没有歧义。一个中缀计算器可以有很多,这取决于您希望用户拥有的自由度。

在您提出的问题中,有人很想说:好吧,如果第二个操作数旁边有一个运算符,那么我必须累加最后一个结果并用它和下一个操作进行运算。但是,存在诸如优先级之类的问题,该方法不会处理。

我会开始创建一个前缀计算器。这要容易得多:

calculate():
   opr = match operator
   op1 = match operand
   if op1 is operator:
       back
       op1 = calculate

   op2 = match operand
   if op2 is operator:
       back
       op2 = calculate

   return calc(opr, op1, op2)

一旦掌握了它,就可以从中缀计算器开始。

例如,在最后一个算法中要做的一件事是更改它以避免递归。

这是一个很好的练习,享受它。希望这会有所帮助。

关于c++ - 如何计算多项式 (x^2^2^2^2+x^2^2^2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37985612/

相关文章:

string - 如何查找字符串 S 是否包含在由 S 组成的字符串中,该字符串插入到 S 本身的任何位置(仅一次)

java - 需要解释排列算法的差异

algorithm - 交替相等项目的排序算法

c++ - 将 smart_pointer 传递给构造函数与原始指针

c++ - 为什么将 "&& true"添加到约束会使函数模板成为更好的重载?

c++ - #include 和可能的循环引用

c++ - 查找满足查询条件的geohashes的算法

arrays - 从数组中加权随机选择

c++ - 我不知道这个随机发生器有什么问题

c++ - 如何在 C++ 中链接追加