📄 calcface.java
字号:
/////// CalcFace.java ///////
import java.io.*;
//用户接口对象,该对象将用户输入报告给Calculator并显示结果
class CalcFace
{
public CalcFace(String k, int pr)
{ keys = k; prec = pr++;
nbuf = new StringBuffer(prec); // 定义字符串缓冲区nbuf
reset();
}
protected CalcFace() {}
public void setCalc(Calculator ca)
{ calc = ca; }
public void showStatus(String e)
{ prompt = ( e.length() == 0 )
? "Calc: "
: "Calc [" + e + "]: ";
}
public void showOp(char op) { }
public void errorInput(String str)
{ errMode = true; // 错误模式
active = str;
}
public void showNumber(String s)
{ System.out.println(prompt + s);
}
public void input()
throws IOException
{ int i;
while ( (i= inchar()) != -1 )
{ enter((char) i); }
}
protected void enter(char c)
{ if ( errMode )
{ if ( active.indexOf(c) == -1) return;
else errMode = false;
}
if ( keys.indexOf(c) != -1 ) // if c an operator
{ showOp(c);
if ( num ) // if num not null
calc.enterNumber(extractNumber(), c); // 调用calc的enterNumber方法
else
calc.enterOp(c); // 否则调用calc对象的enterOp方法只输入操作码
reset();
}
else if ( nump(c) && nbuf.length() < prec ) // if c a num 且在计算器精度范围内
{ num = true;
buildNumber(c);
}
}
protected boolean nump(char c)
{ return( c == '.' || Character.isDigit(c) ); }
protected String extractNumber() // 返回代表输入数的字符串
{ return (nbuf.length() == 0) ? "0"
: nbuf.toString();
}
protected void buildNumber(char c) // 将输入数存储到字符串缓冲区nbuf中
{ int i = nbuf.length();
if ( i == 0 && c == '0') return; // ignore leading zeros
if ( c == '.' ) // at most one decimal point
{ if ( ! before_point ) return;
else before_point = false;
}
nbuf.append(c); // to end of string buffer
}
protected void reset()
{ before_point = true;
nbuf.setLength(0);
num = false;
}
protected int inchar() // 从标准输入中取下一个字符
throws IOException
{ return System.in.read(); }
protected String prompt="Calc: ";
protected boolean errMode = false;
protected String active;
protected Calculator calc;
protected String keys; // keys recognized
protected StringBuffer nbuf; // buffer for input number
protected int prec; // max no of chars displayable
protected boolean before_point = true;
protected boolean num = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -