Question: In C++, you can use the bits/stdc++.h if you wish, just identify what required #include libraries needed in order to compile, otherwise I may have
In C++, you can use the bits/stdc++.h if you wish, just identify what required #include libraries needed in order to compile, otherwise I may have to ask you for it in the comments in which to me is more likely going to be lost and forgotten often times. You would put #include vector, algorithm, math.h, and more so that I know the specifics. Say I have a vector that formed a polynomial equation using push_back() to from this equation (Grabs every char, integers, and operators (*, +, ^), variables here can be a single letter, or a string, examples down below can clarify that):
Y ^ 2 + Z ^ 3 + 7 * 8 * X + 8 * 8 + 8 * X + 7 * Y
That was printed using this for loop:
for (auto i = equation.begin(); i != equation.end(); ++i)
{
cout << *i << " ";
}
Along with the equation vector, We also have the evaluation vector that identifies the variables, and the number vector that's assigned to each variable, in this case, we have:
evaluation: A Y Z W L X
number: 8 12 5 10 3 8
Meaning
A = 8
Y = 12
Z = 5
W = 10
L = 3
X = 8
Things to note:
- Both the evaluation and number vectors' size are always equal, that's already maintained on my part
- You may notice the evaluation vector has additional variables that are not required for the polynomial equation, you can implement a method that would ignore the additional variables and only pull the required variables needed to solve the equation
- Concerned that the evaluation vector doesn't have all the required variables to solve the equation? that's already maintained on my part, you can ensure if it does or dosen't go through if you wish
Next,
After labeling the variables with their assigned values, solve the polynomial equation (convert the equation vector into a string), using the typical order of operations:
The final answer would be an int value, in this case, the output for the above equation would be:
929
You can cout and/or print the output as you wish, it is considerably Important in my case that this value can be push_back into another vector, be manipulated by another function, etc.
|
here are more examples:
Y + W + X + Z
evaluation: X Y W Z T
number: 2 3 1 4 56
Ans: 10
|
myVar + myVar2 * 11
evaluation: myVar myVar2 whatVar
number: 1234 2 900
Ans: 1256
|
X * X * X * X * X
evaluation: Y X
number: 2 5
Ans: 3125
|
X^2 + X^2 + Y * Y * Y^2 + 7 + 8 + 9 * X^2 + 76 * X
evaluation: X Y
number: 12 2
Ans: 2527
If you need clarification, please do let me know, thank you very much for your time.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
