首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何计算多项式(x^2^2^2^2+x^2^2^2)

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

Stack Overflow用户
提问于 2016-06-23 16:05:38
回答 2查看 167关注 0票数 1

我想计算(x^2^2^2^2+x^2^2^2)结果应该是x^256+x^16..but我不能完全做到这一点..我也写了一个代码,这是工作的前半部分(前'+'),但在另一半它无法做到它…

代码语言:javascript
运行
复制
#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");

 }
EN

回答 2

Stack Overflow用户

发布于 2016-06-23 16:15:33

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

在您所暴露的问题中,人们很容易说:如果第二个操作数旁边有一个运算符,那么我必须累加最后一个结果,并使用该结果和下一个操作进行操作。然而,有一些问题,如优先级,将不会用这种方法处理。

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

代码语言:javascript
运行
复制
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)

一旦你掌握了这一点,那么就有可能开始使用中缀计算器。

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

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

票数 1
EN

Stack Overflow用户

发布于 2016-06-25 16:10:12

这闻起来像家庭作业/作业,所以我不会提供代码...

在我看来,你只是想让字符串解析器替换掉power部分。我假设您仍然不理解幂数学,或者错误地编写/解释字符串的表示形式。例如:

代码语言:javascript
运行
复制
x^2^2^2^2 =
(((x^2)^2)^2)^2 =
(((x.x)^2)^2)^2 =
((x.x).(x.x))^2)^2 =
((x.x.x.x))^2)^2 =
((x.x.x.x).(x.x.x.x))^2 =
(x.x.x.x.x.x.x.x)^2 =
(x.x.x.x.x.x.x.x).(x.x.x.x.x.x.x.x) =
(x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x) =
x^16

而不是你的x^256。您不能将父项添加到您想要的位置,它们必须按照数学运算的顺序放置,否则得到的公式将与输入字符串不匹配!。如果您为解析器定义了不同的解析规则,那么在标准数学中,您需要在问题中定义它们。

现在如何实现这一点:

  1. read string

我会从常量硬编码字符串开始,而不是在一遍又一遍地编程/调试时键入它(就像许多学生做的那样……我很少看到人们在每个构建版本上输入5x5矩阵:) ...这太疯狂了)

当程序正常工作时,才使用cin读数...因为您已经do

  • detect字符串的哪一部分是power exponent

代码语言:javascript
运行
复制
1. `exponent=1`
2. Search the string for first `^` and remember the start position `i0` if not found goto **#4**.
3. Now depending on what follows:
代码语言:javascript
运行
复制
    - If number multiply `exponent` by it.
    - If `^` skip it and goto to **#2**
    - if neither stop

粗略的,如果你应该支持父母,那么它将会复杂得多,你需要解码整个事情,这不是微不足道的,你也应该在问题中提到。

包含计算字符串(如果可计算)的

  1. replace原始指数字符串

因此,计算出的字符串将是"^"<<exponent还是"^"+exponent,这取决于您使用....

  • output string的字符串算法的类型

使用cout或其他任何工具,就像您已经在使用一样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37985612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档