看大話資料結構 利用棧實現四則運算這一塊兒正好是只有講解沒有**實現的 於是照著書上的原理自己寫了個四則運算的c#** 以後可以考慮到作為自己的類庫中的元件
主要涉及的是逆波蘭式 程式主要部分為逆波蘭式的產生 以及通過逆波蘭式產生最終的四則運算結果
使用的時候只需輸入四則運算的算是即可 如2*(1+2)+3*(4+5) 沒有中括號大括號 程式會分三個步驟來 首先分割算式 將運算子與數字之間插入空格 方便之後的出入棧操作 如
2 * ( 1 + 2 ) + 3 * ( 4 + 5 )
之後將原式子轉變為逆波蘭式 最後通過逆波蘭式生成最終的算術結果
using system;
using system.collections.generic;
using system.text;
using system.text.regularexpressions;
public static double getvalue(double left, double right, char _operator)
return 0;
}public static double getresult(string source)
)quot;))
else if (operatorlevel.containskey(current))
}return double.parse(stack.pop());
}public static string converttorpn(string source)
)quot;))
else if (operatorlevel.containskey(current))
if (current == "(")
if (current == ")")
//pop the "("
stack.pop();
continue;
}if (operatorlevel[current] < operatorlevel[prev])
else
}stack.push(current);
}else
}else}}
if (stack.count > 0)}}
return result.tostring();
}public static string insertblank(string source)
else
}return sb.tostring();
}//運算子字典 方便查詢運算子優先順序
public static dictionaryoperatorlevel
return _operatorlevel;}}
}}
C 實現四則運算
表示式計算思路 1 準備兩個棧,乙個資料棧,乙個運算子棧。反覆讀取表示式 2 3 4 2 如果是數,入數棧。3 如果是左括號,入運算子棧。如果是右括號,反覆從運算子棧頂取運算符合從資料棧中取兩個資料進行計算,並把運算結果入資料棧,直到遇到棧頂或左括號為止。4 如果是運算子,先跟棧頂的運算子比,只要不...
實現四則運算
總結最近在看資料結構,遇到第乙個實際棧的應用,記錄 將平時的四則運算表示式又稱為中綴表示式轉化為字尾表示式。遇數字輸出,遇符號進棧,符號優先順序低於棧當前符號則輸出,輸出直到同等優先順序符號。例 9 3 1 2 輸出 棧 9 in 9 in 9 3 9 3 in 9 3 1 9 3 1 in 右括號...
C 四則運算
一 問題及 檔名稱 兩個浮點數的四則運算 02.作 者 李欽 03.完成日期 2016 年 3 月 12 日 04.版 本 號 v1.0 05.對任務及求解方法的描述部分 06.輸入描述 07.問題描述 略 08.程式輸出 略 09.問題分析 略 10.演算法設計 略 11.includevoid ...