📄 calculate.java
字号:
/*
* 创建日期 2006-9-9
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package calculator;
/**
* @author
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Calculate {
private String infix; // 四则运算表达式(中缀)
// 构造函数
public Calculate(String s){
infix = s;
}
//判断有误的输入
int Match(String exprString){
int leng = exprString.length(); //所判断字符串长度
char[] exp = new char[leng]; //数组exp用来存储exprString各字符
for(int i = 0; i < leng; i++){
exp[i] = exprString.charAt(i);
}
char[] st = new char[leng]; //定义栈st来存放小括号
int top = -1, i = 0, tag = 1; //定义栈顶top和标签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){
// 判断首字符是否非法(操作数未考虑到正负)
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;
}
// 判断 + - * / .是否有连续情况
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;//***连续
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] == ')'){ // 遇到')',若栈顶是'(',则继续处理,否则以不配对返回
if((top >= 0) && (st[top] == '(')) top --;
else{
tag = 0; //***小括号不对称
break;
}
}
i ++;
}
if(top >= 0) // 若栈不为空,则不配对
tag = 0;
return (tag); //正常情况,tag为1
}
public String getResult(){
String resuStr = ""; // 表示结果的字符串,为正确结果或者是其他信息
String postfix = ""; // 后缀
char[] operators = new char[infix.length()]; // 定义用于存放操作符的栈
int top = -1; // 栈顶
int matc = Match(infix);
if(matc == 3){
resuStr = "首个字符有误.";
}else if(matc == 4){
resuStr = "出现了连续的+-/*.!";
}else if(matc == 2){
resuStr = "您输入了非法字符!";
}else if(matc == 0){
resuStr = "括号不配对.";
}else if(matc == 5){
resuStr = "末尾字符出错.";
}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){ // 栈不为空时
char c = operators[top--]; // 出栈
if(c == '('){
operators[++top] = c; // 遇到'(',将其重新入栈
break;
}else{
postfix = postfix + c; // 添加 c操作符到后缀中参与运算
}
}
operators[++top] = oper; // 当前操作符入栈
postfix += " ";
break;
case '*':
case '/':
while(top >= 0){
char c = operators[top--];
if(c == '('){
operators[++top] = c;
break;
}else{
if(c == '+' || c == '-'){// 遇到加减号,先进行乘除运算,故先将加减号压入栈
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--];
}// 得到的是一条无括号的后缀表达式
// 计算后缀表达式
double[] operands = new double[postfix.length()];// 定义用来存放操作数的栈
double x, y, z;
top = -1;
String operand = ""; // 定义操作数
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); // 完整操作数入栈
operand = "";
}
if(c == '+' || c == '-' || c == '*' || c == '/'){
if(operand != ""){
operands[++top] = Double.parseDouble(operand) ;
operand = "";
}
y = operands[top--]; // 弹出双目运算符的第二操作数
x = operands[top--]; // 弹出双目运算符的第一操作数
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; // 中间结果再次入栈,其位置为上述x的位置
}
}
z = operands[top--]; // 弹出最终结果
resuStr = Double.toString(z);
}
return resuStr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -