Re: [問題] 這個演算法要怎麼寫出來?
※ 引述《bil193 (丁丁)》之銘言:
: 在試寫一個計算器的程式
: 例如輸入 12.5*(10/2.0)-9/3
: 自己心中的演算法為:
: 先讀字串
: while(還沒讀完)
: {
: if(數字)
: 數字存入某double的陣列;
: if(為運算符號)
: 運算符號存入另一陣列;
: }
: 想了很久還是不曉得要怎麼區分'數字'和'運算符號'
: 請問這要怎麼寫啊??
: 因為可能兩個運算符號連在一起 如*(
讀進來分段為 {"12.5", "*", "(", "10", "/", "2.0", ")", "-", "9", "/", "3"},
之後交給中序轉後序程式,以及後序計算程式處理.
輸入分段程式大概是這樣:
//虛擬碼唷,有些是C/C++碼,有些則是口語碼
char term;
char digits = "012345679.";
char operators = "+-*/()";
string pre_input = "";
string inputs[32] = {};
int count_inputs = 0;
term = read_a_byte();
while(term != EOF) {
if (contain(digits, term))
pre_input = append(pre_input, term);
else if (contain(operators, term)) {
if (pre_input != "") {
inputs[count_inputs] = pre_input;
count_inputs++;
pre_input = "";
}
inputs[count_inputs] = charToString(term);
count_inputs++;
}
term = read_a_byte();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.160.112.12
※ 編輯: yauhh 來自: 218.160.112.12 (06/05 18:37)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
14
24