Like many netizens, benefited from vckbase, feel should make a point for him to contribute, so did so a scientific calculator based on expression evaluation and you love programming friends to share.
As you know, there are a lot of procedures in this area, and I've seen Zf.yi works, I have also seen Huangjiang related programs, but I think my computing class has a different feature than two digits, such as a longer (16-bit) of the calculated result, and support for less restrictive expression input (such as COS (34) and Cos (23 ) *sin (34 and Cos23*sin34 equivalent); Support for four kinds of numbers in a single expression, a mixed operation (except that the number of decimal numbers is not limited to integers, such as 12D.3AXH,XH is the identifier of the hexadecimal number that my calculation class can recognize) And the program is also relatively fine (such as the creation of the mouse keyboard, window follow, calculation history, etc.), which makes me feel flops not in impostors, I believe netizens will see another harvest.
A simple way of thinking is this: for user input expressions,
1. To find the first pair of parentheses in the order from the inside and from the left to the right, and extract the expression;
2. Compile all the unary calculation parts of the expression to calculate the result and convert it into a string-return expression;
3. Compile the results of all two-part calculations and convert them into string-return expressions.
4. Go back to the first step, unless there are no parentheses in the expression.
Second, the implementation of some of the code instructions:
The following is a main procedure function in the calculation class:CString Ccalculation::mainpro (CString strexp)
{
if (Strexp.isempty ()) return "expression cannot be null";
Macro (&STREXP);
Strexp.makelower (); Expression All lowercase
/********** the expression with the protection bracket ************/
Strexp.insert (0, "(");
strexp+= ")";
/******************************************/
int Pos=strexp.find ("");
int N=bracheck (STREXP);
CString str;
Str. Format ("%d", ABS (n));
if (n==1) strexp+= ")";
else if (n==-1) strexp.insert (0, "(");
else if (n>0) return "missing" +str+ "closing parenthesis";
else if (n<0) return "missing" +str+ "left parenthesis";
while (POS!=-1)//Remove the spaces in an expression
{
Strexp.delete (POS);
Pos=strexp.find ("");
}
Oct2dec (&STREXP); Converts the number of octal in an expression to decimal
Hex2dec (&STREXP); Converts a hexadecimal number in an expression to decimal
Bin2dec (&STREXP); Converts a binary number in an expression to decimal
while (! Isdigital (STREXP))
{
Delbracket (&STREXP);
if (! Synres (&STREXP)) return strexp;
}
if (! Synres (&STREXP)) return strexp;
else return Modiresult (STREXP);
}