软件工程概论作业一
随机生成四则运算式(整式和真分数式)
程序设计思想:
整数式:
利用Random类的nextInt()方法随机生成0-99之间的两个整数作为运算数,再利用nextInt()生成一个0-3的整数作为运算符(0代表加、1代表减、2代表乘、3代表除)
代码为:
x = rd.nextInt(100); y = rd.nextInt(100); z = rd.nextInt(4);//运算符
生成的运算式存储在一个字符串中,例如加法的
if(z == 0) { t = "" + x + " + " + y + "=" + (x + y); }
因为得数不能为负数和浮点数,所以要增加条件限制:
当运算符是减法(即z == 1)时,x 不能小于 y
当运算符是除法(即z == 3)时,除数y不能是0,而且x 必须是y的倍数,
条件限制代码:
if(z == 0) { t = "" + x + " + " + y + "=" + (x + y); }else if(z == 1 && x >= y) {//减法 t = "" + x + " - " + y + "=" + (x - y); }else if(z == 2) { t = "" + x + " * " + y + "=" + (x * y); }else{//除法 if(y != 0){//除数不能是0 if(x % y == 0)//必须能整除 t = "" + x + " / " + y + "=" + (x / y); } }
因为不能出现重复的题目,所以要对生成的题目跟已生成的题目进行比较,如果重复则丢弃该题目,继续生成题目:
检验是否重复的代码:
if(buChong(jss,t,i)){ jss[i++] = t; } 响应的buChong函数定义 //检测是否重复(检测j是否与jss数组的前n个重复)重复返回FALSE 不重复返回TRUE public static boolean buChong(String[] jss,String j,int n) { for(int i = 0;i < n;i++) { if(jss[i].equals(j)){ return false; } } return true; }
然后循环n次(n为出题数目)
真分数式:
真分数式与整数式设计思想相同,所不同的是限制条件多,随机生成真分数时,先生成两个整数a,b作为分母,
然后再生成两个整数作为分子,但是第一个整数要小于a,第二个整数要小于b,这样生成的四个整数组合起来就是两个真分数
条件限制:分母都不能为0,相加、相乘、相除的结果要小于1,相减不能出现负数。
并且每个分数都要进行约简
条件限制代码:
if(y != 0){ if(z == 0) { if((x * b + y * a) < (a * b)) t = "" + yueJian(x, a) + " + " + yueJian(y, b) + "=" + yueJian((x * b + y * a), (a * b)); }else if(z == 1) { if((x * b - y * a) >= 0 && (x * b - y * a) < (a * b)) t = "" + yueJian(x, a) + " - " + yueJian(y, b) + "=" + yueJian((x * b - y * a), (a * b)); }else if(z == 2) { if((x * y) < (a * b)) t = "" + yueJian(x, a) + " * " + yueJian(y, b) + "=" + yueJian((x * y),(a * b)); }else{ if((y / b) != 0){ if((x * b) < (a * y)) t = "" + yueJian(x, a) + " / " + yueJian(y, b) + "=" + yueJian((x * b),(a * y)); } }
}
约简函数:
先找出分子和分母的最大公约数,然后分子和分母分别除以最大公约数
函数定义:
//约简分子分母,如果分子为0则返回0 public static String yueJian(int a,int b){ int y; int ac = a,bc = b; if(a == 0) return "0"; while(a != b - a){ y = b - a; if(a > y){ b = a; a = y; }else{ b = y; } } y = a; int z = ac / y; int m = bc / y; return "" + z + "/" + m; }
程序总代码:
1 package zuoYe; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class SiZeA { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int N; 11 Scanner scan = new Scanner(System.in); 12 System.out.println("请输入总数:"); 13 N = scan.nextInt(); 14 String[] jss = new String[N]; 15 System.out.println("1、整数计算式,2、真分数计算式"); 16 int p = scan.nextInt(); 17 18 if(p == 1) { 19 jss = createZhengShuJiSuanShi1(N); 20 }else { 21 jss = createZhenFenShuJiSuanShi(N); 22 } 23 24 //显示结果 25 int s = 0; 26 for(int i = 0;i < N;i++) { 27 String[] t = jss[i].split("="); 28 System.out.print((i + 1) + " : " + t[0] + "="); 29 String r = scan.next(); 30 if(r.equals(t[1])){ 31 System.out.println("回答正确!"); 32 s++; 33 }else{ 34 System.out.println("回答错误!"); 35 } 36 } 37 scan.close(); 38 System.out.println("答题完成,共答题" + N + "个,正确" + s + "个,正确率:" + (s * 100 / N) + "%"); 39 } 40 41 //生成真分数计算式 42 public static String[] createZhenFenShuJiSuanShi(int n) { 43 int x,y,z,a,b,i = 0; 44 String[] jss = new String[n]; 45 String t = ""; 46 Random rd = new Random(); 47 48 while(i < n) { 49 a = rd.nextInt(100); 50 b = rd.nextInt(100); 51 52 53 54 if(a != 0 && b != 0){ 55 x = rd.nextInt(a); 56 y = rd.nextInt(b); 57 z = rd.nextInt(4); 58 t = ""; 59 if(y != 0){ 60 if(z == 0) { 61 if((x * b + y * a) < (a * b)) 62 t = "" + yueJian(x, a) + " + " + yueJian(y, b) + "=" + yueJian((x * b + y * a), (a * b)); 63 64 }else if(z == 1) { 65 if((x * b - y * a) >= 0 && (x * b - y * a) < (a * b)) 66 t = "" + yueJian(x, a) + " - " + yueJian(y, b) + "=" + yueJian((x * b - y * a), (a * b)); 67 68 }else if(z == 2) { 69 if((x * y) < (a * b)) 70 t = "" + yueJian(x, a) + " * " + yueJian(y, b) + "=" + yueJian((x * y),(a * b)); 71 72 }else{ 73 if((y / b) != 0){ 74 if((x * b) < (a * y)) 75 t = "" + yueJian(x, a) + " / " + yueJian(y, b) + "=" + yueJian((x * b),(a * y)); 76 } 77 } 78 if((!t.equals("")) && buChong(jss,t,i)){ 79 jss[i++] = t; 80 } 81 } 82 } 83 } 84 return jss; 85 } 86 87 //生成整数计算式 88 public static String[] createZhengShuJiSuanShi1(int n) { 89 int x,y,z,i = 0; 90 String[] jss = new String[n]; 91 String t = ""; 92 Random rd = new Random(); 93 while(i < n) { 94 x = rd.nextInt(100); 95 y = rd.nextInt(100); 96 z = rd.nextInt(4); 97 98 if(z == 0) { 99 t = "" + x + " + " + y + "=" + (x + y); 100 101 }else if(z == 1 && x >= y) { 102 t = "" + x + " - " + y + "=" + (x - y); 103 104 }else if(z == 2) { 105 t = "" + x + " * " + y + "=" + (x * y); 106 107 }else{ 108 if(y != 0){ 109 if(x % y == 0) 110 t = "" + x + " / " + y + "=" + (x / y); 111 } 112 } 113 114 if(buChong(jss,t,i)){ 115 jss[i++] = t; 116 } 117 118 } 119 return jss; 120 } 121 122 123 //约简分子分母,如果分子为0则返回0 124 public static String yueJian(int a,int b){ 125 126 int y; 127 int ac = a,bc = b; 128 129 if(a == 0) 130 return "0"; 131 while(a != b - a){ 132 y = b - a; 133 if(a > y){ 134 b = a; 135 a = y; 136 }else{ 137 b = y; 138 } 139 } 140 y = a; 141 int z = ac / y; 142 int m = bc / y; 143 return "" + z + "/" + m; 144 } 145 146 //检测是否重复(检测j是否与jss数组的前n个重复)重复返回FALSE 不重复返回TRUE 147 public static boolean buChong(String[] jss,String j,int n) { 148 for(int i = 0;i < n;i++) { 149 if(jss[i].equals(j)){ 150 return false; 151 } 152 } 153 return true; 154 } 155 156 157 158 }
运行结果截图:
课上未完成原因:
对程序的整体没有分析透彻,没有一个完整的逻辑思路,逻辑思路不清晰,程序结构没有分析清楚,打字慢。