extracting polynomial coeffincient from string

I want to extract polynomial coefficient out of a string recieved by input, for example if i enter 4x^3+2x^4+3 , the resulting out put be :
2 , 4 , 0 , 0 , 3
I think it is easier to directly enter the coefficients!
it is my school project,,, and this easier way is not accepted :(
The difficulty of the problem lies in analyzing and rearranging the polynomial in descending order of powers of x (with the highest degree term first).
It is assumed that the polynomial has no like terms.
Then the coefficients can be extracted easily. Do you have some code so far?
for example if i enter 4x^3+2x^4+3


surely the coefficients of this are 4 and 2, with 3 being a constant?

wtf are the zero's about?
Last edited on
In fact, after the rearranging of the polynomial in descending order taking into account and the missing powers of x we have the polynomial and its coefficients as follows:

+2x^4+4x^3+0x^2+0x^1+3x^0

+2, +4, +0, +0, +3
Last edited on
Why don't you try string parssing and pattern mathing for the polynomial?
i got really confuesed , i don't have any code yet, i just simply did rest of my project by giving the coeffiecient directly ..
but it is a personal intrest in solving this
can we extract easch sentences of this poly out of string and then procces on them ?! for exaple first we extract 4x^3 , then search for x^ in out new string, before the x^ is our coefficient, and after that is our power, but we have to absorb them by some way and convert them into int and save them in an array like A [ coeeficient] [ power ] :/
You are thinking in right directions, try to code your algorithm.
First go throw some samples programs of string operations, that would help in bootstrapping.
A couple of observations that might help:
1. You can calculate the number of terms you have from the number of '+'s you find in your string.
2. You can store the orders of each of the terms by looking at the number to the right of any '^'s you find.
3. Counting the number of 'x's that appear would be useful, as would be any numbers appearing directly to their left.
alighasemi72 wrote:

it is a personal intrest in solving this

With this intention, as said OP, here's an attempt for the problem solving. It may have bugs despite of many tests I have done.

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
-----------------------







Topic archived. No new replies allowed.