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
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>
#define DEG 100 //for polynomials of max degree = 99
int main()
{
std::string str;
std::cout << " Enter a polynomial without like terms\n";
std::cout << "(use the letter x. for ex.: -x+4.5x^3+2x^4-3.1)\n";
std::cout << "\nEnter: ";
std::cin >> str;
if(str == "") return 1;
int strSize = str.size();
// How many monomials has the polynomial?
int k = 1;
for(int i = 1; i < strSize; ++i)
if(str[i] == '+' || str[i] == '-')
k++;
int monoms = k ;
// Signs "+" are necessary for the string parsing
if(isdigit(str[0])) str.insert(0, "+");
if(str[0] == 'x') str.insert(0, "+");
str.append("+");
strSize = str.size();
// Extracting the monomials as monomStr
k = 0;
int j = 0;
std::string monomStr[DEG];
for(int i = 1; i < strSize; ++i)
if(str[i] == '+' || str[i] == '-')
{
monomStr[k++] = str.substr(j, i - j);
j = i;
}
// Monomials' formatting i.e. to have all the same form: coefficientX^exponent
for(int i = 0; i < monoms; ++i)
{
if(monomStr[i][1] == 'x') //x is after the +/- sign
monomStr[i].insert(1, "1"); //& gets 1 as coefficient
bool flag = false; // assuming that x is not present
int len = monomStr[i].size();
for(int j = 1; j < len; ++j)
if(monomStr[i][j] == 'x') //but we test this
{
flag = true; //& if x is present
if(j == len - 1) //& is the last
monomStr[i].append("^1"); //it gets exponent 1
break; //& exit from j_loop
}
if(!flag) //if x is not present: we have a constant term
monomStr[i].append("x^0"); //who gets "formatting"
}
// Extracting the coefficients and exponents as numbers
int expon[DEG] = {0};
double coeff[DEG] = {0.};
for(int i = 0; i < monoms; ++i)
{
int monomSize = monomStr[i].size();
for(int j = 0; j < monomSize; ++j)
{
if(monomStr[i][j] == '^')
{
expon[i] = stoi(monomStr[i].substr(j + 1, monomSize - j));
coeff[i] = stod(monomStr[i].substr(0, j));
break;
}
}
}
// Looking for the max of exponents
int maxExponent = 0;
for(int k : expon)
if(k >= maxExponent) maxExponent = k;
// Generating the monomials of the null polynomial having degree = maxEponent
std::string newMonom[DEG];
for(int i = maxExponent; i >= 0; i--)
newMonom[i] = "+0x^" + std::to_string(i);
// Mixing the two polynomials: given & null
for(int i = monoms; i >= 0; i--)
newMonom[expon[i]] = monomStr[i];
// Creating the final(complete) form of the polynomial as finalStr
std::string finalStr;
for(int i = maxExponent; i >=0; i--)
finalStr += newMonom[i];
std::cout << "\nComplete: " << finalStr << '\n';
// Extracting( at last!..) the coefficients & exponents
// from the final form of the given polynomial
int finalSize = finalStr.size();
int start[DEG] = {0};
int stop[DEG] = {0};
k = 0;
for(int i = 0; i < finalSize; ++i)
{
if(finalStr.substr(i, 1) == "+" || finalStr.substr(i, 1) == "-")
start[k] = i;
if(finalStr.substr(i, 1) == "x")
stop[k++] = i;
}
std::cout << "\nCoefficients Exponents\n";
std::cout << "-----------------------\n";
for(int i = 0; i < k; ++i)
{
coeff[i] = stod(finalStr.substr(start[i], stop[i] - start[i]));
expon[i] = maxExponent - i;
std::cout << std::setw(6) << coeff[i] << std::setw(13) << expon[i] << '\n' ;
std::cout << "-----------------------\n";
}
std::cout << '\n';
return 0;
}
|
EXAMPLE from OP post
---------------------------------
Enter a polynomial without like terms
(use the letter x. for ex.: -x+4.5x^3+2x^4-3.1)
Enter: 4x^3+2x^4+3
Complete: +2x^4+4x^3+0x^2+0x^1+3x^0
Coefficients Exponents
-----------------------
2 4
-----------------------
4 3
-----------------------
0 2
-----------------------
0 1
-----------------------
3 0
-----------------------
Another EXAMPLE:
-----------------------
Enter: -12.3+x^6-5.1x+2.5x^3-1.3x^4
Complete: +1x^6+0x^5-1.3x^4+2.5x^3+0x^2-5.1x^1-12.3x^0
Coefficients Exponents
-----------------------
1 6
-----------------------
0 5
-----------------------
-1.3 4
-----------------------
2.5 3
-----------------------
0 2
-----------------------
-5.1 1
-----------------------
-12.3 0
----------------------- |