输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
2015-03-28 01:56
633 查看
输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。
例如: 输入:3+8×2/9-2
输出:2
菜鸟初试,还请大神指教。
import java.util.Stack;
public class Calequ {
public int getRet(String str){
char[] arr = str.toCharArray();
Stack<Character> stack1 = new Stack<Character>();
//存储数字
Stack<Character> stack2 = new Stack<Character>();
//存储+-符号
int temp = 0,temp1,temp2;
char tempstr;
for(int i=0;i<arr.length;i++){
switch(arr[i]){
case '×':
temp1 = (int)(stack1.pop())-48;
//ASCII转换
temp = temp1*((int)(arr[i+1])-48);
stack1.push((char)(temp+48));
i++;
break;
case '/':
temp1 = (int)(stack1.pop())-48;
temp = temp1/((int)(arr[i+1])-48);
stack1.push((char)(temp+48));
i++;
break;
case '+':
if(stack2.size()!=0){
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
tempstr = stack2.pop();
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
stack1.push((char)(temp+48));
}
stack2.push(arr[i]);
break;
case '-':
if(stack2.size()!=0){
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
tempstr = stack2.pop();
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
stack1.push((char)(temp+48));
}
stack2.push(arr[i]);
break;
default:
stack1.push(arr[i]);
}
}
tempstr = stack2.pop();
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
return temp;
}
public static void main(String[] args){
String str = "3+8×2/9-2";
Calequ cal = new Calequ();
int result = cal.getRet(str);
System.out.println(result);
}
}
例如: 输入:3+8×2/9-2
输出:2
菜鸟初试,还请大神指教。
import java.util.Stack;
public class Calequ {
public int getRet(String str){
char[] arr = str.toCharArray();
Stack<Character> stack1 = new Stack<Character>();
//存储数字
Stack<Character> stack2 = new Stack<Character>();
//存储+-符号
int temp = 0,temp1,temp2;
char tempstr;
for(int i=0;i<arr.length;i++){
switch(arr[i]){
case '×':
temp1 = (int)(stack1.pop())-48;
//ASCII转换
temp = temp1*((int)(arr[i+1])-48);
stack1.push((char)(temp+48));
i++;
break;
case '/':
temp1 = (int)(stack1.pop())-48;
temp = temp1/((int)(arr[i+1])-48);
stack1.push((char)(temp+48));
i++;
break;
case '+':
if(stack2.size()!=0){
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
tempstr = stack2.pop();
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
stack1.push((char)(temp+48));
}
stack2.push(arr[i]);
break;
case '-':
if(stack2.size()!=0){
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
tempstr = stack2.pop();
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
stack1.push((char)(temp+48));
}
stack2.push(arr[i]);
break;
default:
stack1.push(arr[i]);
}
}
tempstr = stack2.pop();
temp2 = (int)(stack1.pop())-48;
temp1 = (int)(stack1.pop())-48;
if(tempstr=='+'){
temp = temp1+temp2;
}else{
temp = temp1-temp2;
}
return temp;
}
public static void main(String[] args){
String str = "3+8×2/9-2";
Calequ cal = new Calequ();
int result = cal.getRet(str);
System.out.println(result);
}
}
相关文章推荐
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34–5 + 67–8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性。 例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 用java编写在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100
- 项目39.3-1输入一个正整数,输出它的反序数(反序数,即将其所有位的数字反过来。例如,123是321的反序数)
- 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。例如输入数组{32, 321},则输出这两个能排成的最小数字32132
- 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。 输出格式:在一行内输出n的各位数字之和
- 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。
- 从键盘上输入一个后缀表达式,试编写算法计算表达式的值。规定:逆波兰表达式的长度不超过一行,以$符作为输入结束,操作数之间用空格分隔,操作符只可能有+、-、*、/四种运算。例如:234 34+2*$。
- 输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba"
- 从键盘上输入一个后缀表达式,试编写算法计算表达式的值。规定:逆波兰表达式的长度不超过一行,以$符作为输入结束,操作数之间用空格分隔,操作符只可能有+、-、*、/四种运算。例如:234 34+2*$。
- 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列(可以有前导0),2<=n<=79