資源簡介
該圖形化計算器用java編寫,實現了浮點數+、-、*、/、括號等功能。

代碼片段和文件信息
package?newcalculator;
import?java.util.Stack;
public?class?Calculate{
?Stack?wordOfAll=?new?Stack();
?Stack?wordOfOperate=?new?Stack();
?Stack?wordOfOperand=?new?Stack();
public??boolean?isNumber(char?ch){
if(ch>=‘0‘&&ch<=‘9‘)
return?true;
else?
return?false;
}
public??boolean?isOper(char?ch){
if(ch==‘(‘||ch==‘)‘||ch==‘+‘||ch==‘-‘||ch==‘*‘||ch==‘/‘)
return?true;
else?
return?false;
}
public??boolean?isNumber(String?str){
return?isNumber(str.charAt(0));
}
/*public??float?Char_To_Number(char?ch){
return?ch-‘0‘;//數字字符轉為數字
}
public??float?strToFlaot(String?strNumber){
float?fSum=0;
int?i=0;
float?d=10;
while(i {
fSum=fSum*10+Char_To_Number(strNumber.charAt(i));
i++;
}
i++;
while(i {
fSum+=Char_To_Number(strNumber.charAt(i))/d;
d*=10;
i++;
}
return?fSum;
}*/
protected??boolean?getAllWord(String?str){
String?word=new?String();
Stack?transitTack=new?Stack();
for(int?i=0;i
word+=str.charAt(i);
if(isNumber(str.charAt(i))){
char?ch;
boolean?isPointInclude=false;
while(i if(ch==‘.‘)
isPointInclude=true;
word+=ch;
}
if(i==str.length()-1&&(isNumber(ch=str.charAt(i)))??)
i++;
}
else
if(isOper(str.charAt(i))){
i++;
}
else
return?false;
try{
transitTack.push(word);
}catch(Exception?e)?{
System.out.println(e);
}
word=““;
}
wordOfAll.push(“#“);
while(!transitTack.isEmpty()){
wordOfAll.push(transitTack.pop());
}
return?true;
}
public??int?toIndex(char?ch){
if(ch==‘+‘)
return?0;
if(ch==‘-‘)
return?1;
if(ch==‘*‘)
return?2;
if(ch==‘/‘)
return?3;
if(ch==‘(‘)
return?4;
if(ch==‘)‘)
return?5;
if(ch==‘#‘)
return?6;
else?
return?-1;
}
public??int?compare(String?strFirstString?strSecond){
int??arr[][]={{1?1?-1?-1?-1?1?1}
??????{1?1?-1?-1?-1?1?1}
??????{1?1?1??1?-1?1?1}
??????{1?1?1?1?-1?1?1}
??????{-1?-1?-1?-1?-100}
??????{1?1?1?1?1?1?1}
??????{-1-1-1-1-1-10}};
char?chOne=?strFirst.charAt(0);
char?chTwo=strSecond.charAt(0);
return?arr[toIndex(chOne)][toIndex(chTwo)];
}
protected??String?calculate(String?str){
if(!getAllWord(str)){
return?“input?error!“;
}
String?word=new?String();
wordOfOperate.push(“#“);
while(wordOfAll.peek()!=“#“||wordOfOperate.peek()!=“#“){
word=wordOfAll.pop();
if(isNumber(word))
wordOfOperand.push(word);
else{
int?compareResult=compare(wordOfOperate.peek()word);
if
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????301??2012-03-25?12:29??Calculator\.classpath
?????文件????????386??2012-03-25?12:29??Calculator\.project
?????文件????????629??2012-04-09?19:55??Calculator\.settings\org.eclipse.jdt.core.prefs
?????文件???????4481??2012-06-05?11:54??Calculator\bin\newcalculator\Calculate.class
?????文件???????2980??2012-06-05?11:54??Calculator\bin\newcalculator\Calculator_Jfr
?????文件???????4599??2012-04-24?12:26??Calculator\src\newcalculator\Calculate.java
?????文件???????1736??2012-04-10?22:04??Calculator\src\newcalculator\Calculator_Jfr
?????目錄??????????0??2012-06-05?11:54??Calculator\bin\newcalculator
?????目錄??????????0??2012-03-25?17:24??Calculator\src\newcalculator
?????目錄??????????0??2012-03-25?12:29??Calculator\.settings
?????目錄??????????0??2012-06-05?11:54??Calculator\bin
?????目錄??????????0??2012-03-25?12:30??Calculator\src
?????目錄??????????0??2012-03-25?12:29??Calculator
-----------?---------??----------?-----??----
????????????????15112????????????????????13
評論
共有 條評論