设为首页 加入收藏

TOP

用于计算公式的代码(六)
2012-11-06 11:25:17 来源: 作者: 【 】 浏览:3863
Tags:用于 计算 公式 代码


    
    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 // 没有括号,直接计算表达式
   
    {

        

首页 上一页 3 4 5 6 7 8 9 下一页 尾页 6/17/17
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.1 图的基本概念 下一篇银行家算法——C语言

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: