结对编程(四则运算)
结对伙伴:刘芳芳、于淼
主要用java实现编码
内容:给小学生出题的四则运算
四种运算
1-加法
2-减法
3-乘法
4-除法
0-退出
四则运算PSP
类别 |
开始时间 |
结束时间 |
间隔时间 |
累计净时间 |
四则运算分析 |
7:30 |
8:00 |
0 |
30分 |
编码 |
8:00 |
10:43 |
0 |
2小时43分 |
总结 |
10:43 |
10:50 |
0 |
7分 |
补充代码函数进度

代码如下:
1 package arrays.myArray;
2 import java.util.Scanner;
3 public class SortObjec {
4 private static int intercePosition = 0; // 记录单个运算数据的长度
5 private static int[] intercePositionIndex = null; // 记录“(”的下标
6 private static int[] intercePositionEnd = null; // 记录“)”的下标
7 public static void main(String[] args) {
8 Scanner input = new Scanner(System.in);
9 do
10 {
11 System.out.println("请输入你要计算的字符串(注意:只能输入数字和加,减,乘除符号;输入完毕后,请直接回车):");
12 String numberString = input.next().trim();
13 // 判断输入的运算字符串是否符合规定
14 if (ispassString(numberString) == false)
15 {
16 System.out.println("您输入的计算字符串有误,请正确输入!");
17 }
18 else
19 {
20 // 计算结果返回
21 System.out.println(interceResult(numberString));
22 }
23 }
24 while (true);
25 }
26
27 // 判断是否有带括号的运算字符串存在
28 private static String interceResult(String str)
29 {
30 String result = str;
31 char[] numberString = str.toCharArray(); // 1+2+(1*2+1-1*2+5)+2+(1+5+9+10-11)+1*5/2+3
32 // 1+8-9+(1*8/2-5+(1+2+8))+4/5*8/3*2
33 int IndexStart = 0; // 记录“(”的实际数量
34 int EndStart = 0; // 记录“)”的实际数量
35 for (int i = 0; i < numberString.length; i++)
36 {
37 if ('(' == numberString[i])
38 {
39 // 记录最后一个正括号的位置
40 IndexStart = i;
41 }
42 if (')' == numberString[i])
43 {
44 // 记录反括号的最初始下标的位置
45 EndStart = i;
46
47 // 截取最里面一个括号里的运算字符串
48 result = result.substring(IndexStart + 1, EndStart);
49
50 // 截取括号的运算字符串进行运算,生成新的运算字符串
51 result = str.substring(0, IndexStart)
52 + interceptOperation(result, '*', '/')
53 + str.substring(EndStart + 1, str.length());
54
55 // 回调执行,其它小括号的运算字符串
56 return interceResult(result);
57 }
58 if (i == numberString.length - 1)
59 if (EndStart == 0)
60 break;
61 }
62 // 不存在括号了,再进行混合运算
63 result = interceptOperation(str, '*', '/');
64 return result;
65 }
66
67 // 不带括号的四则运算
68 private static String interceptOperation(String operationNumber, char a,char b)
69 {
70 String mess = operationNumber;
71 char[] stringOperation = mess.toCharArray();
72
73 // 循环遍历运算字符串,并做相应的运算
74 for (int i = 0; i < stringOperation.length; i++) {
75
76 // 判断运算符所在的索引
77 if (stringOperation[i] == a || stringOperation[i] == b) {
78 if (i != 0)
79 {
80 // 运算符前的第一个数
81 double num1 = interceptNumIndex(mess.substring(0, i));
82
83 // 记录第一个数据的长度
84 int frontPosition = intercePosition;
85
86 // 运算符前的第二个数
87 double num2 = interceptNumEnd(mess.substring(i + 1,
88 stringOperation.length));
89
90 // 记录第二个数据的长度
91 int backPosition = intercePosition;
92
93 // 算完乘除,将结果替换到原来运算的位置,得到新的运算字符串
94 String IndexMess = mess.substring(0, i - frontPosition + 1);
95 String IndexResult = "";
96
97 // 判断是否运算到最后的结果了
98 if (IndexMess.indexOf('+') == -1
99 && IndexMess.indexOf('*') == -1
100 && IndexMess.indexOf('/') == -1
101 && IndexMess.lastIndexOf('-') == -1)
102 IndexMess = "";
103 if (IndexMess != "")
104 IndexResult = IndexMess.lastIndexOf('-') == IndexMess
105 .length() - 1 ? IndexMess.substring(0, i
106 - frontPosition) : IndexMess;
107
108 // 组装新的运算字符串
109 mess = IndexResult// mess.substring(0,i-frontPosition+1)
110 + reslutString("" + stringOperation[i], num1, num2)
111 + mess.substring(i + backPosition + 1);
112 // 0.111/1212/2/2/2/2/2/2/2
113 if (mess.lastIndexOf('-') == 0 && mess.indexOf('+') == -1
114 && mess.indexOf('*') == -1
115 && mess.indexOf('/') == -1) {
116 break;
117 }
118 // 回调,继续运算
119 return interceptOperation(mess, a, b);// 1+7-5+89/3+4-6*8/2+4-6
120 }
121 else
122 continue;
123 }
124 if (i == stringOperation.length - 1)
125 {
126 // 递归出口,判断是否还有运算字符串在
127 if (mess.indexOf('+') != -1 || mess.indexOf('-') != -1)
128 return interceptOperation(mess, '+', '-');
129 break;
130 }
131 }
132 return mess;
133 }
134
135 // 截取第二个数
136 private static double interceptNumEnd(String str)
137 {
138 double a = 0;
139 int InrerceIndex = 0;
140 char[] stringOperation = str.toCharArray();
141 boolean ispas = false; // 记录是否为负数
142 for (int i = 0; i < stringOperation.length; i++)
143 {
144 switch (stringOperation[i]) {
145 case '*':
146 case '/':
147 case '+':
148 case '-':
149 InrerceIndex = i;
150 if (i != 0) // 判断该数是否为负数
151 ispas = true;
152 break;
153 default:
154 break;
155 }
156 if (ispas)
157 break;
158 }
159 // 判断此数据是否在运算字符串的最后一位
160 if (InrerceIndex == 0) {
161 a = Double.parseDouble(str);
162 intercePosition = str.length();
163 if (ispas)
164 intercePosition++;
165
166 }
167 else
168 {
169 a = Double.parseDouble(str.substring(0, InrerceIndex));
170 // 记录数据的真实长度
171 intercePosition = str.substring(0, InrerceIndex).length();
172 }
173 return a;
174 }
175
176 // 截取第一个数
177 private static double interceptNumIndex(String str)
178 {
179 double a = 0; // 记录数据
180 int InrerceIndex = 0; // 记录运算符的位置
181 boolean temp = false; // 记录数据前运算符的状态
182 char[] stringOperation = str.toCharArray();
183 for (int i = stringOperation.length - 1; i >= 0; i--)
184 {
185 switch (stringOperation[i]) {
186 case '*':
187 case '/':
188
189 case '+':
190 case '-':
191 InrerceIndex = i;
192 temp = true;
193 break;
194 default:
195 break;
196 }
197 if (temp)
198 break;
199 }
200 // 判断此数据是否在运算字符串的第一位
201 if (InrerceIndex == 0) {
202 a = Double.parseDouble(str);
203 intercePosition = str.length();
204 // if(temp)
205 // intercePosition++;
206 }
207 else
208 {
209 a = Double.parseDouble(str.substring(InrerceIndex, str.length()));
210 // 记录数据的真实长度
211 intercePosition = str.substring(InrerceIndex, str.length())
212 .length();
213 }
214 return a;
215 }
216
217 // 计算结果
218 private static double reslutString(String operation, double num1,double num2)
219 {
220 double sumResult = 0;
221 if (operation.equals("*"))
222 sumResult = num1 * num2;
223 if (operation.equals("-"))
224 sumResult = num1 - num2;
225 if (operation.equals("/"))
226 sumResult = num1 / num2;
227 if (operation.equals("+"))
228 sumResult = num1 + num2;
229 return sumResult;
230 }
231
232 // 判断是否正确输入运算方式
233 private static boolean ispassString(String messString)
234 {
235 boolean ispass = false;
236 boolean operationIspass = true; // 记录被除数的状态
237 int ai = 0; // 记录是否有运算符号的存在
238 char[] IsString = messString.toCharArray();
239 int num1 = 0;
240 int num2 = 0;
241 for (int i = 0; i < IsString.length; i++)
242 {
243 // 记录有几对小括号的存在
244 if ('(' == IsString[i])
245 num1++;
246 if (')' == IsString[i])
247 num2++;
248
249 // 判断除数是否为零
250 if ('/' == IsString[i] && IsString[i + 1] == '0')
251 operationIspass = false;
252
253 // 判断是否输入了运算符合
254 if (IsString[i] == '+' || IsString[i] == '-' || IsString[i] == '*'
255 || IsString[i] == '/')
256 ai++;
257
258 if (i == IsString.length - 1)
259 if (ai == 0)
260 num2++;
261 }
262 if (operationIspass)
263 if (num1 == num2)
264 ispass = true;
265 return ispass;
266 }
267 }
package arrays.myArray;
import java.util.Scanner;
public class SortObjec {
private static int intercePosition = 0; // 记录单个运算数据的长度
private static int[] intercePositionIndex = null; // 记录“(”的下标
private static int[] intercePositionEnd = null; // 记录“)”的下标
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
do {
System.out.println("请输入你要计算的字符串(注意:只能输入数字和加,减,乘除符号;输入完毕后,请直接回车):");
String numberString = input.next().trim();
// 判断输入的运算字符串是否符合规定
if (ispassString(numberString) == false) {
System.out.println("您输入的计算字符串有误,请正确输入!");
} else {
// 计算结果返回
System.out.println(interceResult(numberString));
}
} while (true);
}
// 判断是否有带括号的运算字符串存在
private static String interceResult(String str) {
String result = str;
char[] numberString = str.toCharArray(); // 1+2+(1*2+1-1*2+5)+2+(1+5+9+10-11)+1*5/2+3
// 1+8-9+(1*8/2-5+(1+2+8))+4/5*8/3*2
int IndexStart = 0; // 记录“(”的实际数量
int EndStart = 0; // 记录“)”的实际数量
for (int i = 0; i < numberString.length; i++) {
if ('(' == numberString[i]) {
// 记录最后一个正括号的位置
IndexStart = i;
}
if (')' == numberString[i]) {
// 记录反括号的最初始下标的位置
EndStart = i;
// 截取最里面一个括号里的运算字符串
result = result.substring(IndexStart + 1, EndStart);
// 截取括号的运算字符串进行运算,生成新的运算字符串
result = str.substring(0, IndexStart)
+ interceptOperation(result, '*', '/')
+ str.substring(EndStart + 1, str.length());
// 回调执行,其它小括号的运算字符串
return interceResult(result);
}
if (i == numberString.length - 1)
if (EndStart == 0)
break;
}
// 不存在括号了,再进行混合运算
result = interceptOperation(str, '*', '/');
return result;
}
// 不带括号的四则运算
private static String interceptOperation(String operationNumber, char a,
char b) {
String mess = operationNumber;
char[] stringOperation = mess.toCharArray();
// 循环遍历运算字符串,并做相应的运算
for (int i = 0; i < stringOperation.length; i++) {
// 判断运算符所在的索引
if (stringOperation[i] == a || stringOperation[i] == b) {
if (i != 0) {
// 运算符前的第一个数
double num1 = interceptNumIndex(mess.substring(0, i));
// 记录第一个数据的长度
int frontPosition = intercePosition;
// 运算符前的第二个数
double num2 = interceptNumEnd(mess.substring(i + 1,
stringOperation.length));
// 记录第二个数据的长度
int backPosition = intercePosition;
// 算完乘除,将结果替换到原来运算的位置,得到新的运算字符串
String IndexMess = mess.substring(0, i - frontPosition + 1);
String IndexResult = "";
// 判断是否运算到最后的结果了
if (IndexMess.indexOf('+') == -1
&& IndexMess.indexOf('*') == -1
&& IndexMess.indexOf('/') == -1
&& IndexMess.lastIndexOf('-') == -1)
IndexMess = "";
if (IndexMess != "")
IndexResult = IndexMess.lastIndexOf('-') == IndexMess
.length() - 1 ? IndexMess.substring(0, i
- frontPosition) : IndexMess;
// 组装新的运算字符串
mess = IndexResult// mess.substring(0,i-frontPosition+1)
+ reslutString("" + stringOperation[i], num1, num2)
+ mess.substring(i + backPosition + 1);
// 0.111/1212/2/2/2/2/2/2/2
if (mess.lastIndexOf('-') == 0 && mess.indexOf('+') == -1
&& mess.indexOf('*') == -1
&& mess.indexOf('/') == -1) {
break;
}
// 回调,继续运算
return interceptOperation(mess, a, b);// 1+7-5+89/3+4-6*8/2+4-6
} else
continue;
}
if (i == stringOperation.length - 1) {
// 递归出口,判断是否还有运算字符串在
if (mess.indexOf('+') != -1 || mess.indexOf('-') != -1)
return interceptOperation(mess, '+', '-');
break;
}
}
return mess;
}
// 截取第二个数
private static double interceptNumEnd(String str) {
double a = 0;
int InrerceIndex = 0;
char[] stringOperation = str.toCharArray();
boolean ispas = false; // 记录是否为负数
for (int i = 0; i < stringOperation.length; i++) {
switch (stringOperation[i]) {
case '*':
case '/':
case '+':
case '-':
InrerceIndex = i;
if (i != 0) // 判断该数是否为负数
ispas = true;
break;
default:
break;
}
if (ispas)
break;
}
// 判断此数据是否在运算字符串的最后一位
if (InrerceIndex == 0) {
a = Double.parseDouble(str);
intercePosition = str.length();
if (ispas)
intercePosition++;
} else {
a = Double.parseDouble(str.substring(0, InrerceIndex));
// 记录数据的真实长度
intercePosition = str.substring(0, InrerceIndex).length();
}
return a;
}
// 截取第一个数
private static double interceptNumIndex(String str) {
double a = 0; // 记录数据
int InrerceIndex = 0; // 记录运算符的位置
boolean temp = false; // 记录数据前运算符的状态
char[] stringOperation = str.toCharArray();
for (int i = stringOperation.length - 1; i >= 0; i--) {
switch (stringOperation[i]) {
case '*':
case '/':
case '+':
case '-':
InrerceIndex = i;
temp = true;
break;
default:
break;
}
if (temp)
break;
}
// 判断此数据是否在运算字符串的第一位
if (InrerceIndex == 0) {
a = Double.parseDouble(str);
intercePosition = str.length();
// if(temp)
// intercePosition++;
} else {
a = Double.parseDouble(str.substring(InrerceIndex, str.length()));
// 记录数据的真实长度
intercePosition = str.substring(InrerceIndex, str.length())
.length();
}
return a;
}
// 计算结果
private static double reslutString(String operation, double num1,
double num2) {
double sumResult = 0;
if (operation.equals("*"))
sumResult = num1 * num2;
if (operation.equals("-"))
sumResult = num1 - num2;
if (operation.equals("/"))
sumResult = num1 / num2;
if (operation.equals("+"))
sumResult = num1 + num2;
return sumResult;
}
// 判断是否正确输入运算方式
private static boolean ispassString(String messString) {
boolean ispass = false;
boolean operationIspass = true; // 记录被除数的状态
int ai = 0; // 记录是否有运算符号的存在
char[] IsString = messString.toCharArray();
int num1 = 0;
int num2 = 0;
for (int i = 0; i < IsString.length; i++) {
// 记录有几对小括号的存在
if ('(' == IsString[i])
num1++;
if (')' == IsString[i])
num2++;
// 判断除数是否为零
if ('/' == IsString[i] && IsString[i + 1] == '0')
operationIspass = false;
// 判断是否输入了运算符合
if (IsString[i] == '+' || IsString[i] == '-' || IsString[i] == '*'
|| IsString[i] == '/')
ai++;
if (i == IsString.length - 1)
if (ai == 0)
num2++;
}
if (operationIspass)
if (num1 == num2)
ispass = true;
return ispass;
}
}

结对编程体会:
结对编程是两个人在一起完成四则运算项目,我自己能力不足,在一起商量的过程中发现对方很多优点是我不及的,那么也让我在这个过程中向自己的小伙伴学习了很多,
她熟悉Java,而我只是知道一点点,在Java这一面我可以向她请教。两个人在一起可以有商量的去做项目,两个人可以互通想法,这样每个人都有了两个idea,结对编程很好,
让我看到自己的不足,并且改正。