这个涉及到后缀表达式的求法。
中缀表达式:9+3*(4+5)
后缀表达式:9345+*+
中缀转后缀算法:
1. 遍历中缀表达式:
1. –>遇到左括号直接入栈;
2. –>遇到数字直接输出;
3. –>遇到操作符,优先级比栈顶高的入栈,否则出栈;
4. –>遇到右括号,输出栈中左括号前边的所有符号(包括左括号)。
后缀表达式的计算算法:
1. 遍历后缀表达式中的数字和符号:
1. –>对于数字:进栈
2. –>对于符号:
3. • • • • • • 从栈中弹出右操作数;
4. • • • • • • 从栈中弹出左操作数;
5. • • • • • • 根据符号进行运算;
6. • • • • • • 将运算结果压入栈中 。
/*中缀转后缀算法代码*/
#include "linkStack.h"
bool isNumber( const char c ) {
return (c >= '0') && (c <= '9');
}
int priority( const char c ) {
switch ( c ) {
case '+' :
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
bool isOperator( const char c ) {
return (c=='+')||(c=='-')||(c=='*')||(c=='/') ;
}
void output( const char c , char *dst ) {
if (c != '\0') {
cout << c;
while (*dst != '\0')
++dst;
*dst = c;
*(++dst) = '\0';
}
}
bool isLeft( const char c ) {
return (c=='(');
}
bool isRight(const char c) {
return (c==')');
}
void tranform( const char *format , char *dst ) {
linkStack chen;
LinkStack *stack = chen.LinkStack_Create();
char *pos = const_cast<char*>( format );
while ( *pos!='\0' ) {
if (isNumber(*pos)) {
output( *pos , dst );
}
else if ( isOperator(*pos) ) {
while ( priority(*pos) <= priority( (char)(int)chen.LinkStack_Top(stack) ) ) {
output( (char)(int)chen.LinkStack_Pop(stack) ,dst);
}
chen.LinkStack_Push(stack, (void*)(int)(*pos) );
/*想想:这里为什么不直接将pos指针压入栈,而是将pos指针的内容变成整型地址压入栈中。是为了,和上面的判断语句一致,因为在第一次进行操作符判断优先级的时候,是跟栈中的空指针做比较。(void*)0,这里要理解(int)nullptr的值才行*/
}
else if ( isLeft(*pos) ){
chen.LinkStack_Push(stack, (void*)(int)(*pos));
}
else if (isRight(*pos)){
while (!isLeft((char)(int)chen.LinkStack_Top(stack))) {
output((char)(int)chen.LinkStack_Pop(stack) , dst );
}
chen.LinkStack_Pop(stack);/*pop left kuohao*/
}
else {
printf("Invalid expression!");
break;
}
++pos;
}
while ((chen.LinkStack_Size(stack) > 0) && (*pos == '\0')){
output((char)(int)chen.LinkStack_Pop(stack),dst);
}
chen.LinkStack_Destroy(stack);
}
int main(int argc, char **argv) {
char a[50] = {0};
tranform("9-2",a);
system( "pause" );
return 0;
}
/*后缀表达式算法*/
#include "linkStack.h"
bool isNumber( const char c ) {
return (c >= '0') && (c <= '9');
}
int priority( const char c ) {
switch ( c ) {
case '+' :
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
bool isOperator( const char c ) {
return (c=='+')||(c=='-')||(c=='*')||(c=='/') ;
}
void output( const char c , char *dst ) {
if (c != '\0') {
cout << c;
while (*dst != '\0')
++dst;
*dst = c;
*(++dst) = '\0';
}
}
bool isLeft( const char c ) {
return (c=='(');
}
bool isRight(const char c) {
return (c==')');
}
void tranform( const char *format , char *dst ) {
linkStack chen;
LinkStack *stack = chen.LinkStack_Create();
char *pos = const_cast<char*>( format );
while ( *pos!='\0' ) {
if (isNumber(*pos)) {
output( *pos , dst );
}
else if ( isOperator(*pos) ) {
while ( priority(*pos) <= priority( (char)(int)chen.LinkStack_Top(stack) ) ) {
output( (char)(int)chen.LinkStack_Pop(stack) ,dst);
}
chen.LinkStack_Push(stack, (void*)(int)(*pos) );
}
else if ( isLeft(*pos) ){
chen.LinkStack_Push(stack, (void*)(int)(*pos));
}
else if (isRight(*pos)){
while (!isLeft((char)(int)chen.LinkStack_Top(stack))) {
output((char)(int)chen.LinkStack_Pop(stack) , dst );
}
chen.LinkStack_Pop(stack);/*pop left kuohao*/
}
else {
printf("Invalid expression!");
break;
}
++pos;
}
while ((chen.LinkStack_Size(stack) > 0) && (*pos == '\0')){
output((char)(int)chen.LinkStack_Pop(stack),dst);
}
chen.LinkStack_Destroy(stack);
}
int getResult( int &first , int &second , char *&opt ) {
switch ( *opt ) {
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
}
int computer( const char *format ) {
int ret;
char *pos = const_cast<char*>(format);
linkStack chen;
LinkStack *stack = chen.LinkStack_Create();
int first, second, result;
while ( '\0'!=*pos ) {
if ( isNumber(*pos) ) {
chen.LinkStack_Push(stack , (void*)(int)(*pos));
}
else if ( isOperator(*pos) ) {
second = (int)chen.LinkStack_Pop(stack);
first = (int)chen.LinkStack_Pop(stack);
result = getResult( first , second , pos );
chen.LinkStack_Push( stack , (void*)(result) );
}
else{
cout << "Invalid expression!" << endl;
break;
}
++pos;
}
if ((chen.LinkStack_Size(stack) == 1) && (*pos == '\0')){
ret = (int)chen.LinkStack_Pop(stack);
}
else{
cout << "Invalid expression!" << endl;
}
chen.LinkStack_Destroy(stack);
return ret;
}
int main(int argc, char **argv) {
char a[50] = {0};
tranform("9-2",a);
cout << endl;
cout << computer(a) << endl;
system( "pause" );
return 0;
}