I am new to c++, and I would greatly appreciate the help.
This is what it was given to me, and I have to do the sum and product of the polynomials.
Here is the link to all of this for better understanding:
http://www.mediafire.com/?mpy78l2pvxnmg75
Consider the dense representation of polynomials
f = a
n*x^n +...+ a
1 x + a
0
as linked lists of nodes of the form
a
0-> a
1->...->a
n/
Every node is assumed to be an instance of the class
class poly {
public:
double coeff; // the value of the coefficient
poly *next; // pointer to the next element
}
Define the following operations:
poly* sum(poly *f, poly *g) { ... }
which computes a dense representation of the sum of the polynomials
represented by f and g.
poly* prod(poly *f, poly *g) { ... }
which computes a dense representation of the product of polynomials rep-
resented by f and g.
Make your program interactive, to display the menu
Choose what operation to do next:
f - Read the polynomial f
g - Read the polynomial g
d - Display the values of polynomials f and g
s - Compute the polynomial sum f+g
p - Compute the polynomial product f*g
q - Quit program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <iostream>
using namespace std;
#include <iostream>
#include <sstream>
#include <string>
class poly {
public:
double coeff;
poly *next;
poly(double c, poly* n) : coeff(c), next(n) { }
};
/**
* Auxiliary method that displays the program menu.
*/
char makeChoice() {
cout << "Choose what operation to do next:" << endl <<
"f - Read the polynomial f" << endl <<
"g - Read the polynomial g" << endl <<
"c - Display the coefficient of a polynomial" << endl <<
"d - Display the values of polynomials f and g" << endl <<
"s - Compute the polynomial sum f+g" << endl <<
"p - Compute the polynomial product f*g" << endl <<
"q - Quit program" << endl;
char c;
cin >> c;
return c;
}
/**
* stringPoly(poly *f) computes a string representation of polynomial h
*/
string stringPoly(poly* h) {
stringstream convert;
if (h == 0)
convert << "nil";
else {
int deg = 0;
while( h != 0) {
if (deg == 0) {
convert << h->coeff;
} else if (h->coeff < 0) {
convert << " " << h->coeff <<"*x^" << deg;
} else if (h->coeff > 0) {
convert << " +" << h->coeff << "*x^" << deg;
}
deg++;
h = h->next;
}
}
return convert.str();
}
/**
* clearPoly(poly *f) clears the memory allocated to store the dense representation of f
*/
void clearPoly(poly *f) {
if(f != 0) {
clearPoly(f->next);
delete f;
}
}
/**
* auxiliary function that computes the degree of a polynomial.
* A null polynomial is defined to have degree -1.
*/
int degree(poly *f) {
if (f == 0)
return -1;
else return 1 + degree(f->next);
}
/**
* auxiliary function that returns the coefficient of x^d in polynomial f.
*/
int coeff(poly *f, int d) {
int deg = degree(f);
if (deg < d)
return 0;
int i = 0;
while (i != d) {
f = f->next;
i++;
}
return f->coeff;
}
/**
* sum(poly *f, poly *g) computes the dense representation of the polynomial f+g
*/
poly *sum(poly *f, poly *g) {
if (f == 0) return g;
if (g == 0) return f;
// TODO: allocate space for the dense representation of f+g, and compute it
return 0;
}
/**
* prod(poly *f, *poly *g) computes the dense representation of the polynomial f*g
*/
poly *prod(poly *f, poly *g) {
// TODO
return 0;
}
/**
* Reads the coefficients of a polynomial from the console
*/
poly* readPoly(string polyName) {
cout << "Enter the sequence of coefficients a_n ... a_1 a_0"
<< " of " << polyName << endl << "separated by spaces:" << endl << endl;
double coeff;
poly *f = 0;
string str="";
while (str=="")
getline(cin,str);
stringstream ss(str);
while (ss >> coeff) {
f = new poly(coeff,f);
}
return f;
}
void selectPolyAndCoeff(char &c, int &i) {
cout << "Choose the polynomial you want to analyze (f/g): ";
c= ' ';
while(c!='f' && c != 'g') {
cin >> c;
}
cout << "Choose the power of x whose coefficient you want to know: ";
cin >> i;
}
int main() {
poly *f, *g, *h;
f = 0; g = 0;
char answer = makeChoice();
while (answer != 'q') {
switch (answer) {
case 'f':
clearPoly(f);
f = readPoly("f");
break;
case 'g':
clearPoly(g);
g = readPoly("g");
break;
case 'd':
cout << "f=" << stringPoly(f) << endl
<< "g=" << stringPoly(g) << endl;
break;
case 's':
h = sum(f,g);
cout << "f+g=" << stringPoly(h) << endl;
break;
case 'c':
char c;
int i;
selectPolyAndCoeff(c,i);
switch(c) {
case 'f':
cout << "Coeff. of x^"<<i<<" in f is "<< coeff(f,i) << endl;
break;
case 'g':
cout << "Coeff. of x^"<<i<<" in g is "<< coeff(g,i) << endl;
break;
}
break;
case 'p':
h = prod(f,g);
cout << "f*g=" << stringPoly(h) << endl;
break;
case 'q':
continue;
}
answer = makeChoice();
}
return 0;
}
|