package org.crazyit.cal;

import java.math.BigDecimal;

/**
* 计算业务类
*
* @author yangenxiong yangenxiong2009@gmail.com
* @author Kelvin Mak kelvin.mak125@gmail.com
* @version 1.0
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br>Copyright (C), 2009-2010, yangenxiong
* <br>This program is protected by copyright laws.
*/
public class CalService {
// 存储器,默认为0,用于保存需要暂时保存的计算结果
private double store = 0;
// 第一个操作数
private String firstNum = null;
// 上次操作
private String lastOp = null;
// 第二个操作数
private String secondNum = null;
// 是否第二个操作数,如果是,点击数字键时,则在文本框中重新输入
private boolean isSecondNum = false; // 数字
private String numString = "0123456789.";
// 四则运算
private String opString = "+-*/"; /**
* 默认构造器
*/
public CalService() {
super();
} /**
* 调用方法并返回计算结果
*
* @return String
*/
public String callMethod(String cmd, String text) throws Exception {
if (cmd.equals("C")) {
return clearAll();
} else if (cmd.equals("CE")) {
return clear(text);
} else if (cmd.equals("Back")) {
return backSpace(text);
} else if (numString.indexOf(cmd) != -1) {
return catNum(cmd, text);
} else if (opString.indexOf(cmd) != -1) {
return setOp(cmd, text);
} else if (cmd.equals("=")) {
return cal(text, false);
} else if (cmd.equals("+/-")) {
return setNegative(text);
} else if (cmd.equals("1/x")) {
return setReciprocal(text);
} else if (cmd.equals("sqrt")) {
return sqrt(text);
} else if (cmd.equals("%")) {
return cal(text, true);
} else {
return mCmd(cmd, text);
}
} /**
* 计算四则运算结果
*
* @param text
* String 输入框中的值
* @param isPercent
* boolean 是否有"%"运算
* @return String 封闭成字符串的计算结果
*/
public String cal(String text, boolean isPercent) throws Exception {
// 初始化第二个操作数
double secondResult = secondNum == null ? Double.valueOf(text)
.doubleValue() : Double.valueOf(secondNum).doubleValue();
// 如果除数为0,不处理
if (secondResult == 0 && this.lastOp.equals("/")) {
return "0";
}
// 如果有"%"操作,则第二个操作数等于两数相乘再除以100
if (isPercent) {
secondResult = MyMath.multiply(Double.valueOf(firstNum), MyMath
.divide(secondResult, 100));
}
// 四则运算,返回结果赋给第一个操作数
if (this.lastOp.equals("+")) {
firstNum = String.valueOf(MyMath.add(Double.valueOf(firstNum),
secondResult));
} else if (this.lastOp.equals("-")) {
firstNum = String.valueOf(MyMath.subtract(Double.valueOf(firstNum),
secondResult));
} else if (this.lastOp.equals("*")) {
firstNum = String.valueOf(MyMath.multiply(Double.valueOf(firstNum),
secondResult));
} else if (this.lastOp.equals("/")) {
firstNum = String.valueOf(MyMath.divide(Double.valueOf(firstNum),
secondResult));
}
// 给第二个操作数重新赋值
secondNum = secondNum == null ? text : secondNum;
// 把isSecondNum标志为true
this.isSecondNum = true;
return firstNum;
} /**
* 计算倒数
*
* @param text
* String 输入框中的值
* @return String 封闭成字符串的结果
*/
public String setReciprocal(String text) {
// 如果text为0,则不求倒数
if (text.equals("0")) {
return text;
} else {
// 将isSecondNum标志为true
this.isSecondNum = true;
// 计算结果并返回
return String.valueOf(MyMath.divide(1, Double.valueOf(text)));
}
} /**
* 计算开方
*
* @param text
* String 输入框中的值
* @return String 封闭成字符串的结果
*/
public String sqrt(String text) {
// 将isSecondNum标志为true
this.isSecondNum = true;
// 计算结果并返回
return String.valueOf(Math.sqrt(Double.valueOf(text)));
} /**
* 设置操作符号
*
* @param cmd
* String 操作符号
* @param text
* String 输入框中的值
* @return String 封闭成字符串的结果
*/
public String setOp(String cmd, String text) {
// 将此操作符号设置为上次的操作
this.lastOp = cmd;
// 设置第一个操作数的值
this.firstNum = text;
// 将第二个操作数赋值为空
this.secondNum = null;
// 将isSecondNum标志为true
this.isSecondNum = true;
// 返回空值
return null;
} /**
* 设置正负数
*
* @param text
* String 输入框中的值
* @return String 封闭成字符串的结果
*/
public String setNegative(String text) {
// 如果text是负数,就将它变为正数
if (text.indexOf("-") == 0) {
return text.substring(1, text.length());
}
// 否则,将正数变成负数
return text.equals("0") ? text : "-" + text;
} /**
* 连接输入的数字,每次点击数字 把新加的数字追加到后面
*
* @param cmd
* String 操作符号
* @param text
* String 输入框中的值
* @return String 封闭成字符串的结果
*/
public String catNum(String cmd, String text) {
String result = cmd;
// 如果目前的text不等于0
if (!text.equals("0")) {
if (isSecondNum) {
// 将isSecondNum标志为false
isSecondNum = false;
} else {
// 刚返回结果为目前的text加上新点击的数字
result = text + cmd;
}
}
// 如果有.开头,刚在前面补0
if (result.indexOf(".") == 0) {
result = "0" + result;
}
return result;
} /**
* 实现backspace功能
*
* @param text
* String 现在文体框的结果
* @return String
*/
public String backSpace(String text) {
return text.equals("0") || text.equals("") ? "0" : text.substring(0,
text.length() - 1);
} /**
* 实现存储操作命令
*
* @param cmd
* String 操作符号
* @param text
* String 现在文体框的结果
* @return String
*/
public String mCmd(String cmd, String text) {
if (cmd.equals("M+")) {
// 如果是"M+"操作,刚把计算结果累积到store中
store = MyMath.add(store, Double.valueOf(text));
} else if (cmd.equals("MC")) {
// 如果是"MC"操作,则清除store
store = 0;
} else if (cmd.equals("MR")) {
// 如果是"MR"操作,则把store的值读出来
isSecondNum = true;
return String.valueOf(store);
} else if (cmd.equals("MS")) {
// 如果是"MS"操作,则把计算结果保存到store
store = Double.valueOf(text).doubleValue();
}
return null;
} /**
* 清除所有计算结果
*
* @return String
*/
public String clearAll() {
// 将第一第二操作数恢复为默认值
this.firstNum = "0";
this.secondNum = null;
return this.firstNum;
} /**
* 清除上次计算结果
*
* @param text
* String 现在文体框的结果
* @return String
*/
public String clear(String text) {
return "0";
} /**
* 返回存储器中的结果
*
* @return double
*/
public double getStore() {
return this.store;
} }

  

CalService的更多相关文章

  1. GWT RPC

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  2. Android(java)学习笔记165:Android的Junit调试

    编写android应用的时候,往往我们需要编写一些业务逻辑实现类,但是我们可能不能明确这个业务逻辑是否可以成功实现,特别是逻辑代码体十分巨大的时候,我们不可能一行一行检查自己的代码,为了解决这样的问题 ...

  3. GWT RPC机制

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  4. Android_Service组件详解

    1.Service概述 Service服务是一个没有用户界面的在后台运行执行操作的应用组件,其它组件可以通过Intent意图启动这个Service去完成特定的功能,比如通过Service可以完成播放音 ...

  5. Asp.Net MVC 3

    Asp.Net MVC 3   wcf基础教程之 契约(合同)Contract 摘要: 在前几篇博客中我有说到服务的寄宿,就是服务要运行起来必须采取的几种方式,相当于我们可以照葫芦画瓜的效果运行一个w ...

  6. Hessian 使用例子

    一.协议包(数据对象需要实现序列化接口,可以用于服务端接口.客户端调用服务之用) /** * */ package com.junge.demo.protocol.model; import java ...

  7. WCF宿主Window Service Demo

    尝试了下将服务寄宿在window 服务上.具体步骤如下 整个解决方案截图 一.创建window 服务 Wcf.WinService namespace Wcf.WinService { public ...

  8. android开发系列之aidl

    aidl在android开发中的主要作用就是跨进程通讯来着,说到进程相比很多人都是非常熟悉了,但是为什么会有跨进程通讯这个概念呢?原来在android系统中,有这么一套安全机制,为了各个Apk数据的独 ...

  9. Web Service 之JAX-WS 与CXF实现

    Web Service的实现方式有很多种,本篇介绍的是基于JAX-WS(纯Java)实现的,然后借由CXF工具生成Javabean. 第一步:创建一个Java工程,编写CalService接口,此接口 ...

随机推荐

  1. VB程序逆向反汇编常见的函数(修改版)

    VB程序逆向常用的函数 1) 数据类型转换: a) __vbaI2Str    将一个字符串转为8 位(1个字节)的数值形式(范围在 0 至 255 之间) 或2 个字节的数值形式(范围在 -32,7 ...

  2. 【c++】面向对象程序设计之继承中的类作用域

    当存在继承关系时,派生类的作用域嵌套在其基类的作用域之内. 一个对象.引用或指针的静态类型决定了该对象的哪些成员是可见的.即使静态类型与动态类型可能不一致,但我们使用哪些成员仍然是由静态类型决定的.基 ...

  3. Win7 Windows Update更新的文件默认在哪个位置

    C:\Windows\SoftwareDistribution\download    

  4. JavaSE----API之集合(Collection、List及其子类、Set及其子类、JDK1.5新特性)

    5.集合类 集合类的由来: 对象用于封装特有数据,对象多了须要存储:假设对象的个数不确定.就使用集合容器进行存储. 集合容器由于内部的数据结构不同,有多种详细容器.不断的向上抽取,就形成了集合框架. ...

  5. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  6. 链接脚本在编程中的高级运用之二——执行时库和C++特性支持

    我们在链接脚本在编程中的高级运用之中的一个可变长数组中已经讲述了编译链接的原理,并且以uboot命令为例具体介绍链接脚本怎样实现可变长数组. 本章在前者的基础上继续讲述链接脚本在执行时库中的高级应用技 ...

  7. android真机调试 INSTALL_FAILED_MEDIA_UNAVAILABLE 问题解决方案

    前提是手机用数据线连到电脑,安装好手机对应的驱动. 1:打开cmd 2:cd切换到sdk安装目录的platform-tools目录,比如我安装到了D盘根目录,则输入: cd d:\android-sd ...

  8. ExtJs中多个form情况下指定某个form使能

    采用extjs的时候,如果一个页面存在多个,那么提交之时,究竟是哪个form使能,就要指明.我今天就遇到了这种情况:明明页面已经有提交,为啥没有提交到内容?一查才知道,我的页面是有2个form,我本意 ...

  9. 2016/05/16 UEditor 文本编辑器 使用教程与使用方法

    第一:百度UEditor编辑器的官方下载地址 ueditor 官方地址:http://ueditor.baidu.com/website/index.html 开发文档地址:http://uedito ...

  10. linux 监控进程所消耗的资源(内存),达到阈值(绝对值、相对值)后,将其杀死

    监控某个python进程是否存在,如不存在则启动 #!/bin/bashwhile [ 1 ]do #打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v " ...