JAVA 计算器(外加期末报告) - CoderMAX | My Blog

这个学期的期末,因为之前没有写过JAVA的程序,所以有不妥之处欢迎提出~


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
public class Calculator extends  implements 
{
   private boolean ifAction;
   private double ansPre;
   private  ansStr = "";
   private int sign = 0; // 1: + 2: - 3: x 4: /
   private boolean ifDot = true;
   private  textField;
   private  fnt = new ("Monospace", .PLAIN, 26);
   private  ButtonPanel = new ();
   private  TextPanel = new ();
   private []  = new [20];
   private  mb = new ();
   private  fileMenu = new ("文件");
   private  aboutMenu = new ("关于");
   private [] filem = {new ("退出")};
   private [] aboutm = {new ("关于作者")};
    name[] = {"Bksp", "CE", "Clr", "+/-",
               " 7 ", " 8 ", " 9 ", " / ",
              " 4 ", " 5 ", " 6 ", " x ",
               " 1 ", " 2 ", " 3 ", " - ",
               " 0 ", " . ", " = ", " + "};
   public Calculator() {
        super("计算器");
        fileMenu.add(filem[0]);
        aboutMenu.add(aboutm[0]);
    filem[0].addActionListener(this);
    aboutm[0].addActionListener(this);
    mb.add(fileMenu);
    mb.add(aboutMenu);
    setJMenuBar(mb);
    textField = new (15);
    textField.setText("0");
    textField.setEditable(false);
    textField.setFont(fnt);
    textField.setBackground(.white);
    textField.setHorizontalAlignment(.RIGHT);
    TextPanel.add(textField);
    for (int i = 0; i < name.length; i++) {
         [i] = new (name[i]);
         [i].addActionListener(this);
    }
    setLayout(new (10, 10));
    add(TextPanel, .NORTH);
    TextPanel.setBorder(.createEmptyBorder(20,10,10,10));
    for (int i = 0; i < name.length; i++) {
         ButtonPanel.add([i]);
    }
    ButtonPanel.setBorder(.createEmptyBorder(0,10,30,10));
    add(ButtonPanel, .CENTER);
    ButtonPanel.setLayout(new (5, 4, 10, 10));
 }
 public void initAll() {
    ansStr = "";
    sign = 0;
    ifDot = true;
    ifAction = false;
    refresh();
 }
 public void init() {
    ansStr = "";
    ifDot = true;
    refresh();
 }
 public void refresh() {
    if (ansStr.length() == 0) {
          textField.setText("0");
    } else {
          textField.setText(ansStr);
    }
 }
 public void cal() {
    if (sign == 1) {
          ansPre = ansPre + .parseDouble(ansStr);
    } else if (sign == 2) {
          ansPre = ansPre - .parseDouble(ansStr);
    } else if (sign == 3) {
          ansPre = ansPre * .parseDouble(ansStr);
    } else if (sign == 4) {
          ansPre = ansPre / .parseDouble(ansStr);
    }
     bd = new (ansPre);
    bd=bd.setScale(13, .ROUND_HALF_UP);
    ansPre = bd.doubleValue();
    if (sign != 0) {
          if ((ansPre - (int)ansPre == 0)) {
                ansStr = "" + (int)ansPre;
          } else {
                ansStr = "" + ansPre;
          }
    }
    sign = 0;
 }
 
 public void actionPerformed ( e) {
     B = e.getActionCommand();
    if (B.charAt(1) == '0' && (ansStr.length() < 15 || ifAction)) {
          if (ifAction) {
                init();
                ifAction = false;
          }
          if (!(ansStr.length() == 1 && ansStr.charAt(0) == '0')) {
                ansStr = ansStr + B.charAt(1);
          }
     } else if ((B.charAt(1) >= '1' && B.charAt(1) <= '9')
            && (ansStr.length() < 15 || ifAction)) {
          if (ifAction) {
                init();
                ifAction = false;
          }
          if (ansStr.length() == 1 && ansStr.charAt(0) == '0') {
                ansStr = "" + B.charAt(1);
          } else {
                ansStr = ansStr + B.charAt(1);
          }
     } else if (B.charAt(0) == 'B' && ansStr.length() > 0) {
         int len = ansStr.length();
         if (ansStr.charAt(len - 1) == '.') {
               ifDot = true;
         }
         ansStr = ansStr.substring(0, len - 1);
    } else if (B.charAt(0) == '+') {
         if (ansStr.charAt(0) == '-') {
               int len = ansStr.length();
               ansStr = ansStr.substring(1, len);
         } else if (ansStr.length() > 0){
               ansStr = '-' + ansStr;
         }
    } else if (B.charAt(1) == '.' && ifDot) {
         ansStr = ansStr + B.charAt(1);
         ifDot = false;
    } else if (B.charAt(1) == 'l') {
         initAll();
    } else if (B.charAt(1) == 'E') {
         init();
    } else if (B.charAt(1) == '+') {
         if (!ifAction) {
               if (sign != 0) {
                     cal();
               }
               ansPre = .parseDouble(ansStr);
         }
         sign = 1;
         ifAction = true;
    } else if (B.charAt(1) == '-') {
         if (!ifAction) {
               if (sign != 0) {
                     cal();
               }
               ansPre = .parseDouble(ansStr);
         }
         sign = 2;
         ifAction = true;
    }else if (B.charAt(1) == 'x') {
         if (!ifAction) {
               if (sign != 0) {
                     cal();
               }
               ansPre = .parseDouble(ansStr);
         }
         sign = 3;
         ifAction = true;
    }else if (B.charAt(1) == '/') {
         if (!ifAction) {
               if (sign != 0) {
                     cal();
                 }
                 ansPre = .parseDouble(ansStr);
            }
            sign = 4;
            ifAction = true;
       } else if (B.charAt(1) == '=') {
            cal();
            ifAction = true;
            sign = 0;
       } else if (B.compareTo("退出") == 0) {
            .exit(0);
       } else if (B.compareTo("关于作者") == 0) {
            .showMessageDialog(null, " 作者:CoderMAX\n 专业:软件工
程\n Blog: www.codermax.com", "关于作者", .PLAIN_MESSAGE);
       }
       refresh();
   }
   public static void main([] args) {
       Calculator frame = new Calculator();
       frame.setTitle("计算器 By CoderMAX");
       frame.setSize(390, 370);
       frame.setVisible(true);
       frame.setResizable(false);
       frame.setDefaultCloseOperation(.EXIT_ON_CLOSE);
   }
}

如果需要期末报告留下邮箱~


原创文章,转载请注明来源并保留原文链接

本文链接:


相关日志

  • (4)

  • 蒸米 CHINA Internet Explorer Windows 说:

    我们也刚学完java,java要是编写windows小程序比mfc方便多了。容器的确很好用。

    []

    CHINA Mozilla Firefox Windows 回复:

    ==* ==*
    但是JAVA比C++效率低啊-。=做做小程序和外壳还可以- -

    []

    CHINA Google Chrome Linux 回复:

    我现在想想,其实这个问题要从两个方向思考,一个是开发效率,一个是运行效率,有时候开发效率会更重要,google地球和Picasa在linux下的就是用java开发的~ ==!

    []

  • CHINA Internet Explorer Windows 说:

    我似乎也写过类似的,N年前。。。

    []

    CHINA Google Chrome Linux 回复:

    这东西就应付应付考试拉,没有实质意义~ ==! ==!

    []

    CHINA Mozilla Firefox Windows 回复:

    我那时候也是啊,记得只能按一定规律去按,否则结果就有可能不对了

    []

    CHINA Google Chrome Linux 回复:

    我这个还可以,用BigDecimal提高精度,可以正常使用的,也不用考虑按键顺序,相对还是比较xx的~

    []

  • 留下评论:

    ==* ==! :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :@ :?: :-| :-x :-o :-P :-D :-? :-* :) :( :!: 8-O 8)
    XHTML: 您可以使用这些标记: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    郑重声明:资讯 【JAVA 计算器(外加期末报告) - CoderMAX | My Blog】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
    —— 相关资讯 ——