Special Grading Considerations: Do not use global variables You will process a f
ID: 3824393 • Letter: S
Question
Special Grading Considerations: Do not use global variables
You will process a file that contains a sequence of strings representing polynomial arithmetic. Each line of the file will consist of a string that contains two polynomials that are to be added or multiplied. Each polynomial will be contained in parentheses and they will be separated by the appropriate operator. For example,
(1*x+2*x^3-1*x+7)+(1+1*x^2-1*x+1*x^4)
or
(1*x+2*x^3-1*x+7)*(1+1*x^2-1*x+1*x^4)
Facts about the input string:
The string will contain no spaces.
The terms may not be in order of degree.
A term with coefficient 1 will have the 1 present.
The coefficients will always be integers.
Zero will never appear as a coefficient.
Each file will contain some unspecified number of strings (one per line) that meet the above criteria. The program should read in each problem as a whole and then break it into two polynomials. The terms of the operands may be stored in the order they are specified. After the appropriate operation is performed, the polynomial must be sorted from highest degree to smallest and displayed to the console. If you would like to be eligible for full credit, the display must look like a typical polynomial:
5 3
3x + x - 12x + 7
You may opt to output in the format of the input
3*x^5+x^3-12*x+7
but that will cap your grade at 90.
A polynomial will be a dynamically allocated two dimensional arrays where each row represents a monomial and each row has two columns: the first is the exponent and the second is the coefficient. That is, a polynomial will be declared as
int ** polynomial;
You should define a function per operation; i.e.
int** add(int **left, int leftCount, int **right, int rightCount, int &resultCount)
int** mutliply(int **left, int leftCount, int **right, int rightCount, int &resultCount)
and have another operation that parses a string to extract one polynomial:
int** getPolynomial(string polynomial, int &resultCount)
Explanation / Answer
Due to inssufficient time, I could not do other operation, but the I have parsing correctly which was the most complex part of this program.Now rest is easy.You can maintain the value of j as count of polynomial terms.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include<string>
using namespace std;
typedef struct poly
{
int e,c;
} poly;
int len;
int **getPolynomial(string s)
{
int i=0;
poly *p=new poly[100];
int j=0;
while(i<s.size())
{
if(i-1>=0 && s[i-1]=='-')
p[j].c=-(s[i]-'0');
else
p[j].c=s[i]-'0';
if(s[i+1]!=''&& s[i+1]=='-' || s[i+1]=='+') //If the first one is constant
{
p[j].e=0;
i=i+2;
j++;
continue;
}
i=i+2;
if(s[i]=='x')
{
if(s[i+1]!=''&& s[i+1]=='-' || s[i+1]=='+')
{
p[j].e=1;
i=i+2;
}
else
{
i=i+2;
p[j].e=s[i]-'0';
i=i+2;
}
}
else{
p[j].e=0;
i++;
}
j++;
}
int ** pol=(int **)malloc(sizeof(int *)*j);
for(int k=0;k<j;k++)
pol[k]=(int *)malloc(sizeof(int)*2);
for(int i=0;i<j;i++)
{
pol[i][0]=p[i].e;
pol[i][1]=p[i].c;
}
return pol;
}
int main()
{
string line;
ifstream myfile ("poly.txt");
string pol;
int ** polynomial1;
int **polynomial2;
int **result;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
if(line.empty())
break;
istringstream iss(line);
string s;
getline( iss, s, ')' );//split using space ),for each line
s.erase(s.begin());
polynomial1=getPolynomial(s);
getline( iss, s, ')' );
s.erase(0,2);
polynomial2=getPolynomial(s);
//operations
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.