patterncppMinor
Mathematical input parser
Viewed 0 times
inputparsermathematical
Problem
I made a simple math expressions solver that using a "for" loop iterates over all the characters and stores the numbers and signs in std::vector.
It give support for unlimited parentheses and does multiplications and division before other calculations.
I really appreciate if you give any suggestions.
Here's the code:
It give support for unlimited parentheses and does multiplications and division before other calculations.
I really appreciate if you give any suggestions.
Here's the code:
using std::cout;
using std::endl;
using std::cin;
using std::vector;
using std::string;
void input(vector &, vector &, vector &, vector &, string);
int numLenght(int index, string poly)
{
int lenght = 0;
for (int i = index; i num, vector sign)
{
float result = 0;
for (unsigned i = 0; i &num, vector &sign, vector &firstPos, vector minus)
{
for (unsigned i = 0; i &num)
{
double tempNum = 0;
string parOp;
vector num1;
vector sign1;
vector firstPos1;
vector minus1;
i += 2;
for (int j = i; j &sign, string poly, int &i, vector &firstPos, vector &minus)
{
char next = poly[i + 1];
if (!(i == poly.length() - 1))
{
sign.push_back(next);
int posTemp = sign.size() - 1;
if (next == '*' || next == '/')
{
firstPos.push_back(posTemp);
}
if (poly[i + 2] == '-')
{
int posMinus = sign.size() - 1;
minus.push_back(posMinus);
i++;
}
i++;
}
}
void input(vector &num, vector &sign, vector &firstPos, vector &minus, string poly)
{
for (int i = 0; i num;
vector sign;
vector firstPos;
vector minus;
string poly;
cout > poly;
input(num, sign, firstPos, minus, poly);
for (int i = 0; i > choose;
if (choose == 's')
main();
return 0;
}Solution
Think more about the logic
Right now, your
It appears that the test in the
Use the standard library
Your
Names
At least to me, some of you names seem fairly meaningless. For example, I'm not sure what
Conclusion
Ultimately, I agree with Loki Astari this this would be much better handled with a recognizable lexer and parser. Rather than post some code for that, I'll point you to a previous question where both he and I posted our ideas of how to accomplish roughly what you're doing here.
Right now, your
numlenght also has some logic that doesn't seem to me like it makes much sense at all:if (i == poly.length() - 1)
{
break;
}
else
{
break;
}It appears that the test in the
if statement makes no real difference at all--you're going to carry out the same action whether it's true or false. You might as well replace that whole chunk with break; (but before you edit, see the next point below).Use the standard library
Your
numLenght [sic] could be implemented a lot more easily using find_first_not_of:return poly.find_first_not_of("0123456789");Names
At least to me, some of you names seem fairly meaningless. For example, I'm not sure what
parHandler is supposed to mean.Conclusion
Ultimately, I agree with Loki Astari this this would be much better handled with a recognizable lexer and parser. Rather than post some code for that, I'll point you to a previous question where both he and I posted our ideas of how to accomplish roughly what you're doing here.
Code Snippets
if (i == poly.length() - 1)
{
break;
}
else
{
break;
}return poly.find_first_not_of("0123456789");Context
StackExchange Code Review Q#133042, answer score: 2
Revisions (0)
No revisions yet.