中缀表达式转换到后缀表达式(java实现)

Java实现中缀表达式转换成逆波兰表达式(后缀表达式) 实现基本步骤 用数组接收输入的原始表达式。 用两个栈来进行操作,一个栈用来存放运算符,另一个栈用来存放输出的逆波兰表达式。 依次遍历原始表达式,再根据遍历的元素分别进行操作。 如果遍历为数字,直接输出到逆波兰表达式栈中。 如果遍历为“(”,入运算符栈里。 如果遍历为“)”,则依次把运算符栈栈顶的运算符加入逆波兰表达式栈中,直到出现“(”,接着从栈中删除“(”,逆波兰表达式中... 阅读详情
/*
将中缀表达式转换为后缀表达式
定义优先级:'(' : 4
'*',  '/' : 3
'+', '-' : 2
')'       : 1
思路:
循环遍历输入字符数组
if input[i]是数字,则print; continue;
循环
if栈空,则input[i]入栈, break;
if outputPriority < inputPriority , 则input[i]入栈,break;
if outputPriority >= inputPriority 
if output != '(' ,则print(output),continue;
else
if(input != ')'), 则出栈; break;
else input[i] 入栈; break;
*/
import java.util.*;
public class MidExpToLaterExp{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		char[] input = scan.next().toCharArray();
		Stack stack = new Stack(10);
		
			for(int i = 0; i < input.length; i ++){
				int inputPriority = getPriority(input[i]);
				if(inputPriority == 0){
					System.out.print(input[i]);
					continue;
				}
				while(true){
					if(stack.isEmpty()){
						stack.push(input[i]);
						break;
					}
					char output = stack.peek();
					int outputPriority = getPriority(output);
					if(outputPriority < inputPriority){
						stack.push(input[i]);
						break;
					}else{
						if(output != '('){
							stack.pop();
							System.out.print(output);
							continue;
						}else{
							if(input[i] != ')'){
								stack.push(input[i]);
								break;
							}else{
								stack.pop();
								break;
							}
						}						
					}
				}
			}
			while(!stack.isEmpty()){
				System.out.print(stack.pop());
			}
	}
	
	public static int getPriority(char ch){
		int priority = 0;
		switch(ch){
			case '(' : priority = 4; break;
			case '*' : 
			case '/' : priority = 3; break;
			case '+' : 
			case '-' : priority = 2; break;
			case ')' : priority = 1; break;
		}
		return priority;
	}
}
class Stack{
	private char[] array;
	private int top;
	private int maxSize;
	public Stack(int maxSize){
		this.maxSize = maxSize;
		array = new char[maxSize];
		top = 0;
	}
	public void push(char ch){
		array[top++] = ch;
	}
	public char pop(){
		return array[--top];
	}
	public char peek(){
		return array[top -1];
	}
	public boolean isEmpty(){
		return top == 0;
	}
	public boolean isFull(){
		return top == maxSize;
	}
}


用Java实现输入算数式计算值 ---中缀表达式转换为后缀表达式 基本要求 实现文本计算器,支持加减乘除、括号。 基本思路: 首先是先将输入的文本字符串 通过正则表达式转换为一个个字符 再将其转换为后缀表达式: 其中中缀表达式转换为后缀表达式 需要使用到栈的相关操作 还有一些基本的规则: 1.遇到操作数,直接输出; 2.栈为空时,遇到运算符,入栈; 3.遇到左括号,将其入栈; 4.遇到右括号,执行出栈操作,并将出栈的元素输出,直到弹出栈的是... 阅读详情

相关推荐

输入一个中缀表达式,将其转换为等价的后缀表达式后输出并计算结果。

输入一个中缀表达式,将其转换为等价的后缀表达式后输出并计算结果。

java后缀表达式_Java中缀表达式转后缀表达式实现方法详解

本文实例讲述了Java中缀表达式转后缀表达式实现方法。分享给大家供大家参考,具体如下:本文先给出思路与方法,最后将给出完整代码项目实战:算法综述:一、中缀表达式转后缀表达式:1.中缀表达式要转后缀表达式,首先需要两个Stack(栈),其中一个应用于存放字符,另一个用于存放数字。2.读到数字直接存入数字栈中,读到字符时,要咸鱼栈内前一元素(字符)进行比较,当当前(要存入的字符)优先级大于迁移字符时才...

weixin_36068796的博客 647

中缀表达式转后缀表达式并求值(java)

NULL 博文链接:https://128kj.iteye.com/blog/1623312

中缀表达式转后缀表达式详解(Java描述)

先扯个蛋@.@ 后缀表达式,作用在于方便计算机来计算表达式的一种表达式。=。=好啰嗦。 直接举一个例子吧: 比如ab*cd+/: 从左到右扫描,扫描到一个运算符,就将前面两个数字来做运算 ab*—->变成a*b,作为一个整体的值。然后继续扫描到+号,就把cd加起来成为一个整体的值c+d,然后继续扫描到/号,就把前面计算的两个值相除,即(a*b)/(c+d)。这个表达

sinat_26706135的专栏 686

手把手教你自制Type-C OTG转换器(附电路图及避坑指南)

本文详细指导如何自制Type-C OTG转换器,包括核心电路设计、元器件选型及焊接技巧,特别强调硬件检测电路的关键作用。通过实测数据和避坑指南,帮助开发者解决兼容性问题,实现高效数据传输。

weixin_27038245的博客 713

通义灵码+高德 MCP:40分钟开发出游攻略网站

本文介绍了通义灵码与高德MCP在40分钟内快速开发出游攻略网站的全流程。主要内容包括:1)LingmaIDE开发环境的使用方法,涵盖界面操作、代码编辑、调试和智能编码功能;2)通义灵码智能体与高德MCP2.0的集成步骤,通过输入旅行需求即可自动生成包含天气卡片、景点路线等元素的HTML页面;3)重点展示了如何将行程规划导入高德地图APP,实现导航、打车等出行功能。整个开发过程充分利用了通义灵码的智能编码能力和高德地图的服务接口,显著提升了旅游类应用的开发效率。

soso1968的博客 986

中缀表达式转换为后缀表达式Java实现

后缀表达式适合计算机进行计算,但是并不适合我们日常计算习惯,特别是碰到表达式特别长的情况下,因此我们使用栈,我们将中缀表达式准成后缀表达式来进行计算。将中缀表达式“1+((2+3)×4)-5”转换为后缀表达式的过程如下 因此结果为: 1 23+4 ×+5-......

xiangjunyes的博客 918

使用 C# 开发海康设备实时视频流的监控与报警系统

本文介绍了如何使用C#结合海康威视SDK(HCNetSDK)开发安防监控系统,涵盖设备登录、视频流获取、报警处理和云台控制等核心功能。内容包括开发环境配置、系统架构设计、关键代码实现(如设备登录、实时视频播放、报警设置)以及用户界面设计。通过Windows Forms/WPF,开发者可构建完整的监控应用,实现设备管理、视频监控、报警响应等功能。系统采用模块化设计,便于扩展和维护,适用于各类安防场景。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1357

JAVA中实现中缀表达式向后缀表达式的转换,并求值

最近在学习JAVA,来更一下最近练习的一下小题目 使用栈来进行中缀表达式和后缀表达式的转换,并对输出的后缀表达式进行计算求值 package baoxinhai_test; import java.util.*; public class Exp { private char[] inputlist; //输入的中缀表达式 private char[] signlist; //符...

qq_42013013的博客 444

中兴问天 BE7200MAX SFP 光口用华为猫棒替代光猫完整配置教程

PPPoE 账号密码:若原光猫为 Route 模式,在宽带设置页可见用户名,密码需按 F12 打开浏览器开发者模式,点击密码输入框,查看代码中 “value=” 后的内容即为明文密码。若仍失败,核对 PON 类型是否与运营商网络匹配。PON 认证信息:LOID(逻辑 ID,部分地区含密码)、设备 SN 码(标签或设备信息页)、PON 类型(GPON/EPON,运营商网络类型)。电脑保持与路由器连接,访问路由器管理地址(默认通常为 192.168.5.1,可参考路由器说明书),登录后台。

Gulper_Eel的博客 3341

极智AI | 详解 ViT 算法实现

大家好,我是极智视界,本文详细介绍一下 ViT 算法的设计与实现,包括代码。

极智视界 5604

java代码实现中缀表达式转换为后缀表达式

## java代码实现中缀表达式转换为后缀表达式 实现思路: ![在这里插入图片描述](https://img-blog.csdnimg.cn/013beab0de9b41d4aee647c2a506054b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rGf5bCP55m955So5oi3,size_20,color_FFFFFF,t_70,g_se,x_16#pic_cent

qq_45675684的博客 455

五、MT8168 音频驱动之SPEAKER

本章主要讲解MT8168平台的内部SPEAKER驱动配置。分为HP接法和LINE OUT接法。2.1.3 audio_custom_exp.h设置。2.1.4 mixer_paths.xml设置。2.2.3 mixer_paths.xml设置。2.1.2 ProjectConfig 设置。2.2.2 ProjectConfig 设置。2.2.1 deconfig设置。2.1.1 DTS设置。......

Joon2020的博客 1664

中缀表达式转换后缀表达式并求值(java实现)

中缀表达式转换后缀表达式并求值中缀表达式转换后缀表达式 为了处理方便,编译程序通常把中缀表达式转化为后缀表达式,后缀表达式也称为“逆波兰式”指代双目运算符在两个运算对象的后面,例如:49*23+。 程序伪代码1.实例化一个空栈S 2.扫描容器中的字符,执行以下操作 2.1若当前字符是运算对象即数字,则存入后缀表达式容器。 2.2若当前字符是运算符,且其优先级

High5753的博客 759

Python爬虫实战:利用Selenium与反反爬技术高效爬取企查查工商信息

本文将详细介绍如何使用Python最新爬虫技术(包括Selenium、Playwright、智能代理池等)爬取企查查工商信息数据。文章包含完整的代码实现、反反爬策略、数据解析与存储方案,以及法律合规性讨论,共计约8000字。企查查作为国内领先的企业信息查询平台,收录了超过2亿家企业的工商信息、司法信息、知识产权等数据。这些数据对于市场分析、商业决策等具有重要价值。法律边界提示禁止大规模爬取导致服务器过载不得绕过付费接口获取数据数据使用需遵守《个人信息保护法》商业用途需获得授权。

2201_76125261的博客 2236

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

《数据结构与算法》(一) 基于JAVA栈 实现中缀表达式后缀表达式互相转换

利用栈实现中缀表达式转后缀表达式一、原理:需要注意的是:二、案例如下:3、代码: 一、原理: a.定义一个栈用于存放运算符,以及 括号 “[()]” 操作符 b.定义一个字符串result用于接收计算结果 c.从表达式第一个字符开始作为判断位;(判断位,用于与栈顶元素对比优先级,从而确定:将判断位入栈 或者 将栈顶元素弹出 ) d.遇到操作数作为判断位,直接将其作为结果添加到result的末尾; e.如果碰到判断位是操作符,当栈为空时直接将其入栈,表达式下一位作为判断位;栈不为空时,比较

bokerr的博客 313

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java中缀表达式转换为后缀表达式_中缀表达式转换到后缀表达式(java实现)

/*将中缀表达式转换为后缀表达式定义优先级:'(':4'*', '/' :3'+', '-':2')':1思路:循环遍历输入字符数组if input[i]是数字,则print; continue;循环if栈空,则input[i]入栈,break;if outputPriority < inputPriority ,则input[i]入栈,break;if outputPriority &g...

weixin_39917811的博客 219

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

java语言实现中缀表达式转换为后缀表达式

import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; import java.util.regex.Pattern; import org.springframework.util.StringUtils; /** * @ClassName: PostfixExpression.ja...

qq_28535633的博客 198

java中缀表达式转后缀表达式_数据结构Java实现06----中缀表达式转换为后缀表达式...

本文主要内容:表达式的三种形式中缀表达式与后缀表达式转换算法一、表达式的三种形式:中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3。我们从小做数学题时,一直使用的就是中缀表达式。后缀表达式:不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则),如:2 1 + 3 *。又比如3+(6-4/2)*5=23的后缀表达式为:3642...

weixin_39746282的博客 437

中缀表达式转换为后缀表达式(Java实现)

常见的四则运算是中缀表达式,为了方便编程,转换为后缀表达式

ylqdh的博客 1186

数据结构Java实现06----中缀表达式转换为后缀表达式

本文主要内容: 表达式的三种形式中缀表达式与后缀表达式转换算法 一、表达式的三种形式: 中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3。我们从小做数学题时,一直使用的就是中缀表达式。后缀表达式:不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则),如:2 1 + 3 *。又比如3+(6-4/2)*5=

蜡笔小新的博客 374

java实现中缀表达式转换成后缀表达式

import java.util.Scanner; import java.util.Stack; import org.junit.Test; /** * 中缀表达式 -&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; 后缀表达式 * @author itis666 */ public class InfixToSuffix { /** * 将中缀表达式转换成后缀表达式 * @param resExp 原...

itis666的博客 1313
上一篇: java编程思想笔记
下一篇: 用栈来模拟实现汉诺塔问题
拓-拓
博客等级 码龄12年 10粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值