首页 > 其他 > 详细

计算器

时间:2015-10-29 09:30:12      阅读:138      评论:0      收藏:0      [点我收藏+]


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;
}
}

计算器

原文:http://www.cnblogs.com/wangchaoyuan/p/4919371.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!