1. 程式人生 > >字首表示式轉中綴中綴表示式

字首表示式轉中綴中綴表示式

import java.util.Scanner;
import java.util.Stack;
 
public class Test {
static String e;
static Scanner scan;
static String[] sarray1;
static String a = "";
public static void main(String[] args) {
scan = new Scanner(System.in);

while(true){
System.out.println("*****************************************");
System.out.println("*************主選單******姓名*************");

System.out.println("1.輸入字首表示式");
System.out.println("2.顯示帶括號的中綴表示式");
System.out.println("3.給表變數賦值");
System.out.println("4.顯示結果");
System.out.println("*****************************************");
int shuru=scan.nextInt();
switch(shuru){
case 1:
scan = new Scanner(System.in);
System.out.println("請輸入字首表示式:例如:+ + a * b - c d / e f");

a=scan.nextLine().trim();
System.out.println("構造成功!!!");
break;
case 2:
if(a.equals("")){
System.out.println("請先輸入字首表示式!!!");
}else{
System.out.println("中綴表示式為:"+toQianZhui(a));
}
break;
case 3:
fuzhi();
break;
case 4:
jisuan();
break;
default:
System.out.println("輸入有誤!");
break;
}
}
 
}
public static String toQianZhui(String s){
e="";
Stack<String> stack = new Stack<String>();
Stack<String> stack1 = new Stack<String>();
//s="/ * + a b - c d e";
//s="+ + a * b - c d / e f";
String[] sarray = s.split(" ");
String regex = "[a-z]*";
int jilu=0;
int jiluweizi=sarray.length;
for (int i = sarray.length - 1; i >= 0; i--) {
if (!sarray[i].matches(regex)) {
stack1.push(sarray[i]);
if(jiluweizi==i+1){
jilu=1;
}
jiluweizi=i;
}
}
for (int i = sarray.length - 1; i >= 0; i--) {
try {
if (sarray[i].matches(regex)) {
stack.push(sarray[i]);

} else {

if(jilu==1){

String a = stack.pop();
if(stack.empty()){
stack.push(sarray[i]+a);
}else{
String b = stack.pop();
if(sarray[i].equals("+")||sarray[i].equals("-")){
stack.push("("+a+sarray[i]+b+")");
}else{
stack.push(a+sarray[i]+b);
}
}

}else{
String aa=stack1.pop();

String a = stack.pop();
if(stack.empty()){
e=a+e;
}else{
String b = stack.pop();
if(sarray[i].equals("+")||sarray[i].equals("-")){
e="("+a+sarray[i]+b+")"+aa+e;
}else{
e=a+sarray[i]+b+e;
}
}
}
}
} catch (Exception e) {

}
}
if(jilu==1){
for (int i = stack.size() - 1; i >= 0; i--) {
try {
if (sarray[i].matches(regex)) {
stack.push(sarray[i]);

} else {
String aa=stack1.pop();
String a = stack.pop();
if(stack.empty()){
e=a+e;
}else{
String b = stack.pop();
if(sarray[i].equals("+")||sarray[i].equals("-")){
e="("+a+sarray[i]+b+")"+aa+e;
}else{
e=a+sarray[i]+b+e;
}
}
}
} catch (Exception e) {

}
}
}
char[] cha=e.toCharArray();
if(cha.length!=0&&cha[0]=='('&&cha[cha.length-1]==')'){
cha[0]=' ';
cha[cha.length-1]=' ';
//System.out.println(String.valueOf(cha).trim());
return String.valueOf(cha).trim();
}else{
return e;
}

}

public static String[] fuzhi(){
int tag=0;
sarray1 = a.split(" ");
String regex = "[a-z]*";
for(int i=0;i<sarray1.length;i++){
if(sarray1[i].matches(regex)){
tag=1;
System.out.println("請輸入變數"+sarray1[i]+"的值:");
scan = new Scanner(System.in);
sarray1[i]=scan.next().trim();
}
}
if(tag==0){
System.out.println("沒有變數需要賦值!!!");
}
return sarray1;

}
public static void jisuan(){
Stack<Double> stack = new Stack<Double>();
String[] sarray = sarray1;
String regex = "[0-9]*";
boolean temp = false;
int fuhao = 0;
int number = 0;
for (int i = sarray.length - 1; i >= 0; i--) {
try {
if (sarray[i].matches(regex)) {
stack.push(Double.parseDouble(sarray[i]));
number++;
} else {
fuhao++;
double a = (double) stack.pop();
double b = (double) stack.pop();
double result = jisuan(a, b, sarray[i]);
stack.push(result);
}
} catch (Exception e) {
temp = true;
}
}
if (temp || !(number - 1 == fuhao) || stack.empty()) {
System.out.println("ERROR");
} else {
double ans = (double) stack.pop();
String s = "";
for(int i=0;i<sarray1.length;i++){
if(sarray1.length!=0&&i!=sarray1.length-1){
s=s+sarray1[i]+" ";
}else{
s=s+sarray1[i];
}
}
//System.out.println(s);
//System.out.println("中綴表示式"+toQianZhui1(s));
System.out.println("結果為"+ans);
}
}
public static double jisuan(double a, double b, String f) {
if(f.equals("*")){
return a * b;
}
if(f.equals("/")){
return a /b;
}
if(f.equals("+")){
return a + b;
}
if(f.equals("-")){
return a - b;
}
/*switch (f) {
case "*":
return a * b;
case "/":
return a / b;
case "+":
return a + b;
case "-":
return a - b;
}*/
//return 0;
return 0;
}
}