当前位置:网站首页>【基础版】整数加减乘除计算器
【基础版】整数加减乘除计算器
2022-08-09 14:49:00 【云海谷天】
前言:
计算器的模拟实现主要分模式匹配和式子解析两部分,本文主要针对后者进行分析并实现。
测试样例:
1+2
3 * (4 + 5)
(4 + 5) / 3 + (1 + 2*1)
预计输出:
1+2 ==> 3 3 * (4 + 5) ==> 27 (4 + 5) / 3 + (1 + 2*1) ==> 6
分以下3个步骤:
1)StringTokenizer切分单个字符;
2)根据计算的优先级,通过当前运算符判断是否进行运算操作;
(括号 > 乘除 > 加减)
3)两栈进行数字运算;
附Java代码:
package com.yeezhao.common.elt;
import java.util.Stack;
import java.util.StringTokenizer;
/**
* 正整数计算器
*
* @author Administrator -> junhong
* @since 2016年12月13日 下午1:22:40
*/
public class CaculatorSimulate {
private Stack<Character> operator = new Stack<>(); // 操作符
private Stack<Integer> operand = new Stack<>(); // 操作数 0-9
public void testCaculate() {
}
public void testOutput() {
}
/**
* 返回计算结果
*
* @param exp
* 正整数的加减乘除表达式
* @return
*/
public Integer caculate(String exp) {
if (exp == null || exp.isEmpty()) {
System.out.println("invalid exp!");
return 0;
}
StringTokenizer arr = new StringTokenizer(exp, "+-/*()", true);
while (arr.hasMoreTokens()) {
String text = arr.nextToken().trim();
if (text.length() == 0) { // fix '(('
continue;
}
char c = text.charAt(0);
if (c == '+' || c == '-') {
while (!operator.isEmpty() && ((operator.peek() == '+') || (operator.peek() == '-')
|| (operator.peek() == '/') || (operator.peek() == '*'))) {
oper(operator, operand);
}
operator.push(c);
} else if (c == '/' || c == '*') {
while (!operator.isEmpty() && ((operator.peek() == '/') || (operator.peek() == '*'))) {
oper(operator, operand);
}
operator.push(c);
} else if (c == '(') {
operator.push(c);
} else if (c == ')') {
while (operator.peek() != '(') {
oper(operator, operand);
}
operator.pop();
} else {
operand.push(Integer.parseInt(text));
}
}
while (!operator.isEmpty()) {
oper(operator, operand);
;
}
return operand.pop();
}
public void oper(Stack<Character> operator, Stack<Integer> operand) {
Integer num1 = operand.pop();
Integer num2 = operand.pop();
Character oper = operator.pop();
Integer ret = 0;
if (oper == '-')
ret = num2 - num1;
if (oper == '+')
ret = num2 + num1;
if (oper == '*')
ret = num2 * num1;
if (oper == '/') {
if (num1 == 0) {
System.out.println("'0' can not be divided!");
} else {
ret = num2 / num1;
}
}
operand.push(ret);
}
public static void main(String[] args) {
String[] exps = { "1+2", "3 * (4 + 5)", "(4 + 5) / 3 + (1 + 2*1)" };
System.out.println("---***---");
for (String exp : exps) {
int ans = new CaculatorSimulate().caculate(exp);
System.out.println(exp + " ==> " + ans);
}
}
}后记:
加强版的计算器可增加如下内容:
1.平方,根号等复杂运算
2.正负数
3.小数
后续有余力之时自当尽快补上!
边栏推荐
猜你喜欢

实践数据湖iceberg 第三十八课 spark sql, Procedures语法进行数据治理(小文件合并,清理快照)

和月薪5W的测试聊过后,才知道自己一直在打杂...

leetcode 剑指 Offer 07. 重建二叉树

vivo手机上的系统级消息推送平台的架构设计实践

暴雨天,看天翼云如何“快准稳”防涝

由于谷歌版本自动更新,导致selenium项目报错,如何关闭谷歌浏览器的自动更新?

Refuse to "reinvent the wheel", Baidu EasyDL lets you play with AI custom development

结合实际聊聊电平转换电路(常用电平转换电路总结)

防汛添利器,数字技术筑起抗洪“大堤”

【NodeJs篇】关于path 路径模块的学习和使用
随机推荐
[DevOps] jekins deployment (1)
【OpenGL】三、OpenGL总结:OpenGL坐标系
table中 You may have an infinite update loop in a component render function问题解决
[Microservice] Detailed explanation of nacos registration center and configuration center
第五讲 测试技术与用例设计
MySQL lock mechanism and lock algorithm
正则表达式实战:最新豆瓣top250爬虫超详细教程
Fiddler弱网测试
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000
[manjaro]更新后内核文件加载失败
The title icon favicon.icon on the browser tab
C语言——void指针、NULL指针、指向指针的指针、常量和指针
Analysis of select principle in golang
看完腾讯大佬90天整理的“Redis深度笔记”,我直接当场膜拜
听书项目总结
【OpenGL】四、OpenGL入门总结:LearnOpenGL CN教程中关于欧拉角公式推导
Introduction to JUC Containers
OpenCV笔记整理【视频处理】
.NET现代应用的产品设计 - DDD实践
【微信小程序】利用MPFlutter开发微信小程序