Hey guys, I made a simple calculator that takes user input, and does operations and then returns the results. Currently, to do an operation, the user has to enter everything seperated by a space, which is inconvenient. Are there other ways to calculate input without seperating by spaces?
//---------------------------------------------------------------------------
// Example
//---------------------------------------------------------------------------
int main(void) {
string strNumber1,
strOperator,
strNumber2;
TReggae Reggea;
Reggea.str = "12+13="; // simulated user input
for (int i=0;i<Reggea.str.length();i++) {
try {
Reggea.iPos = i;
Reggea.boIgnoreBlock = false;
Reggea.number(strNumber1)
.word (strOperator)
.number(strNumber2)
.text ("=");
wostringstream os;
if (Reggea.boFound) {
//---------------------------------------------------------------------------
// here is your turn: calculate the result of strNumber1, strOperator, strNumber2
...
//---------------------------------------------------------------------------
// set i after "=" for next operations e.g. "12+13=-4/3=..."
i = Reggea.iPos;
}
else
i = Reggea.iPosChecked;
}
catch(...) {
// cxMemo1->Lines->Add("Exception: No match!");
}
}
return 0;
}
Im sorry, but being a noob, I really don't understand your code. Could you explain it to me in simple terms, and explain what the interpreter actually is/does?
first it searches the pattern of a number. if it finds a number it stores the sub-string in the variable strNumber1, then it searches next for a word : in this case it finds "+" and stores that in strOperator and so on. if the last token is a "=" it has found the total sequence of this pattern: <number> <operator> <number> <=>
and now you can be sure that the input was correct and evaluate the result.
Maybe you have to modify the word() function. the pattern is alphanum() instead only alpha() I think...
Try to understand this code, it is simple but powerfull to find any syntax or skip parts in a text... It was a tool I build for my own purposes to analyse DFM-Files in C++ Builder. Any try with regular expressions failed.
You'll see that you need a couble of tools which helps you in coding.