I am having a little bit of trouble finishing a project I have made for myself. Also, before we begin, it is important to note I don't have much experience with C++ programming and programming in general. I realize my code is probably not the most efficient or even elegant. The project is to read in a file, which I call "functions.txt," with an unknown amount of equations dealing with polynomials in the form of (2x^3+4x-7x^2)+(1x-8x^3+2). The operation between the two parenthesis can be subtraction, multiplication, or addition. There is no spaces in the file and each equation is on a separate line. I feel I almost have it but I cannot get it to output the right answer. I have been working on it all day and I feel a fresh pair of eyes may be able to help me out. I hate to dump all of my code, but I don't believe there is another way to go about solving this since I do not know exactly where the problem is. Also, this code only deals with one equation. Any help on how to make it go about solving an unknown amount would be great. I believe putting all of the code in a loop might do it, but I want to fix my problem first. Again, here is my code and thanks for any help.

``````const int MAX_SIZE = 100;

string multiplication(double polyArray0[MAX_SIZE], double polyArray1[MAX_SIZE], 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] *= polyArray1[n];
polyArray0[i] += polyArray1[n];
}
}

line = counter;

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

return line;
}

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

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

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

{
polyArray2[updated] = polyArray0[i];
polyArray2[updated] = polyArray0[i];
++updated;
}
}

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

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

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

return line;
}

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

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

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

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

{
polyArray2[updated] = polyArray0[i];
polyArray2[updated] = polyArray1[i];
++updated;
}
}

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

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

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

return line;
}

int main()
{
double polyArray0[MAX_SIZE];
double polyArray1[MAX_SIZE];
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 subtract;
bool multiply;

ifstream polyFile;
polyFile.open("functions.txt");

if(polyFile.good())
{
getline(polyFile, polyInfo);
}
else
{
cout << "The file could not be open." << endl;
}

polynomial = true;

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;

if(polyInfo.substr(i + 1, 1) == "+")
{
}

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

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

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

// put the string into the polynomial array
polynomial2 = polynomial0;

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

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

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

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

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

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

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

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

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

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

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

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

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

else if(polynomial2.substr(i, 1) == "^")
{
number = polynomial2.substr(i + 1, 1);
polyArray0[counter] = 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] = 1;

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

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

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

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

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

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

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

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

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

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

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

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

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

cout << polynomial0 << endl;
cout << polynomial1 << endl;
cout << polyArray0 << "x^" << polyArray0 << endl;
cout << polyArray0 << "x^" << polyArray0 << endl;
cout << polyArray1 << "x^" << polyArray1 << endl;
cout << polyArray1 << "x^" << polyArray0 << endl << endl;

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

else if(subtract)
{
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] = atoi(number.c_str());
number = newPolynomial.substr((i + updated), 1);
polyArray0[i] = atoi(number.c_str());
}

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

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

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

## All 2 Replies

Just a few points:
°°° This C++ used like C, rethink your design. 