码迷,mamicode.com
首页 > 其他好文 > 详细

软件工程个人作业02

时间:2017-03-13 00:02:38      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:size   for   tin   ann   导致   ret   编码   阶段   ++   

设计思想:采用随机数来决定是否右括号,计算结果用到递归方法,先对运算符中的乘除进行计算,在进行加减计算,在输出结果。

源程序代码:(未完成)

package 四则运算;

import java.util.Random;
import java.util.*;

public class Sizeyunsuan2 {
    public static void main(String[] args) {
        int m = 0;
        int n = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入题目中需要参与计算的数字个数(大于2但不要超过10个):");
        m = scan.nextInt();
        if (m > 1&& m < 11) {
            System.out.println("请输入需要生成的题目的数量:");
            n = scan.nextInt();
            shengcheng(m, n);
        } else
            System.out.println("超出范围!请重新输入!");
        main(args);
    }

    public static void shengcheng(int m, int n) {
        int[] shu = new int[m];
        String[] fuhao = new String[m - 1];
        System.out.println("生成的题目如下:");
        for (int i = 0; i < n;) {
            int panduan = new Random().nextInt(2);
            if (panduan == 0) {
                shu = shu(m);
                fuhao = fuhao(m - 1);
                for (int x = 0; x < m -1;) {
                    System.out.print(shu[x]);
                    if (fuhao[x] == "/" && shu[x + 1] == 0) {
                        fuhao[x]="+";
                    }
                    System.out.print(fuhao[x]);
                    x++;
                }
                System.out.println(shu[shu.length-1]+"="+"        答案:" + jisuan(shu, fuhao));
            } else if (panduan == 1) {
                shu = shu(m);
                int weizhi1 = new Random().nextInt(shu.length - 1);
                int weizhi2 =-weizhi1+ new Random().nextInt(shu.length - weizhi1-1) +1;
                fuhao = fuhao(m - 1);
                for (int x = 0; x < m;) {
                    if (x!=weizhi1&&x<m-1) {
                    System.out.print(shu[x]);
                    if (fuhao[x] == "/" && shu[x + 1] == 0) {
                        fuhao[x]="+";
                    }
                    System.out.print(fuhao[x]);
                    }
                    else if (x== weizhi1) {
                        System.out.print("("+shu[x]);
                        if (fuhao[x] == "/" && shu[x + 1] == 0) {
                            fuhao[x]="+";
                        }
                        System.out.print(fuhao[x]);
                    }
                    else if (x ==weizhi2&&x<m-1) {
                        System.out.print(shu[x]+")"+fuhao[x]);
                    }
                    else if (x ==weizhi2&&x==m-1) {
                        System.out.print(shu[x]+")");
                    }
                    x++;
                }
                System.out.print("=");
                int[] shu2 = new int[weizhi2 - weizhi1 + 1];
                String[] fuhao2 = new String[shu2.length - 1];
                shu2[0] = shu[weizhi1];
                for (int a = 1; a < shu2.length;) {
                    shu2[a] = shu[weizhi1 + i];
                    fuhao2[a - 1] = fuhao[weizhi1];
                    a++;
                }
                int jieguo = jisuan(shu2, fuhao2);
                shu[weizhi1] = jieguo;
                for (int x = weizhi1 + 1; x < shu.length - 1;) {
                    shu[x] = shu[x + 1];
                    x++;
                }
                for (int x = weizhi1 + 1; x < shu.length - 2;) {
                    fuhao[x] = fuhao[x + 1];
                    x++;
                }
                System.out.println("        答案:" + jisuan(shu, fuhao));
            }
            i++;
        }
    }

    public static int[] shu(int x) {
        int[] shu = new int[x];
        for (int i = 0; i < x;) {
            shu[i] = new Random().nextInt(100);
            i++;
        }
        return shu;
    }

    public static String[] fuhao(int x) {
        String fh[] = { "+", "-", "*", "/" };
        String[] fuhao = new String[x];
        for (int i = 0; i < x;) {
            int n = new Random().nextInt(4);
            fuhao[i] = fh[n];
            i++;
        }
        return fuhao;
    }

    public static int jisuan(int[] shu, String[] fuhao) {
        int[] shu1=new int[shu.length-1];
        String[] fuhao1=new String[fuhao.length-1];
        if (fuhao.length > 1) {
            int n = 0;
            for (int i = 0; i < fuhao.length;) {
                if (fuhao[i] == "*") {
                    n = shu[i] * shu[i + 1];
                    for (int x = 0; x <=fuhao.length;) {
                       if(x<i){
                        shu1[x]=shu[x];
                        fuhao1[x]=fuhao[x];
                       }
                       else if(x==i){
                           shu1[x]=n;
                           fuhao1[x]=fuhao[x+1];
                       }
                       else if(x>i&&x<fuhao.length-1){
                           shu1[x]=shu[x+1];
                           fuhao1[x]=fuhao[x+1];
                       }
                       else if(x>i&&x==fuhao.length-1){
                           shu1[x]=shu[x+1];
                       }
                       x++;
                       }
                  }
                else if (fuhao[i] == "/") {
                    n = shu[i] / shu[i + 1];
                    for (int x = 0; x <=fuhao.length;) {
                           if(x<i){
                            shu1[x]=shu[x];
                            fuhao1[x]=fuhao[x];
                           }
                           else if(x==i){
                               shu1[x]=n;
                               fuhao1[x]=fuhao[x+1];
                           }
                           else if(x>i&&x<fuhao.length-1){
                               shu1[x]=shu[x+1];
                               fuhao1[x]=fuhao[x+1];
                           }
                           else if(x>i&&x==fuhao.length-1){
                               shu1[x]=shu[x+1];
                           }
                           x++;
                        jisuan(shu1, fuhao1);
                    }
                    i++;
                }
                for (int b = 0; b < fuhao.length;) {
                    if (fuhao[b] == "+") {
                        n = shu[b] + shu[b + 1];
                        for (int x = 0; x <=fuhao.length;) {
                               if(x<i){
                                shu1[x]=shu[x];
                                fuhao1[x]=fuhao[x];
                               }
                               else if(x==i){
                                   shu1[x]=n;
                                   fuhao1[x]=fuhao[x+1];
                               }
                               else if(x>i&&x<fuhao.length-1){
                                   shu1[x]=shu[x+1];
                                   fuhao1[x]=fuhao[x+1];
                               }
                               else if(x>i&&x==fuhao.length-1){
                                   shu1[x]=shu[x+1];
                               }
                               x++;
                        }
                    }
                    else if (fuhao[b] == "-") {
                        n = shu[b] - shu[b + 1];
                        for (int x = 0; x <=fuhao.length;) {
                               if(x<i){
                                shu1[x]=shu[x];
                                fuhao1[x]=fuhao[x];
                               }
                               else if(x==i){
                                   shu1[x]=n;
                                   fuhao1[x]=fuhao[x+1];
                               }
                               else if(x>i&&x<fuhao.length-1){
                                   shu1[x]=shu[x+1];
                                   fuhao1[x]=fuhao[x+1];
                               }
                               else if(x>i&&x==fuhao.length-1){
                                   shu1[x]=shu[x+1];
                               }
                               x++;
                            jisuan(shu1, fuhao1);
                        }
                    }
                    b++;
                }
            }
        }
        else if (fuhao.length == 1) {
            if (fuhao[0] == "+") {
                shu[0] = shu[1] + shu[0];
            }
            if (fuhao[0] == "-") {
                shu[0] = shu[1] - shu[0];
            }
            if (fuhao[0] == "*") {
                shu[0] = shu[1] * shu[0];
            }
            if (fuhao[0] == "/") {
                shu[0] = shu[1] / shu[0];
            }
        }
        int jieguo = shu[0];
        return jieguo;
    }
}

结果总结:在除法运算中,本来是想如果除号后为零则将除号换成加号,但是这将会导致输出的式子与结果不一致,符号不能替换后再输出。

学生   胡泽杰

日期   17/3/12

教员   王建明

程序    四则运算题目生成器

日期

编号

类型

引入阶段

排除阶段

修复时间

修复缺陷

3/5

1

四则运算

编码

编译

30min

 

描述:能进行简单计算,可以化简并且输出分数结果

3/10

2

四则运算

编码

编译

2h

 

描述:能输出多个数进行计算,但是不能进行分数的计算,进去额度不够

3/12

3

四则运算

编码

编译

3h

 

描述:加入了括号,但是输出易出错,也不能判断除号后数是否为0

软件工程个人作业02

标签:size   for   tin   ann   导致   ret   编码   阶段   ++   

原文地址:http://www.cnblogs.com/liulitianxia/p/6540008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!