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][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()
{
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 add;
bool subtract;
bool multiply;
//read in file
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) == "+")
{
add = true;
}
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][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;
}
}
cout << polynomial0 << endl;
cout << polynomial1 << endl;
cout << polyArray0[0][0] << "x^" << polyArray0[0][1] << endl;
cout << polyArray0[1][0] << "x^" << polyArray0[1][1] << endl;
cout << polyArray1[0][0] << "x^" << polyArray1[0][1] << endl;
cout << polyArray1[1][0] << "x^" << polyArray0[1][1] << endl << endl;
if(add)
{
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][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;
}