标签:参考 size 需求分析 tip import 分割 出错 opera 主函数
多语言支持:简体中文
, 繁體中文
, English
将带括号的四则运算式的字符串转化为逆波兰式,具体思路在[2016-2017-2 《Java 程序设计》课堂实践项目]中有明确的说明:
1.设立一个栈,存放运算符,首先栈为空;
2.从左到右扫描中缀式,若遇到操作数,直接输出,并输出一个空格作为两个操作数的分隔符;
3.若遇到运算符,则与栈顶比较,比栈顶级别高则进栈,否则退出栈顶元素并输出,然后输出一个空格作分隔符;
4.若遇到左括号,进栈;若遇到右括号,则一直退栈输出,直到退到左括号止。
5.当栈变成空时,输出的结果即为后缀表达式。
根据上述思路,完成代码ChangeExpress.java
,将前缀式改为后缀式,并且完成分析括号匹配的功能,若左右括号不匹配,输出错误并退出程序运行:
import java.util.*;
public class ChangeExpress{
String originalExpression;
String changedExpression= "";
int countLeft=0,countRight=0;
public void setOriginalExpression(String str){
originalExpression=str;
}
public void changedWay(){
Stack stackChange=new Stack();//创立栈
int opValue []=new int[100];
for (int i=0;i<originalExpression.length() ;i++) {
char chi=originalExpression.charAt(i);
if (chi>=‘0‘&&chi<=‘9‘){
changedExpression=changedExpression+chi;
}
else if (chi==‘+‘||chi==‘-‘||chi==‘*‘||chi==‘÷‘||chi==‘/‘) {
changedExpression=changedExpression+" ";//有运算符,数字之间就要有空格,否则是一个整体
if (stackChange.empty()){//栈为空直接压栈
stackChange.push(chi);
}
else if (judgeValue(chi)>=judgeValue((char)stackChange.peek())) {//运算级别高或者相等压入栈
stackChange.push(chi);
}
else{
changedExpression=changedExpression+ String.valueOf(stackChange.pop())+" ";//否则直接进入字符串,空格分割运算符
i--;
}
}
else if(chi==‘(‘){
countLeft++;
stackChange.push(chi);//左括号压栈
}
else if(chi==‘)‘){
changedExpression+=" ";
countRight++;
while((char)stackChange.peek()!=‘(‘){//直到(为止
changedExpression=changedExpression+ String.valueOf(stackChange.pop())+" ";//弹出栈内东西,空格分割
}
stackChange.pop();
}
}
changedExpression+=" ";
while(!stackChange.empty()){
changedExpression=changedExpression+String.valueOf(stackChange.pop())+" ";
}
/*int size = stackChange.size();
for (int i=0;i<size;i++){
changedExpression=changedExpression+String.valueOf(stackChange.pop())+" ";
}*/
if (countLeft!=countRight) {
System.out.println("括号不匹配");
System.exit(0);
}
}
public int judgeValue(char c){
int value=0;
switch(c){
case ‘(‘:
value=1;
break;
case ‘+‘:
case ‘-‘:
value=2;
break;
case ‘*‘:
case ‘÷‘:
value=3;
break;
case ‘/‘:
value=4;
break;
case ‘)‘:
value=5;
default:
value=0;
}
return value;
}
}
import java.util.StringTokenizer;
import java.util.Stack;
public class MyDcRational
{
/** constant for addition symbol */
private final char ADD = ‘+‘;
/** constant for subtraction symbol */
private final char SUBTRACT = ‘-‘;
/** constant for multiplication symbol */
private final char MULTIPLY = ‘*‘;
/** constant for division symbol */
private final char DIVIDE1 = ‘÷‘;
private final char DIVIDE2=‘/‘;
/** the stack */
private Stack stack;//存放操作数的栈
public MyDcRational()
{
stack = new Stack();
}
public Rational evaluate (String expr)
{
Rational op1=new Rational();
Rational op2=new Rational();
Rational result=new Rational();
result.setNumerator(0);
String token;
StringTokenizer tokenizer = new StringTokenizer (expr);//划分表达式
while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();//将算数表达式分解的
if (isOperator(token))//见下方isOperateor方法,当是运算符的时候进入if语句
{
op2 = (Rational) stack.pop();
op1 = (Rational)stack.pop();//弹出最上面两个操作数
result = evalSingleOp (token.charAt(0), op1, op2);//见下方evaSingleOp方法
stack.push (result);//将计算结果压栈
}
else{
Rational num=new Rational();
num.setNumerator(Integer.parseInt(token));//将操作数由string转变为Rational
stack.push (num);//操作数入栈
}
}
return result;//输出结果
}
private boolean isOperator (String token)//判断是否为运算符,注意用equal语句比较字符串
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("÷")||token.equals("/"));
}
private Rational evalSingleOp (char operation, Rational op1, Rational op2)
{
Rational result=new Rational();
result.setNumerator(0);
switch (operation)
{
case ADD:
result = op1.add(op2);
break;
case SUBTRACT:
result = op1.sub(op2);
break;
case MULTIPLY:
result = op1.muti(op2);
break;
case DIVIDE1:
case DIVIDE2:
result = op1.div(op2);
break;
default:
System.out.println("Error!");
}
return result;
}
}
import java.util.*;
public class Calculation {
public static void main(String[] args) {
System.out.println("请输入要生成的题目数:");
Scanner number = new Scanner(System.in);
int n = number.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println("题目" + i + ":");
Scanner reader = new Scanner(System.in);
Rational result = new Rational();
String str = reader.nextLine();
ChangeExpress change = new ChangeExpress();
change.setOriginalExpression(str);
//System.out.println(change.originalExpression);
change.changedWay();//后缀式化
//System.out.println(change.changedExpression);
MyDcRational calculate = new MyDcRational();//后缀式计算
result = calculate.evaluate(change.changedExpression);
int a = result.getNumerator();
int b = result.getDenominator();
if (b == 1) {
System.out.println("result=" + a);
} else {
System.out.println("result=" + a + "/" + b);
}
}
}
}
标签:参考 size 需求分析 tip import 分割 出错 opera 主函数
原文地址:https://www.cnblogs.com/atbaoi/p/8850316.html