當前位置:首頁 » 操作系統 » c計算器源碼

c計算器源碼

發布時間: 2022-02-28 14:41:40

㈠ win7自帶計算器的源代碼

win7在運行處 輸入calc.exe,可以直接進如計算器!

㈡ c++計算器源代碼

#include<iostream>//簡單的計算器
using namespace std;//DEV c++
int main()
{
int a,b;
char c;//這里變數名只能為char
cout<<"計算器"<<endl;
cout<<"你想使用哪種計算器(+,-,*,/)"<<endl;
cin>>c;
switch(c)//這里是對+,-,*,/進行使用(c)
{
case '+'://加法運算
{
cout<<"請輸入第一個數"<<endl;
cin>>a;
cout<<"請輸入第二個數"<<endl;
cin>>b;
cout<<a<<"+"<<b<<"="<<a+b;
}
break;//break是終止
case '-'://減法運算
{
cout<<"請輸入第一個數"<<endl;
cin>>a;
cout<<"請輸入第二個數"<<endl;
cin>>b;
cout<<a<<"-"<<b<<"="<<a-b;
}
break;
case '*'://乘法運算
{
cout<<"請輸入第一個數"<<endl;
cin>>a;
cout<<"請輸入第二個數"<<endl;
cin>>b;
cout<<a<<"*"<<b<<"="<<a*b;
}
break;
case '/'://除法運算
{
cout<<"請輸入第一個數"<<endl;
cin>>a;
cout<<"請輸入第二個數"<<endl;
cin>>b;
if(b!=0)
{
cout<<a<<"/"<<b<<"="<<a/b;
}
else cout<<"除數不能為0"<<endl;
}
break;
default:cout<<"請輸入正確的運算符號"<<endl;//特殊情況
}
return 0;
}

㈢ 如何查看電腦上某程序的源代碼 如計算器

可以通過GitHub源代碼ping在計算機中檢查計算器的源代碼。具體操作方式如下:

1、進入GitHub的Microsoft個人問題主頁,如下圖所示。

(3)c計算器源碼擴展閱讀:

GitHub的Windows應用

GitHub 使用 git 分布式版本控制系統,而 git 最初是 LinusTorvalds 為幫助Linux開發而創造的,它針對的是 Linux 平台,因此 git 和 Windows 從來不是最好的朋友,因為它一點也不像Windows。

GitHub 發布了GitHub for Windows,為 Windows 平台開發者提供了一個易於使用的 Git 圖形客戶端。

GitHub forWindows是一個 Metro 風格應用程序,集成了自包含版本的 Git,bash 命令行 shell,PowerShell 的 posh-git 擴展。

GitHub 為 Windows 用戶提供了一個基本的圖形前端去處理大部分常用版本控制任務,可以創建版本庫,向本地版本庫遞交補丁,在本地和遠程版本庫之間同步。微軟也通過CodePlex向開發者提供 git 版本控制系統,而 GitHub 創造了一個更具有吸引力的 Windows 版本。

㈣ 求C++編寫的科學計算器源代碼

給你個地址,自己去下載。
★★[源碼庫]811款導彈-制導-彈道-慣性導航-GPS-雷達等模擬源代碼★★
http://bbs.81tech.com/read.php?tid=209564

其中有兩款計算機源代碼:
1. 科學計算器程序--VC++源代碼.rar (30 K)
2. 非常實用的一款計算器-VC++源代碼.rar (138 K)

㈤ 關於java做的計算器源代碼

關於計算器的設計其實是一個很大的學問,如果你按照嚴格的設計來完成一個計算器的話,完成之後。你就是一個很牛的人了。注意我說的是很牛
關於這方面的資料我建議你看本書
《大話設計模式》
這本書就是從計算器來展開的設計模式的運用和講解

c語言計算器程序源代碼

棧操作~
輸入的弄成字元串。
如:a+(b+c*(d+e))
先檢索「(」入棧,b入棧,+入棧,c入棧,*入棧,後面的不是數字那麼「(」入棧,+,入棧,e入棧,
「)」入棧,遇到「)」,出棧操作,直到遇到第一個「(」。
得到字元:d+e,通過演算法計算出值D,然後把D入棧。那麼棧中的字元為:a+(b+c*D
向後判斷一步看是不是乘或除,不是繼續向後入棧,是就出棧。
得到c*D,通過演算法計算值為C,入棧,那麼棧中的字元為:a+(b+C,向後判斷,無乘或除運算,繼續入棧:「)」,遇到「)」,出棧操作,直到遇到第一個「(」,得到字元串:b+c,演算法計算值為B,入棧
棧中字元串為:a+B 字元串結束,出棧操作計算出a+B,值。
如果字元串的式子很長的話,就反復的入棧出棧計算。
思路是這樣的......
關於COS,SIN類的關鍵字,也是上面的思路,
(入棧,直到遇到)出棧,先計算出括弧里的,然後後退一步判斷前面的是乘除或者關鍵字,
如:sin(0.1+0.2)
操作得出sin0.3然後判斷前面的是不是關鍵字,是那句計算sin0.3。
但是遇到關鍵字的時候不是一個一個字元在一起的,你可以入棧的時候判斷是不是字母,是字母,那麼入棧直到不是字母為止,出棧判斷關鍵字也是一樣......
不明白的繼續問~

㈦ C#計算器源代碼

全部按鈕的事件代碼可以省略成1個方法
private void Btn_click(object sender, EventArgs e)
{
//(button)sender,把sender參數強制轉換成按鈕類型,根據按鈕的文本屬性判斷是哪個按鈕
string buttonType= ((button)sender).Text;
}

㈧ 計算器源代碼

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 一個計算器,與Windows附件自帶計算器的標准版功能、界面相仿。
* 但還不支持鍵盤操作。
*/
public class Calculator extends JFrame implements ActionListener {
/** 計算器上的鍵的顯示名字 */
private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
"*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
/** 計算器上的功能鍵的顯示名字 */
private final String[] COMMAND = { "Backspace", "CE", "C" };
/** 計算器左邊的M的顯示名字 */
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
/** 計算器上鍵的按鈕 */
private JButton keys[] = new JButton[KEYS.length];
/** 計算器上的功能鍵的按鈕 */
private JButton commands[] = new JButton[COMMAND.length];
/** 計算器左邊的M的按鈕 */
private JButton m[] = new JButton[M.length];
/** 計算結果文本框 */
private JTextField resultText = new JTextField("0");

// 標志用戶按的是否是整個表達式的第一個數字,或者是運算符後的第一個數字
private boolean firstDigit = true;
// 計算的中間結果。
private double resultNum = 0.0;
// 當前運算的運算符
private String operator = "=";
// 操作是否合法
private boolean operateValidFlag = true;
/**
* 構造函數
*/
public Calculator(){
super();
//初始化計算器
init();
//設置計算器的背景顏色
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("計算器");
//在屏幕(500, 300)坐標處顯示計算器
this.setLocation(500, 300);
//不許修改計算器的大小
this.setResizable(false);
//使計算器中各組件大小合適
this.pack();
}
/**
* 初始化計算器
*/
private void init() {
// 文本框中的內容採用右對齊方式
resultText.setHorizontalAlignment(JTextField.RIGHT);
// 不允許修改結果文本框
resultText.setEditable(false);
// 設置文本框背景顏色為白色
resultText.setBackground(Color.WHITE);

//初始化計算器上鍵的按鈕,將鍵放在一個畫板內
JPanel calckeysPanel = new JPanel();
//用網格布局器,4行,5列的網格,網格之間的水平方向間隔為3個象素,垂直方向間隔為3個象素
calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
//運算符鍵用紅色標示,其他鍵用藍色表示
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);

//初始化功能鍵,都用紅色標示。將功能鍵放在一個畫板內
JPanel commandsPanel = new JPanel();
//用網格布局器,1行,3列的網格,網格之間的水平方向間隔為3個象素,垂直方向間隔為3個象素
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i < COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}

//初始化M鍵,用紅色標示,將M鍵放在一個畫板內
JPanel calmsPanel = new JPanel();
//用網格布局管理器,5行,1列的網格,網格之間的水平方向間隔為3個象素,垂直方向間隔為3個象素
calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i < M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}

//下面進行計算器的整體布局,將calckeys和command畫板放在計算器的中部,
//將文本框放在北部,將calms畫板放在計算器的西部。

//新建一個大的畫板,將上面建立的command和calckeys畫板放在該畫板內
JPanel panel1 = new JPanel();
//畫板採用邊界布局管理器,畫板里組件之間的水平和垂直方向上間隔都為3象素
panel1.setLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("Center", calckeysPanel);

//建立一個畫板放文本框
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);

//整體布局
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);
//為各按鈕添加事件偵聽器
//都使用同一個事件偵聽器,即本對象。本類的聲明中有implements ActionListener
for (int i = 0; i < KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i < COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i < M.length; i++) {
m[i].addActionListener(this);
}
}

/**
* 處理事件
*/
public void actionPerformed(ActionEvent e) {
//獲取事件源的標簽
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){
//用戶按了"Backspace"鍵
handleBackspace();
} else if (label.equals(COMMAND[1])) {
//用戶按了"CE"鍵
resultText.setText("0");
} else if (label.equals(COMMAND[2])){
//用戶按了"C"鍵
handleC();
} else if ("0123456789.".indexOf(label) >= 0) {
//用戶按了數字鍵或者小數點鍵
handleNumber(label);
// handlezero(zero);
} else {
//用戶按了運算符鍵
handleOperator(label);
}
}
/**
* 處理Backspace鍵被按下的事件
*/
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i > 0) {
//退格,將文本最後一個字元去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
//如果文本沒有了內容,則初始化計算器的各種值
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
//顯示新的文本
resultText.setText(text);
}
}
}

/**
* 處理數字鍵被按下的事件
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
//輸入的第一個數字
resultText.setText(key);
} else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)){
//輸入的是小數點,並且之前沒有小數點,則將小數點附在結果文本框的後面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
//如果輸入的不是小數點,則將數字附在結果文本框的後面
resultText.setText(resultText.getText() + key);
}
//以後輸入的肯定不是第一個數字了
firstDigit = false;
}
/**
* 處理C鍵被按下的事件
*/
private void handleC() {
//初始化計算器的各種值
resultText.setText("0");
firstDigit = true;
operator = "=";
}

/**
* 處理運算符鍵被按下的事件
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
//除法運算
//如果當前結果文本框中的值等於0
if (getNumberFromText() == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("除數不能為零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
//倒數運算
if (resultNum == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("零沒有倒數");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")){
//加法運算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
//減法運算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
//乘法運算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
//平方根運算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")){
//百分號運算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")){
//正數負數運算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
//賦值運算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
//雙精度浮點數的運算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
//運算符等於用戶按的按鈕
operator = key;
firstDigit = true;
operateValidFlag = true;
}

/**
* 從結果文本框中獲取數字
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}

public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

㈨ 求計算器源代碼

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace jisuan
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(312, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(448, 72);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(88, 21);
this.textBox3.TabIndex = 2;
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"+",
"-",
"*",
"/"});
this.comboBox1.Location = new System.Drawing.Point(152, 72);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 4;
this.button1.Text = "計算";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(216, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "清除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 192);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 6;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 366);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public double jia(double a,double b)
{
return a+b;
}
public double jian(double a,double b)
{
return a-b;
}
public double cheng(double a,double b)
{
return a*b;
}
public double chu(double a,double b)
{
return a/b;
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
string i=this.comboBox1.SelectedItem.ToString();

switch(i)
{

case "+":this.textBox3.Text=this.jia(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();

break;

case "-":this.textBox3.Text=this.jian(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case "*":this.textBox3.Text=this.cheng(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
case"/" :this.textBox3.Text=this.chu(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();
break;
}

}

private void button2_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=null;
this.textBox2.Text=null;
this.textBox3.Text = null;
}

private void button3_Click(object sender, EventArgs e)
{

//this.Hide();
Application.Exit();
//this.Close();

}
}
}
是否可以解決您的問題?

㈩ 簡單計算器的源代碼

package calculator;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.SystemColor;
import java.awt.Color;

/**
* <p>Title:</p>
*
* <p>Description: 這個類用來實現一個簡單的計算器</p>
*
* <p>Copyright: Copyright (c) 2006年</p>
*
* <p>Company: </p>
*
* @author
* @version 1.0
*/
public class CalculatorFrame extends JFrame {

boolean flag = false;
String operand1;
String operand2;
double result;
String action;

BorderLayout borderLayout1 = new BorderLayout();
JTextField txtresult = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
JButton btn5 = new JButton();
JButton btn6 = new JButton();
JButton btn7 = new JButton();
JButton btn8 = new JButton();
JButton btn9 = new JButton();
JButton btn0 = new JButton();
JButton btnplus = new JButton();
JButton btnminus = new JButton();
JButton btnjultiply = new JButton();
JButton btndivide = new JButton();
JButton btnclear = new JButton();
JButton btnequal = new JButton();

public CalculatorFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
txtresult.setBackground(SystemColor.WHITE);
txtresult.setBorder(BorderFactory.createLoweredBevelBorder());
txtresult.setDisabledTextColor(Color.black);
txtresult.setEditable(false);
txtresult.setHorizontalAlignment(JTextField.RIGHT);
txtresult.setBounds(new Rectangle(50, 50, 305, 25));
btn1.setBounds(new Rectangle(50, 200, 65, 30));
btn1.setBorder(BorderFactory.createRaisedBevelBorder());
btn1.setText("1");
btn1.addActionListener(new CalculatorFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(130, 200, 65, 30));
btn2.setBorder(BorderFactory.createRaisedBevelBorder());
btn2.setText("2");
btn2.addActionListener(new CalculatorFrame_btn2_actionAdapter(this));
btn3.setBounds(new Rectangle(210, 200, 65, 30));
btn3.setBorder(BorderFactory.createRaisedBevelBorder());
btn3.setText("3");
btn3.addActionListener(new CalculatorFrame_btn3_actionAdapter(this));
btn4.setBounds(new Rectangle(50, 150, 65, 30));
btn4.setBorder(BorderFactory.createRaisedBevelBorder());
btn4.setText("4");
btn4.addActionListener(new CalculatorFrame_btn4_actionAdapter(this));
btn5.setBounds(new Rectangle(130, 150, 65, 30));
btn5.setBorder(BorderFactory.createRaisedBevelBorder());
btn5.setText("5");
btn5.addActionListener(new CalculatorFrame_btn5_actionAdapter(this));
btn6.setBounds(new Rectangle(210, 150, 65, 30));
btn6.setBorder(BorderFactory.createRaisedBevelBorder());
btn6.setText("6");
btn6.addActionListener(new CalculatorFrame_btn6_actionAdapter(this));
btn7.setBounds(new Rectangle(50, 100, 65, 30));
btn7.setBorder(BorderFactory.createRaisedBevelBorder());
btn7.setText("7");
btn7.addActionListener(new CalculatorFrame_btn7_actionAdapter(this));
btn8.setBounds(new Rectangle(130, 100, 65, 30));
btn8.setBorder(BorderFactory.createRaisedBevelBorder());
btn8.setText("8");
btn8.addActionListener(new CalculatorFrame_btn8_actionAdapter(this));
btn9.setBounds(new Rectangle(210, 100, 65, 30));
btn9.setBorder(BorderFactory.createRaisedBevelBorder());
btn9.setText("9");
btn9.addActionListener(new CalculatorFrame_btn9_actionAdapter(this));
btn0.setBounds(new Rectangle(50, 250, 65, 30));
btn0.setBorder(BorderFactory.createRaisedBevelBorder());
btn0.setText("0");
btn0.addActionListener(new CalculatorFrame_btn0_actionAdapter(this));
btnplus.setBounds(new Rectangle(290, 250, 65, 30));
btnplus.setBorder(BorderFactory.createRaisedBevelBorder());
btnplus.setText("+");
btnplus.addActionListener(new CalculatorFrame_btnplus_actionAdapter(this));
btnminus.setBounds(new Rectangle(290, 200, 65, 30));
btnminus.setBorder(BorderFactory.createRaisedBevelBorder());
btnminus.setText("-");
btnminus.addActionListener(new CalculatorFrame_btnminus_actionAdapter(this));
btnjultiply.setBounds(new Rectangle(290, 150, 65, 30));
btnjultiply.setBorder(BorderFactory.createRaisedBevelBorder());
btnjultiply.setText("*");
btnjultiply.addActionListener(new
CalculatorFrame_btnjultiply_actionAdapter(this));
btndivide.setBounds(new Rectangle(290, 100, 65, 30));
btndivide.setBorder(BorderFactory.createRaisedBevelBorder());
btndivide.setText("/");
btndivide.addActionListener(new CalculatorFrame_btndivide_actionAdapter(this));
btnclear.setBounds(new Rectangle(130, 250, 65, 30));
btnclear.setBorder(BorderFactory.createRaisedBevelBorder());
btnclear.setText("C");
btnclear.addActionListener(new CalculatorFrame_btnclear_actionAdapter(this));
btnequal.setBounds(new Rectangle(210, 250, 65, 30));
btnequal.setBorder(BorderFactory.createRaisedBevelBorder());
btnequal.setText("=");
btnequal.addActionListener(new CalculatorFrame_btnequal_actionAdapter(this));
this.getContentPane().setBackground(UIManager.getColor(
"MenuItem.background"));
this.setTitle("計算器");
this.getContentPane().add(txtresult);
this.getContentPane().add(btn1);
this.getContentPane().add(btn0);
this.getContentPane().add(btnclear);
this.getContentPane().add(btnplus);
this.getContentPane().add(btnequal);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btnminus);
this.getContentPane().add(btn4);
this.getContentPane().add(btn7);
this.getContentPane().add(btn5);
this.getContentPane().add(btn6);
this.getContentPane().add(btnjultiply);
this.getContentPane().add(btndivide);
this.getContentPane().add(btn9);
this.getContentPane().add(btn8);
}

public void btnplus_actionPerformed(ActionEvent e) {
action = "plus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnminus_actionPerformed(ActionEvent e) {
action = "minus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnjultiply_actionPerformed(ActionEvent e) {
action = "multiply";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btndivide_actionPerformed(ActionEvent e) {
action = "divide";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnclear_actionPerformed(ActionEvent e) {
txtresult.setText("");
}

public void btn1_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn1.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn1.getActionCommand());
}

}

public void btn2_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn2.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn2.getActionCommand());
}

}

public void btn3_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn3.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn3.getActionCommand());
}

}

public void btn4_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn4.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn4.getActionCommand());
}

}

public void btn5_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn5.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn5.getActionCommand());
}

}

public void btn6_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn6.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn6.getActionCommand());
}

}

public void btn7_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn7.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn7.getActionCommand());
}

}

public void btn8_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn8.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn8.getActionCommand());
}

}

public void btn9_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn9.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn9.getActionCommand());
}

}

public void btn0_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn0.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn0.getActionCommand());
}

}

public void btnequal_actionPerformed(ActionEvent e) {
double digit1, digit2;
operand2 = txtresult.getText();
if (flag == false) {
if (action.equals("plus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 + digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("minus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 - digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("multiply")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 * digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("divide")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 / digit2;
if (digit2==0) {
txtresult.setText("ERROR");
flag=true;
} else {
txtresult.setText(String.valueOf((int) result));
flag = true;
}
}

}

}

class CalculatorFrame_btnequal_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnequal_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnequal_actionPerformed(e);
}
}

class CalculatorFrame_btn0_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn0_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn0_actionPerformed(e);
}
}

class CalculatorFrame_btn9_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn9_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn9_actionPerformed(e);
}
}

class CalculatorFrame_btn8_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn8_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn8_actionPerformed(e);
}
}

class CalculatorFrame_btn7_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn7_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn7_actionPerformed(e);
}
}

class CalculatorFrame_btn6_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn6_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn6_actionPerformed(e);
}
}

class CalculatorFrame_btn5_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn5_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn5_actionPerformed(e);
}
}

class CalculatorFrame_btn4_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn4_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}

class CalculatorFrame_btn2_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn2_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn2_actionPerformed(e);
}
}

class CalculatorFrame_btn3_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn3_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}

class CalculatorFrame_btnclear_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnclear_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnclear_actionPerformed(e);
}
}

class CalculatorFrame_btndivide_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btndivide_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btndivide_actionPerformed(e);
}
}

class CalculatorFrame_btnjultiply_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnjultiply_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnjultiply_actionPerformed(e);
}
}

class CalculatorFrame_btnminus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnminus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnminus_actionPerformed(e);
}
}

class CalculatorFrame_btnplus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnplus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnplus_actionPerformed(e);
}
}

class CalculatorFrame_btn1_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn1_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
}

public class Calculator {

public static void main(String[] args) {

CalculatorFrame Objcal=new CalculatorFrame();

Objcal.setSize(410,320);
Objcal.setVisible(true);
}
}

熱點內容
ubuntu編譯arm 發布:2024-11-15 23:58:33 瀏覽:620
excel隱藏工作表加密 發布:2024-11-15 23:55:41 瀏覽:451
十大挖掘演算法 發布:2024-11-15 23:55:41 瀏覽:133
隱藏文件夾如何打開 發布:2024-11-15 23:55:02 瀏覽:698
為什麼安卓沒法下載神武4 發布:2024-11-15 23:46:18 瀏覽:335
巫師3和英雄聯盟哪個吃配置 發布:2024-11-15 23:46:10 瀏覽:805
qt文件加密 發布:2024-11-15 23:40:35 瀏覽:738
咸陽做腳本的 發布:2024-11-15 23:38:57 瀏覽:773
途觀與探岳哪個配置高 發布:2024-11-15 23:30:39 瀏覽:518
dhcp伺服器如何更新ip地址 發布:2024-11-15 23:18:40 瀏覽:126