#include "Definition.h"
int menu1(int count)
{
if (0 == count)
{
int n = 0;
cout << "---Polynomial Solver.----" << endl;
cout << "1. Input Two Polynomial." << endl;
cout << "0. Exit." << endl;
do {
cout << "Choose: ";
cin >> n;
} while (0 > n || 1 < n);
return n;
}
else
{
int n = 0;
cout << "---Polynomial Solver.----" << endl;
cout << "1. Input New Two Polynomial." << endl;
cout << "2. Continue." << endl;
cout << "0. Exit." << endl;
do {
cout << "Choose: ";
cin >> n;
} while (0 > n || 2 < n);
return n;
}
}
int menu2()
{
int n = 0;
cout << "1. Value of Polynomial At x."<<endl;
cout << "2. Sum 2 Polynomial." << endl;
cout << "3. Substract 2 Polynomial." << endl;
cout << "4. Product 2 Polynomial." << endl;
cout << "5. Quotient 2 Polynomial." << endl;
cout << "6. Display 2 Poly." << endl;
cout << "0. Exit." << endl;
do {
cout << "Choose: ";
cin >> n;
} while (0 > n || 7 < n);
return n;
}
double Polynomial::Value_At(double n)
{
queue<Term> xx = x;
double valueAt = 0;
while (!xx.empty())
{
Term b = xx.front();
valueAt = valueAt + (b.coefficient * pow(n, b.degree));
xx.pop();
}
return valueAt;
}
unsigned int Polynomial::degree() //Find Max degree in Poly
{
unsigned int max = 0;
queue<Term>xx = x;
Term a = xx.front();
xx.pop();
max = a.degree;
while (!xx.empty())
{
a = xx.front();
if (max < a.degree)
{
max = a.degree;
}
xx.pop();
}
return max;
}
Polynomial Polynomial::ShortPolynomial() //Short member same degree and sort decrease
{
Polynomial q;
queue<Term> xx = x;
queue<Term> xy;
int maxDegree = degree();
while (0 <= maxDegree && 0 < xx.size())
{
q.a.coefficient = 0;
while (!xx.empty())
{
Term a = xx.front();
if (a.degree == maxDegree && 0 != a.coefficient)
{
q.a.coefficient = q.a.coefficient +a.coefficient;
q.a.degree = a.degree;
xx.pop();
}
else
{
xy.push(a);
xx.pop();
}
}
xx = xy;
while (!xy.empty())
{
xy.pop();
}
if (0 != q.a.coefficient)
{
q.x.push(q.a);
}
maxDegree--;
}
return q;
}
bool Polynomial::Quotient(Polynomial p)
{
//cout << "Quotient: a / b = ";
Polynomial q;
Polynomial w;
Polynomial overbalance;
queue<Term> xx = x;
queue<Term> xy = p.x;
int maxDegree_1 = degree();
int maxDegree_2 = p.degree();
if (maxDegree_1 >= maxDegree_2)
{
while (maxDegree_1 >= maxDegree_2)
{
Term a = xx.front();
Term b = xy.front();
q.a.coefficient = a.coefficient / b.coefficient;
q.a.degree = a.degree - b.degree;
q.x.push(q.a);
overbalance = q.Product(p);
overbalance = Difference(overbalance);
xx = overbalance.x;
w.x = xx;
if (!xx.empty())
{
maxDegree_1 = w.degree();
}
else
{
maxDegree_1 = -1;
}
}
q = q.ShortPolynomial();
q.print();
cout << "Over Balance: ";
w.print();
return true;
}
else
{
return false;
}
}
Polynomial Polynomial::Product(Polynomial p)
{
//cout << "Product: a * b = ";
Polynomial q;
queue<Term> xx = x;
while (!xx.empty())
{
queue<Term> xy = p.x;
Term a = xx.front();
while (!xy.empty())
{
Term b = xy.front();
q.a.coefficient = a.coefficient * b.coefficient;
q.a.degree = a.degree + b.degree;
xy.pop();
q.x.push(q.a);
}
xx.pop();
}
q = q.ShortPolynomial();
return q;
}
Polynomial Polynomial::Difference(Polynomial p)
{
//cout << "Substract: a - b = ";
Polynomial q;
Polynomial w;
queue<Term> xx = x;
queue<Term> xy = p.x;
queue<Term> xz;
Term a;
Term b;
int maxDegree_1;
int maxDegree_2;
while (0 != xx.size() || 0 != xy.size())
{
if (0 == xx.size())
{
maxDegree_1 = -1;
}
else
{
w.x = xx;
maxDegree_1 = w.degree();
}
if (0 == xy.size())
{
maxDegree_2 = -1;
}
else
{
w.x = xy;
maxDegree_2 = w.degree();
}
if (maxDegree_1 > maxDegree_2)
{
while (0 != xx.size())
{
a = xx.front();
if (maxDegree_1 == a.degree)
{
q.a.coefficient = a.coefficient;
q.a.degree = a.degree;
xx.pop();
continue;
}
xz.push(a);
xx.pop();
}
xx = xz;
}
else if (maxDegree_1 < maxDegree_2)
{
while (0 != xy.size())
{
b = xy.front();
if (maxDegree_2 == b.degree)
{
q.a.coefficient = 0 - b.coefficient;
q.a.degree = b.degree;
xy.pop();
continue;
}
xz.push(b);
xy.pop();
}
xy = xz;
}
else if(maxDegree_1 == maxDegree_2)
{
a = xx.front();
b = xy.front();
q.a.coefficient = a.coefficient - b.coefficient;
q.a.degree = a.degree;
xx.pop();
xy.pop();
}
while (!xz.empty())
{
xz.pop();
}
q.x.push(q.a);
}
q = q.ShortPolynomial();
return q;
}
Polynomial Polynomial::Sum(Polynomial p)
{
Polynomial q;
queue<Term> xx = x;
queue<Term> xy = p.x;
Term a;
Term b;
while (0 != xx.size() || 0 != xy.size())
{
if (0 != xx.size())
{
a = xx.front();
}
else
{
b = xy.front();
q.x.push(b);
xy.pop();
continue;
}
if (0 != xy.size())
{
b = xy.front();
}
else
{
a = xx.front();
q.x.push(a);
xx.pop();
continue;
}
if (a.degree > b.degree)
{
q.x.push(a);
xx.pop();
}
else if (a.degree < b.degree)
{
q.x.push(b);
xy.pop();
}
else
{
q.a.coefficient = a.coefficient + b.coefficient;
q.a.degree = a.degree;
q.x.push(q.a);
xx.pop();
xy.pop();
}
}
q = q.ShortPolynomial();
return q;
}
void Polynomial::read()
{
if (!x.empty())
{
while (!x.empty())
{
x.pop();
}
}
int n = 0;
cout << "Enter member of Poly: ";
cin >> n;
cout << "====================" << endl;
for (int i = 0; i < n; i++)
{
cout << "Enter coefficient: ";
cin >> a.coefficient;
cout << "Enter degree: ";
cin >> a.degree;
x.push(a);
cout << "---------" << endl;
}
cout << endl << "--------------------" << endl;
}
void Polynomial::print()
{
queue<Term> xx = x;
//cout << "P(x) = ";
if (xx.empty())
{
cout << "0";
}
else
{
while (!xx.empty())
{
Term b = xx.front();
int c = 0;
if (xx.size() == x.size())
{
if (0 > b.coefficient)
{
c = 45;
cout << (char)c << fabs(b.coefficient) << "x^" << b.degree << " ";
}
else
{
cout << b.coefficient << "x^" << b.degree << " ";
}
xx.pop();
continue;
}
else
{
if (0 > b.coefficient)
{
c = 45;
cout << (char)c << " " << fabs(b.coefficient) << "x^" << b.degree << " ";
}
else
{
c = 43;
cout << (char)c << " " << b.coefficient << "x^" << b.degree << " ";
}
xx.pop();
continue;
}
}
}
//cout << "= 0" << endl;
cout << endl;
}
void Run()
{
int n = 0;
int m = 0;
int count = 0;
Polynomial a;
Polynomial b;
Polynomial c;
do
{
n = menu1(count);
switch (n)
{
case 1:
system("cls");
a.read();
a = a.ShortPolynomial();
b.read();
b = b.ShortPolynomial();
system("cls");
do
{
system("cls");
m = menu2();
switch (m)
{
case 1:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
int x;
cout << "Enter value at x0: ";
cin >> x;
cout << "Value of Poly A at x0 = " << x << " is: " << a.Value_At(x) << endl;
cout << "Value of Poly B at x0 = " << x << " is: " << b.Value_At(x) << endl;
system("pause");
break;
case 2:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Sum: A + B = ";
c = a.Sum(b);
c.print();
system("pause");
break;
case 3:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Substract: A - B = ";
c = a.Difference(b);
c.print();
system("pause");
break;
case 4:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Product: A * B = ";
c = a.Product(b);
c.print();
system("pause");
break;
case 5:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Quotient: A / B = ";
if (true != a.Quotient(b))
{
cout << "False. Degree Of A must be higher than B." << endl;
}
system("pause");
break;
case 6:
system("cls");
cout << "Display 2 poly." << endl;
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
system("pause");
break;
default:
break;
}
} while (m != 0);
break;
case 2:
system("cls");
do
{
system("cls");
m = menu2();
switch (m)
{
case 1:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
int x;
cout << "Enter value at x0: ";
cin >> x;
cout << "Value of Poly A at x0 = " << x << " is: " << a.Value_At(x) << endl;
cout << "Value of Poly B at x0 = " << x << " is: " << b.Value_At(x) << endl;
system("pause");
break;
case 2:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Sum: A + B = ";
c = a.Sum(b);
c.print();
system("pause");
break;
case 3:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Substract: A - B = ";
c = a.Difference(b);
c.print();
system("pause");
break;
case 4:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Product: A * B = ";
c = a.Product(b);
c.print();
system("pause");
break;
case 5:
system("cls");
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
cout << "Quotient: A / B = ";
if (true != a.Quotient(b))
{
cout << "False. Degree Of A must be higher than B." << endl;
}
system("pause");
break;
case 6:
system("cls");
cout << "Display 2 poly." << endl;
cout << "Poly A: P(x) = ";
a.print();
cout << "Poly B: P(x) = ";
b.print();
system("pause");
break;
default:
break;
}
} while (m != 0);
default:
break;
}
system("cls");
count++;
} while (n != 0);
}