我需要一些关于多项式的帮助

时间:2015-11-29 00:47:33

标签: c# arrays console polynomial-math

如何使用数组创建多项式函数?基本上我想显示多项式(例如x ^ 2 + 3x + 5)。然后,我想表明最高多项式度是多少。例如,x ^ 2 + 3x + 5 = 2(最高度)。

2 个答案:

答案 0 :(得分:1)

// initial highest exponent
int exponent = 0;

// We're assuming [in] is a stream such as istream in C++.
if (char == ^)
{
     in >> char;

     if (char > initial)
     {
          initial = char)
     }
 }

我们还必须检查char之后的x+-还是空格。我们还希望检查该指数。

答案 1 :(得分:1)

我可能会建议这样做。

        string polynomial = "x^2 + 3x + 5";
        int index = 0,highestdegree = 0;
        foreach (char character in polynomial) {
            if(character == '^')
            {
                index++;
                try{
                    int test;
                    int.TryParse(polynomial[index],out test);
                    if(test >highestdegree)
                        highestdegree = test;
                    index--;
                }
                catch{
                    index--;
                }
            }
            index += 1;
        }
      if(highest degree == 0)
       { 
            highestdegree == 1;
       }
      return highest degree;

您必须首先将多项式转换为字符串。

相关问题