Java實現(xiàn)計算器設(shè)計
本文實例為大家分享了Java實現(xiàn)計算器設(shè)計的具體代碼,供大家參考,具體內(nèi)容如下
需求分析
- 目的是實現(xiàn)一個基于Java的可以求解帶括號加減乘除表達(dá)式的帶界面的計算器。
- 需要知道的Java技術(shù):Java Swing(Java圖形界面設(shè)計)、Java集合(棧)、lambda表達(dá)式、Java基礎(chǔ)等。
設(shè)計思路
1、實現(xiàn)一個Java計算器界面類
2、實現(xiàn)一個Java計算帶括號加減乘除表達(dá)式的類
3、實現(xiàn)主函數(shù)調(diào)用
設(shè)計實現(xiàn)
Java計算器項目結(jié)構(gòu):

Calculator類為計算器界面設(shè)計、Calculate類為計算帶括號加減乘除表達(dá)式的類,Main函數(shù)為項目程序入口。
Java計算器界面設(shè)計實現(xiàn)代碼:
package Calculator;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame{
private double result=0;
private int count=0;
public Calculator() {
this.setSize(330,399);
this.setTitle("計算器");
init();
// this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {//初始化界面
this.setLayout(new BorderLayout()); //總體布局為邊框式布局
/*
* 總體邊框式布局north放置文本框
*/
JTextField textField=new JTextField();
textField.disable();
textField.setPreferredSize(new Dimension(this.getWidth(),50));
this.add(textField,BorderLayout.NORTH);
/*
* 總體邊框式布局center放置@panel(邊框式布局)
* @panel邊框式布局north放置@panelN(網(wǎng)格布局)
* @panel邊框式布局center放置@panelC(卡片式布局)
* @panelC卡片來切換@panel0(標(biāo)準(zhǔn))和@panel1(科學(xué))兩種模式
* @panel0,@panel1均為網(wǎng)格布局
*/
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
this.add(panel, BorderLayout.CENTER);
JPanel panelN=new JPanel();
panelN.setLayout(new GridLayout(1,6));
JButton MC=new JButton("MC");
JButton MR=new JButton("MR");
JButton M0=new JButton("M+");
JButton M1=new JButton("M-");
JButton MS=new JButton("MS");
JButton M=new JButton("M");
panelN.add(MC);panelN.add(MR);panelN.add(M0);
panelN.add(M1);panelN.add(MS);panelN.add(M);
panel.add(panelN,BorderLayout.NORTH);
CardLayout cardLayout=new CardLayout();
JPanel panelC=new JPanel();
panelC.setLayout(cardLayout);
JPanel panel0=new JPanel();
panel0.setLayout(new GridLayout(6,4));
JButton[] standredButton=new JButton[24];
String str[]={"%","√","x²","1/x",
"CE","C","×","/",
"7","8","9","*",
"4","5","6","-",
"1","2","3","+",
"±","0",".","="
};
for(int i=0;i<standredButton.length;i++) {
standredButton[i]=new JButton(str[i]);
String text=standredButton[i].getText();
standredButton[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(text.equals("CE")||text.equals("C")) {
textField.setText("");
}
else if(text.equals("=")) {
String expression=textField.getText();
Calculate cal=new Calculate();
textField.setText(cal.evaluateExpression(expression)+"");
}
else if(text.equals("%")) {
}
else if(text.equals("√")) {
result=Double.parseDouble(textField.getText());
result=Math.sqrt(result);
textField.setText(result+"");
}
else if(text.equals("x²")) {
result=Double.parseDouble(textField.getText());
result*=result;
textField.setText(result+"");
}
else if(text.equals("1/x")) {
result=Double.parseDouble(textField.getText());
result=1/result;
textField.setText(result+"");
}
else if(text.equals("±")) {
if(count==0) {
textField.setText(textField.getText()+"-");
count=1;
}
else {
textField.setText(textField.getText()+"+");
count=0;
}
}
else if(text.equals("×")) {
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
}
else {
textField.setText(textField.getText()+text);
}
}
}
);
panel0.add(standredButton[i]);
}
panelC.add(panel0);
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(7,5));
JButton scienceButton[]=new JButton[35];
String str1[]= {
"x²","x^y","sin","cos","tan",
"√","10^x","log","Exp","Mod",
"↑","CE","C","×","/",
"π","7","8","9","*",
"n!","4","5","6","-",
"±","1","2","3","+",
"(",")","0",".","="
};
for(int i=0;i<str1.length;i++) {
scienceButton[i]=new JButton(str1[i]);
//scienceButton[i].addActionListener();
panel1.add(scienceButton[i]);
}
panelC.add(panel1);
panel.add(panelC,BorderLayout.CENTER);
/*
* 菜單
*/
JMenuBar menuBar=new JMenuBar();
this.setJMenuBar(menuBar);
JMenu modelMenu=new JMenu("模式");
menuBar.add(modelMenu);
JMenuItem standred=new JMenuItem("標(biāo)準(zhǔn)");
standred.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cardLayout.first(panelC);
}
});
modelMenu.add(standred);
JMenuItem science=new JMenuItem("科學(xué)");
science.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cardLayout.last(panelC);
}
});
modelMenu.add(science);
}
/*
private class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
*/
}
Java計算帶括號加減乘除表達(dá)式類的實現(xiàn):
package Calculator;
import java.util.*;
/*
*使用此類直接調(diào)用evaluateExpression方法即可,傳入需計算的表達(dá)式,返回計算結(jié)果
*/
public class Calculate {
//這個函數(shù)的作用就是使用空格分割字符串,以便后面使用分割函數(shù)使得將字符串分割成數(shù)組
public String insetBlanks(String s) {
String result = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
s.charAt(i) == '+' || s.charAt(i) == '-'
|| s.charAt(i) == '*' || s.charAt(i) == '/')
result += " " + s.charAt(i) + " ";
else
result += s.charAt(i);
}
return result;
}
public double evaluateExpression(String expression) {
Stack<Double> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
expression = insetBlanks(expression);
String[] tokens = expression.split(" ");
for (String token : tokens) {
if (token.length() == 0) //如果是空格的話就繼續(xù)循環(huán),什么也不操作
continue;
//如果是加減的話,因為加減的優(yōu)先級最低,因此這里的只要遇到加減號,無論操作符棧中的是什么運算符都要運算
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
//當(dāng)棧不是空的,并且棧中最上面的一個元素是加減乘除的人任意一個
while (!operatorStack.isEmpty()&&(operatorStack.peek() == '-' || operatorStack.peek() == '+' || operatorStack.peek() == '/' || operatorStack.peek() == '*')) {
processAnOperator(operandStack, operatorStack); //開始運算
}
operatorStack.push(token.charAt(0)); //運算完之后將當(dāng)前的運算符入棧
}
//當(dāng)前運算符是乘除的時候,因為優(yōu)先級高于加減,因此要判斷最上面的是否是乘除,如果是乘除就運算,否則的話直接入棧
else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
while (!operatorStack.isEmpty()&&(operatorStack.peek() == '/' || operatorStack.peek() == '*')) {
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(token.charAt(0)); //將當(dāng)前操作符入棧
}
//如果是左括號的話直接入棧,什么也不用操作,trim()函數(shù)是用來去除空格的,由于上面的分割操作可能會令操作符帶有空格
else if (token.trim().charAt(0) == '(') {
operatorStack.push('(');
}
//如果是右括號的話,清除棧中的運算符直至左括號
else if (token.trim().charAt(0) == ')') {
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack); //開始運算
}
operatorStack.pop(); //這里的是運算完之后清除左括號
}
//這里如果是數(shù)字的話直接如數(shù)據(jù)的棧
else {
operandStack.push(Double.parseDouble(token)); //將數(shù)字字符串轉(zhuǎn)換成數(shù)字然后壓入棧中
}
}
//最后當(dāng)棧中不是空的時候繼續(xù)運算,知道棧中為空即可
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
return operandStack.pop(); //此時數(shù)據(jù)棧中的數(shù)據(jù)就是運算的結(jié)果
}
//這個函數(shù)的作用就是處理棧中的兩個數(shù)據(jù),然后將棧中的兩個數(shù)據(jù)運算之后將結(jié)果存儲在棧中
public void processAnOperator(Stack<Double> operandStack, Stack<Character> operatorStack) {
char op = operatorStack.pop(); //彈出一個操作符
Double op1 = operandStack.pop(); //從存儲數(shù)據(jù)的棧中彈出連個兩個數(shù)用來和操作符op運算
Double op2 = operandStack.pop();
if (op == '+') //如果操作符為+就執(zhí)行加運算
operandStack.push(op1 + op2);
else if (op == '-')
operandStack.push(op2 - op1); //因為這個是棧的結(jié)構(gòu),自然是上面的數(shù)字是后面的,因此用op2-op1
else if (op == '*')
operandStack.push(op1 * op2);
else if (op == '/')
operandStack.push(op2 / op1);
}
}
主函數(shù):
package Calculator;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator calculator=new Calculator();
}
}
設(shè)計實現(xiàn)展示

可以隨意縮小放大界面,界面部件會跟隨界面大小自適應(yīng)調(diào)整。
其他功能
目前實現(xiàn)了標(biāo)準(zhǔn)型計算,科學(xué)型計算更加復(fù)雜,實現(xiàn)了界面,沒有計算功能,后續(xù)可能會繼續(xù)開發(fā),敬請期待。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring MVC攔截器實現(xiàn)session控制
這篇文章主要介紹了詳解Spring MVC攔截器實現(xiàn)session控制,使用session監(jiān)聽,重復(fù)登錄后,強制之前登錄的session過期。有興趣的可以了解一下。2017-01-01
Springboot解決no main manifest attribute錯誤
在開發(fā)Springboot項目時,使用java -jar命令運行jar包可能出現(xiàn)no main manifest attribute錯誤,本文就來介紹一下該錯誤的解決方法,感興趣的可以了解一下2024-09-09
FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細(xì)解讀
這篇文章主要介紹了FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細(xì)解讀,當(dāng)直接進去注冊的方法中,一步步放下走,都是直接放bean的定義信息中放入值,然后轉(zhuǎn)成BeanDefinitionHolder,最后在注冊到IOC容器中,需要的朋友可以參考下2023-11-11
Springboot整合GateWay+Nacos實現(xiàn)動態(tài)路由
本文主要介紹了Springboot整合GateWay+Nacos實現(xiàn)動態(tài)路由,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
Spring根據(jù)URL參數(shù)進行路由的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring根據(jù)URL參數(shù)進行路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起來看看吧。2017-12-12
解決創(chuàng)建springboot后啟動報錯:Failed?to?bind?properties?under‘spri
在Spring?Boot項目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡潔但不支持層次結(jié)構(gòu),而yml格式支持層次性,可讀性更好,在yml文件中,要注意細(xì)節(jié),比如冒號后面需要空格2024-10-10

