203
社区成员
发帖
与我相关
我的任务
分享| 这个作业属于哪个课程 | 广工软件工程学习 |
|---|---|
| 这个作业要求在哪里 | 结对编程:小学四则运算 |
| 这个作业的目标 | 结对编程实现小学四则运算程序 |
| 姓名 | 学号 | GitHub地址 |
|---|---|---|
| 张繁荣 | 3120005177 | https://github.com/Ralph1346/-3120005177 |
| 张威浩 | 3120005179 | https://github.com/Zwh-max/sizeyuansuan |
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| · Planning | 计划 | 20 | 30 |
| · Estimate | 估计这个任务需要多少时间 | 5 | 10 |
| · Development | 开发 | 250 | 300 |
| · Analysis | 需求分析 (包括学习新技术) | 40 | 35 |
| · Design Spec | 生成设计文档 | 25 | 20 |
| · Design Review | 设计复审 | 45 | 30 |
| · Coding Standard | 代码规范 | 15 | 20 |
| · Design | 具体设计 | 35 | 30 |
| · Coding | 具体编码 | 200 | 250 |
| · Code Review | 代码复审 | 60 | 60 |
| · Test | 测试(自我测试,修改代码,提交修改) | 80 | 60 |
| Reporting | 报告 | 50 | 80 |
| · Test Repor | 测试报告 | 35 | 20 |
| · Size Measurement | 计算工作量 | 25 | 20 |
| · Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 45 | 60 |
| 单元格 | 合计 | 910 | 1025 |





public class sizeyunsuan {
private static Random random = new Random();
public static int range;
public static String reductionofFraction(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";
}
if(m==1) return z+"";
else return biaodashi(z,m);
}
public static String biaodashi(int a,int b) {//判断假分数,并化假分数为带分数
if(a>=b) {
int c;
c=a/b;
int d;
d=a%b;
{if(d==0) {return c+"";}
return c+"'"+d+"/"+b;}
}return a+"/"+b;
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("请输入产生几以内的数字:");
range=sc.nextInt();
System.out.println("请输入产生多少个运算表达式:");
int num=sc.nextInt();
int rightcount[]=new int[num+2];
int wrongcount[]=new int[num+2];
int right1=0;
int wrong1=0;
String[] results=new String[num];int i;
for( i=0;i<num;i++){
String expArr[]=new String[2];//定义生成的题目
int a= (int) (random.nextInt(range));//分子
int b= (int) (random.nextInt(range));//分母
int c= (int) (random.nextInt(range));//另一个分子
int d= (int) (random.nextInt(range));//另一个分母
int fuhao;//运算符
fuhao= (int) (random.nextInt(4));
if(b!=0&&d!=0) {//分母均不为0时生成带有分数的计算题,同时计算结果
if(fuhao==0) {
int fenzi=a*d+b*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'+'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==1&&a*d-b*c>=0) {
int fenzi=a*d-b*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==1&&a*d-b*c<0) {
int fenzi=b*c-a*d;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==2) {
int fenzi=a*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'×'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==3&&c!=0) {
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==3&&c==0) {
break;
/*c=1;
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);*/
}
}
else {//分母至少一个为0时生成只含有整式的运算式,同时计算结果
b=1; d=1;
if(fuhao==0) {
int fenzi=a*d+b*c;
int fenmu=b*d;
expArr[0]=a+"+"+c+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==1&&a*d-b*c>=0) {
int fenzi=a*d-b*c;
int fenmu=b*d;
expArr[0]=a+"-"+c+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==1&&a*d-b*c<0) {
int fenzi=b*c-a*d;
int fenmu=b*d;
expArr[0]=c+"-"+a+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==2) {
int fenzi=a*c;
int fenmu=b*d;
expArr[0]=c+"×"+a+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==3&&c!=0) {
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=a+"÷"+c+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);
}
if(fuhao==3&&c==0) {
break;
/*c=1;
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=a+"÷"+c+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);*/
}
}
FileWriter fw = null;
try {
File f=new File("Exersies.txt");//题目写入
fw = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}if(expArr[0]!=null) {
PrintWriter pw = new PrintWriter(fw);
pw.println(i+1+"."+expArr[0]);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}}FileWriter fn = null;
try {
File f=new File("Answer.txt");//答案写入
fn = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}if(expArr[0]!=null) {
PrintWriter pn = new PrintWriter(fn);
pn.println(i+1+"."+results[i]);
pn.flush();
try {
fn.flush();
pn.close();
fn.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
System.out.println("输入ok提交!");
Scanner sc1=new Scanner(System.in);
String submit=sc1.nextLine();
if(submit.equals("ok")){
String array[]=new String[num];
try
{ int k=0;
FileReader fr = new FileReader("C://Users//brave//Desktop//sizeyunsuan//Answer.txt");
BufferedReader br = new BufferedReader(fr);
String s ;
while((s = br.readLine())!=null&&k<num) {//读取小学生的答案
array[k]=s;
k++;
}br.close();
fr.close();
}catch(IOException e){
System.out.println("指定文件不存在");
}
for(int j=0;j<num;j++){
if(array[j].equals(results[j])) {//验证答案,统计正确和错误的个数
rightcount[j]=j+1;
right1++;
}
else {
wrongcount[j]=j+1;
wrong1++;
}
}
FileWriter fg = null;
try {
//反馈正确与错误题目的信息
File f=new File("Grade.txt");
fg = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pg = new PrintWriter(fg);
pg.println(" ");
pg.print("Correct:"+right1+"(");
for (int j = 0; j <= num; j++) {
if (rightcount[j] != 0) {
pg.print(rightcount[j] + ",");
}
}
pg.println(")");
pg.print("Wrong:"+wrong1+"(");
for (int j = 0; j <= num; j++) {
if (wrongcount[j] != 0) {
pg.print(wrongcount[j] + ",");
}
}
pg.print(")");
pg.flush();
try {
fg.flush();
pg.close();
fg.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
}
正常测试:




判断:

10000测试:


遇到的困难主要是数组越界,通过严谨的检查以及步步验证的方式找出了错误并解决了。另一个须待解决的问题是生成题目的速度有些缓慢,因此完善了多次的算法来提高代码的执行效率。总的来说,在结对编程中我们有各自的想法,通过代码的形式来进行人与人之间的交流不失为一种好的方法,在这个过程中我们培养了团队协作的能力和与他人交际的能力,同时也促使自身的编程能力不断提高,这使我们都受益匪浅。