import java.util.Scanner;

 public class CaculationTest {

     public static void main(String[] args) {

         Scanner reader = new Scanner(System.in);
double a, b, result = 0;
String operator;
Operation operation = null; System.out.println("************************");
System.out.println("* 学号:1308060310 *");
System.out.println("* 班级:网络131班 *");
System.out.println("* 姓名:王朝远 *");
System.out.println("************************"); TwoFromConsole twoFromConsole = new TwoFromConsole();
a = twoFromConsole.getFirstDoubleFromConsole(); // 获取第一个数
b = twoFromConsole.getTwoDoubleFromConsole(); // 获取第二个数 OperatorFromConsole operatorFromConsole = new OperatorFromConsole();
operator = operatorFromConsole.getOperator(); // 获取运算符号
do {
if (operator.equals("/") && b == 0) {
System.out.print("除法运算分母不能为0,请重新输入,");
b = twoFromConsole.getTwoDoubleFromConsole(); // 获取第二个数
continue;
}
break;
} while (true); // 获取要运算的对象
operation = Factory.getInstance(operator);
result = operation.getResult(a, b); // 判断用户是否继续对数运算,如果是继续对数运算,结果的输出方式就不一样,并且让用户选择是否再次计算
if (operator.equals("log")) { System.out.println("log" + "(" + b + ")" + a + "=" + result);
} else {
System.out.println(a + operator + b + "=" + result);
}
}
} class TwoFromConsole { Scanner reader = new Scanner(System.in); // 获取数字的方法的具体实现
public double getFirstDoubleFromConsole() { double x = 0;
System.out.print("请输入第一个数字:");
do {
double temp = 0;
try {
temp = reader.nextDouble();
} catch (Exception e) {
System.out.print("请重新输入第一个数字:");
continue;
}
x = temp;
break;
} while (true);
return x;
} public double getTwoDoubleFromConsole() { double x = 0;
System.out.print("请输入第二个数字:");
do {
double temp = 0;
try {
temp = reader.nextDouble();
} catch (Exception e) {
System.out.print("请重新输入第二个数字:");
continue;
}
x = temp;
break;
} while (true);
return x;
}
} /**
* 获取运算符类
*/
class OperatorFromConsole { Scanner reader = new Scanner(System.in); /**
* @return 合理的运算符
*/
public String getOperator() {
System.out.print("请输入运算符:"); String operator;
boolean b;
do {
operator = reader.nextLine();
b = !(operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/")
|| operator.equals("log"));
if (b == true) {
System.out.print("请重新输入运算符:");
}
} while (b); return operator;
}
} /**
* 功能:各个运算的父接口,子类必须实现父接口里面的方法
*/
interface Operation { double getResult(double x, double y);
} /**
* 实现加法运算的类
*/
class Add implements Operation { /**
* 重写接口里面的方法,并实现加法功能
*/
@Override
public double getResult(double x, double y) {
// TODO Auto-generated method stub
return x + y;
} } /**
* 实现减法运算的类
*/
class Sub implements Operation { /**
* 重写接口里面的方法,并实现减法功能
*/
@Override
public double getResult(double x, double y) {
// TODO Auto-generated method stub
return x - y;
}
} /**
* 实现乘法运算的类
*/
class Mul implements Operation { /**
* 重写接口里面的方法,并实现乘法功能
*/
@Override
public double getResult(double x, double y) {
// TODO Auto-generated method stub
return x * y;
} } /**
* 实现除法运算的类
*/
class Div implements Operation { /**
* 重写接口里面的方法,并实现除法功能
*/
@Override
public double getResult(double x, double y) {
// TODO Auto-generated method stub
return x / y;
} } /**
* 实现对数运算的类
*/
class Logarithm implements Operation { /**
* 重写接口里面的方法,并实现对数运算功能
*/
@Override
public double getResult(double x, double y) {
// TODO Auto-generated method stub
return Math.log(x) / Math.log(y); // x表示对数,y表示底数
} } /**
* 生成用户所需要的对象工厂类
*/
class Factory { /**
* @param operator
* 用户选择的运算
* @return 用户所需要的对象
*/
public static Operation getInstance(String operator) {
Operation operation = null;
switch (operator) {
case "+":
operation = new Add(); // 实例化加法对象
break;
case "-":
operation = new Sub(); // 实例化减法对象
break;
case "*":
operation = new Mul(); // 实例化乘法对象
break;
case "/":
operation = new Div(); // 实例化除法对象
break;
case "log":
operation = new Logarithm(); // 实例化对数运算对象
break;
} return operation;
}
}

最新文章

  1. Yii框架CURD方法
  2. python生成RSS(PyRSS2Gen)
  3. zepto源码--核心方法7(管理包装集)--学习笔记
  4. PyCharm 远程连接linux中Python 运行pyspark
  5. 自定义圆的半径layout
  6. iOS多线程的初步研究(二)-- 锁
  7. ajax上传文件进度条
  8. 第四章 Spring与JDBC的整合
  9. 深入了解css3新特性
  10. git使用系列(一)
  11. 1、MyEclipse插件配置以及通过MyEclipse生成表对应的JPA代码
  12. Java开源生鲜电商平台-监控模块的设计与架构(源码可下载)
  13. Design Mobile实现国际化
  14. Bound Found [POJ2566] [尺取法]
  15. Linux 系统运行命令 > 查看系统信息
  16. hbase系列
  17. python,栈的小例子
  18. Convert Binary Search Tree to Doubly Linked List
  19. bzoj 1295 最长距离 - 最短路
  20. 如何获取xml循环体报文内容

热门文章

  1. [VS]vs的宏
  2. 无刷新 checkbox列表的删除
  3. HDU 1114 Piggy-Bank (poj1384)
  4. objectARX 获取指定图层上所有实体ID
  5. Oge中Mesh的渲染流程详述
  6. python 中的json解析库
  7. C++ offsetof
  8. Office word excel电子表格在线编辑的实现方法
  9. 让NSURLConnection在子线程中运行
  10. 一个Android Socket的例子