java - 用于 n 位的 Java 计算器
问题描述
我用 Java 为计算器编写了这段代码,根据设计,它只适用于一位数。它适用于递归。我试图让它适用于多位数但没有成功。如何更改代码以使其适用于多位数字?你能给我一些想法吗?还是代码?无论你想要什么。
这是适用于个位数的版本。它假设一个数字后面总是跟着
- 算术运算符,即
+
,-
,*
或/
- 一个括号,即
(
或)
- 输入字符串的结尾
import java.io.IOException;
import java.io.InputStream;
public class Calculator {
private int lookAheadToken;
private InputStream inputStream;
public Calculator(InputStream inputStream) throws IOException,ParseError {
this.inputStream = inputStream;
lookAheadToken = inputStream.read();
}
private void consume(int symbol) throws IOException, ParseError {
if (lookAheadToken != symbol) throw new ParseError();
lookAheadToken = inputStream.read();
}
private int evalDigit(int digit) {
return digit - '0';
}
private void goal() throws ParseError, IOException {
int result = expr();
if(lookAheadToken == '\n') {
System.out.println("result = " + result);
}
consume(lookAheadToken);
if(lookAheadToken == -1) return;
goal();
}
private int expr() throws ParseError, IOException {
int result = term();
return rest_expr(result);
}
private int rest_expr(int factor) throws ParseError, IOException {
if(lookAheadToken == '\n' || lookAheadToken == ')' || lookAheadToken == -1 ) return factor;
if(lookAheadToken != '+' && lookAheadToken != '-') throw new ParseError();
char operator = (char)lookAheadToken;
consume(lookAheadToken);
int result = term();
if(operator == '+') {
return rest_expr(factor + result);
}
else if(operator == '-') {
return rest_expr(factor - result);
}
else return factor;
}
private int term() throws ParseError, IOException {
int result = factor();
return rest_term(result);
}
private int rest_term(int factor) throws ParseError, IOException {
if(lookAheadToken == '\n' || lookAheadToken == ')' || lookAheadToken == -1 ) return factor;
if(lookAheadToken == '+' || lookAheadToken == '-') return factor;
if(lookAheadToken != '*' && lookAheadToken != '/') throw new ParseError();
char operator = (char)lookAheadToken;
consume(lookAheadToken);
int result = factor();
if(operator == '*') {
return rest_term(factor * result);
}
else if(operator == '/') {
return rest_term(factor / result);
}
return factor;
}
private int factor() throws ParseError, IOException {
if(lookAheadToken == '(') {
consume(lookAheadToken);
int result = expr();
if(lookAheadToken == ')') {
consume(lookAheadToken);
return result;
}
else throw new ParseError();
}
if(lookAheadToken < '0' || lookAheadToken > '9') throw new ParseError();
int result = evalDigit(lookAheadToken);
consume(lookAheadToken);
return result;
}
public static void main(String[] args) throws IOException, ParseError {
InputStream inputStream = System.in;
Calculator calculator = new Calculator(inputStream);
calculator.goal();
}
}
解决方案
推荐阅读
- firebase-hosting - 将 React 网站部署到 Firebase 的问题
- javascript - 盖茨比:如何在 head 标签中注入原始 html
- powershell - 制作和显示与命令提示符相同的信息
- python-2.7 - 在 python 中用海龟创建网格
- python - 使用无服务器框架进行本地开发
- python-3.x - 避免调试装饰器,而只调试装饰器
- ios - 如何在 PercentEncoding 中只编码所有特殊字符是 swift 5
- javascript - 使用 JS 删除 facebook 像素 cookie
- typescript - 如何从“导出模块”块导入类型?
- java - 并行运行测试时输出一次日志文件横幅