?? calculate.java
字號(hào):
/*
* 創(chuàng)建日期 2006-9-9
*
* TODO 要更改此生成的文件的模板,請(qǐng)轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
package calculator;
/**
* @author
*
* TODO 要更改此生成的類型注釋的模板,請(qǐng)轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
public class Calculate {
private String infix; // 四則運(yùn)算表達(dá)式(中綴)
// 構(gòu)造函數(shù)
public Calculate(String s){
infix = s;
}
//判斷有誤的輸入
int Match(String exprString){
int leng = exprString.length(); //所判斷字符串長(zhǎng)度
char[] exp = new char[leng]; //數(shù)組exp用來存儲(chǔ)exprString各字符
for(int i = 0; i < leng; i++){
exp[i] = exprString.charAt(i);
}
char[] st = new char[leng]; //定義棧st來存放小括號(hào)
int top = -1, i = 0, tag = 1; //定義棧頂top和標(biāo)簽tag
// 判斷式子中是否含有操作符
for(int k = 0; k < leng; k ++){
if(exp[k] == '*'||exp[k] == '/'||exp[k] == '+'||exp[k] == '-')
break;
else if(k == leng - 1)
tag = 6;
}
while(i < leng && tag ==1){
// 判斷首字符是否非法(操作數(shù)未考慮到正負(fù))
if(exp[0] == '*'||exp[0] == '/'||exp[0] == '.'||exp[0] == '+'||exp[0] == '-'){
tag = 3; //***首字符非法
break;
}
// 判斷末尾字符是否非法
if(exp[leng - 1] == '*'||exp[leng - 1] == '/'||exp[leng - 1] == '.'
||exp[leng - 1] == '+'||exp[leng - 1] == '-'){
tag = 5; //***末尾字符非法
break;
}
// 判斷 + - * / .是否有連續(xù)情況
if(exp[i] == '*'||exp[i] == '/'||exp[i] == '.'||exp[i] == '+'||exp[i] == '-'){
if(i > 0){
i --;
if(exp[i] == '*'||exp[i] == '/'||exp[i] == '.'||exp[i] == '+'||exp[i] == '-'){
tag = 4;//***連續(xù)
break;
}else{
i ++;
}
}
}
// 遇到非法字符
if(!(exp[i] == '1'|| exp[i] == '2'|| exp[i] == '3'|| exp[i] == '4'|| exp[i] == '5'|| exp[i] == '6'
|| exp[i] == '7'|| exp[i] == '8'|| exp[i] == '9'|| exp[i] == '0'|| exp[i] == '+'|| exp[i] == '-'
|| exp[i] == '*'|| exp[i] == '/'|| exp[i] == '('|| exp[i] == ')'|| exp[i] == '.'|| exp[i] == ' ')){
tag = 2; //***非法字符
break;
}
if(exp[i] == '('){ // 遇到'(',將其入棧
top ++;
st[top] = exp[i];
}
if(exp[i] == ')'){ // 遇到')',若棧頂是'(',則繼續(xù)處理,否則以不配對(duì)返回
if((top >= 0) && (st[top] == '(')) top --;
else{
tag = 0; //***小括號(hào)不對(duì)稱
break;
}
}
i ++;
}
if(top >= 0) // 若棧不為空,則不配對(duì)
tag = 0;
return (tag); //正常情況,tag為1
}
public String getResult(){
String resuStr = ""; // 表示結(jié)果的字符串,為正確結(jié)果或者是其他信息
String postfix = ""; // 后綴
char[] operators = new char[infix.length()]; // 定義用于存放操作符的棧
int top = -1; // 棧頂
int matc = Match(infix);
if(matc == 3){
resuStr = "首個(gè)字符有誤.";
}else if(matc == 4){
resuStr = "出現(xiàn)了連續(xù)的+-/*.!";
}else if(matc == 2){
resuStr = "您輸入了非法字符!";
}else if(matc == 0){
resuStr = "括號(hào)不配對(duì).";
}else if(matc == 5){
resuStr = "末尾字符出錯(cuò).";
}else if(matc == 6){
resuStr = "式子中缺少操作符.";
}else{
// 遍歷中綴
for(int i = 0; i < infix.length(); i ++){
char oper = infix.charAt(i);
switch(oper)
{
case ' ':// 忽略空格
break;
case '+':// 操作符
case '-':
while(top >= 0){ // 棧不為空時(shí)
char c = operators[top--]; // 出棧
if(c == '('){
operators[++top] = c; // 遇到'(',將其重新入棧
break;
}else{
postfix = postfix + c; // 添加 c操作符到后綴中參與運(yùn)算
}
}
operators[++top] = oper; // 當(dāng)前操作符入棧
postfix += " ";
break;
case '*':
case '/':
while(top >= 0){
char c = operators[top--];
if(c == '('){
operators[++top] = c;
break;
}else{
if(c == '+' || c == '-'){// 遇到加減號(hào),先進(jìn)行乘除運(yùn)算,故先將加減號(hào)壓入棧
operators[++top] = c;
break;
}else{
postfix = postfix + c;
}
}
}
operators[++top] = oper;
postfix += " ";
break;
case '(':
operators[++top] = oper;
postfix += " ";
break;
case ')':
while(top >= 0){
char c = operators[top--];
if(c == '('){ // 由于遇到')','('此后不再有效,故不需重新入棧
break;
}else{
postfix = postfix + c;
}
}
postfix += " ";
break;
default:
postfix = postfix + oper;
break;
}
}
while(top >= 0){
postfix = postfix + operators[top--];
}// 得到的是一條無括號(hào)的后綴表達(dá)式
// 計(jì)算后綴表達(dá)式
double[] operands = new double[postfix.length()];// 定義用來存放操作數(shù)的棧
double x, y, z;
top = -1;
String operand = ""; // 定義操作數(shù)
for(int j = 0; j < postfix.length(); j ++){
char c = postfix.charAt(j);
if((c >= '0' && c <= '9') || c == '.'){
operand += c;
}
if(c == ' ' && operand != ""){
operands[++top] = Double.parseDouble(operand); // 完整操作數(shù)入棧
operand = "";
}
if(c == '+' || c == '-' || c == '*' || c == '/'){
if(operand != ""){
operands[++top] = Double.parseDouble(operand) ;
operand = "";
}
y = operands[top--]; // 彈出雙目運(yùn)算符的第二操作數(shù)
x = operands[top--]; // 彈出雙目運(yùn)算符的第一操作數(shù)
switch(c)
{
case '+' :
z = x + y;
break;
case '-' :
z = x - y;
break;
case '*' :
z = x * y;
break;
case '/' :
z = x / y;
break;
default :
z = 0;
break;
}
operands[++top] = z; // 中間結(jié)果再次入棧,其位置為上述x的位置
}
}
z = operands[top--]; // 彈出最終結(jié)果
resuStr = Double.toString(z);
}
return resuStr;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -