实验二 结对编程第二阶段

一、实验目标:

1)体验敏捷开发中的两人合作。

2)进一步提高个人编程技巧与实践。

二 、实验内容:

1)根据以下问题描述,练习结对编程(pair programming)实践;

2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。

3)要求在结对编程工作期间,两人的角色至少切换 4 次;

4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。

三 、实验过程记录 

阶段二我和张丽通过QQ屏幕分享的方式进行讨论结对编程模块设计,并在网上查找资料进行代码设计。

1)程序总体设计

 

 

 2)结对编程过程

 实验二 结对编程第二阶段_第1张图片

 

3)运行结果:实验二 结对编程第二阶段_第2张图片

 实验二 结对编程第二阶段_第3张图片

附代码:

 

public class calculate {
private Map> priorityMap = new HashMap>();
private Stack optStack = new Stack();
private Stack numStack = new Stack();

public double calcualte1(String exp) {
StringTokenizer vn = new StringTokenizer(exp);
while (vn.hasMoreTokens()) {
String token = vn.nextToken();
transversion(token);
}
return numStack.pop();
}
public double calcualte(String exp) {
char[] strExp = exp.toCharArray();
for(char a: strExp){
transversion(""+a+"");
}
return numStack.pop();
}

 

private void transversion(String token) {
while (false == "=".equals(optStack.peek()) || false == token.equals("=")) {
if (true == judgenum(token)) {
numStack.push(Double.parseDouble(token));
break;

} else {
String priority = priority(optStack.peek(), token);
if ("<".equals(priority)) {
optStack.push(token);
break;
} else if ("=".equals(priority)) {
optStack.pop();
break;
} else {
double res = calculate(optStack.pop(), numStack.pop(), numStack.pop());
numStack.push(res);
}
}
}
}

 

private double calculate(String opt, double a1, double a2) {
if ("+".equals(opt)) {
return a2 + a1;
} else if ("-".equals(opt)) {
return a2 - a1;
} else if ("*".equals(opt)) {
return a2 * a1;
} else if ("/".equals(opt)) {
return a2 / a1;
} else {
throw new RuntimeException("unsupported operator:" + opt);
}
}

 

private boolean judgenum(String token) {
int LEN = token.length();
for (int ix = 0 ; ix < LEN ; ++ix) {
char ch = token.charAt(ix);
if (ch == '.') {
continue;
}
if (false == judgenum(ch)) {
return false;
}
}
return true;
}

 

private boolean judgenum(char ch) {
if (ch >= '0' && ch <= '9') {
return true;
}
return false;
}

 

public String priority(String op1, String op2) {
return priorityMap.get(op1).get(op2);
}

 

public calculate() {

 

optStack.push("=");
Map subMap = new HashMap();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("+", subMap);
// -
subMap = new HashMap();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("-", subMap);
// *
subMap = new HashMap();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("*", subMap);
// /
subMap = new HashMap();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("/", subMap);
// (
subMap = new HashMap();
subMap.put("+", "<");
subMap.put("-", "<");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", "=");
//subMap.put("#", ">");
priorityMap.put("(", subMap);
// )
subMap = new HashMap();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
//subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put(")", subMap);
// #
subMap = new HashMap();
subMap.put("+", "<");
subMap.put("-", "<");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
//subMap.put(")", ">");
subMap.put("=", "=");
priorityMap.put("=", subMap);
}
}

 

(2)

 

public class timu {
private int n;
private int range;

 

public int getN() {
return n;
}

 

public void setN(int n) {
this.n = n;
}

 

public int getRange() {
return range;
}

 

public void setRange(int range) {
this.range = range;
}

 

public void generate(int n, int range) {
char[] symbol = { '+', '-', '*', '/', '(' };
int op; // 操作数
try { 
File writename = new File("E:\\Java_szys\\szys.txt"); 
BufferedWriter out = new BufferedWriter(new FileWriter(writename)); 
for (int i = 0; i < n; i++) {

 

String eHead = "" + (i + 1) + ". ";
String e ="";
int exlength = (int) (Math.random() * 4 + 2);
int lbracket = 0;
int rbracket = 0;
int[] whe = new int[exlength]; 
for (int j = 0; j < exlength; j++) {
char s = symbol[(int) (Math.random() * 5)];
if (j == exlength - 1 && s == '(') { 
int count1 = lbracket;
if (lbracket == 0) {
e += "" + (int) (Math.random() * range) + "";
} else {
e += "" + (int) (Math.random() * range) + "";
for (int q = 0; q < count1; q++) {
e += ")";
lbracket--;
}
}
e += "=";
continue;
}
if (lbracket == 0) { 
if (s == '(') {
e = e + "" + s + "" + (int) (Math.random() * range)
+ "" + symbol[(int) (Math.random() * 4)] + "";

 

whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);

 

lbracket++;
rbracket++;
} else {
e = e + "" + (int) (Math.random() * range) + "" + s
+ "";
}
} else if (s != '(') {
int count2 = lbracket;
int count_match = 0;
for (int l = 0; l < count2; l++) {
if (whe[(rbracket-count2) + l ] == j) {
if (count_match == 0) {
e = e + "" + (int) (Math.random() * range)
+ ")";
count_match++;
lbracket--;
} else {
e += ')';
lbracket--;
}
}
}
if (count_match == 0) {
e = e + "" + (int) (Math.random() * range) + "" + s
+ "";
} else {
e += "" + s + "";
}
} else {
int count3 = lbracket;
int count = 0;
for (int m = 0; m < count3; m++) {

 

if (whe[rbracket-count3 + m] == j) {
if (count == 0) {
e = e + "" + (int) (Math.random() * range)
+ ")";
count++;
lbracket--;

 

} else {
e += ')';
lbracket--;

 

}

 

}

 

}
if (count == 0) {
e = e + "(" + (int) (Math.random() * range) + ""
+ symbol[(int) (Math.random() * 4)] + "";
whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);
lbracket++;
rbracket++;
} else {
e += "" + symbol[(int) (Math.random() * 4)] + "("
+ (int) (Math.random() * range) + ""
+ symbol[(int) (Math.random() * 4)] + "";
whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);
rbracket++;
lbracket++;
}
}

 

}
char[] strChar = e.toCharArray();
strChar[strChar.length - 1] = '=';
for(int g=0 ;gif(strChar[g]=='/'){
if(strChar[g+1]=='0'){
i=i-1;
System.out.print("非法表达式");
}

}
}
System.out.print(eHead);
System.out.println(strChar);
calculate ee = new calculate();
double result = ee.calcualte(e);
out.write(eHead+"答案:"+result+"\r\n"); 
out.flush();

 

}
out.close(); 
} catch (Exception e1) { 
e1.printStackTrace(); 

}

 

}

 

4)Github使用自己的远程仓库

实验二 结对编程第二阶段_第4张图片

 

实验二 结对编程第二阶段_第5张图片

 

 

 实验二 结对编程第二阶段_第6张图片

 

 4)github项目地址

实验二 结对编程第二阶段_第7张图片

 

 四、实验小结

Github使用自己的远程仓库时 git push origin master 总是推送不上去,git pull可以,实验需弄清楚git push和git pull关系,结对编程让我体会到了两个人协同编程的好处,和以往一个人编程不同。还需继续学习。

你可能感兴趣的:(实验二 结对编程第二阶段)