## 结对编程练习_四则运算（二）

leveltest类代码如下：
import java.util.*;
import java.util.Random;
import java.util.Scanner;

public class levelclass {
public levelclass(){
char[] operator = new char[]{'+', '-', '*', '÷'};
Random random = new Random();
Scanner scan = new Scanner(System.in);
int num;
int level;
System.out.println("该程序只能计算一到五个操作符的算式。");
System.out.println("输入你需要测试的算式等级：");
level = scan.nextInt();
System.out.println("输入你需要的式子数量：");
num = scan.nextInt();
switch (level)
{
//一级算式
case 1:

``````            ArrayList<String> expression1 = new ArrayList<String>();
for (int i = 0; i < num; i++) {
int n = random.nextInt(1) + 1; //1个运算符
int[] number = new int[n + 1];
String ex = new String();

for (int j = 0; j <= n; j++) {
number[j] = random.nextInt(100) + 1; //2个数字
}
for (int j = 0; j < n; j++) {
int s = random.nextInt(4);//随机选择某个运算符

ex += String.valueOf(number[j]) + String.valueOf(operator[s]);
if (s == 3) {
number[j + 1] = decide(number[j], number[j + 1]);
}
}
ex += String.valueOf(number[n]);
ex += "=";

}
for(int ii = 0; ii < expression1.size() ; ii++)
{
System.out.print(expression1.get(ii) + "\n");
}``````

// System.out.println(expression1);
break;
}

``````private static int decide(int x,int y){//通过递归实现整除
Random random=new Random();
if(x%y!=0){
y=random.nextInt(100)+1;
return decide(x,y);
}
else{
return y;
}
}

public void produce(){

}``````

}

fenshu类代码如下：
import java.util.Scanner;

public class fenshu {
public fenshu() {

``````    String formula = null;
int a, b,c,d;
System.out.println("请输入题目的数量");
Scanner scan2 = new Scanner(System.in);
c = scan2.nextInt();
String sz[] = new String[c];
int x1, x2, m1, m2;
for (d = 0; d < c; d++) {
m1 = 1 + (int) (Math.random() * 10);
x1 = 1 + (int) (Math.random() * m1);
m2 = 1 + (int) (Math.random() * 10);
x2 = 1 + (int) (Math.random() * m2);
int operate = (int) (Math.random() * 3);//生成运算符
if (operate == 0) {
b = x1 * m2 + x2 * m1;
a = m1 * m2;
formula = yuefen(b, a);
System.out.print(x1 + "/" + m1 + "+" + x2 + "/" + m2 + "=       ");
}
if (operate == 1) {
b = x1 * m2 - x2 * m1;
a = m1 * m2;
formula = yuefen(b, a);
System.out.print(x1 + "/" + m1 + "-" + x2 + "/" + m2 + "=       ");
}
if (operate == 2) {
b = x1 * x2;
a = m1 * m2;
formula = yuefen(b, a);
System.out.print(x1 + "/" + m1 + "*" + x2 + "/" + m2 + "=       ");
}
if (operate == 3) {
b = m1 * x2;
a = m2 * x1;
formula  = yuefen(b, a);
System.out.print(x1 + "/" + m1 + "/" + x2 + "/" + m2 + "=       ");
}
if ((d + 1) % 3 == 0) {
System.out.println();
}
sz[d] = formula;

}

}
public static String yuefen(int a,int b){
int y = 1;
for(int i=a;i>=1;i--){
if(a%i==0&&b%i==0){
y = i;
break;
}
}
int z = a/y;
int m = b/y;
if(z==0) {
return "0";
}
return ""+z+"/"+m;
}
public void fs(){

}``````

}

int wrong = 0;
for(String st :expression)
{
System.out.println(st);
String str;
str = st;
Zhan lt = new Zhan();
List list = lt.work(str);
List list2 = lt.InfixToPostfix(list);
System.out.println("输入答案");
int daan = scan.nextInt();

``````    System.out.print("后缀表达式为：");
lt.printList(list2);
System.out.println(" ");

if(daan != lt.doCal(list2)){
System.out.println("错误    正确答案为："+lt.doCal(list2));
System.out.println(" ");
wrong = wrong+1;}
else{
System.out.println("正确");
System.out.println(" ");}
}
int sum=100-(wrong*100/num);
System.out.println("正确率："+sum+"%");``````

PSP2.1 Personal Software Process Stages 预估耗时（分钟） 实际耗时（分钟）
Planning 计划 60
Estimate 估计这个任务需要多少时间 140
Development 开发 300
Analysis 需求分析 (包括学习新技术) 300
Coding Standard 代码规范 (为目前的开发制定合适的规范) 100
Design UML 设计项目UML类图 40
Coding 具体编码 200
Code Review 代码复审 60
Test 测试（自我测试，修改代码，提交修改） 120
Size Measurement 计算工作量（实际时间) 250
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 15

0条评论