android怎么实现让计算器结果入栈,数据结构:栈结构实现计算器

本文介绍了一种使用栈数据结构实现计算器的方法。通过解析表达式,利用两个栈分别存储数字和运算符,实现了对算术表达式的正确计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构:栈结构实现计算器

数据结构:栈结构实现计算器

package datastpr;

public class stackdemo {

public static void main(String[] args) {

String ex = "5*200+3+6";

ArrayStack2 shu = new ArrayStack2(10);

ArrayStack2 fuhao = new ArrayStack2(10);

int num1;//进入数字栈的num1

int num2;//进入数字栈的num2

int oper;//运算符号

int res;

int index = 0;

String keepNum = "";//处理多位数的情况

while (true) {

char temp = ex.substring(index, index + 1).charAt(0);

if (fuhao.isOper(temp))//判断你是不是一个运算符号

{//是一个运算符号,判断栈是不是空的

if (fuhao.isEmpty()) {

//是空的直接进栈

fuhao.push(temp);

} else {//不是空的,判断优先级

if (fuhao.priority(temp) <= fuhao.priority((char) fuhao.pick())) {

//栈顶元素出来

oper = fuhao.pop();

num1 = shu.pop();

num2 = shu.pop();

res = fuhao.cal(num1, num2, oper);

shu.push(res);

fuhao.push(temp);

} else {

fuhao.push(temp);

}

}

} else {//不是一个运算符号,那肯定就是一个数字了,push进shu栈

keepNum += temp;

if (index == ex.length() - 1) {

shu.push(Integer.parseInt(keepNum));

} else {

if (shu.isOper(ex.substring(index + 1, index + 2).charAt(0))) {

shu.push(Integer.parseInt(keepNum));

keepNum = "";

}

}

}

index++;

if (index >= ex.length()) {

break;

}

}

while (true) {

if (!fuhao.isEmpty()) {

oper = fuhao.pop();

num1 = shu.pop();

num2 = shu.pop();

res = fuhao.cal(num1, num2, oper);

shu.push(res);

} else {

break;

}

}

System.out.println(shu.pop());

/* if (fuhao.isEmpty())//符号栈用完了证明运算已经完成

{

System.out.println(shu.pop()-48);//栈顶的数字代表最后的运算结果

}*/

}

}

class ArrayStack2 {

private int maxSize;

private int[] stack;

private int top = -1;

//构造函数

public ArrayStack2(int maxSize) {

this.maxSize = maxSize;

stack = new int[this.maxSize];

}

//判断是不是一个运算符

public boolean isOper(char c) {

return c == '*' || c == '/' || c == '+' || c == '-';

}

//判断优先级1代表是一个乘除法,0代表是一个加减法,-1就是一个不合法的符号

public int priority(char a) {

if (a == '*' || a == '/') {

return 1;

} else if (a == '+' || a == '-') {

return 0;

} else return -1;

}

//弹出来之后的运算

public int cal(int num1, int num2, int oper) {

int res = 0;

switch (oper) {

case '+':

res = num1 + num2;

break;

case '-':

res = num2 - num1;

break;

case '*':

res = num1 * num2;

break;

case '/':

res = num2 / num1;

break;

default:

break;

}

return res;

}

//pick输出栈顶的第一个数

public int pick() {

return stack[top];

}

//判断栈是不是空了

public boolean isEmpty() {

return top == -1;

}

//判断栈是不是满了

public boolean isFull() {

return top == (maxSize - 1);

}

//往栈里面加数据

public void push(int value) {

//判断栈是不是满了

if (isFull()) {

throw new RuntimeException("您的栈已经满了");

}

top++;

stack[top] = value;

}

//往栈里面取数据

public int pop() {

int value;

if (isEmpty()) {

throw new RuntimeException("您的栈里面已经是空了的");

}

value = stack[top];

top--;

return value;

}

//遍历栈

public void list() {

if (isEmpty()) {

throw new RuntimeException("您的链表已经是空的,没有数据可以输出");

}

while (true) {

System.out.println(stack[top]);

top--;

if (top == -1) {

break;

}

}

}

}

81b21cd6dc6b8aa0d115f36472836025.png

24996c2f538dac5ac260bb303f571836.png

数据结构:栈结构实现计算器相关教程

微信小程序云数据库模糊查询实现一对一聊天

微信小程序云数据库模糊查询实现一对一聊天 示例:一对一聊天,查找自己对应了哪些聊天室 前提:房间ID=我的ID+你的ID; 实现:根据房间ID把房间分组 再模糊查询找到自己的ID在房间ID里的分组作为自己的聊天室 mygroup() {const $ = db.command.aggregatedb.c

最小生成树之Kruskals算法的实现

最小生成树之Kruskals算法的实现 首先要实现图的封装 package _7图;/** * 边的封装 * 边集可以用来表示图 */public class EdgeTimplements ComparableEdge { private T start ; //重要的就是三个属性 private T end; private int distance;public Edge(T star

用node.js实现HTML5原生的comet(长连接)

用node.js实现HTML5原生的comet(长连接) 为什么80%的码农都做不了架构师? comet 跟 ajax 不同的地方在于, ajax 是主动’拉’服务端的内容,而 comet 是服务端主动’推’内容给客户端。 实现成本及其简单,比起 ajax 模拟的 间隔一段去查询服务端内容 的方式

JS实现简单的倒计时

JS实现简单的倒计时 1、第一种 !DOCTYPE htmlhtmlheadmeta charset=utf-8titlewww.jb51.net JS倒计时/title/headbodydiv id=div/divscript type=text/javascriptwindow.οnlοad=clock;function clock(){var today=new Date(),//当前时间h=today.getHours(),m

对象以及数据结构的区别

对象以及数据结构的区别 对象和数据结构在面向对象的过程中慢慢的变得不是那么明显了,在读到代码整洁之道(clean code)的第6章甚至对什么事对象和什么事数据结构产生了疑问。 先用书中的一句话进行总结:对象是暴露行为,数据结构暴露数据。 概念 我们先看

系统目录结构lsalias

系统目录结构,ls,alias 1、linux目录结构及说明 1.1 linux目录结构图 1.2 常用的目录文件说明 /: 根目录,一般根目录下只存放目录,不要存放文件,/etc、/bin、/dev、/lib、/sbin应该和根目录放置在一个分区中 /bin:/usr/bin: 可执行二进制文件的目录,如

SSM实现原理以及SSM开发环境搭建整合

SSM实现原理以及SSM开发环境搭建整合 文章目录 前言 一、SSM的实现原理 1、作用 2、原理 二、SSM开发环境搭建整合 整体结构图 Maven配置依赖 Spring 配置 整合SpringMVC 配置web.xml文件 Log4j的配置 Mybatis的配置 mybatis-config文件内容 总结 前言 最近这

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值