这次作业是结对编程 由于还有其他事情 我就和小伙伴提前商量好 分工合作。 所谓合作就是共同努力完成任务于是我们就进行了角色扮演 首先进行计划 分析1. 0-10四则运算 进行加减乘除3.整理思路4.合作编写代码5.有我提出修改意见。6.调试代码7.完成作业 代码部分
import java.util.Stack;
public class Operate {
private Stack<Character> priStack = new Stack<Character>();
private Stack<Integer> numStack = new Stack<Integer>();;
public int caculate(String str) {
String temp;
StringBuffer tempNum = new StringBuffer();
StringBuffer string = new StringBuffer().append(str);
while (string.length() != 0) {
temp = string.substring(0, 1);
string.delete(0, 1);
if (!isNum(temp)) {
if (!"".equals(tempNum.toString())) {
int num = Integer.parseInt(tempNum.toString());
numStack.push(num);
tempNum.delete(0, tempNum.length());
}
while (ompare(temp.charAt(0)) && (!priStack.empty())) {
int a = (int) numStack.pop();
int b = (int) numStack.pop();
char ope = priStack.pop();
int result = 0;
switch (ope) {
case ‘+‘:
result = b + a;
numStack.push(result);
break;
case ‘-‘:
result = b - a;
numStack.push(result);
break;
case ‘*‘:
result = b * a;
numStack.push(result);
break;
case ‘/‘:
result = b / a;
numStack.push(result);
break;
}
}
if (temp.charAt(0) != ‘#‘) {
priStack.push(new Character(temp.charAt(0)));
if (temp.charAt(0) == ‘)‘) {
priStack.pop();
priStack.pop();
}
}
} else
tempNum = tempNum.append(temp);
}
return numStack.pop();
}
private boolean isNum(String temp) {
return temp.matches("[0-9]");
}
private boolean compare(char str) {
if (priStack.empty()) {
return true;
}
char last = (char) priStack.lastElement();
if (last == ‘(‘) {
return true;
}
switch (str) {
case ‘#‘:
return false;
case ‘(‘:
return true;
case ‘)‘:
return false;
case ‘*‘: {
if (last == ‘+‘ || last == ‘-‘)
return true;
else
return false;
}
case ‘/‘: {
if (last == ‘+‘ || last == ‘-‘)
return true;
else
return false;
}
case ‘+‘:
return false;
case ‘-‘:
return false;
}
return true;
}
public static void main(String args[]) {
Operate operate = new Operate
int t = operate.caculate("(3+4*(4*10-10/2)#");
System.out.println(t);
}
}
总结 合作很重要 分工同样很重要 分工过程中会出现各种分歧 意见不同 想法不同都会出现不同结果。但是对于合作就要有求两个人有共同目标才能实现其真正的意义通过这次结对编程我体会
自己的不足自己的固执导致代码的很多错误最后在小伙伴的细心劝说下改正过来。我的小伙伴:吕明霞博客名imxhappy
原文:http://www.cnblogs.com/WANGDI1995/p/4887623.html