/*
* 后缀表达式求值
*/
public string postfix() {
int a = 0 , b = 0;//栈中弹出的两数
int sum ;//求两数运算
for (int i = 0; i < calculatelength ; i++ ) {
if (i == 0) {
num.push(calculate[i]);
}else if (calculate[i].equals("+") ) {
b = integer.parseint(num.pop());
a = integer.parseint(num.pop());
sum = a + b ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("-") ) {
b = integer.parseint(num.pop());
a = integer.parseint(num.pop());
sum = a - b ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("*") ) {
b = integer.parseint(num.pop());
a = integer.parseint(num.pop());
sum = a * b ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("/") ) {
b = integer.parseint(num.pop());
a = integer.parseint(num.pop());
sum = a / b ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("%") ) {
b = integer.parseint(num.pop());
a = integer.parseint(num.pop());
sum = a / b ;
num.push(string.valueof(sum));
}else {
num.push(calculate[i]);
}
}
return num.pop();
}
}
最后附上完整代码
//注:代码中有很多输出 方便读者实时查看运算过程中 各内容
// 这些输出导致篇幅较长 大家看明白后 删去即可
public class calculate {
private stack<string> num; //后缀用栈 中转后数字栈
private stack<string> charnum;//中转后字符栈
private string []calculate;//存字符串数组
private int calculatelength;//字符串数组长度
public calculate() {
// todo auto-generated constructor stub
num = new stack<>(); //后缀用栈 中转后数字栈
charnum = new stack<>();//中转后字符栈
calculate = new string[1000];//存字符串数组
calculatelength = 0 ;//字符串数组长度
}
//转字符串数组
public void tostringarray(string input) {
boolean pointfalg = false;
char chararray[] = input.tochararray();
double number = 0;//用于导入多位数
int j = 0 ;//用于计入当前字符串数组的位数
int sizeofarray = chararray.length;
int pointbelow =1
;
for(int i = 0 ; i < sizeofarray ; i++){
if(chararray[i] == '('){
calculate[j++] = "(";
}else if(chararray[i] == ')'){
calculate[j++] = ")";
}else if (chararray[i] == '+') {
calculate[j++] = "+";
}else if (chararray[i] == '-') {
calculate[j++] = "-";
}else if (chararray[i] == '*') {
calculate[j++] = "*";
}else if (chararray[i] == '/') {
calculate[j++] = "/";
}else if (chararray[i] == '%') {
calculate[j++] = "%";
}else if (chararray[i] == '#') {
calculate[j++] = "#";
}else if (chararray[i] == '.') {
system.out.println("find new . :");
pointbelow = 1;
// sizeofarray -- ;
pointfalg = true;
}else {
string str=string.valueof(chararray[i]);
if (pointfalg == false) {
system.out.println("1:" + number);
number = number * 10 + double.parsedouble(str);
}else {
system.out.println("2:" + chararray[i]);
number = number + double.parsedouble(str) * math.pow(0.1, pointbelow);
}
system.out.println("3:" + number + "i==" + i);
if( (i + 1) == sizeofarray ||( chararray[i+1] < '0' || chararray[i+1] > '9' ) && chararray[i+1] != '.'){
if ( (i + 1) != sizeofarray && chararray[i+1] == ' ') {
i++;
}
calculate[j++] = string.valueof(number);
system.out.println("number:" + number);
number = 0 ;
pointfalg = false;
}
}
system.out.println("---z->" + calculate[i]);
}
calculatelength = j-- ;//不--会将‘#'存入
}
public void outputcalculate() {
for(int i = 0 ; i < calculatelength ; i ++ ){
system.out.println(calculate[i]);
}
}
public void calculatetozero() {
for(int i = 0 ; i < calculatelength ; i ++ ){
calculate[i]= calculate[999] ;
}
}
//中缀转后缀
public void topostfix() {
// todo auto-generated method stub
system.out.println("789");
int sum = 0 ;//用于记入”()“总个数
int j = 0 ;//用于读到”)“时循环出栈
string outstack = null;
charnum.push(null);
system.out.println(calculatelength);
for( int i = 0 ; i < calculatelength ; i ++){
system.out.println(calculate[i]);//-----------------------------
if ( calculate[i].equals("(")) {
charnum.push(calculate[i]);
system.out.println("1-1 charpush " + calculate[i]);//-----------------------------
sum ++ ;
}else if ( calculate[i].equals(")") ) {
system.out.println("2-1 charpush " + calculate[i]);//-----------------------------
outstack = charnum.pop();//进入前先出一个
system.out.println("2-1 charpush " + outstack);//-----------------------------
while ( !outstack.equals("(") ){
system.out.println("2-2 charpush " + outstack);//-----------------------------
num.push(outstack);
outstack = charnum.pop();
}//最后一次outstack正好接到”(“不入栈
system.out.println("qiangxing 1 = " + outstack );
// outstack = charnum.pop();
system.out.println("qiangxing 2 = " + outstack );
sum ++ ;
}else if (calculate[i].equals("*")) {
outstack = charnum.pop();
charnum.push(outstack);
system.out.println("3-2 charpush " + outstack);//-----------------------------
while( ( outstack == "*" || outstack == "/" || outstack == "%" ) && !(outstack == null) ){
system.out.println("3-1 charpush " + outstack);//-----------------------------
num.push(outstack);
charnum.pop();//由于前第三行又将outstack存入栈中,座椅此处再次弹出
outstack = charnum.pop();
charnum.push(outstack);
}
system.out.println("3-3 charpush " + outstack);//-----------------------------
charnum.push("*");
}else if (calculate[i].equals("%")) {
outstack = charnum.pop();
charnum.push(outstack);
system.out.println("3-2 charpush " + outstack);//-----------------------------
while( ( outstack == "*" || outstack == "/" || outstack == "%" ) && !(outstack == null) ){
system.out.println("3-1 charpush " + outstack);//-----------------------------
num.push(outstack);
charnum.pop();//由于前第三行又将outstack存入栈中,座椅此处再次弹出
outstack = charnum.pop();
charnum.push(outstack);
}
system.out.println("3-3 charpush " + outstack);//-----------------------------
charnum.push("%");
}else if (calculate[i].equals("/")) {
system.out.println("5-1-0 charpush " + "1-1-1-1-1-1-1-1");//-----------------------------
outstack = charnum.pop();
system.out.println("5-1-1 charpush " + "2-2-2-2-2-2-22-2");//-----------------------------
charnum.push(outstack);
system.out.println("4-1 charpush " + outstack);//-----------------------------
while( ( outstack == "*" || outstack == "/" || outstack == "%") && !(outstack == null) ){
system.out.println("4-2 charpush " + outstack);//-----------------------------
num.push(outstack);
charnum.pop();//由于前第三行又将outstack存入栈中,座椅此处再次弹出
outstack = charnum.pop();
charnum.push(outstack);
}
system.out.println("4-3 charpush " + outstack);//-----------------------------
system.out.println("5-1-2 charpush " + outstack);//-----------------------------
charnum.push("/");
}else if (calculate[i].equals("+")) {
outstack = charnum.pop();
charnum.push(outstack);
system.out.println("5-1 charpush " + outstack);//-----------------------------
while( !(outstack=="(") && !(outstack == null) ){
system.out.println("5-2 charpush " + outstack);//-----------------------------
num.push(outstack);
charnum.pop();
outstack = charnum.pop();
charnum.push(outstack);
}
system.out.println("5-3 charpush " + outstack);//-----------------------------
charnum.push("+");
}else if (calculate[i].equals("-")) {
outstack = charnum.pop();
charnum.push(outstack);
system.out.println("6-1 charpush " + outstack);//-----------------------------
while( !(outstack=="(") && !(outstack == null) ){
system.out.println("6-2 charpush " + outstack);//-----------------------------
num.push(outstack);
charnum.pop();
outstack = charnum.pop();
charnum.push(outstack);
}
system.out.println("6-3 charpush " + outstack);//-----------------------------
charnum.push("-");
}else {
system.out.println("7-7 " + calculate[i]);
num.push(calculate[i]);
}
}
system.out.println("匹配结束" + outstack);
outstack = charnum.pop();
system.out.println("finish 1 == " + outstack);
while ( outstack != null ) {
num.push(outstack);
outstack = charnum.pop();
system.out.println("finish 2 == " + outstack);
}
calculatelength = calculatelength - sum ;
system.out.println( "0.0.0.0 charpush " );//-----------------------------
system.out.println("sum = " + sum + " calculate = " +
calculatelength + "calculatelength-sum = " + (calculatelength-sum));
system.out.println("over ~~~~~0 ");
stack<string> zanshi = new stack<>();
// num.pop();
for(int i = 0 ; i < calculatelength ; i ++ ){
zanshi.push(num.pop());
// system.out.println(num.pop());
}
calculatetozero();
system.out.println("over ~~~~~1 ");
for(int i = 0 ; i < calculatelength ;i ++ ){
calculate[i] = zanshi.pop();
}
system.out.println("over ~~~~~2 ");
for(int i = 0 ; i < calculatelength ;i ++ ){
system.out.println(calculate[i]);
}
system.out.println("over ~~~~~3 ");
// num.push("#");
}
//后缀计算
public string postfix() {
bigdecimal a , b ;//栈中弹出的两数
bigdecimal sum ;//求两数运算
for (int i = 0; i < calculatelength ; i++ ) {
// system.out.println("目前符号:" + calculate[i]);
if (i == 0) {
num.push(calculate[i]);
}else if (calculate[i].equals("+") ) {
b = new bigdecimal(num.pop());
a = new bigdecimal(num.pop());
sum = a.add(b) ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("-") ) {
b = new bigdecimal(num.pop());
a = new bigdecimal(num.pop());
sum = a.subtract(b) ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("*") ) {
b = new bigdecimal(num.pop());
a = new bigdecimal(num.pop());
sum = a.multiply(b) ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("/") ) {
b = new bigdecimal(num.pop());
a = new bigdecimal(num.pop());
sum = a.divide(b,10,roundingmode.half_up) ;
num.push(string.valueof(sum));
}else if (calculate[i].equals("%") ) {
b = new bigdecimal(num.pop());
a = new bigdecimal(num.pop());
sum = a.divideandremainder(b)[1] ;
num.push(string.valueof(sum));
}else {
num.push(calculate[i]);
}
}
return num.pop();
}
}
结果如下: 一、前缀转后缀并输出
其中over前为转化后的后缀表达式
over后为计算结果
public class main {
public static void main(string[] args) {
calculate text = new calculate();
text.tostringarray("1+2*(3-1+2)-3");
text.outputcalculate();
text.topostfix();
system.out.println(text.postfix());
}
}
public class main {
public static void main(string[] args) {
calculate text = new calculate();
text.tostringarray("1 2 3 1-2+*+3-");
text.outputcalculate();
system.out.println(text.postfix());
}
}