java计算利息_求JAVA编写的银行利息计算器代码?

import java.util.ListIterator;

import java.util.Stack;

public class CalStr {

private String src;

/**

* constructor

*

* @param srcthe string(expression) to calculate

*/

public CalStr(String src) {

this.src = src;

}

/**

* calculate to get the result

*

* @return(double)result

*/

public double getResult() {

String postfix = getPostfix();

Stack stk = new Stack();

// System.out.println(postfix);

String parts[] = postfix.split(" +");

double result = 0;

for (int i = 0; i < parts.length; i++) {

char tmp = parts[i].charAt(0);

if (!isOperator(tmp)) {

stk.push(parts[i]);

} else {

double a = Double.parseDouble((String) stk.pop());

double b = Double.parseDouble((String) stk.pop());

// b is followed by a in the orignal expression

result = calculate(b, a, tmp);

stk.push(String.valueOf(result));

}

}

return result;

}

/**

* test if the character is an operator,such +,-,*,/

*

* @param opthe character to test

* @returntrue if op is an operator otherwise false

*/

private boolean isOperator(char op) {

return (op == '+' || op == '-' || op == '*' || op == '/');

}

/**

* calculate an expression such (a op b)

*

* @param anumber 1

* @param bnumber 2

* @param opthe operator

* @return(double)(a op b)

*/

public double calculate(double a, double b, char op) {

switch (op) {

case '+':

return a + b;

case '-':

return a - b;

case '*':

return a * b;

case '/':

return a / b;

}

return -1;

}

/**

* convert the suffix to postfix

*

* @returnthe postfix as a string

*/

private String getPostfix() {

Stack stk = new Stack();

String postfix = new String();

char op;

int i = 0;

while (i < src.length()) {

if (Character.isDigit(src.charAt(i)) || src.charAt(i) == '.') {

postfix += " ";

do {

postfix += src.charAt(i++);

} while ((i < src.length())

&& (Character.isDigit(src.charAt(i))));

postfix += " ";

}

else {

switch (op = src.charAt(i++)) {

case '(':

stk.push("(");

break;

case ')':

while (stk.peek() != "(") {

String tmp = (String) stk.pop();

postfix += tmp;

if (tmp.length() == 1 && isOperator(tmp.charAt(0)))

postfix += " ";

}

stk.pop();

postfix += " ";

break;

case '+':

case '-':

while ((!stk.empty()) && (stk.peek() != "(")) {

postfix += stk.pop() + " ";

}

stk.push(String.valueOf(new Character(op)));

break;

case '*':

case '/':

while ((!stk.empty())

&& ((stk.peek() == "*") || (stk.peek() == "/"))) {

postfix += stk.pop() + " ";

}

stk.push(String.valueOf(new Character(op)));

break;

}

}

}

ListIterator it = stk.listIterator(stk.size());

while (it.hasPrevious())

postfix += it.previous() + " ";

return postfix.trim().replaceAll(" +\\.", ".");

}

/**

* main function

*

* @param args

*/

public static void main(String args[]) {

System.out.println(new CalStr("((1.5+6.000)*9+9.36)*(8+9-8*8+8*7)")

.getResult());

}

}

new CalStr( 写上你的计算公式 );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值