導航:首頁 > 源碼編譯 > 裝修計算器源碼大全

裝修計算器源碼大全

發布時間:2023-08-27 19:10:53

Ⅰ 計算器源代碼

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);
}
}

Ⅱ 求JAVA計算器模擬程序源代碼

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testZ extends JFrame implements ActionListener{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;

public testZ(){
super("計算器");
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));
resultField=new JTextField("0");
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);
s1=new JButton(" 1 "); s1.addActionListener(this);
s2=new JButton(" 2 "); s2.addActionListener(this);
s3=new JButton(" 3 "); s3.addActionListener(this);
s4=new JButton(" 4 "); s4.addActionListener(this);
s5=new JButton(" 5 "); s5.addActionListener(this);
s6=new JButton(" 6 "); s6.addActionListener(this);
s7=new JButton(" 7 "); s7.addActionListener(this);
s8=new JButton(" 8 "); s8.addActionListener(this);
s9=new JButton(" 9 "); s9.addActionListener(this);
s0=new JButton(" 0 "); s0.addActionListener(this);
b1=new JButton(" + "); b1.addActionListener(this);
b2=new JButton(" - "); b2.addActionListener(this);
b3=new JButton(" * "); b3.addActionListener(this);
b4=new JButton(" / "); b4.addActionListener(this);
f1=new JButton(" . "); f1.addActionListener(this);
f2=new JButton(" = "); f2.addActionListener(this);
jPanel2.add(s1);
jPanel2.add(s2);
jPanel2.add(s3);
jPanel2.add(b1);
jPanel2.add(s4);
jPanel2.add(s5);
jPanel2.add(s6);
jPanel2.add(b2);
jPanel2.add(s7);
jPanel2.add(s8);
jPanel2.add(s9);
jPanel2.add(b3);
jPanel2.add(s0);
jPanel2.add(f1);
jPanel2.add(f2);
jPanel2.add(b4);
con.add(jPanel2,BorderLayout.CENTER);

}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果數字輸入結束,則將文本框置零,重新輸入
resultField.setText("0");
end=false;

}
if((resultField.getText()).equals("0")){
//如果文本框的內容為零,則覆蓋文本框的內容
resultField.setText(s);

}
else{
//如果文本框的內容不為零,則在內容後面添加數字
str = resultField.getText() + s;
resultField.setText(str);

}
}

public void actionPerformed(ActionEvent e){ //數字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);

//符號事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
//等號
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());

if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}

}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
testZ th1=new testZ();
th1.show();
}
}

Ⅲ java計算器的源代碼

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class Counter extends Frame
{
//聲明三個面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//顯示屏所顯示的字元串
double x,y;//x和y都是運算數
int z;//Z表示單擊了那一個運算符.0表示"+",1表示"-",2表示"*",3表示"/"
static double m;//記憶的數字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//實例化三個面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);

tf1=new JTextField(27);//顯示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new TextField(10);//顯示記憶的索引值
tf2.setEditable(false);
//實例化所有按鈕、設置其前景色並注冊監聽器
b0=new Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new Bt());
b1=new Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new Bt());
b2=new Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new Bt());
b3=new Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new Bt());
b4=new Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new Bt());
b5=new Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new Bt());
b6=new Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new Bt());
b7=new Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8=new Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9=new Button("9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10=new Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b11=new Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new Bt());
b12=new Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new Bt());
b13=new Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new Bt());
b14=new Button("6");
b14.setForeground(Color.blue);
b14.addActionListener(new Bt());
b15=new Button("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b16=new Button("%");
b16.setForeground(Color.blue);
b16.addActionListener(new Bt());
b17=new Button("1");
b17.setForeground(Color.blue);
b17.addActionListener(new Bt());
b18=new Button("2");
b18.setForeground(Color.blue);
b18.addActionListener(new Bt());
b19=new Button("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20=new Button("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b21=new Button("1/X");
b21.setForeground(Color.blue);
b21.addActionListener(new Bt());
b22=new Button("0");
b22.setForeground(Color.blue);
b22.addActionListener(new Bt());
b23=new Button("+/-");
b23.setForeground(Color.blue);
b23.addActionListener(new Bt());
b24=new Button(".");
b24.setForeground(Color.blue);
b24.addActionListener(new Bt());
b25=new Button("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26=new Button("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());

//實例化四個面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//創建一個空字元串緩沖區
str=new StringBuffer();

//添加面板p0中的組件和設置其在框架中的位置和大小
p0.add(tf1);
p0.setBounds(10,25,300,40);
//添加面板p1中的組件和設置其在框架中的位置和大小
p1.setLayout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.setBounds(10,65,300,25);
//添加面板p2中的組件並設置其的框架中的位置和大小
p2.setLayout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBounds(10,110,40,150);
//添加面板p3中的組件並設置其在框架中的位置和大小
p3.setLayout(gl3);//設置p3的布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.setBounds(60,110,250,150);
//設置框架中的布局為空布局並添加4個面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止調整框架的大小
//匿名類關閉窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
System.exit(0);
}
});
setBackground(Color.lightGray);
setBounds(100,100,320,280);
setVisible(true);

}
//構造監聽器
class Bt implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{
try{

if(e2.getSource()==b1)//選擇"CE"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);//清空字元串緩沖區以准備接收新的輸入運算數
}
else if(e2.getSource()==b2)//選擇"C"清零
{
tf1.setText("0");//把顯示屏清零
str.setLength(0);
}
else if(e2.getSource()==b23)//單擊"+/-"選擇輸入的運算數是正數還是負數
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(-x));
}
else if(e2.getSource()==b25)//單擊加號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空緩沖區以便接收新的另一個運算數
y=0d;
z=0;
}
else if(e2.getSource()==b20)//單擊減號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e2.getSource()==b15)//單擊乘號按鈕獲得x的值和z的值並清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e2.getSource()==b10)//單擊除號按鈕獲得x的值和z的值並空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e2.getSource()==b26)//單擊等號按鈕輸出計算結果
{
str.setLength(0);
switch(z)
{
case 0 : tf1.setText(""+(x+y));break;
case 1 : tf1.setText(""+(x-y));break;
case 2 : tf1.setText(""+(x*y));break;
case 3 : tf1.setText(""+(x/y));break;
}
}
else if(e2.getSource()==b24)//單擊"."按鈕輸入小數
{
if(tf1.getText().trim().indexOf(′.′)!=-1)//判斷字元串中是否已經包含了小數點
{

}
else//如果沒數點有小
{
if(tf1.getText().trim().equals("0"))//如果初時顯示為0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}

y=0d;

}
else if(e2.getSource()==b11)//求平方根
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText("數字格式異常");
if(x<0)
tf1.setText("負數沒有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b16)//單擊了"%"按鈕
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b21)//單擊了"1/X"按鈕
{

x=Double.parseDouble(tf1.getText().trim());
if(x==0)
{

tf1.setText("除數不能為零");
}
else
{
tf1.setText(""+(1/x));
}
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b3)//MC為清除內存
{
m=0d;
tf2.setText("");
str.setLength(0);
}
else if(e2.getSource()==b4)//MR為重新調用存儲的數據
{
if(tf2.getText().trim()!="")//有記憶數字
{
tf1.setText(""+m);
}
}
else if(e2.getSource()==b5)//MS為存儲顯示的數據
{

m=Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b6)//M+為將顯示的數字與已經存儲的數據相加要查看新的數字單擊MR
{
m=m+Double.parseDouble(tf1.getText().trim());
}
else//選擇的是其他的按鈕
{
if(e2.getSource()==b22)//如果選擇的是"0"這個數字鍵
{
if(tf1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作
{

}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
else if(e2.getSource()==b0)//選擇的是「BackSpace」按鈕
{
if(!tf1.getText().trim().equals("0"))//如果顯示屏顯示的不是零
{
if(str.length()!=1)
{
tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字元串越界異常
}
else
{
tf1.setText("0");
str.setLength(0);
}
}
y=Double.parseDouble(tf1.getText().trim());
}
else//其他的數字鍵
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
}
catch(NumberFormatException e){
tf1.setText("數字格式異常");
}
catch( e){
tf1.setText("字元串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
}

}

你在JAVA的環境中運行一下。
這題目也是我的作業,我運行的了。

Ⅳ Android新手入門,求一個計算器的源代碼,不要太復雜的就好。。。

#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include "unistd.h"
#define N 200

SDL_Window *win = NULL;
SDL_Renderer *ren = NULL;

SDL_Texture *tex[3] = { NULL };
SDL_Surface *sur[2] = { NULL };

SDL_Color color[2] = {
{255, 0, 0}
, {0, 255, 0}
};

SDL_Rect p[3];

SDL_Event e;

TTF_Font *font[3] = { NULL };

char *pic = "/picture/cbg.png",*ttf = "/font/DroidSans.ttf"; /*
所應用文件的相對路徑*/
char way[2][100]; /*
存儲路徑 */
char token[N]; /* 存放表達式字元串的數組 */
char str[N]; /* 獲取輸入字元串數組 */
char result[N / 4]; /* 存放結果的數組 */
char K = 'k'; /* 用來判斷觸摸字元 */
int W, H; /* 屏幕寬高 */
int x, y, y_1, pst = -1; /* 用來獲取觸摸位置 */
int n = 0, m = 0, t = 0; /* 初始化 */
int computed = 0; /* 判斷是否已經計算0表示沒有計算 */
bool quit = false;

void init(); // 載入

void clear(); // 清理

Ⅳ java計算器源代碼

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
initComponents();
}

private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
result = new javax.swing.JTextField();
addBtn = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Num1:");

jLabel2.setText("Num2:");

jLabel3.setText("Num3:");

addBtn.setText("Add");
addBtn.addActionListener(new jisuanAC());

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addBtn)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(result)
.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGap(168, 168, 168))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(61, 61, 61)
.addComponent(addBtn)
.addContainerGap(81, Short.MAX_VALUE))
);

jMenu1.setText("Operation");

jMenu3.setText("Add");
jMenu1.add(jMenu3);

jMenuBar1.add(jMenu1);

jMenu2.setText("Exit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private class jisuanAC implements ActionListener
{

public void actionPerformed(ActionEvent e) {
if(e.getSource()== addBtn)
{
int number1 = Integer.parseInt(num1.getText());
int number2 = Integer.parseInt(num2.getText());
int rs = number1 + number2;
result.setText(String.valueOf(rs));

}
}

}

// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField result;

}

這是只有一個加法的例子!希望幫到你

Ⅵ C#計算器源代碼

namespace jisuanji
{
public partial class jisuanqi : Form
{
public jisuanqi()
{
InitializeComponent();
}

private void jisuanqi_Load(object sender, EventArgs e)
{
txtMath.Text = "0.0";
}

//存放輸入的數字

private string str1 = "";

//存放第二個數字

private string str2 = "";

//存放執行的什麼操作 如:+,-,*,/

private string action = "";

//判斷是否點擊了「=」,默認是沒有點擊

private bool Equal_flag = false;

private void check(Button btnname)
{
if (action.Equals(""))
{
if (txtMath.Text != "0" || txtMath.Text != "0.0")
{
str1 += btnname.Text;
txtMath.Text = str1.ToString();
}
//如果是第一次輸入數字
if (txtMath.Text.Equals("0.0") || txtMath.Text.Equals("0"))
{
txtMath.Text = btnname.Text;
str1 = btnname.Text.ToString();
}
}

//如果已經輸入了數字且沒有點擊「=」
if(!action.Equals("") && txtMath.Text.Length != 0 && (!txtMath.Text.Equals("0.0")) && (!txtMath.Text.Equals("0")))
{
str2 += btnname.Text;
txtMath.Text += btnname.Text;

}

//如果已經輸入了數字並執行了加減乘除的操作,第二個數字輸入時
else if ((!action.Equals("")) && txtMath.Text.Length != 0 && !txtMath.Text.Equals(0.0) && (!txtMath.Text.Equals("0")))
{
txtMath.Text += btnname.Text;
//str2 = txtMath.Text.ToString();
str2 += btnname.Text;

}
else if (txtMath.Text.Equals("") && (!action.Equals("")))
{
txtMath.Text += btnname.Text;
str2 = txtMath.Text;

}
}

private void btnZero_Click(object sender, EventArgs e)
{
if (Equal_flag)
{

//如果沒有點擊「+-*/」
if (action.Equals(""))
{
if (str1.Equals(""))
{
MessageBox.Show("您的操作有誤", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
check(btnZero);
}
else
{
check(btnZero);
Equal_flag = false;
}
}

private void btnDot_Click(object sender, EventArgs e)
{
//點擊過等號
if (Equal_flag)
{
//第二個數字沒有輸入的時候
if (str2.Equals(""))
{
//第一個數字已經有點號存在
if (str1.IndexOf('.') > -1)
{
MessageBox.Show("您的操作有誤", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtMath.Text = str1;
return;
}
check(btnDot);
}
//第二個數字輸入了的時候
else
{
//第二個數字中不存在點號的時候
if (!(str2.IndexOf('.') > -1))
{
check(btnDot);
return;
}
//第二個數字中存在點號的時候
MessageBox.Show("您的操作有誤", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//沒有點擊過等號
else
{
//第一個數字存在點號
if (str1.IndexOf('.') > -1)
{
MessageBox.Show("您的操作有誤", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtMath.Text = str1;
return;
}
//第一個數字不存在點號的時候
else
{
//如果第一個數字還是空的時候
if (!str1.Equals(""))
{
check(btnDot);
//Equal_flag = false;
return;
}
txtMath.Text = "0" + btnDot.Text;
str1 = txtMath.Text;
}
}
}

private void btnOne_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnOne);
}
else
{
check(btnOne);
Equal_flag = false;
}
}

private void btnTwo_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnTwo);
}
else
{
check(btnTwo);
Equal_flag = false;
}
}

private void btnthree_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnthree);
}
else
{
check(btnthree);
Equal_flag = false;
}
}

private void btnFour_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
check(btnFour);
}
else
{
check(btnFour);
Equal_flag = false;
}
}

private void btnFive_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnFive);
}
else
{
check(btnFive);
Equal_flag = false;
}
}

private void btnSix_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnSix);
}
else
{
check(btnSix);
Equal_flag = false;
}
}

private void btnSeven_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnSeven);
}
else
{
check(btnSeven);
Equal_flag = false;
}
}

private void btnEight_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnEight);
}
else
{
check(btnEight);
Equal_flag = false;
}
}

private void btnNine_Click(object sender, EventArgs e)
{
if (Equal_flag)
{
if (!str1.Equals(""))
{
if (action.Equals(""))
{
MessageBox.Show("請選擇運算符", "信息提示");
return;
}
}
check(btnNine);
}
else
{
check(btnNine);
Equal_flag = false;
}
}
private void btnQual_Click(object sender, EventArgs e)
{
str_check(btnQual);
str2 = "";
Equal_flag = true;
action = "";

}

private string calcute(string str1, string str2, string action)
{
switch (action)
{
case "+":
str1 = (double.Parse(str1) + double.Parse(str2)).ToString();
return str1;
case "-":
str1 = (double.Parse(str1) - double.Parse(str2)).ToString();
return str1;
case "*":
str1 = (double.Parse(str1) * double.Parse(str2)).ToString();
return str1;
case "/":
if (str2.Equals("0.0") || str2.Equals("0"))
{
MessageBox.Show("被除數不能為0", "警告");
return str1;
}
str1 = (double.Parse(str1) / double.Parse(str2)).ToString();
return str1;
default :
MessageBox.Show("操作錯誤!", "提示");
return "wrong";
}
}

private void str_check(Button btnname)
{
//完成上一次計算後點擊運算按鈕應將結果作為第一個數
if (Equal_flag)
{
//第二個數還沒輸入的時候
if ((!str1.Equals("")) && (str2.Equals("")))
{
if (!(btnname.Text.IndexOf('=') > -1))
{
action = btnname.Text;
txtMath.Text += btnname.Text;
}
}
//兩個都不為空,第二個數已經輸入,應將他們進行運算
if ((!str1.Equals("")) && (!str2.Equals("")))
{
str1 = calcute(str1, str2, action);
txtMath.Text = str1.ToString();
}
//直接點擊操作錯誤
else if (str1.Equals(""))
{
MessageBox.Show("請輸入要進行運算的數字", "信息提示");
}
}

//沒有點擊「=」
else if (!Equal_flag)
{

//第二個數還沒輸入的時候
if ((!str1.Equals("")) && (str2.Equals("")))
{
if (!(btnname.Text.IndexOf('=') > -1))
{
action = btnname.Text;
txtMath.Text += btnname.Text;
}
}
//兩個都不為空,第二個數已經輸入,應將他們進行運算
if ((!str1.Equals("")) && (!str2.Equals("")))
{
str1 = calcute(str1, str2, action);
txtMath.Text = str1.ToString();
}
//直接點擊操作錯誤
else if (str1.Equals(""))
{
MessageBox.Show("請輸入要進行運算的數字", "信息提示");
}
}
}

private void btnPlus_Click(object sender, EventArgs e)
{
str_check(btnPlus);
}

private void btnMinus_Click(object sender, EventArgs e)
{
str_check(btnMinus);
}

private void btnTimes_Click(object sender, EventArgs e)
{
str_check(btnTimes);
}

private void btnDivide_Click(object sender, EventArgs e)
{
str_check(btnDivide);
}

private void btnRest_Click(object sender, EventArgs e)
{
txtMath.Text = "0.0";
str1 = str2 = "";
action = "";
Equal_flag = false;
}
}
}

閱讀全文

與裝修計算器源碼大全相關的資料

熱點內容
如何做伺服器服務商 瀏覽:759
su剖切命令 瀏覽:726
devc編譯背景 瀏覽:209
學習單片機的意義 瀏覽:49
音頻演算法AEC 瀏覽:909
加密貨幣容易被盜 瀏覽:82
蘋果平板如何開啟隱私單個app 瀏覽:704
空調壓縮機一開就停止 瀏覽:528
如何下載虎牙app 瀏覽:847
日語年號的演算法 瀏覽:955
dev裡面的編譯日誌咋調出來 瀏覽:298
php函數引用返回 瀏覽:816
文件夾和文件夾的創建 瀏覽:259
香港加密貨幣牌照 瀏覽:838
程序員鼓勵自己的代碼 瀏覽:393
計算機網路原理pdf 瀏覽:752
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:94
php中sleep 瀏覽:490
vr怎麼看視頻演算法 瀏覽:87
手機app如何申報個人所得稅零申報 瀏覽:694