软件工程——四则运算练习题
通过Java来设计实现该程序
package cal; import java.util.Scanner; import java.util.Stack; import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default; public class Practice1 { public static void main(String[] args) { int num[] = new int[3]; char op[] = new char[2]; int on[] = new int[2]; boolean isExist[][][][][] = new boolean[100][100][100][4][4]; Scanner sc = new Scanner(System.in); System.out.println("请输入题目数量"); int n = sc.nextInt(); for(int k=0;k<n;k++) { Practice1 p = new Practice1(); int opNum[] = new int[2]; num[0] = p.getNum(); opNum[0] = p.getOpNum(); op[0] = p.getOp(opNum[0]); num[1] = p.getNum(); while(op[0]=='/' && num[1]==0 ) //防止被除数为0 { num[1] = p.getNum(); } opNum[1] = p.getOpNum(); op[1] = p.getOp(opNum[1]); num[2] = p.getNum(); while(op[1]=='/' && num[2]==0) //同上 { num[2] = p.getNum(); } if(isExist[num[0]][num[1]][num[2]][opNum[0]][opNum[1]]) //防止重复 k--; isExist[num[0]][num[1]][num[2]][opNum[0]][opNum[1]] = true; String str1 = num[0]+""+op[0]+""+num[1]+op[1]+""+num[2]+"=?"; String str2 = num[0]+""+op[0]+""+num[1]+op[1]+""+num[2]; //Info info = new Info(num[0],num[1],num[2],op[0],op[1]); //System.out.println(str2); calculator cal = new calculator(); double res = cal.calculate(str2); if(res>100 || res<0){ k--; continue; } System.out.println(str2+"="+res); } } public char getOp(int opNum) { switch (opNum) { case 0: return '+'; case 1: return '-'; case 2: return '*'; case 3: return '/'; default: return '='; } } public int getOpNum() { int opNum = (int)(Math.random()*100%4); return opNum; } public int getNum() { int num = (int)(Math.random()*100%100); return num; } }
package cal; import java.util.Stack; public class calculator { public int priority(char c){ switch (c){ case '+': return 1; case '-': return 1; case '/': return 2; case '*': return 2; } return 0; } public double calculate(String str) { String num=""; String strs[] = new String[15]; Stack<String> ops = new Stack<String>(); Stack<Double> d = new Stack<Double>(); int len=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)>='0' && str.charAt(i)<='9'){ num+=str.charAt(i); }else { strs[len++] = num; char op = str.charAt(i); if(ops.empty() || getPriority(""+op)>getPriority(ops.peek())){ ops.push(""+op); }else{ while(!ops.empty() && getPriority(""+op)<=getPriority( ops.peek() ) ){ strs[len++] = ops.pop(); } ops.push(""+op); } num = ""; } } strs[len++] = num; while(!ops.empty()){ strs[len++] = ops.pop(); } for(int i=0;i<len;i++){ if(strs[i].charAt(0)>='0' && strs[i].charAt(0) <='9'){ d.push(Double.parseDouble(strs[i])); }else{ char op = strs[i].charAt(0); double d1 = d.pop(); double d2 = d.pop(); double d3 =0; switch (op){ case '+': d3 = d2+d1; break; case '-': d3 = d2-d1; break; case '*': d3 = d2*d1; break; case '/': d3 = d2/d1; break; } d.push(d3); } } return d.pop(); } public int getPriority(String str){ char op = str.charAt(0); switch (op){ case '+': case '-': return 1; case '*': case '/': return 2; } return 0; } public double solution(double d1,double d2,String str){ char op = str.charAt(0); switch (op){ case '+': return d2+d1; case '-': return d2-d1; case '*': return d2*d1; case '/': return d2/d1; } return 0; } }
程序效果如下图所示:
思路:
1、通过Math.random()来生成随机数,如int num = (int)(Math.random()*100%100);生成的是0到99的整数,运算符则是用0-3来代替
2、排除重复情况:通过一个boolean类型五维数组标记出现过的情况,如果该组数据已经出现过则重新生成
3、通过栈来求表达式的值:先将中缀表达式转化为逆波兰式,然后计算
1)中缀转后缀:
①扫描中缀表达式,遇到数字则存入字符串数组strs中
②定义一个String类型的栈ops
③遇到操作符时:如果ops为空或栈顶元素的优先级小于当前操作运算符op,直接入栈。否则将栈中优先级小于或等于op的数据依次出栈添加到strs中,再将op压入栈(每个op都要入栈),扫描完成后将栈中的元素依次出栈添加到strs中
2)逆波兰式求值:(扫描逆波兰式)
①遇到数字则将数字压入栈中
②遇到运算符则从栈中提取出两个数字进行运算,然后压入栈中(第一个出栈的数据放在运算符右边)
③扫描完成后,栈中还会剩下一个数字,即结果