• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Java编写的计算器程序及源代码(2)

发布: 2007-7-14 21:19 | 作者: 佚名    | 来源: 网络转载     | 查看: 40次 | 进入软件测试论坛讨论

领测软件测试网 if (prekey == 5) {
if (key == 0) {
answerd += vard;
textAnswer.setText(df.format(answerd));
}
if (key == 1) {
answerd -= vard;
textAnswer.setText(df.format(answerd));
}
if (key == 2) {
answerd *= vard;
textAnswer.setText(df.format(answerd));
}
if (key == 3) {
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else {
answerd /= vard;
textAnswer.setText(df.format(answerd));
}
}
}
else {
vard = Double.parseDouble(textAnswer.getText());
if (key == 0) {
prekey = -1;
answerd += Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key == 1) {
prekey = -1;
answerd -= Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key == 2) {
prekey = -1;
answerd *= Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key == 3) {
prekey = -1;
if (Double.parseDouble(textAnswer.getText()) == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else {
answerd /= Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
}
}
prekey = 5;
}
//´%´操作,对第二个操作数除以100
if (temp == buttonMod && clickable == true) {
if (answerd == 0) {
String s = textAnswer.getText();
textAnswer.setText(s);
}
else {
boolean isDot = false;
for (int i = 0; i < textAnswer.getText().length(); i++)
if (´.´ == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
//如果是double数,除100
if (isDot == true) {
double dtemp = Double.parseDouble(textAnswer.getText());
dtemp = dtemp / 100.0;
textAnswer.setText(Double.toString(dtemp));
}
else {
//如果是int数但能被100整除,则去掉末尾两个零
if (Integer.parseInt(textAnswer.getText()) % 100 == 0) {
int itemp = Integer.parseInt(textAnswer.getText());
itemp /= 100;
textAnswer.setText(Integer.toString(itemp));
}
//如果是int数,但不能被100整除,则按double数处理
else {
double dtemp = Double.parseDouble(textAnswer.getText());
dtemp = dtemp / 100.0;
textAnswer.setText(Double.toString(dtemp));
}
}
}
}
//开根号运算
if (temp == buttonSqrt && clickable == true) {
String s = textAnswer.getText();
if (s.charAt(0) == ´-´) {
textAnswer.setText("负数不能开根号");
clickable = false;
}
else
textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.
parseDouble(textAnswer.getText()))));
}
//倒数运算
if (temp == buttonDao && clickable == true) {
if (textAnswer.getText().charAt(0) == ´0´ &&
textAnswer.getText().length() == 1) {
textAnswer.setText("零不能求倒数");
clickable = false;
}
else {
boolean isDec = true;
int i, j, k;
String s = Double.toString(1 / Double.parseDouble(textAnswer.getText()));
for (i = 0; i < s.length(); i++)
if (s.charAt(i) == ´.´)
break;
for (j = i + 1; j < s.length(); j++)
if (s.charAt(j) != ´0´) {
isDec = false;
break;
}
if (isDec == true) {
String stemp = "";
for (k = 0; k < i; k++)
stemp += s.charAt(k);
textAnswer.setText(stemp);
}
else
textAnswer.setText(s);
}
}
//按下´+/-´按钮时处理
if (temp == buttonAddAndSub && clickable == true) {
boolean isNumber = true;
String s = textAnswer.getText();
for (int i = 0; i < s.length(); i++)
if (! (s.charAt(i) >= ´0´ && s.charAt(i) <= ´9´ || s.charAt(i) == ´.´ ||
s.charAt(i) == ´-´)) {
isNumber = false;
break;
}
if (isNumber == true) {
//如果当前字符串首字母有´-´号,代表现在是个负数,再按下时,则将首符号去掉
if (s.charAt(0) == ´-´) {
textAnswer.setText("");
for (int i = 1; i < s.length(); i++) {
char a = s.charAt(i);
textAnswer.setText(textAnswer.getText() + a);
}
}
//如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
else
textAnswer.setText(´-´ + s);
}
}
//计算器有关内存操作
//´MC´的操作,将内存清0
if (temp == buttonMC && clickable == true) {
memoryd = memoryi = 0;
textMemory.setText("");
}
//´MS´的操作,将当前文本框内容保存入内存,显示´M´
if (temp == buttonMS && clickable == true) {
boolean isDot = false;
textMemory.setText(" M");
for (int i = 0; i < textAnswer.getText().length(); i++)
if (´.´ == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
//如果是double,则存入memoryd(double存储器)
if (isDot == true) {
memoryd = Double.parseDouble(textAnswer.getText());
memoryi = 0; //保证存储器中存放最新的值
}
//如果是int,则存入memoryi(int存储器)
else {
memoryi = Integer.parseInt(textAnswer.getText());
memoryd = 0; //保证存储器中存放最新的值
}
}
//´MR´的操作,将存储器中的信息输出
if (temp == buttonMR && clickable == true) {
if (memoryd != 0)
textAnswer.setText(Double.toString(memoryd));
if (memoryi != 0)
textAnswer.setText(Integer.toString(memoryi));
}
//´M+´的功能,将当前文本框里的数据和存储器中数据相加后,再存入存储器
if (temp == buttonMAdd && clickable == true) {
boolean isDot = false;
for (int i = 0; i < textAnswer.getText().length(); i++)
if (´.´ == textAnswer.getText().charAt(i)) {
isDot = true;
break;
}
if (memoryi != 0) { //存储中是一个int型数
if (isDot == false) //被加数是一个int型数
memoryi += Integer.parseInt(textAnswer.getText());
else { //被加数是一个double型数,则将int存储器中数传入double存储器与当前数相加,int存储器清零
memoryd = memoryi + Double.parseDouble(textAnswer.getText());
memoryi = 0;
}
}
else
memoryd += Double.parseDouble(textAnswer.getText());
}
//按下´Backspace´键,利用循环将当前字符串中的最后一个字母删除
if (temp == buttonBk && clickable == true) {
String s = textAnswer.getText();
textAnswer.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
textAnswer.setText(textAnswer.getText() + a);
}
}
//按下´CE´按钮,将当前文本框内数据清除
if (temp == buttonCe) {
textAnswer.setText("");
clickable = true;
}
//按下´C´按钮,文本框内数据清除,同时var,answer清0
if (temp == buttonC) {
vard = answerd = 0;
textAnswer.setText("");
clickable = true;
}
//按下´复制´菜单栏
if (temp == copyItem) {
copy = textAnswer.getText();
}
//按下´粘贴´菜单栏
if (temp == pasteItem) {
textAnswer.setText(copy);
}
if (temp == sItem) {
JOptionPane.showMessageDialog(panel, "当前是标准型计算器,\n科学型计算器有待更新。");
}
//按下´帮助主题´菜单栏
if (temp == topHelp) {
JOptionPane.showMessageDialog(panel, scrollHelp);
}
//按下´数字分组´菜单栏
if (temp == numberGroup) {
if (numberGroup.getText().compareTo(" 数字分组(I)") == 0)
numberGroup.setText("√数字分组(I)");
else
numberGroup.setText(" 数字分组(I)");
}
//按下´关于´菜单栏
if (temp == aboutCal) {
JOptionPane.showMessageDialog(panel, "计算器1.00版\n开发者:楼竞");
}
}
//输入中如果有操作非法,比如按下两次´+´,捕获异常
catch (Exception e) {
textAnswer.setText("操作非法");
clickable = false;
}
}
//主函数
public static void main(String args[]) {
new Calculator();
}
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网