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: