标签:练习 buffere return rac for 运行 实验 语言 ogr
一、实验目标:
1)体验敏捷开发中的两人合作。
2)进一步提高个人编程技巧与实践。
二 、实验内容:
1)根据以下问题描述,练习结对编程(pair programming)实践;
2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。
3)要求在结对编程工作期间,两人的角色至少切换 4 次;
4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。
三 、实验过程记录
1)总体设计
2) 结对编程过程
刚开始我们只是简单的讨论了一下大概的模块设计。然后我们在网上查找了相关资料后通过QQ屏幕分享的方式进行结对编程。
代码
(1)
public class calculate<optStack> {
private Map<String, Map<String, String>> priorityMap = new HashMap<String, Map<String, String>>();
private Stack<String> optStack = new Stack<String>();
private Stack<Double> numStack = new Stack<Double>();
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<String, String> subMap = new HashMap<String, String>();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("+", subMap);
// -
subMap = new HashMap<String, String>();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("-", subMap);
// *
subMap = new HashMap<String, String>();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("*", subMap);
// /
subMap = new HashMap<String, String>();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put("/", subMap);
// (
subMap = new HashMap<String, String>();
subMap.put("+", "<");
subMap.put("-", "<");
subMap.put("*", "<");
subMap.put("/", "<");
subMap.put("(", "<");
subMap.put(")", "=");
//subMap.put("#", ">");
priorityMap.put("(", subMap);
// )
subMap = new HashMap<String, String>();
subMap.put("+", ">");
subMap.put("-", ">");
subMap.put("*", ">");
subMap.put("/", ">");
//subMap.put("(", "<");
subMap.put(")", ">");
subMap.put("=", ">");
priorityMap.put(")", subMap);
// #
subMap = new HashMap<String, String>();
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 ;g<strChar.length;g++){
if(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();
}
}
}
3)运行结果
4)下图是我的github项目地址
四 实验总结
这次实验不仅学到了新的知识,也让我回顾了前面所学的git。在实验中刚开始也遇到了很多问题,像代码托管,以及程序设计等,虽然花了点时间,但两个人以及编程,也不觉得枯燥。而且我发现结对编程比自己一个人要好很多,因为两个人考虑问题相对一个人要更加全面,而且在解决问题方面也会有更多想法。
标签:练习 buffere return rac for 运行 实验 语言 ogr
原文地址:https://www.cnblogs.com/zeroli/p/12630421.html