壁画不多说直接上代码
import java.util.Scanner;
import java.util.Stack;
public class Calculate {
public static void main(String[]args)
{
System.out.println("请输入一个表达式,并且以#结束");
Scanner scanner=new Scanner(System.in);
String expression=scanner.next();
char[] chars=expression.toCharArray();
Calculate c1=new Calculate(chars,expression);
}
public Stack<Character> OPVAL;
public Calculate(char[] chars,String expression)
{
chars=expression.toCharArray();
OPVAL=new Stack();
int i=1;
int n=0;
char b;
char c = chars[0];
JUDGE.PanduanChuhao(expression);//除号不能为0
/**if (expression.contains("/0"))
{
int j=expression.indexOf("/0");
String str=expression.substring(expression.indexOf("/0")+2);
System.out.println("第"+);
}*/
OPVAL.push('#');//压入栈底
while (c!='#'||OPVAL.peek()!='#')
{
if(i==1&&expression.startsWith("("))
{
OPVAL.push(c);
//System.out.println("0"+OPVAL.peek());
c=chars[++n];
++i;
while (JUDGE.JudgeOpvalOrNot(c))
{
System.out.println("第 "+i+" 位 "+c+" 输入错误。");
try{c=chars[++n];
++i;}
catch (Exception e)
{
System.exit(0);
}
}
}
else if(i==1&&JUDGE.JudageOpter(c)!=7&&!expression.startsWith("("))
{
System.out.println("第1位 "+c+" 输入有误。");
c=chars[++n];
++i;
while (JUDGE.JudgeOpvalOrNot(c))
{
System.out.println("第 "+i+" 位 "+c+" 输入错误。");
try
{c=chars[++n];
++i;}
catch (Exception e)
{
System.exit(0);
}
}
}
if(c>='0'&&c<='9')
{
while (c>='0'&&c<='9')
{
c=chars[++n];
++i;
}
}
else if(JUDGE.JudageOpter(c)!=7) {
if (c == '(') {
System.out.println("第 " + i + " 位输入错误。数字后面不能直接加(。");
c = chars[++n];
++i;
while (JUDGE.JudgeOpvalOrNot(c)) {
System.out.println("第 " + i + " 位 " + c + " 输入错误");
try {
c = chars[++n];
++i;}
catch (Exception e)
{
System.exit(0);
}
}
break;
}
b = JUDGE.CompareOpter(OPVAL.peek(), c);
//System.out.println(b);
switch (b) {
case '>':
OPVAL.pop();
//System.out.println("1"+OPVAL.peek());
break;
case '<':
OPVAL.push(c);
//System.out.println("2"+OPVAL.peek());
c = chars[++n];
++i;
if (c == ')') {
System.out.println("第 " + i + " 位输入错误。运算符不能加)");
c = chars[++n];
++i;
}
if (c == '(') {
OPVAL.push(c);
//System.out.println("3"+OPVAL.peek());
c = chars[++n];
++i;
}
while (JUDGE.JudgeOpvalOrNot(c)) {
System.out.println("第 " + i + " 位 " + c + " 输入错误。");
try
{c = chars[++n];
++i;}
catch (Exception e)
{
System.exit(0);
}
}
break;
case '=':
OPVAL.pop();
//System.out.println("4"+OPVAL.peek());
c = chars[++n];
++i;
break;
case 'F':
System.out.println("第 " + i + " 位" + c + "输 入 错误");
c = chars[++n];
++i;
break;
case 'L':
OPVAL.pop();
//System.out.println("5"+OPVAL.peek());
System.out.println("第 " + i + " 位缺少 ) ");
break;
}
}
else
{
System.out.println("第"+i+"位,"+c+"是 非 法 字 符 !!");
c=chars[++n];
++i;
}
}
}
}
class JUDGE {
/**
* static boolean JudgeLetterOrNot(char c)
* {
* <p>
* }
*/
static int anInt=0;
static void PanduanChuhao(String expression)
{
if (!expression.contains("/0")) return;
String newstring=expression.substring(expression.indexOf("/0")+2);
//System.out.println(expression.indexOf("/0"));
anInt=anInt+expression.indexOf("/0")+2;
System.out.println("第 "+anInt+" 除 号 不 能 为 0 !!!");
PanduanChuhao(newstring);
}
static boolean JudgeOpvalOrNot(char c) {
if (JudageOpter(c) != 7) {
return true;//是运算符
}
return false;//不是运算符
}
static int JudageOpter(char c) {//判断是不是运算符,并确定是哪个运算符。
int i = 7;
switch (c) {
case '+':
i = 0;
break;
case '-':
i = 1;
break;
case '*':
i = 2;
break;
case '/':
i = 3;
break;
case '(':
i = 4;
break;
case ')':
i = 5;
break;
case '#':
i = 6;
break;
default:
i = 7;
}
return i;
}
static char CompareOpter(char a, char b) {//辅助二维数列
int n, m;
n = JudageOpter(a);
//System.out.println(n);
m = JudageOpter(b);
//System.out.println(m);
char priority[][] = //算符间的优先级关系
{
{'>', '>', '<', '<', '<', '>', '>'},
{'>', '>', '<', '<', '<', '>', '>'},
{'>', '>', '>', '>', '<', '>', '>'},
{'>', '>', '>', '>', '<', '>', '>'},
{'<', '<', '<', '<', '<', '=', 'L'},
{'>', '>', '>', '>', 'l', '>', '>'},
{'<', '<', '<', '<', '<', 'F', '='},
};
return priority[n][m];
}
}