模拟计算器

在这里插入图片描述

import java.util.Scanner;
public class Main{
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		int a,b;
		String c;
		char d;
		a=sc.nextInt();
		b=sc.nextInt();
		c=sc.next();//将字符串保存在c
		d=c.charAt(0);//将c中的第一个字符赋值给d
		switch(d) {
		case'+':System.out.println(a+b);break;
		case'-':System.out.println(a-b);break;
		case'*':System.out.println(a*b);break;
		case'/':System.out.println(a/b);break;
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个模拟计算器的 Java 代码示例,利用了栈的数据结构实现了四则运算: ``` import java.util.*; public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入待计算的表达式: "); String expression = input.nextLine(); List<String> postfixExpression = infixToPostfix(expression); // 将中缀表达式转换成后缀表达式 double result = evaluatePostfix(postfixExpression); // 计算后缀表达式的值 System.out.println("计算结果为: " + result); } // 将中缀表达式转换成后缀表达式 public static List<String> infixToPostfix(String expression) { List<String> postfixExpression = new ArrayList<>(); Stack<String> operatorStack = new Stack<>(); String[] tokens = expression.split("\\s+"); // 通过空格分隔字符串,得到运算符和操作数 for (String token : tokens) { if (token.matches("\\d+(\\.\\d+)?")) { // 如果是数字,直接加入后缀表达式列表 postfixExpression.add(token); } else if (token.equals("(")) { // 如果是左括号,入栈 operatorStack.push(token); } else if (token.equals(")")) { // 如果是右括号,将栈顶的运算符全部弹出并加入后缀表达式列表 while (!operatorStack.isEmpty() && !operatorStack.peek().equals("(")) { postfixExpression.add(operatorStack.pop()); } operatorStack.pop(); // 弹出左括号 } else { // 如果是运算符 while (!operatorStack.isEmpty() && getPrecedence(operatorStack.peek()) >= getPrecedence(token)) { postfixExpression.add(operatorStack.pop()); // 弹出优先级高于或等于当前运算符的所有运算符,并加入后缀表达式列表 } operatorStack.push(token); // 将当前运算符入栈 } } while (!operatorStack.isEmpty()) { postfixExpression.add(operatorStack.pop()); // 将剩余的运算符全部弹出并加入后缀表达式列表 } return postfixExpression; } // 计算后缀表达式的值 public static double evaluatePostfix(List<String> postfixExpression) { Stack<Double> operandStack = new Stack<>(); for (String token : postfixExpression) { if (token.matches("\\d+(\\.\\d+)?")) { // 如果是数字,入栈 operandStack.push(Double.parseDouble(token)); } else { // 如果是运算符,弹出两个操作数进行计算,并将结果入栈 double operand2 = operandStack.pop(); double operand1 = operandStack.pop(); switch (token) { case "+": operandStack.push(operand1 + operand2); break; case "-": operandStack.push(operand1 - operand2); break; case "*": operandStack.push(operand1 * operand2); break; case "/": operandStack.push(operand1 / operand2); break; } } } return operandStack.pop(); // 返回最终结果 } // 获取运算符的优先级 public static int getPrecedence(String operator) { switch (operator) { case "+": case "-": return 1; case "*": case "/": return 2; default: return 0; } } } ``` 该程序通过将中缀表达式转换成后缀表达式,再计算后缀表达式的值,实现了模拟计算器的功能。可以根据需要对代码进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值