应如何将输入字符串读取到调车场算法计算器中

How should an input string be read into a shunting yard algorithm calculator?

本文关键字:调车场 算法 计算器 读取 字符串 输入      更新时间:2024-05-10

我已经实现了调车场算法的基本结构,但我不知道如何读取多位数或函数值。以下是我目前要阅读的价值观:

string input;
getline(cin, input);
input.erase(remove_if(input.begin(), input.end(), ::isspace), input.end());
//passes into function here
for (int i = 0; i < input.length(); ++i) {
string s = input.substr(i, 1);
//code continues
}

正如您所看到的,这种方法一次只能解析一个字符,因此它存在极大的缺陷。我也尝试过搜索读入值或解析它们,但在这里没有找到相关的结果。

完整代码:https://pastebin.com/76jv8k9Y

为了运行调车场,您需要首先标记您的字符串。也就是说,把12+4变成{'12','+','4'}。然后你可以使用代币运行调车场。一个天真的中缀词法算法可能是这样的:

lex(string) {
buffer = ""
output = {}
for character in string {
if character is not whitespace {
if character is operator {
append buffer to output
append character to output
buffer = ""
} else {
append character to buffer
}
}
append buffer to output
return output
}

真正的lexer要复杂得多,是编译器设计中的一个主要研究领域。