Re: [問題] 這個演算法要怎麼寫出來?

看板C_and_CPP (C/C++)作者 (喲)時間16年前 (2009/06/05 18:36), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《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)
文章代碼(AID): #1AAFKSlg (C_and_CPP)
文章代碼(AID): #1AAFKSlg (C_and_CPP)