zoukankan      html  css  js  c++  java
  • 软件工程概论作业三

                                           加减程序3

    一、设计思想

    这次代码主要是在加减乘除2的基础上实现了对输出的格式要求,即数字与运算符之间添加有空格分隔符,加减负数的时候输出括号分开。

    二、程序代码(具体在github上更新)

    import java.util.Scanner;

    public class test
    {
        public static void main(String[] args)
        {
            Scanner scanner=new Scanner(System.in);
            System.out.print("请输入要生成的随机数个数:");
            int num=scanner.nextInt(),i;
            Shi[] shi=new Shi[num];
            System.out.print("请输入参与数的个数:");
            int n=scanner.nextInt();
            System.out.print("请选择是否有乘除(0有 1没有):");
            int cc=scanner.nextInt();
            int cy=2;
            int jf=0;
            if(cc==1)
            {
                System.out.print("请选择有无负数(0有 1没有):");
                jf=scanner.nextInt();
            }
            System.out.print("请选择是否有括号(0有 1没有):");
            int kh=scanner.nextInt();
    //        System.out.print("请输入参与数的个数:");
    //        int n=scanner.nextInt();
            System.out.print(String.format("%-6s","序号"));
            System.out.println(String.format("%-6s","算式"));
            for(i=0;i<num;i++)
            {
                shi[i]=new Shi(n);
                shi[i].chuangjian(cc,jf,kh);
                chachong(i,shi,cc,jf,cy,kh);
                System.out.print(String.format("%-6s",i+1));
                shi[i].shuchu(kh);
            }
        }
        
        static void chachong(int n,Shi[] x,int cc,int jf,int cy,int kh)
        {
            int i;
            for(i=0;i<n;i++)
            {
                if(x[n].equals(x[i]))
                {
                    x[n].chuangjian(cc,jf,kh);
                    chachong(n,x,cc,jf,cy,kh);
                    break;
                }
            }
        }
    }

    class Shi
    {
        Fenshu a[];
        int b[];
        int n;
        int m1,m2;//括号位置
        Shi()
        {
        }
        Shi(int n)
        {
            this.n=n;
            a=new Fenshu[n];
            for(int i=0;i<n;i++)
            {
                a[i]=new Fenshu();
            }
            b=new int[n-1];
        }
        void chuangjian(int cc,int jf,int kh)
        {
            a[0].chuangjian();
            for(int i=1;i<n;i++)
            {
                if(cc==0)
                {
                    b[i-1]=(int)(Math.random()*4);
                    a[i].chuangjian();
                    while(b[i-1]==3&&a[i].zi==0)
                    {
                        a[i].chuangjian();
                    }
                }
                else if(cc==1)
                {
                    b[i-1]=(int)(Math.random()*2);
                    a[i].chuangjian();
                    if(jf==0)
                    {
                        int k=(int)(Math.random()*2);
                        if(k==1) a[i].zi*=(-1);
                    }
                }
            }

            if(kh==0)
            {
                m1=(int)(Math.random()*(n-1));
                m2=(int)(Math.random()*(n-1)+1);
                while(m2<m1+1)
                {
                    m2=(int)(Math.random()*(n-1)+1);
                }
            }
        }
        
        void shuchu(int kh)
        {
            if(kh==1)
            {
                String output="";
                for(int i=0;i<n-1;i++)
                {
                    output+=(a[i].output()+" "+fuhao(b[i])+" ");
                }
                output+=(a[n-1].output()+" =");
                
                System.out.println(String.format("%-20s",output));
            }
            else if(kh==0)
            {
                String output="";
                int i;

                for(i=0;i<n-1;i++)
                {
                    if(i==m1) output+="( ";
                    output+=(a[i].output()+" ");
                    if(i==m2) output+=") ";
                    output+=(fuhao(b[i])+" ");
                }
                if(i==m2) output+=(a[n-1].output()+") =");
                else output+=(a[n-1].output()+" =");
                
                System.out.println(String.format("%-20s",output));
            }
        }
        
        String fuhao(int a)
        {
            String fuhao="";
            if(a==0) fuhao="+";
            if(a==1) fuhao="-";
            if(a==2) fuhao="*";
            if(a==3) fuhao="÷";
            return fuhao;
        }
        
        boolean equals(Shi y)
        {
            boolean flag=true;
            for(int i=0;i<n;i++)
            {
                if(!a[i].equals(y.a[i]))
                {
                    flag=false;
                    break;
                }
            }
            for(int i=0;i<n-1;i++)
            {
                if(!(b[i]==y.b[i]))
                {
                    flag=false;
                    break;
                }
            }
            if(!(m1==y.m1&&m2==y.m2)) flag=false;
            return flag;
        }
    }

    class Fenshu
    {
        int zi,mu;
        
        Fenshu()
        {
        }
        
        boolean equals(Fenshu x)
        {
            if(zi*x.mu==mu*x.zi) return true;
            return false;
        }
        
        void chuangjian()
        {
            zi=(int)(Math.random()*100);
            mu=(int)(Math.random()*100+1);
            while(zi>mu&&zi%mu!=0)
            {
                zi=(int)(Math.random()*100);
                mu=(int)(Math.random()*100+1);
            }
            if(zi!=0)
            {
                int gongyue=gongyue(zi,mu);
                zi=zi/gongyue;
                mu=mu/gongyue;
            }
        }
        
        int gongyue(int x,int y)
        {
            int k,i;
            k=x<y?x:y;
            for(i=k;i>=1;i--)
            {
                if(x%i==0&&y%i==0) break;
            }
            return i;
        }
        
        String output()
        {
            String out="";
            if(zi==0) out+=zi;
            else if(mu==1)
            {
                if(zi<0) out="("+zi+")";
                else out+=zi;
            }
            else out="("+zi+"/"+mu+")";
            return out;
        }
    }

    三、程序截图

    四、情况汇总

    估计耗费时间

    PSP2.1

    Personal Software Process Stages

    Time

    Planning

    计划

     1h

      · Estimate

      · 估计这个任务需要多少时间

     1h

    Development

    开发

     10h50min

      · Analysis

      · 需求分析 (包括学习新技术)

     1h

      · Design Spec

      · 生成设计文档

     20min

      · Design Review

      · 设计复审 (和同事审核设计文档)

     0

      · Coding Standard

      · 代码规范 (为目前的开发制定合适的规范)

     0

      · Design

      · 具体设计

     1h

      · Coding

      · 具体编码

    6h

      · Code Review

      · 代码复审

    1h

      · Test

      · 测试(自我测试,修改代码,提交修改)

     1h30min

    Reporting

    报告

     2h10min

      · Test Report

      · 测试报告

     1h

      · Size Measurement

      · 计算工作量

     20min

      · Postmortem & Process Improvement Plan

      · 事后总结, 并提出过程改进计划

     50min

    合计

     14h

     实际耗费时间

    PSP2.1

    Personal Software Process Stages

    Time

    Planning

    计划

     1h

      · Estimate

      · 估计这个任务需要多少时间

     1h

    Development

    开发

     11h30min

      · Analysis

      · 需求分析 (包括学习新技术)

     1h

      · Design Spec

      · 生成设计文档

     30min

      · Design Review

      · 设计复审 (和同事审核设计文档)

     0

      · Coding Standard

      · 代码规范 (为目前的开发制定合适的规范)

     0

      · Design

      · 具体设计

     1h

      · Coding

      · 具体编码

     8h

      · Code Review

      · 代码复审

     30min

      · Test

      · 测试(自我测试,修改代码,提交修改)

     30min

    Reporting

    报告

     2h10min

      · Test Report

      · 测试报告

     1h

      · Size Measurement

      · 计算工作量

     20min

      · Postmortem & Process Improvement Plan

      · 事后总结, 并提出过程改进计划

     50min

    合计

     14h30min

  • 相关阅读:
    2016.5.15——leetcode:Number of 1 Bits ,
    2016.5.14——leetcode-HappyNumber,House Robber
    记录学习过程
    npm 模块安装机制简介
    搭建Vue.js开发环境(window10)
    pwd 显示当前所在的工作路径
    Lucene 6.5.0 入门Demo
    java.lang.UnsupportedClassVersionError
    window.onload 和 $(document).ready(function(){}) 的区别
    plsql + 客户端 连接oracle数据库
  • 原文地址:https://www.cnblogs.com/lizhaoxuan/p/6591207.html
Copyright © 2011-2022 走看看