个人开发流程

2021/4/10 18:41:33

本文主要是介绍个人开发流程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

四则运算练习

需求分析:编写四则运算(不能有括号和负数存在 可用符号 +-x/ )

主要代码:

 1   //首先完成最基础的加减乘除运算
 2     public static double add(double... n) {
 3         double sum = 0;
 4         for (int i = 0; i < n.length; i++) {
 5             sum += n[i];
 6         }
 7         return sum;
 8     }
 9 
10     public static double jian(double... n) {
11         double sum = n[0];
12         for (int i = 0; i < n.length - 1; i++) {
13             sum -= n[i + 1];
14         }
15         return sum;
16     }
17 
18     public static double ride(double... n) {
19         double sum = n[0];
20         for (int i = 0; i < n.length - 1; i++) {
21             sum *= n[i + 1];
22         }
23         return sum;
24     }
25 
26     public static double chu(double... n) throws jisuan_error {
27         double sum = n[0];
28         for (int i = 0; i < n.length - 1; i++) {
29             if (n[i + 1] == 0) {
30                 throw new jisuan_error("除数不能为0");
31             }
32             sum /= n[i + 1];
33         }
34         return sum;
35     }
36 自定义异常类
37 public class jisuan_error extends Exception{
38     public jisuan_error (String message) {
39         super(message);
40     }
41 }

然后进行断言测试

 1 import org.junit.Assert;
 2 import org.junit.Test;
 3 
 4 import error.jisuan_error;
 5 import gongneng.Jisuan;
 6 
 7 public class Demo {
 8     @Test
 9     public void test() throws jisuan_error {
10         Assert.assertEquals((Double) 6.0, (Double) Jisuan.add(1, 2, 3));
11         Assert.assertEquals((Double) 3.0, (Double) Jisuan.jian(8, 2, 3));
12         Assert.assertEquals((Double) 12.0, (Double) Jisuan.ride(2, 2, 3));
13         Assert.assertEquals((Double) 2.0, (Double) Jisuan.chu(8,2,2));
14 
15 
16     }
17 }

测试结果

开始编写四则运算(不能有括号和负数存在 可用符号 +-x/ )

  1 当字符串为 zfc="7x8-20/10+3" 时    
  2 public static Object hunHe(String zfc) {
  3 
  4         // 获取"+-x/()"符号数量
  5         int cheng = shuLiang(zfc, 'x');
  6         int chu = shuLiang(zfc, '/');
  7         int jia = shuLiang(zfc, '+');
  8         int jian = shuLiang(zfc, '-');
  9 
 10         // 获取对应符号对应的索引数组
 11         int[] cheng_arr = indexs(zfc, 'x');
 12         int[] chu_arr = indexs(zfc, '/'); 
 13         int[] jia_arr = indexs(zfc, '+'); 
 14         int[] jian_arr = indexs(zfc, '-');
 15         int zs = cheng + chu + jia + jian;
 16         int[] zs_arr = new int[zs]; // 将几个数组中的结果都存进zs_arr数组
 17         int i = 0;
 18         for (int j = 0; i < cheng_arr.length; i++, j++) {
 19             zs_arr[i] = cheng_arr[j];
 20         }
 21 
 22         for (int j = 0; i < chu_arr.length + cheng_arr.length; i++, j++) {
 23             zs_arr[i] = chu_arr[j];
 24         }
 25 
 26         for (int j = 0; i < chu_arr.length + cheng_arr.length + jia_arr.length; i++, j++) {
 27             zs_arr[i] = jia_arr[j];
 28         }
 29 
 30         for (int j = 0; i < chu_arr.length + cheng_arr.length + jia_arr.length + jian_arr.length; i++, j++) {
 31             zs_arr[i] = jian_arr[j];
 32         }
 33         //冒泡排序zs_arr
 34         //zs_arr [1,3,6,9] 存储的为符号依次出现顺序的索引
 36         for (int j = 0; j < zs_arr.length; j++) {
 37             for (int k = 0; k < zs_arr.length - 1 - j; k++) {
 38                 if (zs_arr[k] > zs_arr[k + 1]) {
 39                     int num = zs_arr[k];
 40                     zs_arr[k] = zs_arr[k + 1];
 41                     zs_arr[k + 1] = num;
 42                 }
 43             }
 44         }
 45         //用来存储所有数值
 46         int[] num = new int[zs + 1];
 47 
 48         //用于给num数组赋值
 49         int s = 0;
 50         for (int j = 0; j < zs + 1; j++) {
 51 
 52             if (j == 0) {
 53                 num[j] = Integer.parseInt(zfc.substring(0, zs_arr[s]));
 54             } else if (j == zs) {
 55                 num[j] = Integer.parseInt(zfc.substring(zs_arr[s - 1] + 1, zfc.length()));
 56             } else {
 57                 num[j] = Integer.parseInt(zfc.substring(zs_arr[s - 1] + 1, zs_arr[s]));
 58             }
 59             s++;
 60         }
 61 
 62         //num [7,8,20,10,3] 存储各位数
 63         //list [7,x,8,-,20,/,10,+,3]
 64         List list = new ArrayList();
 65         int cishu = zs_arr.length + num.length;
 66         int a = 0;
 67         int b = 0;
 68         for (int j = 0; j < cishu; j++) {
 69             if (j % 2 != 0) {
 70                 list.add(String.valueOf(zfc.charAt(zs_arr[a])));
 71                 a++;
 72             } else {
 73                 list.add(num[b]);
 74                 b++;
 75             }
 76         }
 77 
 78         while (list.contains("x") || list.contains("/")) {
 79             for (int w = 0; w < list.size(); w++) {
 80                 if ("x".equals(list.get(w))) {
 81                     int result = (int) list.get(w - 1) * (int) list.get(w + 1);
 82                     list.set(w, result);
 83                     list.remove(w - 1);
 84                     list.remove(w);
 85                     break;
 86                 }
 87                 if ("/".equals(list.get(w))) {
 88                     int result = (int) list.get(w - 1) / (int) list.get(w + 1);
 89                     list.set(w, result);
 90                     list.remove(w - 1);
 91                     list.remove(w);
 92                     break;
 93                 }
 94             }
 95 
 96         }
 97 
 98         while (list.contains("+") || list.contains("-")) {
 99             for (int w = 0; w < list.size(); w++) {
100                 if ("+".equals(list.get(w))) {
101                     int result = (int) list.get(w - 1) + (int) list.get(w + 1);
102                     list.set(w, result);
103                     list.remove(w - 1);
104                     list.remove(w);
105                     break;
106                 }
107                 if ("-".equals(list.get(w))) {
108                     int result = (int) list.get(w - 1) - (int) list.get(w + 1);
109                     list.set(w, result);
110                     list.remove(w - 1);
111                     list.remove(w);
112                     break;
113                 }
114             }
115 
116         }
117         return list.get(0);
118     }
119 
120     /**
121      * 获取指定字符串中某字符数量
122      * 
123      * @param zfc
124      * @param zf
125      * @return
126      */
127     public static int shuLiang(String zfc, char zf) {
128         int num = 0;
129         if (zfc != null) {
130             for (int i = 0; i < zfc.length(); i++) {
131                 if (zf == (zfc.charAt(i))) {
132                     num++;
133                 }
134 
135             }
136         }
137         return num;
138 
139     }
140 
141     public static int[] indexs(String zfc, char zf) {
142         int shuLiang = shuLiang(zfc, zf);
143         int[] sz = new int[shuLiang];
144         // 为了获取四则运算符位置
145         int j = 0;
146         for (int i = 0; i < sz.length; i++) {
147             for (; j < zfc.length(); j++) {
148                 if (zf == zfc.charAt(j)) {
149                     sz[i] = j;
150                     break;
151                 }
152 
153             }
154             j++;
155         }
156         return sz;
157     }
158 
159 }

开始测试该功能

 1 import org.junit.Test;
 2 
 3 import error.jisuan_error;
 4 import gongneng.Jisuan;
 5 
 6 public class Demo {
 7     @Test
 8     public void test() throws jisuan_error {
 9 
10         Object ceshi1 = Jisuan.hunHe("7x8-20/10+3");
11         Object ceshi2 = Jisuan.hunHe("30x41-9x7+80/2");
12         System.out.println(ceshi1);
13         System.out.println(ceshi2);
14 
15     }
16 }

测试结果

 

psp展示

 

PSP2.1任务内容计划共完成需要的时间(h)实际完成需要的时间(h)
Planning 计划 48 72
· Estimate · 估计这个任务需要多少时间,并规划大致工作步骤 35 40
Development 开发  13 32
· Analysis 需求分析 (包括学习新技术)  3  8
· Design Spec · 生成设计文档  6  8
· Design Review · 设计复审 (和同事审核设计文档)  5  10
· Coding Standard 代码规范 (为目前的开发制定合适的规范)  10  20
· Design 具体设计  20  25
· Coding 具体编码 24 30
· Code Review · 代码复审  2 3
· Test · 测试(自我测试,修改代码,提交修改) 1 1
Reporting 报告  1 2
· Test Report · 测试报告 1  2
· Size Measurement 计算工作量  6  8
· Postmortem & Process Improvement Plan · 事后总结 ,并提出过程改进计划  8  10

总结:在具体编码与代码复审的阶段比较耗时间,有许多方法都忘记了,需要借助帮助文档,在后期可以增加带括号计算的功能,和负数等运算,还可以增加自动生成四则运算试子。



这篇关于个人开发流程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程