# Operations on Polynomial code

Page 1 of 1

## 1 Replies - 166 Views - Last Post: 21 October 2019 - 07:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417628&amp;s=222e59814b664289264b419541439c72&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 KYLAE

Reputation: 0
• Posts: 1
• Joined: 21-October 19

# Operations on Polynomial code

Posted 21 October 2019 - 06:49 PM

Question: How can i Modify this code in order to create 3 new textfiles that contain the answers for every operation.

```#include<cstdlib>
#include<ios>
#include<limits>
#include<iostream>
#include<list>
#include<fstream>
#include<math.h>
#include<windows.h>
#include<conio.h>

using namespace std;

typedef list< pair<int, int> > myList;

//FUNCTION DECLARATIONS

int option();
void insert(myList &l1, myList &l2);
void eval(myList &l1, float val);
void insPoly(myList &l1, myList &l2, myList &l3);
int evalList(myList &l1,myList &l2,myList &l3,myList &l4);
int analChoice();
void checkin();

int main()
{
myList l1;
myList l2;
myList l3;
myList l4;
insert(l1, l2);
again:
system("cls");
option();

cout<<endl;
char ch;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter your choice:  ";
cin >> ch;

{
switch(ch)
{
case '1':
system("cls");
insPoly(l1, l2, l4);
l4.clear();
goto again;
system("cls");
return option();

case '2':
system("cls");
Poly_Prod(l1,l2,l3);
l3.clear();
goto again;
system("cls");
return option();

case '3':
system("cls");
evalList(l1,l2,l3,l4);
goto again;
system("cls");
return option();

case '4':
system("cls");
sort_deg(l1);
sort_deg(l2);
goto again;
system("cls");
return option();

case '5':
analChoice();
goto again;
system("cls");
break;

default:
{
cout << "\n\n\n\n---------------Enter the right key !---------------\n\n" << endl;
system("pause");
cout<<"\n";
goto again;
system("cls");
return option();

}
}
}

getch();
return 0;
}

int option()
{
system("cls");
cout << "=============================================================================" << endl;
cout << "                                                           " << endl;
cout << "           Polynomial Operations Menu                     " << endl;
cout << "                                                          " << endl;
cout << "=============================================================================" << endl;
cout << "                                                           " << endl;
cout << "              1.	ADDITION OF POLYNOMIALS                " << endl;
cout << "                                               	            " << endl;
cout << "                                               	            " << endl;
cout << "              2.	MULTIPLICATION OF POLYNOMIALS                    " << endl;
cout << "                                               	             " << endl;
cout << "                                              	             " << endl;
cout << "              3.	EVALUATION OF POLYNOMIALS                  " << endl;
cout << "                                              	             " << endl;
cout << "                                               	             " << endl;
cout << "              4.	Print first and second polynomial         " << endl;
cout << "                                               	            " << endl;
cout << "                                                	            " << endl;
cout << "              5.	Exit                                      " << endl;
cout << "                                               	            " << endl;
cout << "\n\n				Press any Key to Choose Option			"<<endl;
cout << "=============================================================================" << endl;

}

void insert(myList &l1, myList &l2)
{
char filename1[30];
char filename2[30];
int coeff1, expo1,coeff2, expo2;
char garb1, garb2;
cout<<"\n\n----------------Welcome to the Operations of Polynomials !----------------------\n\n"
<<"****************************Press Enter to Start********************************\n\n";
again:
cout<<endl;

cout << "Enter filename that contains first polynomial: ";
cin.getline(filename1, 30);
ifstream poly1;
ifstream poly2;

poly1.open(filename1);
if(!poly1.fail()){

poly1.seekg(0, ios::end);
int place = poly1.tellg();
if( place == 0)
{
cout << "There are no content present in the file..\n";
goto again;
}
else
{
poly1.seekg(0, ios::beg);
while(!poly1.eof())
{
poly1 >> coeff1 >> garb1>> garb2>> expo1;
l1.push_back(make_pair (coeff1, expo1));
}
}
}
else{
cout<<"error opening file!"<<endl;
goto again;
}

sort_deg(l1);
cout << "\n\n";
cout<<endl;
again2:
cout << "Enter filename containing the second polynomial: ";
cin.getline(filename2, 30);

poly2.open(filename2);
if(!poly2.fail()){

poly2.seekg(0, ios::end);
int spot = poly2.tellg();
if( spot == 0)
{
cout << "Empty file\n";
goto again;
}

else

{
poly2.seekg(0, ios::beg);
while(!poly2.eof())
{
poly2 >> coeff2 >> garb1>> garb2>> expo2;
l2.push_back(make_pair (coeff2, expo2));
}
}
}

else{
cout<<"error ! error ! error !"<<endl;
goto again2;
system("cls");
}
sort_deg(l2);
cout << endl;
poly1.close();
poly2.close();
}

{
int a, b;
int s3 = linklist3.size();  //s is for size

int coeff1[s1], coeff2[s2], coeff3[s3];
int expo1[s1],expo2[s2],expo3[s3];

int r = 0;
for (a = 0; a < s1; a++){

coeff1[a] = (*pos1).first;   							//transferring values.
expo1[a] = (*pos1).second;

pos2 = linklist2.begin();        						// Declaring the iterator to be
// pointing at the first node.
for (b = 0; b < s2; b++)
{
coeff2[b] = (*pos2).first;    							//transferring values.
expo2[b] = (*pos2).second;

//START OF MULTIPLICATION OF THE GIVEN POLYNOMIALS

coeff3[r] = coeff1[a] * coeff2[b];
expo3[r] = expo1[a] + expo2[b];

//Here, we put the product to list3.
r++;
pos2++; 												//changing the pointing position.

}
pos1++;
}

int count = 0;
int size = linklist3.size(); 									//"list_name.size()" is a predefined function for
cout << endl;													//getting the size of the list.

//PRINTING POLYNOMIALS

cout<<"The product of the two polynomials: "<<endl<<endl;
count ++;
cout << (*pos3).first << "x^" << (*pos3).second;
if (count < size)
cout << " + ";
}
cout <<endl<<endl;
system("pause");

}

{
int a, b;
int coeff[s];
int expo[s];
for (a = 0; a < s ; a++)
{
coeff[a] = (*pos).first;  					// Passing the values from list
expo[a] = (*pos).second;    				// to an array for easier sorting.
pos++;
}
int temp1, temp2;  									// setting "temporary" containers for swapping later.
int checkNull = 0;
for(a =0;a < s ; a++)
for(b = a +1; b < s; b++)
{
if(expo[a] < expo[b])          			// Here, we swap the
{										// values if the first
temp1 = coeff[a];				// value is smaller than
temp2 = expo[a];				// the second one.
coeff[a] = coeff[b];
expo[a] =  expo[b];
coeff[b] = temp1;
expo[b] = temp2;
}
else if (expo[a] == expo[b])  			//Here if the exponents are equal
{									  	// we add the coefficients then
coeff[a] = coeff[a] + coeff[b]; 	// copy the exponent.
expo[b] = 0;
coeff[b] = 0;
}
}

for (a = 0; a <s; a++)             //Here, we transfer the sorted data
{									  // back to the list.
(*pos).first = coeff[a];
(*pos).second=expo[a];
pos++;
}
pos = linklist.begin();  			 //Here, we remove those coefficients
for(int a = 0; a < s; a++)		 // that are equal to zero
{
if (0 == (*pos).first)
linklist.erase(pos);			//"list_name.erase()" is use to remove
pos++;							     // an entry from a list.
}
int count = 0;
cout << endl;
cout << "The polynomial is: ";
count ++;
cout << (*pos).first << "x^" << (*pos).second;
if (count < s)
cout << " + ";
}
cout <<"\n\n\n\n\n\n";
system("pause");

}

{
int i, j; 								// for array indexes.

int coefficient1[size1], coefficient2[size2], coefficient3[size3];
int exponent1[size1],exponent2[size2],exponent3[size3];

for (i = 0; i < size1; i++)
{
coefficient1[i] = (*pos1).first;  //Here we transfer values from list to
exponent1[i] = (*pos1).second;	  //an array.
pos1++;
}
for (i = 0; i < size2; i++)
{
coefficient2[i] = (*pos2).first;
exponent2[i] = (*pos2).second;
pos2++;
}
int k = 0;
int check = 0;
for(i =0;i < size1 ; i++)
{
for(j = 0; j < size2; j++)
{
if(exponent1[i] == exponent2[j])
{
coefficient3[k] = coefficient1[i] + coefficient2[j];
exponent3[k] = exponent1[i];
coefficient2[j] = 0;
coefficient1[i] = coefficient3[k];
exponent1[i] = exponent3[k];
}
else check++;

}
if (check == size2)
check = 0;
}
for(int a = 0; a < size2; a++)
{
if (0 != coefficient2[a])
}

}
void eval(myList &l1, float value)
{
double termValue, tmp;
myList::iterator pos = l1.begin();
int size = l1.size();
cout << endl;
for (int i = 0; i < size; i++)
{
termValue = (*pos).first * pow(value,(*pos).second);
pos++;
}

cout << "Substituted value is: " << value << endl<<endl;
cout << "Result is: " << answer << endl<<endl<<endl<<endl;
system("pause");
}
int analChoice()
{

here:
system("cls");
cout<<endl;
cout << "Do you really want to exit? (Y/N):  ";

{
case 'y':
{
exit(0);
break;
}
case 'Y':
{
exit(0);
break;
}

case 'n':
{
return 0;
break;
}
case 'N':
{
return 0;
break;
}

default:
{
system("color 0c");
cout << "Enter the right key!" << endl;
goto here;
break;
}
}
}
int evalList(myList &l1,myList &l2,myList &l3,myList &l4)
{

double value;
char ans;
system("cls");
cout<<endl;
cout << "Enter value to be substituted: ";
cin >> value;

cout<<"you cannot input a letter"<<endl;

system("cls");
here:
cout<<endl;
cout << "Substitute to: " << endl;
again3:
cout<<"========================================================================"<<endl;
cout << "A- First Given Polynomial" << endl<<endl;
cout << "B- Second Polynomial" << endl<<endl;
cout << "C- Product of first Polynomial and Second Polynomial" << endl<<endl;
cout << "D- Sum of first Polynomial and Second Polynomial" << endl<<endl;
cout <<"======================================================================="<<endl;
cout <<endl<<endl;
cout << "Enter choice: ";
cin  >> ans;

switch(ans)
{
case 'A':
eval(l1, value);
system ("cls");
goto again3;

case 'B':
eval(l2, value);
system ("cls");
goto again3;

case 'C':
Poly_Prod(l1, l2, l3);
eval(l3, value);
l3.clear();
system ("cls");
goto again3;

case 'D':
insPoly(l1, l2, l4);
eval(l4, value);
l4.clear();
system ("cls");
goto again3;

case 'E':
cout << "System returning to Main Options :)/>/>\n\n";
system ("cls");
return option();

default:
cout << "Please enter the right key" << endl;
//system("pause");
system("cls");
goto here;
break;
}
}

void checkin()
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
}

```

Question: How can i Modify this code in order to create 3 new textfiles that contain the answers for every operation.

Is This A Good Question/Topic? 0

## Replies To: Operations on Polynomial code

### #2 Skydiver

• Code herder

Reputation: 7132
• Posts: 24,222
• Joined: 05-May 12

## Re: Operations on Polynomial code

Posted 21 October 2019 - 07:37 PM

First, to answer your question, to do what you want to do with the minimal amount of change, you make each of your functions that perform the operation take an ostream parameter. Inside the function, instead of writing out to cout, write out to the ostream instead. Before you call the function, instantiate an ofstream and pass that in to your function.

Now on to the bigger issue at hand: You really need to re-write your code. All those gotos are obscuring the true flow and shape of your code. Strive to remove them. Also try to separate your code out so that you keep I/O separate from processing. Once you do this, it'll become blindingly obvious where all the screen output is being done. Since cout is just an ostream you can simply replace the stream object with either the file object or cout depending on whether you need output to a file, or the screen. Or in cases where you need it to go out to both, you could just call the output function twice: once with the the file stream, and once with cout.