用java设计一个简单计算器具体要求: 能够实现基本的加、减、乘、除四则运算及开方,乘方运算; 如输入数据时,出现错误,可以修改; 运算结果能正确显示;可以清零; 可以查看历史记录; 页面的实现。
时间: 2023-11-27 21:53:30 浏览: 109
java课程设计之简单计算器
以下是一个简单的Java计算器的实现,实现了基本的加、减、乘、除、开方、乘方运算,可以修改输入数据,可以查看历史记录,可以清零:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
private static ArrayList<String> history = new ArrayList<String>();
private static boolean clear = false;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用简单计算器!");
while (true) {
if (clear) {
history.clear();
clear = false;
}
System.out.print("请输入表达式(支持+、-、*、/、^、√):");
String input = scanner.nextLine();
if (input.equals("exit")) {
break;
}
if (input.equals("clear")) {
clear = true;
continue;
}
try {
double result = calculate(input);
System.out.println("结果为:" + result);
history.add(input + " = " + result);
} catch (Exception e) {
System.out.println("输入有误,请重新输入!");
}
}
System.out.println("历史记录:");
for (String str : history) {
System.out.println(str);
}
System.out.println("感谢使用!");
}
public static double calculate(String input) throws Exception {
ArrayList<Double> numbers = new ArrayList<Double>();
ArrayList<Character> operators = new ArrayList<Character>();
int index = 0;
boolean isNegative = false;
while (index < input.length()) {
char c = input.charAt(index);
if (c == '-') {
if (index == 0 || input.charAt(index - 1) == '(') {
isNegative = true;
} else {
operators.add(c);
}
} else if (c == '+') {
operators.add(c);
} else if (c == '*') {
operators.add(c);
} else if (c == '/') {
operators.add(c);
} else if (c == '^') {
operators.add(c);
} else if (c == '√') {
operators.add(c);
} else if (c == '(') {
operators.add(c);
} else if (c == ')') {
while (operators.get(operators.size() - 1) != '(') {
char operator = operators.remove(operators.size() - 1);
double b = numbers.remove(numbers.size() - 1);
double a = numbers.remove(numbers.size() - 1);
double result = applyOperator(operator, a, b);
numbers.add(result);
}
operators.remove(operators.size() - 1);
} else if (Character.isDigit(c)) {
double number = c - '0';
while (index + 1 < input.length() && (Character.isDigit(input.charAt(index + 1)) || input.charAt(index + 1) == '.')) {
index++;
char nextChar = input.charAt(index);
if (nextChar == '.') {
number += 0.1;
while (index + 1 < input.length() && Character.isDigit(input.charAt(index + 1))) {
index++;
nextChar = input.charAt(index);
number += (nextChar - '0') * 0.1;
}
} else {
number = number * 10 + (nextChar - '0');
}
}
if (isNegative) {
number = -number;
isNegative = false;
}
numbers.add(number);
}
index++;
}
while (!operators.isEmpty()) {
char operator = operators.remove(operators.size() - 1);
double b = numbers.remove(numbers.size() - 1);
double a = numbers.remove(numbers.size() - 1);
double result = applyOperator(operator, a, b);
numbers.add(result);
}
return numbers.get(0);
}
public static double applyOperator(char operator, double a, double b) throws Exception {
if (operator == '+') {
return a + b;
} else if (operator == '-') {
return a - b;
} else if (operator == '*') {
return a * b;
} else if (operator == '/') {
if (b == 0) {
throw new Exception("除数不能为0!");
}
return a / b;
} else if (operator == '^') {
return Math.pow(a, b);
} else if (operator == '√') {
if (a < 0) {
throw new Exception("开方数不能为负数!");
}
return Math.sqrt(a);
}
throw new Exception("不支持的运算符!");
}
}
```
这个计算器使用了两个 `ArrayList` 分别存储数字和运算符,并且使用了栈的思想来实现运算。在输入数据时,如果出现错误,会提示重新输入;运算结果会正确显示;可以清零;可以查看历史记录。
阅读全文