• JAVA——简单科学计算器设计


      1 package Calculator_JCoder;
      2 
      3 import java.awt.*;
      4 import java.awt.event.ActionEvent;
      5 import java.awt.event.ActionListener;
      6 import java.awt.event.KeyEvent;
      7 import java.awt.event.KeyListener;
      8 import java.text.DecimalFormat; 
      9 import javax.swing.*;
     10 import java.util.*;
     11 import java.math.*;
     12 
     13 public class Calculator extends JFrame implements ActionListener {
     14     static Font F = new Font("宋体",Font.BOLD,25);
     15     static Font _F = new Font("宋体",Font.BOLD,40);
     16     static DecimalFormat DF = new DecimalFormat("0.000");
     17     static String IN = "";
     18     static JTextField Text_Res = new JTextField("0");
     19     static JTextField Text_Now = new JTextField("");
     20     static JButton Keys[] = new JButton[21];
     21     static String KeysName[] = {
     22             "7", "8", "9", "4", "5", "6", "1", "2",
     23             "3", "0", ".", "+", "-", "*", "/", "=", 
     24             "CE", "(", ")", "^", "C"
     25     };
     26     
     27     static int CMP(char a){
     28         if(a == '#'){return 0;}
     29         if(a == '('){return 1;}
     30         if(a == '+' || a == '-'){return 3;}
     31         if(a == '*' || a == '/'){return 4;}
     32         if(a == '^'){return 5;}
     33         if(a == ')'){return 6;}
     34         return -1;
     35     }
     36     
     37     static String Change(String in){
     38         in += "#";
     39         int L = in.length();
     40         String t = "";
     41         String NPR = "";
     42         Stack<Character> S = new Stack();
     43         for(int i = 0;i < L;i ++){
     44             if(in.charAt(i) >= '0' && in.charAt(i) <= '9'){t += in.charAt(i);}
     45             else if(in.charAt(i) == '.'){t += in.charAt(i);}
     46             else if(in.charAt(i) == '#'){
     47                 if(t.length() != 0){NPR += t + " ";}
     48                 t = "";
     49             }
     50             else if(in.charAt(i) == '+' || in.charAt(i) == '-' || in.charAt(i) == '*' || in.charAt(i) == '/' || in.charAt(i) == '^'){
     51                 if(t.length() != 0){NPR += t + " ";}
     52                 t = "";
     53                 if(S.size() == 0){
     54                     S.push(in.charAt(i));
     55                 }
     56                 else if(CMP(S.peek()) < CMP(in.charAt(i))){
     57                     S.push(in.charAt(i));
     58                 }
     59                 else if(CMP(S.peek()) >= CMP(in.charAt(i))){
     60                     while(S.size() > 0 && CMP(S.peek()) >= CMP(in.charAt(i))) {
     61                         NPR += S.peek() + " ";
     62                         S.pop();
     63                     }
     64                     S.push(in.charAt(i));
     65                 }
     66             }
     67             else if(in.charAt(i) == '('){
     68                 if(t.length() != 0){NPR += t + " ";}
     69                 t = "";
     70                 S.push(in.charAt(i));
     71             }
     72             else if(in.charAt(i) == ')'){
     73                 if(t.length() != 0){NPR += t + " ";}
     74                 t = "";
     75                 while(S.peek() != '('){
     76                     NPR += S.peek() + " ";
     77                     S.pop();
     78                 }
     79                 S.pop();
     80             }
     81         }
     82         while(S.size() != 0){
     83             NPR += S.peek() + " ";
     84             S.pop();
     85         }
     86         //System.out.println(NPR);
     87         return NPR;
     88     }
     89     
     90     static double Solve(double a,double b,char c){
     91         double out = 0.0;
     92         if(c == '+'){
     93             out = a + b;
     94         }
     95         else if(c == '-'){
     96             out = a - b;
     97         }
     98         else if(c == '*'){
     99             out = a * b;
    100         }
    101         else if(c == '/'){
    102             out = a / b;
    103         }
    104         else if(c == '^'){
    105             out = Math.pow(a,b);
    106         }
    107         return out;
    108     }
    109     
    110     static double Cal(String now){
    111         String Sp[] = now.split("\ ");
    112         Stack<Double> S = new Stack();
    113         for(int i = 0;i < Sp.length;i ++){
    114             if(Sp[i].length() == 0){continue;}
    115             if(Sp[i].charAt(0) <= '9' && Sp[i].charAt(0) >= '0'){
    116                 S.push(Double.valueOf(Sp[i]));
    117             }
    118             else{
    119                 double b = S.peek();S.pop();
    120                 double a = S.peek();S.pop();
    121                 double c = Solve(a,b,Sp[i].charAt(0));
    122                 S.push(c);
    123             }
    124         }
    125         double ans = S.peek();
    126         return ans;
    127     }
    128     
    129     static String SetS(int x){
    130         if(x == 1){return "+";}
    131         if(x == 2){return "-";}
    132         if(x == 3){return "*";}
    133         if(x == 4){return "/";}
    134         return "0";
    135     }
    136 
    137     
    138     public void init() {
    139         JPanel KeysP = new JPanel();
    140         KeysP.setLayout(null);
    141         KeysP.setSize(500,500);
    142         for(int i = 0;i <= 20;i ++){
    143             Keys[i] = new JButton(KeysName[i]);
    144             KeysP.add(Keys[i]);
    145             Keys[i].setFont(F);
    146         }
    147         Keys[0].setBounds(20,20,60,60);
    148         Keys[1].setBounds(85,20,60,60);
    149         Keys[2].setBounds(150,20,60,60);
    150         Keys[3].setBounds(20,85,60,60);
    151         Keys[4].setBounds(85,85,60,60);
    152         Keys[5].setBounds(150,85,60,60);
    153         Keys[6].setBounds(20,150,60,60);
    154         Keys[7].setBounds(85,150,60,60);
    155         Keys[8].setBounds(150,150,60,60);
    156         Keys[9].setBounds(20,215,125,60);
    157         Keys[10].setBounds(150,215,60,60);
    158         Keys[11].setBounds(215,20,60,60);
    159         Keys[12].setBounds(280,20,60,60);
    160         Keys[13].setBounds(215,85,60,60);
    161         Keys[14].setBounds(280,85,60,60);
    162         Keys[15].setBounds(215,150,125,60);
    163         Keys[16].setBounds(215,215,125,60);
    164         Keys[17].setBounds(345,20,60,60);
    165         Keys[18].setBounds(345,85,60,60);
    166         Keys[19].setBounds(345,150,60,60);
    167         Keys[20].setBounds(345,215,60,60);    
    168         
    169         Text_Res.setHorizontalAlignment(JTextField.RIGHT);
    170         Text_Now.setHorizontalAlignment(JTextField.RIGHT);
    171         Text_Res.setEditable(false);
    172         Text_Now.setEditable(false);
    173         Text_Res.setBackground(Color.WHITE);
    174         Text_Now.setBackground(Color.WHITE);
    175         JPanel TextP = new JPanel();
    176         TextP.setLayout(null);
    177         TextP.setSize(500,100);
    178         TextP.add(Text_Res);
    179         Text_Res.setBounds(20, 60, 385, 60);
    180         TextP.add(Text_Now);
    181         Text_Now.setBounds(20, 20, 385, 40);
    182         Text_Res.setFont(_F);
    183         Text_Now.setFont(F);
    184         
    185         JPanel BigP = new JPanel();
    186         BigP.setSize(800,600);
    187         BigP.setLayout(null);
    188         BigP.add(KeysP);
    189         BigP.add(TextP);
    190         KeysP.setBounds(0, 120, 600, 600);
    191         TextP.setBounds(0, 0, 500, 200);
    192         getContentPane().setLayout(null);
    193         getContentPane().add(BigP);
    194         
    195         for (int i = 0; i <= 20; i ++) {
    196             Keys[i].addActionListener(this);
    197         }
    198         
    199     }
    200     
    201     public void actionPerformed(ActionEvent e) {
    202         String Get = e.getActionCommand();
    203         if("0123456789.+-*/^()".indexOf(Get) >= 0){
    204             IN = IN + Get;
    205             Text_Now.setText(IN);
    206         }
    207         else if("=".indexOf(Get) >= 0){
    208             double show = Cal(Change(IN));
    209             //System.out.println(show);
    210             //System.out.println(show);
    211             String t1 = String.valueOf(show);
    212             String t2 = String.valueOf(DF.format(show));
    213             Text_Res.setText(t1);
    214             IN = t2;
    215         }
    216         else if("CE".compareTo(Get) == 0){
    217             int L = IN.length();
    218             if(L != 0){IN = IN.substring(0,L - 1);}
    219             Text_Now.setText(IN);
    220         }
    221         else if("C".compareTo(Get) == 0){
    222             IN = "";
    223             Text_Now.setText(IN);
    224             Text_Res.setText("0");
    225         }
    226     }
    227     
    228     public Calculator(){
    229         super();
    230         init();
    231         this.setTitle("Calculator By-J_Coder");
    232         this.setResizable(false);
    233         this.setLocation(100,100);
    234         this.setSize(440,450);
    235     }
    236 
    237     public static void main(String[] args) {
    238         Calculator window = new Calculator();
    239         window.setVisible(true);
    240         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    241     }
    242 }
  • 相关阅读:
    MySQL概述
    Seleniumselenium基础入门
    MySQL数据库的安装与使用
    元素定位_id
    Selenium浏览器的前进、后退、刷新
    元素定位_tag_name
    Selenium浏览器操作_窗口大小设置
    元素定位_name
    搭建maven服务器(repository)
    使用dos命令生成目录树
  • 原文地址:https://www.cnblogs.com/love-fromAtoZ/p/9704113.html
Copyright © 2020-2023  润新知