Calculation of arbitrary mathematical expressions

Source: Internet
Author: User

1. Programming Ideas

Any expression can be divided into 3 categories: 1. A simple expression, that is, an operational expression with only subtraction, such as 1+2,1+3/2,1*2*3+4*6. 2. Expressions with function notation without parentheses, such as sin[30],exp[3+4*5],sin[1+sin[2]], etc. 3. General expressions, i.e. expressions with parentheses, simple expressions, and symbolic functions, such as (1+2+sin[30]) *3,2+sin[1+ (sin[30]+1) *2].

Basic algorithm Flowchart:

The problem boils down to a few sub processes: 1. To determine whether parentheses are present in the expression 2. Get the most inner bracket content 3. Zero bracket Calculation function 4. Given a string position, replace the original string content with a known string.

Step 1,2,4 can be implemented in one function, respectively:

//判断表达式中是否有括号
bool IsContainBracket(CString str);
//若存在括号,则获得最内层括号位置
void GetBracketPos(CString str,int *start,int *end);
//用一段字符代替两个位置之间的内容
CString ReplaceBetweenPos(CString toBeReplace,CString str,int start,int end);

Step 3 is more complex and can be decomposed into several sub steps, thought as follows:

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.