两年没做开发了,java也忘得差不多了,刚才想做个小东西,遇到困难了!看如下代码段:int a = 1;
ing b = 2;
int c = 3;String d = "+";
String e = "*";我想要的是运算公式:1+2*3=?
因为运算符是由用户输入的,而且运算的个数也不一定,可能还会出现减法和除法,请问怎么实现!
ing b = 2;
int c = 3;String d = "+";
String e = "*";我想要的是运算公式:1+2*3=?
因为运算符是由用户输入的,而且运算的个数也不一定,可能还会出现减法和除法,请问怎么实现!
解决方案 »
- 在校学习java的,现在实习在一家软件公司做的android小程序,如何在空间时间学习java web
- 请各位大侠帮帮忙,马上就要交了~万分感谢
- 有两个不同时区的Calendar对象,如何计算它们之间的时间差。
- jdk1_5_0_08 与 jdk1_5_0_15有区别吗
- 如何用JAVA串口编程来控制一个GSM模块,从而实现收发短信功能??
- 免费的java聊天室,大家提提意见。谢谢
- 怎么设置菜单条的背景颜色呀?和窗口大小的控制问题!(在线等)一句代码10分
- jb7中如何单独编译一个java,就象编译单独一个helloworld的文件成class?
- 大家好我是刚刚学电脑语言 给点提示吗 c++/c/c#/java 啊 谢谢
- 关于编写frame的问题!!!!
- 一道计算机组成原理的基础题,大家帮我分析一下啊
- JDB_ODBC连接SQL的问题,——————在线等回答
import java.util.*;public class CalStr {
private String src;
/**
* constructor
* @param src the 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<String> stk = new Stack<String>();
//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(stk.pop());
double b = Double.parseDouble(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 op the character to test
* @return true 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 a number 1
* @param b number 2
* @param op the 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
* @return the postfix as a string
*/
private String getPostfix() {
Stack<String> stk = new Stack<String>();
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 = 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(new Character(op).toString());
break;
case '*':
case '/':
while ((!stk.empty())
&& ((stk.peek() == "*") || (stk.peek() == "/"))) {
postfix += stk.pop()+" ";
}
stk.push(new Character(op).toString());
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());
}
}
这个是jdk1.5泛型的写法
如果你是1.5以前的,改成Stack stk = new Stack();
后面get的时候强制转换为String
package test;
import java.io.*;
import java.util.*;
import java.sql.*;
public class testBean
{
public int a = 1;
public int b = 2;
public int c = 3; public String d = "+";
public String e = "*"; public String getResult()
{
String sTemp = "";
sTemp = a+d+b+e+c;
String str1 = sTemp.substring(0,sTemp.indexOf("+"));
String str2 = sTemp.substring((sTemp.indexOf("+")+1),sTemp.indexOf("*"));
String str3 = sTemp.substring((sTemp.indexOf("*")+1));
int nResult = ((Integer.parseInt(str1))+(Integer.parseInt(str2))*(Integer.parseInt(str3)));
String sResult = String.valueOf(nResult);
return sResult;
}
}
class testBeanMain extends testBean
{
public static void main(String [] args)
{
testBean tb = new testBean();
System.out.println("The Result is : "+tb.getResult());
}
}
{
char str;
int b1,b2;
public int setstr(int b1,char str,int b2)
{
this.str=str;
this.b1=b1;
this.b2=b2;
if(str=='+')
{
return b1+b2;
}
if(str=='-')
{
return b1-b2;
}
if(str=='*')
{
return b1*b2;
}
if(str=='/')
{
return b1/b2;
}
return 0;
}public static void main(String[] args)
{
a a1=new a();
int b1=3,b2=4;
System.out.println(a1.setstr(b1,'*',b2));
}
}
在写一个表单,就可以,加上BEAN 用JSP完成任务.
免费招聘、求职。IT人工作的天堂。希望能够为大家所用。
http://www.javaresearch.org/article/showarticle.jsp?column=31&thread=26059