国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C++中用于計算四則混合運算表達式的遞歸函數

2019-11-17 05:43:43
字體:
來源:轉載
供稿:網友
用于計算四則混合運算表達式的遞歸函數
by billow3(QQ:41965573)
//---------------------------------------------------------------------------
AnsiString __fastcall Calc(String sEXP)
{
    // 計算不帶變量的四則混合運算表達式(只含數字、小數點、+-*/號和括號)
    // 正數不許帶正號
    int posL, pos, posR;
    // pos->當前考慮的運算符的位置
    // posL->當前考慮的運算符之前最近的運算符的位置
    // posL->當前考慮的運算符之前后近的運算符的位置
    String sTmp, sL, sR;
    // sL->當前考慮的運算符的左操作數字符串,sR->當前考慮的運算符的右操作數字符串
    bool IsMinus; // IsMinus->當前*/序列的符號
    if(sExp.Ans        return(sExp);
    while(pos = sExp.AnsiPos(" "))
        sExp = sExp.Delete(pos, 1); // 去除表達式中的空格
    if(sExp.IsEmpty())
        return("0");
    while((pos = sExp.AnsiPos("[")) > 0
             (pos = sExp.AnsiPos("{")) > 0) // 統一左括號為(
        sExp = sExp.SubString(1, pos - 1) + "("
                + sExp.SubString(pos + 1, sExp.Length());
    while((pos = sExp.AnsiPos("]")) > 0
             (pos = sExp.AnsiPos("}")) > 0) // 統一右括號為)
        sExp = sExp.SubString(1, pos - 1) + ")"
                + sExp.SubString(pos+1, sExp.Length());
    // 處理括號:遞歸計算括號中的表達式,最后消去括號
    while(posL=sExp.LastDelimiter("(")) // 最里層(
    {
        sTmp = sExp.SubString(posL + 1, sExp.Length());
        posR = sTmp.AnsiPos(")"); // 最里層)
        if(posR == 0)
            return("error:沒有配對的), 公式錯!");
        sExp = sExp.SubString(1, posL - 1)
                + Calc(sTmp.SubString(1, posR - 1))
                + sTmp.SubString(posR + 1, sTmp.Length());
    }
    // 以下處理不帶括號表達式中的*/序列
    IsMinus = false; // IsMinus->當前*/序列的符號
    while(sExp.LastDelimiter("*/")) // 存在*或/
    {
        for(pos = 1; !sExp.IsDelimiter("*/", pos)
                && pos <= sExp.Length(); pos++); // 第一個*或/
        if(pos == 1  pos == sExp.Length())
            return("error:首或尾字符是*/運算符, 公式錯!");
        posL = sExp.SubString(1, pos - 1).LastDelimiter("+-");
        // posL->第一個*/之前的第一個+-
Minus0:
        for(posR = pos + 1; !sExp.IsDelimiter("+-*/", posR)
                && posR <= sExp.Length(); posR++);
        // posR->第一個*/之后的第一個+-*/運算符
        if(posR == sExp.Length())
            return("error:尾字符是+-*/運算符, 公式錯!");
        if(sExp.SubString(pos, 2) == "*-"
                 sExp.SubString(pos, 2) == "/-") // 乘數或除數為負
        {
            sExp.Delete(pos+1, 1);
            IsMinus = !IsMinus;
            goto Minus0;
        }
        sL = sExp.SubString(posL + 1, pos - posL - 1);
        sR = sExp.SubString(pos + 1, posR - pos - 1);
        if(sExp.IsDelimiter("/", pos) && sR == "0")
            return("error:除數為零,無意義!");
        sExp = (posL == 0? String(""): sExp.SubString(1, posL))
                + (sExp.IsDelimiter("*", pos)?
                (sL.ToDouble() * sR.ToDouble()):
                (sL.ToDouble() / sR.ToDouble()))
                + sExp.SubString(posR, sExp.Length());
    }
    if(IsMinus)
        sExp = String("-") + sExp;
    // 經過上面的系列處理,sExp中的運算


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灯塔市| 达日县| 江陵县| 呼和浩特市| 旬邑县| 调兵山市| 富川| 靖远县| 莱西市| 潮州市| 福清市| 张家川| 玛纳斯县| 四平市| 闵行区| 福鼎市| 胶州市| 开封县| 民乐县| 易门县| 南皮县| 邵阳县| 清新县| 铁力市| 灵璧县| 昌黎县| 平和县| 如东县| 北宁市| 石渠县| 瑞昌市| 车致| 洛隆县| 和平县| 澄迈县| 光泽县| 普兰县| 石门县| 通州市| 依兰县| 仁布县|