算术运算模拟
1.任务描述
该程序是对“+、-、、/”四种简单运算的模拟,在命令行输入类似“+ 1 2”的三个参数,即一个运算符和两个操作数,若参数个数多于或少于3个,程序会出错提示;若参数个数为3,但运算符不是+、-、、/中的一个,程序也会出现错误提示;在进行除法运算时,若被除数为0,程序也会出现错误提示。
2.运行结果
3.实现思路
- 符号、数字的提取:利用Scanner类的方法useDelimiter(string)结合正则表达式string=“[123456789.]+”匹配输入字符串中的字符。利用StringTokenizer类方法匹配出输入字符串中的数字。
- 判断符号、数字格式: 利用Scanner类的方法hasNext()判断字符是否存在和next()提取字符判断是否为运算符,若不为运算符“±*/”则抛出异常,利用StringTokenizer类的方法countTokens()判断运算数的数量是否为2个,若表达式中的运算数格式错误(多个小数点、多个运算负)择抛出异常。
- 运算:利用Scanner类的方法next()提取字符提取出运算符做相应的运算。
- 被除数不为“0”:其中再除法中利用BigDecimal类的divide方法判断被除数是否为“0”,若为“0”则抛出异常。
4.实现代码
package Three;
import java.math.BigDecimal;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Arithmetic_operation_Try_Coach {
public static void giveNumber(String string) throws ArithmeticException1 {
Scanner scanner = new Scanner(string);
scanner.useDelimiter("[1234567890.]+");// 匹配运算符号
StringTokenizer stringTokenizer = new StringTokenizer(string, "+-*/");
double Result = 0;// 结果
String symbolString;// 记忆符号
double Number1 = 0, Number2 = 0;
if (scanner.hasNext()) {// 判断是否存在符号
symbolString = scanner.next();
if (scanner.hasNext() && string.charAt(0) != '-') {// 判断是否多个符号
throw new ArithmeticException1(1);
}
if (stringTokenizer.countTokens() != 2) {// 运算数不等于两个
throw new ArithmeticException1(2);
} else {
try {
if (string.charAt(0) != '-') {
Number1 = Double.parseDouble(stringTokenizer.nextToken());
} else {
symbolString = scanner.next();// 负号时取下一个符号
Number1 = Double.parseDouble(stringTokenizer.nextToken()) * -1;
}
} catch (NumberFormatException ex) {
throw new ArithmeticException1(6);
}
Number2 = Double.parseDouble(stringTokenizer.nextToken());
}
if (symbolString.equals("+")) {
Result = Number1 + Number2;
} else if (symbolString.equals("-")) {
Result = Number1 - Number2;
} else if (symbolString.equals("*")) {
Result = Number1 * Number2;
} else if (symbolString.equals("/")) {
try {
BigDecimal a = new BigDecimal(Number1);
BigDecimal b = new BigDecimal(Number2);
System.out.println(a.divide(b));
}
catch (ArithmeticException exc) {
throw new ArithmeticException1(3);
}
}
} else {// 没有符号情况
throw new ArithmeticException1(5);
}
System.out.println("结果: " + Result);
}
}
package Three;
public class ArithmeticException1 extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
String messageString;
public ArithmeticException1(int a) {
messageString = "输入非法";
if (a == 1) {
messageString = messageString + "(多个运算符)";
}
if (a == 2) {
messageString = messageString + "(运算数不等于两个)";
}
if (a == 3) {
messageString = messageString + "(被除数等于零)";
}
if (a == 4) {
messageString = messageString + "(非基本运算符)";
}
if (a == 5) {
messageString = messageString + "(无运算符)";
}
if (a == 6) {
messageString = messageString + "(运算数格式错误)";
}
}
public String warnMess() {
return messageString;
}
}
apackage Three;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Arithmetic_operation_Try_Coach arithmetic = new Arithmetic_operation_Try_Coach();
Scanner scanner = new Scanner(System.in);
while (true) {
try {
System.out.print("请输入运算式:");
String string = scanner.next();
arithmetic.giveNumber(string);
} catch (ArithmeticException1 arithmeticException) {
// TODO: handle exception
System.out.println(arithmeticException.warnMess());
}
}
}
}