作业 结对编程
一 结对成员
陈颖锋 201421122113
项目开发的coding.net 地址:https://coding.net/u/ricardoCYF/p/zuoye3/git
二 项目描述
三 项目实现
1.需求分析:
题目自动生成(根据数量和范围) 答题(做题 答案验证 得出正确答案)

业务类:
1 @Service
2 public class MainService {
3
4 public List<String> getQuestList(int num,int random) {
5 if (num <= 0)
6 throw new InputMismatchException();
7 if (random <= 0)
8 throw new InputMismatchException();
9 List<String> list=new ArrayList<String>();
10 for (int i = 0; i <num ; i++) {
11 String str= Util.calStringCreate(random);
12
13 list.add(str);
14
15 }
16 return list;
17
18
19 }
20
21
22 public List<String> getResult(List<String> quest,List<String> answer) {
23 List<String> list=new ArrayList<String>();
24 if(quest.size()!=answer.size())
25 try {
26 throw new Exception("输入不匹配");
27 } catch (Exception e) {
28 // TODO Auto-generated catch block
29 e.printStackTrace();
30 }
31 for (int i = 0; i < quest.size(); i++) {
32 String str=quest.get(i);
33 String rs= Math.calc(str);
34 if(answer.get(i).equals(rs)){
35 list.add("正确");
36 }
37 else{
38 list.add("错误");
39 }
40 }
41 return list;
42 }
43
44
45 }
数学类:
1 /**
2 * Created by computer on 2017/9/26.
3 * 计算运算字符串的类
4 */
5 public class Math {
6 /**
7 * @param str 需要运算的字符串
8 */
9 public static boolean isBracket(String str){
10 String regex=".*\\(.*";
11 return str.matches(regex);
12 }
13
14
15 /**
16 *计算没有括号得到字符串
17 * @param str 运算字符串
18 * @return FenShu的fenshu变量
19 */
20 public static String calc(String str){
21 /*
22 1得到运算符的List集合
23 2得到操作数的List集合
24 3计算
25 */
26 List<Character> opList=getOpList(str);
27 List<FenShu> numList=getNumList(str);
28 FenShu result=jisuan(opList,numList);
29 return Util.trueFenShu(result);
30 }
31
32 /**
33 *
34 * @param i:运算符的下标
35 * @param c:运算符
36 * @param num:操作数集合
37 */
38 public static void mulDiv(int i,Character c,List<FenShu> num){
39 if (c == '*' || c == '/') {//删除*/运算符并进行对应数值的计算 结果保存
40 FenShu opl = num.get(i);
41 FenShu opr = num.get(i + 1);
42 if (c == '*') {
43 num.set(i + 1, fenShuMul(opl, opr));
44 } else if (c == '/') {
45 num.set(i + 1, fenShuDivi(opl, opr));
46 }
47 }
48 }
49
50
51
52 /**
53 *
54 * @param op 操作符集合
55 * @param num 操作数集合
56 * @return 真分数值
57 */
58 public static FenShu jisuan(List<Character> op,List<FenShu> num){
59 //遍历运算符集合提取出+ -的下标并用二维数组保存+ -分割的运算符字符集
60 List <Integer>opList1=new ArrayList<Integer>();
61 for (int i = 0; i <op.size() ; i++) {
62 if (op.get(i)=='+'||op.get(i)=='-'){
63 opList1.add(i);
64 }
65 }
66 if(opList1.size()>0){
67 if (0<opList1.get(0)) {
68 for (int i = 0; i < opList1.get(0); i++) {//下标从0到小于opList1.get(0)
69 Character c = op.get(i);
70 mulDiv(i,c,num);
71 }
72 }
73 if (opList1.size()>1)
74 for (int i = 0; i < opList1.size(); i++) {//目的求被+-分割起来的区域 从运算符集合+-后的*/开始直到下一个+-运算符出现
75 for (int j = opList1.get(i)+1; j <opList1.get(i+1) ; j++) {
76 Character c=op.get(j);
77 mulDiv(j,c,num);
78 }
79 if (opList1.get(i+1)==opList1.get(opList1.size()-1))//末尾时候退出
80 break;
81 }
82 if (opList1.get(opList1.size()-1)!=op.size()-1){//如果+-符号不是是运算符集合的最后一位 需要对于后面的式子进行乘除计算
83 for (int j = opList1.get(opList1.size()-1)+1; j <op.size() ; j++) {
84 Character c=op.get(j);
85 mulDiv(j,c,num);
86 }
87 }
88 }
89 else if (opList1.size()==0){
90 for (int i = 0; i < op.size(); i++) {//下标从0到小于opList1.get(0)
91 Character c = op.get(i);
92 mulDiv(i,c,num);
93 }
94 }
95 //这是先乘除之后的数 和 需要加减的符号
96 // if (opList1.size()>0)
97 // for (int i:opList1) {
98 // System.out.println(num.get(i).getFenzi()+"/"+num.get(i).getFenmu());
99 // System.out.println(op.get(i));
100 // }
101 // System.out.println(num.get(num.size()-1).getFenzi()+"/"+num.get(num.size()-1).getFenmu());
102 List<Character> opList2=new ArrayList<Character>();
103 List<FenShu> numList2=new ArrayList<FenShu>();
104 if (opList1.size()>0)
105 for (int i:opList1) {
106 numList2.add(num.get(i));
107 opList2.add(op.get(i));
108 }
109 numList2.add(num.get(num.size()-1));
110 while (!opList2.isEmpty()){
111 Character c=opList2.remove(0);
112 FenShu opl=numList2.remove(0);
113 FenShu opr=numList2.remove(0);
114
115 if(c=='+'){
116 numList2.add(0, fenShuAdd(opl,opr));
117 }
118 else if(c=='-'){
119 numList2.add(0, fenShuSub(opl,opr));
120 }
121 }
122 return numList2.get(0);
123 }
124
125
126 /**
127 *
128 * @param str 运算的字符串
129 * @return 返回运算符集合
130 */
131
132 public static List<Character> getOpList(String str){
133 List<Character> list = new ArrayList<Character>();
134 String regex = "[0-9]*";
135 Pattern pattern=Pattern.compile(regex);
136 String []split=pattern.split(str);
137 for (int i = 0; i <split.length; i++) {
138 String tmp=split[i].trim();
139 if(tmp.equals("+")|tmp.equals("-")|tmp.equals("*")|tmp.equals("/")){
140 list.add(tmp.trim().charAt(0));
141 }
142 }
143 return list;
144 }
145
146 /**
147 *
148 * @param str 运算的字符串
149 * @return 返回FenShu类型的集合
150 */
151 public static List getNumList(String str){
152 List<FenShu> list = new ArrayList<FenShu>();
153 String regex = "\\+|-|\\*|/";
154 Pattern pattern=Pattern.compile(regex);
155 String []split=pattern.split(str);
156 for (int i = 0; i <split.length; i++) {
157 String tmp=split[i].trim();
158 String regex2="[0-9]+";
159 if(tmp.matches(regex2)){
160 Integer num=Integer.valueOf(tmp);
161 FenShu fenshu=new FenShu(num,1);
162 list.add(fenshu);
163 }
164 }
165 return list;
166 }
167
168 /**
169 * 分数类的加减乘除
170 * @param fenshu1
171 * @param fenshu2
172 * @return
173 */
174
175
176 public static FenShu fenShuAdd(FenShu fenshu1, FenShu fenshu2){
177 int lcm=getMinMul(fenshu1.getFenmu(),fenshu2.getFenmu());//z最小公倍数 未化简的分母
178 int numera=(lcm/fenshu1.getFenmu())*fenshu1.getFenzi()+(lcm/fenshu2.getFenmu())*fenshu2.getFenzi();//最小公倍数除以分母乘以分子相加 这是分子和
179 int gcd= getMaxDiv(numera,lcm);//最大公约数 可以约的值
180 numera/=gcd;
181 int deomina=lcm/gcd;
182 FenShu fenshu3=new FenShu(numera,deomina);
183 return fenshu3;
184 }
185
186 public static FenShu fenShuSub(FenShu fenshu1, FenShu fenshu2){
187 int lcm=getMinMul(fenshu1.getFenmu(),fenshu2.getFenmu());//z最小公倍数 未化简的分母
188 int numera=(lcm/fenshu1.getFenmu())*fenshu1.getFenzi()-(lcm/fenshu2.getFenmu())*fenshu2.getFenzi();//最小公倍数除以分母乘以分子相加 这是分子和
189 int gcd= getMaxDiv(numera,lcm);//最大公约数 可以约的值
190 numera/=gcd;
191 int deomina=lcm/gcd;
192 FenShu fenshu3=new FenShu(numera,deomina);
193 return fenshu3;
194 }
195
196
197 public static FenShu fenShuMul(FenShu fenshu1, FenShu fenshu2){
198 int a,b,c,d;
199 a=fenshu1.getFenzi();
200 b=fenshu1.getFenmu();
201 c=fenshu2.getFenzi();
202 d=fenshu2.getFenmu();
203 int numera=a*c;
204 int deomina=b*d;
205 if(deomina<0) {
206 throw new RuntimeException("分母不能小于0");
207 }
208 int gcd=getMaxDiv(numera,deomina);
209 numera/=gcd;
210 deomina/=gcd;
211 FenShu fenshu3=new FenShu(numera,deomina);
212 return fenshu3;
213 }
214 public static FenShu fenShuDivi(FenShu fenshu1, FenShu fenshu2){
215 int a,b,c,d;
216 a=fenshu1.getFenzi();
217 b=fenshu1.getFenmu();
218 c=fenshu2.getFenmu();
219 d=fenshu2.getFenzi();
220 int numera=a*c;
221 int deomina=b*d;
222 if(deomina<0) {
223 numera = -1*numera;
224 deomina=-1*deomina;
225 }
226 int gcd=getMaxDiv(numera,deomina);
227 numera/=gcd;
228 deomina/=gcd;
229 FenShu fenshu3=new FenShu(numera,deomina);
230 return fenshu3;
231 }
232
233
234 /**
235 * 求最小公倍数
236 */
237
238 public static int getMinMul(int a,int b){
239
240 int c=a*b/getMaxDiv(a,b);
241 return c;
242 }
243
244 /**
245 * 求最大公约数
246 */
247 public static int getMaxDiv(int a,int b){
248 if (a<0)
249 a=java.lang.Math.abs(a);
250 if(b<0)
251 throw new RuntimeException("分母不能小于0");
252 while (b!=0){
253 int c=a%b;
254 a=b;
255 b=c;
256 }
257 return a;
258 }
259
260
261
262 }
工具类:
随机生成运算字符串 将分数类转成真分数的字符串形式
1 public class Util {
2 public static String trueFenShu(FenShu fs){
3 int abs= Math.abs(fs.getFenzi());
4 if (abs<fs.getFenmu())
5 return fs.getFenzi()+"/"+fs.getFenmu();
6 else if (abs==fs.getFenmu())
7 return String.valueOf(fs.getFenzi()/fs.getFenmu());
8 else {
9 int num=abs/fs.getFenmu();
10 int fenzi=abs%fs.getFenmu();
11 if (fenzi==0){
12 if (fs.getFenzi()>=0)
13 return num+"";
14 else
15 return "-"+num;
16 }
17 if (fs.getFenzi()>=0)
18 return num+"'"+fenzi+"/"+fs.getFenmu();
19 else
20 return "-"+num+"'"+fenzi+"/"+fs.getFenmu();
21 }
22 }
23
24
25 public static String calStringCreate(int r){
26 char []c={'+','-','*','/'};//操作符数组
27 Random random=new Random();
28 StringBuffer str=new StringBuffer();
29 int n= random.nextInt(3)+1;
30 int num=random.nextInt(r-1)+1;
31 str.append(num);
32 for (int i = 0; i <n ; i++) {//在1到3范围内随机个数的运算符
33 char c2=c[(int)(c.length* java.lang.Math.random())];//生成随机操作符
34 int num2=random.nextInt(r-1)+1;//生成大于0小于r的自然数
35 str.append(c2);
36 str.append(num2);
37 }
38 return str.toString();
39 }
40
41 public static void main(String[] args) {
42 calStringCreate(20);
43 }
44 }
分数类:
1 public class FenShu {
2 //整数int 分子 分母 真分数
3 private int fenzi;
4 private int fenmu;
5
6
7 public FenShu(int fenzi, int fenmu) {
8 if (fenmu<=0)
9 throw new RuntimeException("分母不能小于0");
10 this.fenzi = fenzi;
11 this.fenmu = fenmu;
12 }
13
14 public int getFenzi() {
15 return fenzi;
16 }
17
18 public int getFenmu() {
19 return fenmu;
20 }
21
22 public void setFenzi(int fenzi) {
23 this.fenzi = fenzi;
24 }
25
26 public void setFenmu(int fenmu) {
27 if (fenmu<=0)
28 throw new RuntimeException("分母不能小于0");
29 this.fenmu = fenmu;
30 }
31 }
四 小结
我们都希望把作业做好,但是可能习惯各做各的,难免缺少沟通,我自己的问题很多,同伴给予了很多帮助,希望多沟通,以后能够一起把更多的事情做好。
|
psp2.1 |
Personal Software Process Stages |
Time Senior Student(min) |
Time(min) |
|
Planning |
计划 |
200 |
270 |
|
· Estimate |
估计这个任务需要多少时间 |
500 |
550 |
|
Development |
开发 |
450 |
600 |
|
· Analysis |
需求分析 (包括学习新技术) |
10 |
20 |
|
· Design Spec |
生成设计文档 |
20 |
30 |
|
· Design Review |
设计复审 |
10 |
10 |
|
· Coding Standard |
代码规范 |
15 |
20 |
|
· Design |
具体设计 |
40 |
60 |
|
· Coding |
具体编码 |
300 |
500 |
|
· Code Review |
代码复审 |
15 |
60 |
|
· Test |
测试(自我测试,修改代码,提交修改) |
20 |
50 |
|
Reporting |
报告 |
60 |
50 |
|
· |
测试报告 |
60 |
60 |
|
· |
计算工作量 |
20 |
30 |
|
· |
并提出过程改进计划 |
30 |
20 |
来源:https://www.cnblogs.com/ricardoCYF/p/7712583.html




