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

字號:

AnsiString __fastcall Calc(String sExp)
    {
    // 計算不帶變量的四則混合運算表達式(只含數字、小數點、+-*/號和括號)
    // 正數不許帶正號
    int posL, pos, posR;
    // pos->當前考慮的運算符的位置
    // posL->當前考慮的運算符之前最近的運算符的位置
    // posL->當前考慮的運算符之前后近的運算符的位置
    String sTmp, sL, sR;
    // sL->當前考慮的運算符的左操作數字符串,sR->當前考慮的運算符的右操作數字符串
    bool IsMinus; // IsMinus->當前*/序列的符號
    if(sExp.AnsiPos("error"))
    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) // 統(tǒng)一左括號為(
    sExp = sExp.SubString(1, pos - 1) + "("
     + sExp.SubString(pos + 1, sExp.Length());
    while((pos = sExp.AnsiPos("]")) > 0
    || (pos = sExp.AnsiPos("}")) > 0) // 統(tǒng)一右括號為)
    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中的運算符號只剩下+和-了
    // 以下處理不帶括號表達式中的+-序列
    IsMinus = false; // 加數或減數的符號
    while(sExp.LastDelimiter("+-")) // 存在+或-
    {
    for(pos=2; !sExp.IsDelimiter("+-", pos)
     && pos <= sExp.Length(); pos++); // 第一個+或-