模塊代碼地址
兩個地址是一樣的,為了方便,所以把測試的代碼很模塊代碼放在一起git了。
封裝階段
public class Core {
public static StringBuffer Suffix(StringBuffer infix){
Stack<Character> stack=new Stack <Character>();
StringBuffer suffix=new StringBuffer();
int i=0;
char tempchar=infix.charAt(i++);
char tempchar2=' ';
try {
while (tempchar!='='){
switch (tempchar){
case '(':
stack.push(tempchar);
tempchar=infix.charAt(i++);
break;
case ')':
while (stack.peek()!='('){
tempchar2=stack.pop();
suffix.append(tempchar2);
suffix.append(' ');
if (stack.empty()) {
break;
}
}
if (!stack.empty()) {
stack.pop();
}
tempchar=infix.charAt(i++);
break;
case '+':
case '-':
while (!stack.empty()&&stack.peek()!='('){
tempchar2=stack.pop();
suffix.append(tempchar2);
suffix.append(' ');
}
stack.push(tempchar);
tempchar=infix.charAt(i++);
break;
case '×':
case '*':
case '/':
case '÷':
Character ch=new Character(' ');
if (!stack.empty()) {
while((ch=stack.peek()).equals('×')||(ch=stack.peek()).equals('÷'))
{
tempchar2=stack.pop();
suffix.append(tempchar2);
suffix.append(' ');
if (stack.empty()) {
break;
}
}
}
stack.push(tempchar);
tempchar=infix.charAt(i++);
break;
case ' ':
tempchar=infix.charAt(i++);
break;
default:
while(tempchar<='9'&&tempchar>='0')
{
suffix.append(tempchar);
tempchar=infix.charAt(i++);
}
suffix.append(' ');
break;
}
}
while(!stack.empty())
{
tempchar2=stack.pop();
suffix.append(tempchar2);
suffix.append(' ');
}
suffix.append('\0');
} catch (Exception e) {
e.printStackTrace();
}
return suffix;
}
//根據后綴表達式計算結果(小數格式的String類型)
public static String Calculate(StringBuffer suffix){
int i=0;
char tempchar=suffix.charAt(i++);
double []answer=new double[20];
int top=0,d;
String Answer= null;
try {
while (tempchar!='\0'){
switch (tempchar){
case '+':
answer[top-1]=answer[top-1]+answer[top];
top--;
tempchar=suffix.charAt(i++);
break;
case '-':
answer[top-1]=answer[top-1]-answer[top];
top--;
tempchar=suffix.charAt(i++);
break;
case '*':
case '×':
answer[top-1]=answer[top-1]*answer[top];
top--;
tempchar=suffix.charAt(i++);
break;
case '/':
case '÷':
try {
if(answer[top]!=0)
answer[top-1]=answer[top-1]/answer[top];
else
{
System.out.println("\n\t除零錯誤!\n");
throw new InvalidExpression("無效的表達式");
}
} catch (InvalidExpression invalidExpression) {
invalidExpression.printStackTrace();
System.exit(0);
}
top--;
tempchar=suffix.charAt(i++);
break;
case ' ':
tempchar=suffix.charAt(i++);
break;
default:
d=0;
while(tempchar>='0'&&tempchar<='9')
{
d=10*d+tempchar-'0';//將數字字符轉化為對應的數值
tempchar=suffix.charAt(i++);
}
top++;
answer[top]=d;
break;
}
}
Answer = null;
if (top!=1){
throw new InvalidExpression("無效的表達式");
}
Double an=new Double(answer[top]);
Answer = new String(an.toString());
}catch (InvalidExpression invalidExpression) {
invalidExpression.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return Answer;
}
}
在封裝的過程中遇到的困難,以及是如何解決的:
對於封裝首先想到幾天前學習的軟件工程的知識,關於模塊的分解、抽象,恰好與本次的任務吻合,可以理論用於實踐。上圖是我畫的UML圖,在嘗試在學習中,算不上很規范,但能基本看懂大概的結構。
因為之前的代碼基於結構化編程,運用函數思想,所以在本次封裝過程中很好的將功能抽象出來,這次完成的雖然不是很完美,但后期可以通過老師的指導不斷改進。
單元測試階段
單元測試代碼 此處粘貼單元測試代碼(用一對 ``` 把代碼括起來)
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
*
* Method: Suffix(StringBuffer infix)
*
*/
@Test
public void testSuffix() throws Exception {
Core s=new Core();
s.Suffix(new StringBuffer("1+2+3="));
s.Suffix(new StringBuffer("1+2×3="));
s.Suffix(new StringBuffer("1+2÷3="));
s.Suffix(new StringBuffer("1÷2×3="));
s.Suffix(new StringBuffer("1÷+2×3="));
s.Suffix(new StringBuffer("1÷2*3="));
s.Suffix(new StringBuffer("1÷0×3="));
s.Suffix(new StringBuffer("1÷2×0="));
}
/**
*
* Method: Calculate(StringBuffer suffix)
*
*/
@Test
public void testCalculate() throws Exception {
Core s=new Core();
s.Calculate(new StringBuffer("1 2 + 3 +"));
s.Calculate(new StringBuffer("1 + 2 + 3 +"));
s.Calculate(new StringBuffer("1 2 +"));
s.Calculate(new StringBuffer("1 2 × 3 +"));
s.Calculate(new StringBuffer("1+ + 2 × 3 +"));
}
}
單元測試運行結果(截圖,含測試是否全部通過,代碼覆蓋率等)
在編寫單元測試代碼過程中遇到的困難,以及是如何解決的
按照老師提供的教程,一開始看不懂,需要自己慢慢琢磨,Junit包,不知道是哪個,IDEA上找到有好多個,后來自己在網上找一個發現可以用。
下面提供它的下載網址。
鏈接:http://pan.baidu.com/s/1o84pWQQ 密碼:1mda
別的大問題沒有,關於對Core的測試,計算表達式都是自己按照規則生成的,不會出現不規則的情況,
所以對於測試情況的全面性沒有更深一步進行。
感受(小結)
Junit很方便,測試的時候,不能以編程時的思維方式去看待問題,否則有些問題永遠測試不出來。
需要打破常規的思路,不能認為自己的代碼運行結果都是對的。