写了2天,终于写出了个结果。
程序如下:下面的程序中,有封装好的实现字符串转换成逆波兰式的方法。
package biaodashi;
public class nibolanshi {
int stack2length = 0;
// *****字符串与字符串数组的转换模块******
public char[] stringTransferChar(String input) {
char[] inputchar;
inputchar = input.toCharArray();
return inputchar;
}
public String[] getPoland(String input) {
Stack stack1 = new Stack();
Stack stack2 = new Stack();
char[] inputchararray = this.stringTransferChar(input);
stack1.push("#");
Boolean pre = false;
for (char chs : inputchararray) {
// ***********************************************************HERE!!!**********
if (chs >= '0' && chs <= '9') {
if (pre) {
int temp = Integer.valueOf(stack2.pop());
stack2.push(String.valueOf(temp * 10 + Integer.parseInt(chs + "")));
stack2length++;
} else {
stack2.push(String.valueOf(chs));
stack2length++;
pre = true;
}
}
switch (chs) {
case '(':
stack1.push(String.valueOf(chs));
pre = false;
break;
case ')':
while (!(stack1.peek().equals("("))) {
stack2.push(stack1.pop());
stack2length++;
}
stack1.pop();
pre = false;
break;
case '+':
case '-':
while (stack1.peek() != "#") {
if (stack1.peek().equals("(")) {
break;
} else {
stack2.push(stack1.pop());
stack2length++;
}
}
stack1.push(String.valueOf(chs));
pre = false;
break;
case '*':
case '/':
while (!(stack1.peek().equals("-")) && !(stack1.peek().equals("+")) && !(stack1.peek().equals("#"))) {
if (stack1.peek().equals("(")) {
break;
} else {
stack2.push(stack1.pop());
stack2length++;
}
}
stack1.push(String.valueOf(chs));
pre = false;
break;
}
}
while (!stack1.isEmpty() && stack1.top.data != "#") {
stack2.push(stack1.pop());
stack2length++;
}
String[] chararrfromstack2 = new String[stack2length-1];
int i = 0;
while (!stack2.isEmpty()) {
chararrfromstack2[i] = stack2.pop();
i++;
}
return chararrfromstack2;
}
public String getResUsePoland(String[] getcha){
Stack stack = new Stack();
int temx = 0;
int temd = 0;
int res = 0;
String[] datas = new String[getcha.length];
int k = getcha.length;
System.out.println("*****"+k+"****");
for(int i = 0;i<getcha.length;i++){
datas[k-1] = getcha[i];
k--;
}
System.out.println(datas[0]);
for(int i = 0;i<datas.length;i++){
if (this.isNum(datas[i])) {
stack.push(datas[i]);
}else {
temx = Integer.valueOf(stack.pop());
temd = Integer.valueOf(stack.pop());
switch (datas[i]) {
case "+":
res = temx + temd;
break;
case "-":
res = temd - temx;
break;
case "*":
res = temd*temx;
break;
case "/":
res = temd/temx;
break;
default:
break;
}
stack.push(String.valueOf(res));
}
}
return stack.pop();
}
public boolean isNum(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
public static void main(String[] args) {
nibolanshi test = new nibolanshi();
String input = "10-(7-3)*2";
String[] getcha = test.getPoland(input);
for (String ch : getcha) {
System.out.print(ch);
}
System.out.println();
String s = test.getResUsePoland(getcha);
System.out.println("RES:"+s);
}
}
class Stack {
Node top = null;
public Boolean isEmpty() {
return top == null;
}
public int getLength(){
return 0;
}
public void push(String data) {
Node node = new Node(data);
node.next = top;
top = node;
}
public String peek() {
if (top == null) {
return null;
}
return top.data;
}
public String pop() {
if (top == null) {
return null;
}
Node tmp = top;
top = top.next;
return tmp.data;
}
}
class Node {
Node next;
String data;
public Node(String data) {
this.data = data;
}
}
从这两天写这个程序上得到的小总结:
1.字符串的比较有时候用!=不好使,可以用equal方法来解决问题。
2.各种基本数据类型之间的转换要熟悉了解,不然会让你很苦恼。
3.模块化编程。
在此感谢:http://javapub.iteye.com/blog/666544
http://www.cnblogs.com/stay-foolish/archive/2012/04/25/2470590.html 这哥们思路解释的真不错。
http://blog.csdn.net/hudie1234567/article/details/5892175 借鉴了这哥们的某些细节,但这哥们的这篇博客里面的程序(正好是我借鉴的地方,即一位数到多位数的处理)貌似有问题。