结对编程:小学四则运算

张繁荣 2022-09-30 13:29:24

目录

  • 一、PSP表格
  • 二、效能分析
  • 三、流程图
  • 四、代码
  • 五、测试运行
  • 六、心得

这个作业属于哪个课程广工软件工程学习
这个作业要求在哪里结对编程:小学四则运算
这个作业的目标结对编程实现小学四则运算程序
姓名学号GitHub地址
张繁荣3120005177https://github.com/Ralph1346/-3120005177
张威浩3120005179https://github.com/Zwh-max/sizeyuansuan

一、PSP表格

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
· Planning计划2030
· Estimate估计这个任务需要多少时间510
· Development开发250300
· Analysis需求分析 (包括学习新技术)4035
· Design Spec生成设计文档2520
· Design Review设计复审4530
· Coding Standard代码规范1520
· Design具体设计3530
· Coding具体编码200250
· Code Review代码复审6060
· Test测试(自我测试,修改代码,提交修改)8060
Reporting报告5080
· Test Repor测试报告3520
· Size Measurement计算工作量2520
· Postmortem & Process Improvement Plan事后总结, 并提出过程改进计划4560
单元格合计9101025

二、效能分析

img

img

img

img

三、流程图

img

四、代码

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();
            }}
    }
}

五、测试运行

正常测试:

img

img

img

img

判断:

img

10000测试:

img

img

六、心得

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

...全文
143 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

203

社区成员

发帖
与我相关
我的任务
社区描述
高校教学社区
其他 高校 广东省·广州市
社区管理员
  • ryue.zh
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧