C ++等式与多项式 [英] C++ Equations with Polynomials

查看:163
本文介绍了C ++等式与多项式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理我为自己制作的专案时遇到问题。我对C ++和编程一般没有经验,所以我的代码可能不是最优雅或高效的。目的是将从文件读入的两个多项式相加,相减或相乘。这些方程有一个未知的数量,但现在我处理一个。

I am having a problem dealing with a project I made for myself. I am rather inexperienced with C++ and programming in general so my code may not be the most elegant or efficient. The objective is to add, subtract, or multiply two polynomials together that are being read in from a file. There is an unknown amount of these equations, but for now I am dealing with one.


  • 方程式为(2 * x ^ 2 + 3 * x ^ 4 + 5 * x + 6)+ 1 * x ^ 5 + 4 * x ^ 2 + 7 * x)。

  • 这是我用来测试的方程。

  • 程序正在输出6x ^ 2 + 6x + 2x ^ 2 + 3x。

但我似乎找不到问题在哪里。我的主要问题是程序不会输出正确的答案,即使它得到第一项正确。我知道你们不喜欢代码转储,但我不知道我们将如何找到这个问题。感谢任何帮助。

I have been working on this for a while now today, but I cannot seem to find where the problem is. My main problem is the program won't output the correct answer, even though it gets the first term correct. I know you guys don't like code-dumping, but I'm not sure how else we'll be able to find the problem. Thanks for any help.

    string multiplication(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
    {
        int updated = (counter + 1);
        string line;

        for(int i = 0; i < counter; ++i)
        {
            for(int n = 0; n < iteration; ++n)
            {
                polyArray0[i][0] *= polyArray1[n][0];
                polyArray0[i][1] += polyArray1[n][1];
            }
        }

        line = counter;

        for(int i = 0; i < counter; ++i)
        {
            line += polyArray0[updated][0];
            line += polyArray0[updated][1];
        }

        return line;
    }

    string addition(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
    {
        double polyArray2[MAX_SIZE][2];
        int updated = 0;
        bool additionTo;
        string line;

        for(int i = 0; i < counter; ++i)
        {
            additionTo = false;

            for(int n = 0; n < iteration; ++n)
            {
                if(polyArray0[i][1] == polyArray1[n][1])
                {
                    polyArray0[i][0] += polyArray1[n][0];
                    additionTo = true;
                }
            }

            if(!additionTo)
            {
                polyArray2[updated][0] = polyArray0[i][0];
                polyArray2[updated][1] = polyArray0[i][1];
                ++updated;
            }
        }

        line = static_cast<ostringstream*>(&(ostringstream() << (updated + counter)))->str();

        for(int i = 0; i < counter; ++i)
        {
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray0[i][0]))->str();
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray0[i][1]))->str();
        }

        for(int i = 0; i < updated; ++i)
        {
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray2[i][0]))->str();
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray2[i][1]))->str();
        }

        return line;
    }

    string subtraction(double polyArray0[MAX_SIZE][2], double polyArray1[MAX_SIZE][2], int counter = 0, int iteration = 0)
    {
        double polyArray2[MAX_SIZE][2];
        int updated = counter;
        string line;
        bool additionTo;

        for(int i = 0; i < iteration; ++i)
        {
            polyArray1[i][0] *= -1;
        }

        for(int i = 0; i < counter; ++i)
        {
            additionTo = false;

            for(int n = 0; n < iteration; ++n)
            {
                if(polyArray0[i][1] == polyArray1[n][1])
                {
                    polyArray0[i][0] += polyArray1[n][0];
                    additionTo = true;
                }
            }

            if(!additionTo)
            {
                polyArray2[updated][0] = polyArray0[i][0];
                polyArray2[updated][1] = polyArray1[i][1];
                ++updated;
            }
        }

        line = static_cast<ostringstream*>(&(ostringstream() << (updated + counter)))->str();

        for(int i = 0; i < counter; ++i)
        {
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray0[i][0]))->str();
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray0[i][1]))->str();
        }

        for(int i = 0; i < updated; ++i)
        {
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray2[i][0]))->str();
            line += static_cast<ostringstream*>(&(ostringstream() << polyArray2[i][1]))->str();
        }

        return line;
    }
int main()
{
    //
    // Variables :
    //  polyArray0 : used to store the left side of the equation
    //  polyArray1 : used to store the right side of the equation
    //  updated    : a counter used to ensure that the bounds of the arrays are not being overstepped
    //  counter    : a counter to find the length of a polynomial
    //  iteration  : see updated
    //  startPosition/endPosition : used to find the length of each polynomial inside of the parenthesis
    //  number     : number being converted into the array
    //  newPolynomial : will store the answer in a string
    //  polyInfo   : the string contained within the file
    //  add        : check to see if the program will be using the addition function
    //  subtract   : check to see if the program will be using the subtraction function
    //  multiply   : check to see if the program will be using the multiplication function
    //
    double polyArray0[MAX_SIZE][2];
    double polyArray1[MAX_SIZE][2];
    double updated;
    int counter = 0;
    int iteration = 0;
    int startPosition;
    int endPosition;
    string number;
    string newPolynomial;
    string polyInfo;
    string polynomial0;
    string polynomial1;
    string polynomial2;
    bool polynomial;
    bool adding;
    bool subtracting;
    bool multiplying;

    //
    // This will read in file. The file must be located in the same folder as the .cpp file.
    //
    ifstream polyFile;
    polyFile.open("functions.txt");

    //
    // Use an if statement to make sure the file is opened and then retrieve the information and store it
    // in a string called "polyFile."
    //
    if(polyFile.good())
    {
        getline(polyFile, polyInfo);
    }

    else
    {
        cout << "The file could not be open." << endl;
    }

    polynomial = true;

    //
    // Use a for loop to search through the string and find the math operator located
    // between ")" and "(."
    //
    for(int i = 0; i < polyInfo.length(); ++i)
    {
        if(polyInfo.substr(i, 1) == "(")
        {
            startPosition = (i + 1);
        }

        else if(polyInfo.substr(i, 1) == ")")
        {
            if(polynomial)
            {
                polynomial0 = polyInfo.substr(startPosition, (i - 1));
                polynomial = false;

                //
                // To determine whether the program will be adding, subtracting, or multiplying, we
                // set one of the booleans to true depending on which character was found.
                //
                if(polyInfo.substr(i + 1, 1) == "+")
                {
                    adding = true;
                }

                else if(polyInfo.substr(i + 1, 1) == "-")
                {
                    subtracting = true;
                }

                else if(polyInfo.substr(i + 1, 1) == "*")
                {
                    multiplying = true;
                }
            }

            else
            {
                polynomial1 = polyInfo.substr(startPosition, i - startPosition);
            }
        }
    }

    polynomial2 = polynomial0;

    //
    // For loops and if statements are used to parse the string into an array so we will be able to 
    // easily handle the coefficients and powers later on for the operations.
    //
    for(int i = 0; i < polynomial2.length(); ++i)
    {
        if(i == 0)
        {
            if(polynomial2.substr(0, 1) == "x")
            {
                polyArray0[counter][0] = 1;

                if(polynomial2.substr(1, 1) != "^")
                {
                    polyArray0[counter][1] = 1;
                    ++counter;
                }
            }

            else
            {
                number = polynomial2.substr(0, 1);
                polyArray0[counter][0] = atoi(number.c_str());

                if(polynomial2.substr(i, 1) == "-")
                {
                    polyArray0[counter][0] *= -1;
                }

                if(polynomial2.substr(1, 1) != "*")
                {
                    polyArray0[counter][1] = 0;
                    ++counter;
                }

                else if((polynomial2.substr(i + 2, 1) == "*") && (polynomial2.substr(i + 5, 1) != "^"))
                {
                    polyArray0[counter][1] = 1;
                    ++counter;
                }
            }
        }

        else if((polynomial2.substr(i, 1) == "-") || (polynomial2.substr(i, 1) == "+"))
        {
            if(polynomial2.substr(i + 1, 1) == "x")
            {
                polyArray0[counter][0] = 1;

                if(polynomial2.substr(i, 1) == "-")
                {
                    polyArray0[counter][0] *= -1;
                }

                if(polynomial2.substr(i + 2, 1) != "^")
                {
                    polyArray0[counter][1] = 1;
                    ++counter;
                }
            }

            else
            {
                number = polynomial2.substr(i + 1, 1);
                polyArray0[counter][0] = atoi(number.c_str());

                if(polynomial2.substr(i, 1) == "-")
                {
                    polyArray0[counter][0] *= -1;
                }

                if(polynomial2.substr(i + 2, 1) != "*")
                {
                    polyArray0[counter][1] = 0;
                }

                else if((polynomial2.substr(i + 2, 1) == "*") && (polynomial2.substr(i + 4, 1) != "^"))
                {
                    polyArray0[counter][1] = 1;
                    ++counter;
                }
            }
        }

        else if(polynomial2.substr(i, 1) == "^")
        {
            number = polynomial2.substr(i + 1, 1);
            polyArray0[counter][1] = atoi(number.c_str());
            ++counter;
        }
    }

        polynomial2 = polynomial1;

        for(int i  = 0; i < polynomial2.length(); ++i)
        {
            if(i == 0)
            {
                if(polynomial2.substr(0, 1) == "x")
                {
                    polyArray0[iteration][0] = 1;

                    if(polynomial2.substr(1, 1) != "^")
                    {
                        polyArray1[iteration][1] = 1;
                        ++iteration;
                    }
                }

                else
                {
                    number = polynomial2.substr(0, 1);
                    polyArray1[iteration][0] = atoi(number.c_str());

                    if(polynomial2.substr(i, 1) == "-")
                    {
                        polyArray0[counter][0] *= -1;
                    }

                    if(polynomial2.substr(1, 1) != "*")
                    {
                        polyArray1[iteration][1] = 0;
                        ++iteration;
                    }

                    else if((polynomial2.substr(i + 1, 1) == "*") && (polynomial2.substr(i + 3, 1) != "^"))
                    {
                        polyArray1[iteration][1] = 1;
                        ++iteration;
                    }
                }
            }

            else if((polynomial2.substr(i, 1) == "+") || (polynomial2.substr(i, 1) == "-"))
            {
                if(polynomial2.substr(i + 1, 1) == "x")
                {
                    polyArray1[iteration][0] = 1;

                    if(polynomial2.substr(i, 1) == "-")
                    {
                        polyArray0[counter][0] *= -1;
                    }

                    if(polynomial2.substr(i + 2, 1) != "^")
                    {
                        polyArray1[iteration][1] = 1;
                        ++iteration;
                    }
                }

                else
                {
                    number = polynomial2.substr(i + 1, 1);
                    polyArray1[iteration][0] = atoi(number.c_str());

                    if(polynomial2.substr(i, 1) == "-")
                    {
                        polyArray0[counter][0] *= -1;
                    }

                    if(polynomial2.substr(i + 2, 1) != "*")
                    {
                        polyArray1[iteration][1] = 0;
                        ++iteration;
                    }

                    else if((polynomial2.substr(i + 2, 1) == "*") && (polynomial2.substr(i + 4, 1) != "^"))
                    {
                        polyArray1[iteration][1] = 1;
                        ++iteration;
                    }
                }
            }

            else if(polynomial2.substr(i, 1) == "^")
            {
                number = polynomial2.substr(i + 1, 1);
                polyArray1[iteration][1] = atoi(number.c_str());
                ++iteration;
            }
        }

        //
        // If the program found a "+" it will call the addition function and store the answer in "newPolynomial."
        // Similar process with subtracting and multiplying.
        //
        if(adding)
        {
            newPolynomial = addition(polyArray0, polyArray1, counter, iteration);
        }

        else if(subtracting)
        {
            newPolynomial = subtraction(polyArray0, polyArray1, counter, iteration);
        }

        else
        {
            newPolynomial = multiplication(polyArray0, polyArray1, counter, iteration);
        }

        number = newPolynomial.substr(0, 1);
        updated = atoi(number.c_str());

        for(int i = 1; i < (updated + 1); ++i)
        {
            number = newPolynomial.substr(i, 1);
            polyArray0[i][0] = atoi(number.c_str());
            number = newPolynomial.substr((i + updated), 1);
            polyArray0[i][1] = atoi(number.c_str());
        }

        for(int i = 0; i < updated; ++i)
        {
            cout << polyArray0[i][0] << "x^" << polyArray0[i][1];

            if(polyArray0[i][0] > 0 && i != updated)
            {
                cout << "+";
            }
        }

    cout << endl;
    system("PAUSE");
    return 0;
}


推荐答案

此程序中值得一提的要点:

Key points in this program that are worth mentioning:


  1. 您可以在一维数组中表示多项式的系数。 x ^ N 的系数位于数组中的 N 中。

  1. You can represent the coefficients of a polynomial in a 1D array. The coefficient for x^N goes in the N-th place in the array. There is no need to deal with the complexities of 2D arrays.

保持多项式的加法操作与方法分开,将多项式转换为可显示的字符串

Keep the operation of addition of polynomials separate from the method to convert a polynomial into a displayable string.

我会留给你扩展代码以实现减法和乘法运算。 >

I'll leave it to you to extend the code to implement the subtraction and multiplication operations.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define MAX_SIZE 10

string polyArrayToString(double polyArray[])
{
   ostringstream ostr;
   bool empty = true;
   for(int n = MAX_SIZE-1; n >= 0; --n)
   {
      if ( polyArray[n] != 0 )
      {
         if ( !empty )
         {
            ostr << " + ";
         }

         ostr << polyArray[n];
         if ( n > 0 )
         {
            ostr << "*x^" << n;
         }
         empty = false;
      }
   }

   return ostr.str();
}

void addition(double polyArray0[],
              double polyArray1[],
              double polyArrayRes[])
{
   for(int n = 0; n < MAX_SIZE; ++n)
   {
      polyArrayRes[n] = polyArray0[n] + polyArray1[n];
   }
}

int main()
{
   // (2*x^2+3*x^4+5*x+6)
   double polyArray0[MAX_SIZE] = {0};
   polyArray0[0] = 6;
   polyArray0[1] = 5;
   polyArray0[2] = 2;
   polyArray0[4] = 3;

   // (1*x^5+4*x^2+7*x)
   double polyArray1[MAX_SIZE] = {0};
   polyArray1[1] = 7;
   polyArray1[2] = 4;
   polyArray1[5] = 1;

   double polyArrayRes[MAX_SIZE] = {0};
   addition(polyArray0, polyArray1, polyArrayRes);

   cout << "polyArray0: " << polyArrayToString(polyArray0) << endl;
   cout << "polyArray1: " << polyArrayToString(polyArray1) << endl;
   cout << "polyArray0 + polyArray1: " << polyArrayToString(polyArrayRes) << endl;

   return 0;
}

这篇关于C ++等式与多项式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆