pNodeOper->left = pNodeValue;
pCurNode = pNodeOper;
*pExpreNode = pNodeOper;
}
else
{
if( priority( pCurNode->value ) > priority( (*pExpreNode)->value ) )
{
if( priority( pCurNode->value ) == priority( pNodeOper->value ) )
{
pCurNode->right = pNodeValue;
pNodeOper->left = pCurNode;
(*pExpreNode)->right = pNodeOper;
}
else if( priority( pCurNode->value ) < priority( pNodeOper->value ) ) // 这是不可能的
{
}
else if( priority( pCurNode->value ) > priority( pNodeOper->value ) )
{
pCurNode->right = pNodeValue;
pNodeOper->left = *pExpreNode;
*pExpreNode = pNodeOper;
}
}
else if( priority( pCurNode->value ) == priority( (*pExpreNode)->value ) )
{
if( priority( pCurNode->value ) == priority( pNodeOper->value ) )
{
pCurNode->right = pNodeValue;
pNodeOper->left = pCurNode;
*pExpreNode = pNodeOper;
}
else if( priority( pCurNode->value ) < priority( pNodeOper->value ) )
{
pCurNode->right = pNodeOper;
pNodeOper->left = pNodeValue;
}
else if( priority( pCurNode->value ) > priority( pNodeOper->value ) )
{
pCurNode->right = pNodeValue;
pNodeOper->left = pCurNode;
*pExpreNode = pNodeOper;
}
}
pCurNode = pNodeOper;
}
index = 0;
number[index] = 0;
}
}
pExpresion ;
}
number[index] = 0;
tagExpreNode *pNodeValue = (tagExpreNode *)malloc( sizeof(tagExpreNode) );
memset( pNodeValue , 0 , sizeof(tagExpreNode) );
pNodeValue->value = atof(number);
pCurNode->right = pNodeValue;
return 0;
};
int sglnum( const char *pexpresion )
{
// 没有运算符,为单个数
if( !strchr(pexpresion , ' ’) && !strchr(pexpresion , '-‘) && !strchr(pexpresion , '*’) && !strchr(pexpresion , '/‘) )
return 1;
// 有且只有一个运算符,且运算符为第1个字符
int cnt = 0;
pexpresion = 1;
while( *pexpresion )
{
if( ('-' == *pexpresion) || (' ' == *pexpresion) || ('*' == *pexpresion) || ('/' == *pexpresion) )
return 0;
pexpresion ;
}
return 1;
};
// 计算pstr代表的字串的值 4 ((1 2)*(3 4) 3)/(1 1)
double calculate(const char *pexpresion)
{
if( 0 == valid(pexpresion) )
{
printf(“算式错误[%s].\n” , pexpresion);
return 0;
}
// 是否是单个数
if( sglnum(pexpresion) )
{
return atof(pexpresion);
}
double result = 0;
char *pleft = strchr(pexpresion , ’(‘);
char *pfirst = pleft; // pfirst 左括号位置 pleft 将指向对应的右括号位置
if( NULL != pleft )
{
char *pSubExpre = (char *)malloc( sizeof(char) * strlen(pexpresion) 1 );
memset( pSubExpre , 0 , sizeof(char) * strlen(pexpresion) 1);
while( 0 != push(*pleft) ) // 扫描对应的右括号位置
{
while(1)
{
pleft ;
if( ’(‘ == *pleft || ’)‘ == *pleft )
break;
}
}
strncpy(pSubExpre , pfirst 1 , pleft - pfirst - 1); // 遇到的第一个括号中的内容 (1 2)*(3 4) 3
result = calculate( pSubExpre ); //
char number[32] = {0};
memcpy( pSubExpre , pexpresion , pfirst - pexpresion );
pSubExpre[ pfirst - pexpresion ] = 0;
sprintf(number , “%.4f\0” , result);
strcat( pSubExpre , number );
strcat( pSubExpre , pleft 1);
result = calculate( pSubExpre ); // 计算出第一个括号之后剩余的内容 4 24 /(1 1) 递归调用
free( pSubExpre );
}
else // 没有括号,直接计算表达式
{