A scientific calculator based on expression calculation

Source: Internet
Author: User
Tags cos expression

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 (&AMP;STREXP); Converts the number of octal in an expression to decimal
Hex2dec (&AMP;STREXP); Converts a hexadecimal number in an expression to decimal
Bin2dec (&AMP;STREXP); Converts a binary number in an expression to decimal
while (! Isdigital (STREXP))
{
Delbracket (&AMP;STREXP);
if (! Synres (&AMP;STREXP)) return strexp;
}
if (! Synres (&AMP;STREXP)) return strexp;
else return Modiresult (STREXP);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.