8 + (7 * (6 + 2 * (3 + 4) - 5) - 2) * 9
How to resolve?
thank you
CodePudding user response:
This is more complex, you can find online expression, Internet open source, have a good, call MTParserCodePudding user response:
C/C + + is used to implement a scientific calculatorCodePudding user response:
After the first, add and subtract, first calculate the brackets; pure mathematicsCodePudding user response:
Try using recursive function, encounter left parenthesis is recursive solution formula in brackets8 + (7 * (6 + 2 * (3 + 4) - 5) - 2) * 9
7 * (6 + 2 * (3 + 4) 5) - 2
6 + 2 * (3 + 4) - 5
3 + 4
CodePudding user response:
Look stick I copy the code before
TCHAR *
GetExpressionValue (TCHAR * expression, __int64 * value)
{
//write code to the classical theory of arithmetic expressions are evaluated by Donald Knuth described in 1962,
//
//Knuth will be divided into three steps:
//
//1. The syntax analysis was carried out on the infix expression
//2. Converting infix expressions to postfix expression
//3. The postfix expression evaluated
TCHAR result [0 x400]=_T (" ");
TCHAR stack [0 x400]=_T (" ");
Int top=1;
//TCHAR end [30]=_T ("; , [] ");
//int I;
//int len.
TCHAR * p=expression;
Int isfirst=1;
//len=(int) _tcslen (end);
TCHAR t [256]=_T (" ");
Int current_len;
//infix expressions to postfix expression
//
//
//to be in the form of expression from the infix expression into the equivalent expression in postfix notation, must understand the operator precedence and associativity,
//priority or the intensity of the operator to determine evaluation order; Operator priority than the operation of the low priority FuXian,
//if all operator precedence, then evaluated the order depends on their associativity,
//operator associativity defines the order of the same priority operator combination (from right to left or from left to right),
//
//conversion process including the following algorithm infix expressions of operands, operators, and parentheses:
//
//1. Initialization of an empty stack, the result string variable blank,
//2. The infix expression from left to right, one character at a time,
//3. If the character is the operand, add it to the result string,
//4. If the character is an operator, pop (pop) operator, until you reach an opening parenthesis (opening parenthesis),
//the lower priority operator or the same priority right combination of symbols,
//pressed the operator into the stack (push),
//5. If the character is an opening parenthesis, push it onto the stack,
//6. If the character is a closing parenthesis (closing parenthesis), before you reach an opening parenthesis, pop all operators, then add them to the result string,
//7. If reach the end of the input string, pop all operators and added to the result string,
//
Do
{
//gettoken (p, t, & amp; Current_len);
//for (I=0; i//{
//if (t [0]==end [I]
//| |
//! T [0]
//)
//{
//goto EXPRESSION_BREAK;
//}
//}
P=gettoken (p, t, & amp; Current_len);
If (0==t [0])
{
break;
}
If (isdec_t (t))
{
_tcscat (result, t);
_tcscat (result, g____newspace);
Isfirst=0;
}
Else if (is0x_t (t))
{
//converted to decimal, then evaluated
Int len=(int) _tcslen (t);
__int64 k=0;
int i;
For (I=2; i{
K & lt; <=4;
If (t [I] & gt;='0' & amp; & T [I] <='9')
{
K |=[I] t - '0';
}
Else if (t [I] & gt;='a' & amp; & T [I] <)
='f'{
K |=t [I] - 'a' + 10;
}
Else if (t [I] & gt;='A' & amp; & T [I] <)
='F'{
K |=t [I] - 'A' + 10;
}
}
TCHAR v [0 x20];
_stprintf (v, _T (" % I64u "), k);
_tcscat (result, v);
_tcscat (result, g____newspace);
Isfirst=0;
}
Else if (! _tcscmp (t, _T (" + ") | |! _tcscmp (t, _T (" - ")))
{
If (isfirst)
{
_tcscat (result, _T (" 0 "));
_tcscat (result, g____newspace);
Isfirst=0;
}
While (1!=top
& &
LEFT_XIAOKUOHAO!=stack [top]
)
{
Result [_tcslen (result)]=stack [top];
_tcscat (result, g____newspace);
Top -;
}
Top++;
Stack [top] [0]=t;
Isfirst=0;
}
Else if (! _tcscmp (t, _T (" * ")) | |! _tcscmp (t, _T ("/")))
{
While (=='*' stack [top]
| |
XIEGANG==stack [top]
)
{
Result [_tcslen (result)]=stack [top];
_tcscat (result, g____newspace);
Top -;
}
Top++;
Stack [top] [0]=t;
Isfirst=0;
}
Else if (! _tcscmp (t, _T (" (")))
{
Top++;
Stack [top] [0]=t;
Isfirst=1;
}
Else if (! _tcscmp (t, _T (") ")))
{
While (LEFT_XIAOKUOHAO!=stack [top])
{
Result [_tcslen (result)]=stack [top];
_tcscat (result, g____newspace);
Top -;
}
Top -;
Isfirst=0;
}
The else
{
INIT.
The mul (20);
If (IsChinese ())
{
RED (_T (" expression error "));
}
The else
{
RED (_T (Expression "Error"));
}
BLUE (p);
The SHOW;
//printf (" expression error % s ", p);
Return expression;
}
} the while (1);
//EXPRESSION_BREAK:
While (1!=top)
{
Result [_tcslen (result)]=stack [top];
_tcscat (result, g____newspace);
Top -;
}
//the postfix expression evaluated
//
//the postfix expression evaluation than directly on the infix expression evaluation is simple,
//in the postfix expression, don't need braces, and operator priority is no longer work,
//you can use the following algorithm for the postfix expression evaluation:
//
//1. Initialization of an empty stack
//2. Read from left to right postfix expression
//3. If the character is an operand, push it onto the stack,
//4. If the character is an operator, pop two operands, perform the appropriate action, and then the result onto the stack, if you are not able to pop up two operands, postfix expression grammar is not correct,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull