作者yauhh (哟)
看板C_and_CPP
标题Re: [问题] 这个演算法要怎麽写出来?
时间Fri Jun 5 18:36:09 2009
※ 引述《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)