作业3:整数算术表达式分析程序
题目类别: B作业
关键字: 类、对象、封装、类之间的关系、字符串
内容要求:
-
编写一个Java应用程序。
-
要求实现功能如下:
(1) 从键盘输入一个表示整数四则运算的字符串,例如:32 + 5 * ( 30 - 40 ) / 3 - 12
四则运算符号:
+代表整数加法、-代表整数减法、*代表整数乘法、/代表整数除法(结果为整数)、() 括号
运算数全部为正整数
假设输入的字符串一定是符合运算规则的整数四则运算的表达式。
为方便输入和处理数据,运算数和运算符号之间有且仅有一个空格。
(2) 程序按照整数四则运算符的优先级和结合性对表达式进行分析并产生输出结果。
- 运行示例
示例1:
输入:32 + 5 * ( 30 - 40 ) / 3 - 12
输出:4
示例2:
输入:3 + 3 * ( 4 - ( ( 66 + 5 ) * 56 ) ) / 3 / 4
输出:-990
- 程序设计要求使用面向对象方法编写。
提交:打包为可以执行的JAR文档,其中要包含源程序文件。
提示:
- 关于字符串分隔处理的提示
Java中可以使用如下方式把字符串形式的表达式分解转换为一个存储表达式可以组成部分的数组,例如:
String exp = “32 + 5 * ( 30 - 40 ) / 3 - 12”; // exp存放由一个空格分隔的表达式字符串
String[] array = exp.split(" "); // 使用一个空格把字符串表达式分解转换为数组
数组array的内容是:[32, +, 5, *, (, 30, -, 40, ), /, 3, -, 12]
- 关于程序算法的提示
参考数据结构中的相应算法,使用栈(2个栈对象,运算符栈和运算数栈)
总结:
代码我是参考之前数据结构写的C版本,步骤一样,主要是构建类
C语音代码:
SCAU 8588 表达式求值
Number类
package com.company;
import java.util.Stack;
public class Number {
int a;
Stack <Integer> stNum = new Stack<Integer>();
Number(){
}
void pushNum(int b){
stNum.push(b);
}
int popNum(){
int t = stNum.pop();
return t;
}
}
Operator类
package com.company;
import java.util.Stack;
public class Operator {
Stack<Character> stChar = new Stack<>();
Operator(){
stChar.push('=');
}
void pushChar(char c){
stChar.push(c);
}
int popChar(){
int a = stChar.pop();
return a;
}
int peekChar(){
return stChar.peek();
}
char getPriority(char e,char c){//比较运算符优先级
char n='\0';
switch(c){
case'+':
case'-':{
if(e=='('||e=='=') n='<'; //c>e
else n='>';}break;
case'*':
case'/':{
if(e=='*'||e=='/'||e==')') n='>';//c<e
else n='<';}break;
case'(':{
if(e==')')
{
System.out.println("括号不匹配\n");
}
else n='<';} //c>e;
break;
case')':{
if(e=='(') n='=';
else if(e=='=') {
System.out.println("缺少左括号\n");}
else n='>';
}//e>c
break;
case'=':{
if(e=='=') n='=';
else if(e=='(') {
System.out.println("缺少右括号\n");}
else n='>';
} //e>c
}//switch
return n;
}
}
Main类
package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String exp = cin.nextLine();
String[] arrayTemt = exp.split(" "); // 使用一个空格把字符串表达式分解转换为数组
String [] array = new String [arrayTemt.length+1];
for(int i=0;i<arrayTemt.length;i++){
array[i]=arrayTemt[i];
}
array[arrayTemt.length] = "=";
//System.out.println(Arrays.toString(array));
Number num;
Operator ope;
ope = new Operator();
num = new Number();
int e;
e = ope.peekChar();
//System.out.println((char)e);
int i =0;
//System.out.println(array.length-1);
while(i!=array.length-1||e !='='){
if(isNumeric(array[i])){
num.pushNum(Integer.parseInt(array[i]));
i++;
}
else {
if (ope.getPriority((char)e, array[i].charAt(0)) == '<') {
ope.pushChar(array[i].charAt(0));
i++;
}else if (ope.getPriority((char)e, array[i].charAt(0)) == '=' && array[i].charAt(0) == ')') {
ope.popChar();
i++;
//System.out.println(i);
}else if (ope.getPriority((char)e, array[i].charAt(0)) == '>') {
int a, b, c = 0, d;
a = num.popNum();
b = num.popNum();
d = ope.popChar();
if (d == '+')
c = a + b;
else if (d == '-')
c = b - a;
else if (d == '/')
c = b / a;
else if (d == '*')
c = b * a;
num.pushNum(c);
//System.out.println(c+"="+b+(char)d+a);
}
}
e = ope.peekChar();
// if (i==array.length-1) break;
//System.out.println(i);
// if(i==13)break;
//System.out.println((char)e);
}
//int result;
System.out.println(num.popNum());
//System.out.println(num.popNum());
}
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
}