當前位置:首頁 » 編程軟體 » 編程實現計算器

編程實現計算器

發布時間: 2022-05-25 09:26:22

『壹』 c語言設計一個簡單的計算器程序

#include<stdio.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(1)編程實現計算器擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

『貳』 用c語言編寫一個簡單計算器程序

#include<stdio.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(2)編程實現計算器擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

『叄』 怎麼用java編程編寫一個計算器

  1. 打開IED:打開自己java編程的軟體,採用的是eclipse軟體。

  2. 建立java工程。

  3. 編寫類。

『肆』 Java編程:2.課題:計算器設計與實現

1、打開eclipse,建好工程,然後建好你的package,包名起bag01
2、建立你的class類,名字起JFrameDemo『
3、刪除掉class文件里所有內容,留出空白頁
4、復制粘貼下面的代碼
註:確保你的jdk版本里有swing圖形化界面

package bag01;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* 本計算器只能運算單次,不支持連等式和連續運算
*/
public class JFrameDemo extends JFrame {
JFrame jf = new JFrame();
Container u = jf.getContentPane();
JPanel mp = new JPanel();
JLabel jl = new JLabel("賊牛逼計算器");

JTextField jt = new JTextField();
int num1; //第一次按下運算符號按鈕之前輸入的數字
int num2; //第二次按下運算符號按鈕之前輸入的數字
int ks = 0; //若按下加號ks賦值1,減號賦值2,乘號賦值3,除號賦值4,並在最終根據ks的值選擇運算方式
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt10 = new JButton("+");
JButton bt11 = new JButton("-");
JButton bt12 = new JButton("=");
JButton bt13 = new JButton("0");
JButton bt14 = new JButton("X");
JButton bt15 = new JButton("/");
List<String> list = new ArrayList();

public void Chuangkou() {
// 設置窗口屬性
jf.setSize(400, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setLayout(null);
jf.setLocationRelativeTo(null);
jf.setTitle("非常好計算器");

jl.setBounds(120, 50, 200, 25);
jl.setFont(new Font("黑體", Font.BOLD, 25));

//設置容器屬性
u.setBackground(Color.white);

//設置面板屬性
mp.setSize(400, 500);
mp.setVisible(true);
mp.setBackground(Color.pink);
mp.add(jl);
mp.setLayout(null);
u.add(mp);

//設置輸入框屬性
jt.setBounds(100, 100, 200, 50);
jt.setFont(new Font("黑體", Font.BOLD, 25));
mp.add(jt);

//設置按鈕屬性
bt1.setBounds(50, 180, 100, 50);
bt1.setFont(new Font("黑體", Font.BOLD, 30));
bt2.setBounds(150, 180, 100, 50);
bt2.setFont(new Font("黑體", Font.BOLD, 30));
bt3.setBounds(250, 180, 100, 50);
bt3.setFont(new Font("黑體", Font.BOLD, 30));
bt4.setBounds(50, 230, 100, 50);
bt4.setFont(new Font("黑體", Font.BOLD, 30));
bt5.setBounds(150, 230, 100, 50);
bt5.setFont(new Font("黑體", Font.BOLD, 30));
bt6.setBounds(250, 230, 100, 50);
bt6.setFont(new Font("黑體", Font.BOLD, 30));
bt7.setBounds(50, 280, 100, 50);
bt7.setFont(new Font("黑體", Font.BOLD, 30));
bt8.setBounds(150, 280, 100, 50);
bt8.setFont(new Font("黑體", Font.BOLD, 30));
bt9.setBounds(250, 280, 100, 50);
bt9.setFont(new Font("黑體", Font.BOLD, 30));
bt10.setBounds(50, 330, 100, 50);
bt10.setFont(new Font("黑體", Font.BOLD, 30));
bt11.setBounds(50, 380, 100, 50);
bt11.setFont(new Font("黑體", Font.BOLD, 30));
bt12.setBounds(250, 380, 100, 50);
bt12.setFont(new Font("黑體", Font.BOLD, 30));
bt13.setBounds(150, 330, 100, 50);
bt13.setFont(new Font("黑體", Font.BOLD, 30));
bt14.setBounds(150, 380, 100, 50);
bt14.setFont(new Font("黑體", Font.BOLD, 30));
bt15.setBounds(250, 330, 100, 50);
bt15.setFont(new Font("黑體", Font.BOLD, 30));
mp.add(bt1);
mp.add(bt2);
mp.add(bt3);
mp.add(bt4);
mp.add(bt5);
mp.add(bt6);
mp.add(bt7);
mp.add(bt8);
mp.add(bt9);
mp.add(bt10);
mp.add(bt11);
mp.add(bt12);
mp.add(bt13);
mp.add(bt14);
mp.add(bt15);

//按鈕事件監聽
bt1.addActionListener(new ButtonAction());
bt2.addActionListener(new ButtonAction());
bt3.addActionListener(new ButtonAction());
bt4.addActionListener(new ButtonAction());
bt5.addActionListener(new ButtonAction());
bt6.addActionListener(new ButtonAction());
bt7.addActionListener(new ButtonAction());
bt8.addActionListener(new ButtonAction());
bt9.addActionListener(new ButtonAction());
bt10.addActionListener(new ButtonAction());
bt11.addActionListener(new ButtonAction());
bt12.addActionListener(new ButtonAction());
bt13.addActionListener(new ButtonAction());
bt14.addActionListener(new ButtonAction());
bt15.addActionListener(new ButtonAction());
}

public class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object s = e.getSource();
if (s == bt1) {
list.add("1");
System.out.println(list);

//定義一個迭代器k,值是迭代的list
Iterator<String> k = list.iterator();
String str1 = ""; //定義一個空字元串

//定義一個循環,用來遍歷list
while (k.hasNext()) { //判斷k是否存在下一個值,如果是true則繼續
String str = (String) k.next(); //定義一個字元串str,值是k當前的遍歷
str1 = str1 + str; //將遍歷出的字元串一個一個添加進str1字元串中
}
jt.setText(str1); //輸出最終字元串實現按下的按鈕都會顯示出來
}
if (s == bt2) {
list.add("2");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt3) {
list.add("3");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt4) {
list.add("4");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt5) {
list.add("5");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt6) {
list.add("6");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt7) {
list.add("7");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt13) {
list.add("0");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt8) {
list.add("8");
System.out.println(list);
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt9) {
list.add("9");
System.out.println(list);
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
jt.setText(str1);
}
if (s == bt10) {
jt.setText("加上");
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
int a = Integer.parseInt(str1); //把字元串str1轉換為數字類型
num1 = a;
ks = 1;
System.out.println(a);
list.removeAll(list);
}
if (s == bt11) {
jt.setText("減去");
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
int a = Integer.parseInt(str1);
num1 = a;
ks = 2;
System.out.println(a);
list.removeAll(list);
}
if (s == bt14) {
jt.setText("乘以");
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
int a = Integer.parseInt(str1);
num1 = a;
ks = 3;
System.out.println(a);
list.removeAll(list);
}
if (s == bt15) {
jt.setText("除以");
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
int a = Integer.parseInt(str1);
num1 = a;
ks = 4;
System.out.println(a);
list.removeAll(list);
}
if (s == bt12) {
Iterator<String> k = list.iterator();
String str1 = "";
while (k.hasNext()) {
String str = (String) k.next();
str1 = str1 + str;
}
int a = Integer.parseInt(str1);
num2 = a;
System.out.println(a);
list.removeAll(list);
if (ks == 1) {
int num3 = num1 + num2;
System.out.println(num3);
Integer inta = Integer.valueOf(num3);
String q = inta.toString();
jt.setText(q);
}
if (ks == 2) {
int num3 = num1 - num2;
System.out.println(num3);
Integer inta = Integer.valueOf(num3);
String q = inta.toString();
jt.setText(q);
}
if (ks == 3) {
int num3 = num1 * num2;
System.out.println(num3);
Integer inta = Integer.valueOf(num3);
String q = inta.toString();
jt.setText(q);
}
if (ks == 4) {
if (num2 == 0) {
jt.setText("除數不能為0");
} else {
int num3 = num1 / num2;
int num4 = num1 % num2;
System.out.println(num3 + "……" + num4);
Integer inta = Integer.valueOf(num3);
String q = inta.toString();
Integer inta2 = Integer.valueOf(num4);
String q1 = inta2.toString();
jt.setText(q + "余" + q1);
}
}
}
}
}

public static void main(String[] args) {
new JFrameDemo().Chuangkou();
}
}

『伍』 編程實現一個科學計算器,能夠實現加減乘除,三角函數計算等。用戶界面自己設計

java寫的,可行

package ex1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;

public class Calcutor extends JFrame {
private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;//單選框
private JCheckBoxMenuItem seeItem3;//復選框
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, flag = false;
private String oper = "=";
private double result = 0;
private Num numActionListener;
private DecimalFormat df;

public Calcutor(){
super("科學計算器");//設置標題欄

df = new DecimalFormat("#.####");//保留四位小數

this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5行6列
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));

/**
* 菜單欄
*/
myBar = new JMenuBar();
menu1 = new JMenu("編輯(E)");
menu2 = new JMenu("查看(V)");
menu3 = new JMenu("幫助(H)");

menu1.setFont(new Font("宋體", Font.PLAIN, 12));
menu2.setFont(new Font("宋體", Font.PLAIN, 12));
menu3.setFont(new Font("宋體", Font.PLAIN, 12));

/**
* 編輯欄
*/
editItem1 = new JMenuItem("復制(C) Ctrl+C");
editItem2 = new JMenuItem("粘貼(P) Ctrl+V");

editItem1.setFont(new Font("宋體",Font.PLAIN,12));
editItem2.setFont(new Font("宋體",Font.PLAIN,12));

/**
* 查看欄
*/
seeItem1 = new JRadioButtonMenuItem("科學型(T)");
seeItem2 = new JRadioButtonMenuItem("標准型(S)");
seeItem3 = new JCheckBoxMenuItem("數字分組(I)");

seeItem1.setFont(new Font("宋體",Font.PLAIN,12));
seeItem2.setFont(new Font("宋體",Font.PLAIN,12));
seeItem3.setFont(new Font("宋體",Font.PLAIN,12));

/**
* 幫助欄
*/
help1 = new JMenuItem("幫助主題(H)");
help2 = new JMenuItem("關於計算器(A)");

help1.setFont(new Font("宋體",Font.PLAIN,12));
help2.setFont(new Font("宋體",Font.PLAIN,12));

bgb = new ButtonGroup();//選項組

menu1.add(editItem1);
menu1.add(editItem2);

menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();//添加一條分割線
menu2.add(seeItem3);

menu3.add(help1);
menu3.addSeparator();//添加一條分割線
menu3.add(help2);

myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);

this.setJMenuBar(myBar);

numActionListener = new Num();//實現數字監聽

/**
* 文本域,即為計算器的屏幕顯示區域
*/
tf = new JTextField();
tf.setEditable(false);//文本區域不可編輯
tf.setBackground(Color.white);//文本區域的背景色
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右對齊
tf.setText("0");
tf.setBorder(BorderFactory.createLoweredBevelBorder());
init();//對計算器進行初始化

}
/**
* 初始化操作
* 添加按鈕
*/
private void init(){
addButton(panel1, "Backspace", new Clear(), Color.red);
addButton(panel1, "CE", new Clear(), Color.red);
addButton(panel1, "C", new Clear(), Color.red);

addButton(panel2, "1/x", new Signs(), Color.magenta);
addButton(panel2, "log", new Signs(), Color.magenta);
addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "÷", new Signs(), Color.red);

addButton(panel2, "n!", new Signs(), Color.magenta);
addButton(panel2, "sqrt", new Signs(), Color.magenta);
addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "×", new Signs(), Color.red);

addButton(panel2, "sin", new Signs(), Color.magenta);
addButton(panel2, "x^2", new Signs(), Color.magenta);
addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new Signs(), Color.red);

addButton(panel2, "cos", new Signs(), Color.magenta);
addButton(panel2, "x^3", new Signs(), Color.magenta);
addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new Clear(), Color.blue);
addButton(panel2, ".", new Dot(), Color.blue);
addButton(panel2, "+", new Signs(), Color.red);

addButton(panel2, "tan", new Signs(), Color.magenta);
addButton(panel2, "%", new Signs(), Color.magenta);
addButton(panel2, "π", numActionListener, Color.orange);
addButton(panel2, "e", numActionListener, Color.orange);
addButton(panel2, "′″", new Signs(), Color.orange);
addButton(panel2, "=", new Signs(), Color.red);

JButton btns = new JButton("計算器");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);//按鈕不可操作
btns.setPreferredSize(new Dimension(20, 20));

panel3.add(btns);//加入按鈕
addButton(panel3, "MC", null, Color.red);
addButton(panel3, "MR", null, Color.red);
addButton(panel3, "MS", null, Color.red);
addButton(panel3, "M+", null, Color.red);

panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);

pack();
this.setResizable(false);//窗口不可改變大小
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 統一設置按鈕的的使用方式
* @param panel
* @param name
* @param action
* @param color
*/
private void addButton(JPanel panel, String name, ActionListener action, Color color){
JButton bt = new JButton(name);
panel.add(bt);//在面板上增加按鈕
bt.setForeground(color);//設置前景(字體)顏色
bt.addActionListener(action);//增加監聽事件
}
/**
* 計算器的基礎操作(+ - × ÷)
* @param x
*/
private void getResult (double x){
if(oper == "+"){result += x;}
else if(oper == "-"){result -= x;}
else if(oper == "×"){result *= x;}
else if(oper == "÷"){result /= x;}
else if(oper == "="){result = x;}
tf.setText(df.format(result));
}
/**
* 運算符號的事件監聽
*/
class Signs implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent對象的getActionCommand()方法
* 取得與引發事件對象相關的字元串
*/
String str = e.getActionCommand();

/* sqrt求平方根 */
if(str.equals("sqrt")){
double i = Double.parseDouble(tf.getText());
if(i>=0){
/*
* String.valueOf() 轉換為字元串
* df.format() 按要求保留四位小數
* Math.sqrt() 求算數平方根
*/
tf.setText(String.valueOf(df.format(Math.sqrt(i))));
}
else{
tf.setText("負數不能開平方根");
}
}

/* log求常用對數 */
else if(str.equals("log")){
double i = Double.parseDouble(tf.getText());
if(i>0){
tf.setText(String.valueOf(df.format(Math.log(i))));
}else{
tf.setText("負數不能求對數");
}
}

/* %求百分比 */
else if(str.equals("%")){
tf.setText(df.format(Double.parseDouble(tf.getText()) / 100));
}

/* 1/x求倒數 */
else if(str.equals("1/x")){
if(Double.parseDouble(tf.getText()) == 0){
tf.setText("除數不能為零");
}else{
tf.setText(df.format(1 / Double.parseDouble(tf.getText())));
}
}

/* sin求正弦函數 */
else if(str.equals("sin")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.sin(i))));
}

/* cos求餘弦函數 */
else if(str.equals("cos")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.cos(i))));
}

/* tan求正切函數 */
else if(str.equals("tan")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.tan(i))));
}

/* n!求階乘 */
else if(str.equals("n!")){
double i = Double.parseDouble(tf.getText());
if((i%2==0)||(i%2==1))//判斷為整數放進行階乘操作
{
int j = (int)i;//強制類型轉換
int result=1;
for(int k=1;k<=j;k++)
result *= k;
tf.setText(String.valueOf(result));
}
else
{
tf.setText("無法進行階乘");
}
}

/* x^2求平方 */
else if(str.equals("x^2")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i)));
}

/* x^3求立方 */
else if(str.equals("x^3")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i*i)));
}

/* ′″角度轉換 */
/**
* 將角度值轉換成弧度值,方便三角函數的計算
*/
else if(str.equals("′″")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(i/180*Math.PI));
}

else{
if(flag){
IfResult = false;
}
if(IfResult){
oper = str;
}else{
getResult(Double.parseDouble(tf.getText()));
oper = str;
IfResult = true;
}
}
}
}
/**
* 清除按鈕的事件監聽
*/
class Clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent對象的getActionCommand()方法
* 取得與引發事件對象相關的字元串
*/
String str = e.getActionCommand();
if(str == "C"){
tf.setText("0");
IfResult = true;
result = 0;
}else if(str == "-/+"){
double i = 0 - Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}else if(str == "Backspace"){
if(Double.parseDouble(tf.getText()) > 0){
if(tf.getText().length() > 1){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
//使用退格刪除最後一位字元
}else{
tf.setText("0");
IfResult = true;
}
}else{
if(tf.getText().length() > 2){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}else{
tf.setText("0");
IfResult = true;
}
}
}else if(str == "CE"){
tf.setText("0");
IfResult = true;
}
}
}
/**
* 數字輸入的事件監聽
*/
class Num implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(IfResult){
tf.setText("");
IfResult = false;
}
if(str=="π")
{
tf.setText(String.valueOf(Math.PI));
}
else if(str=="e")
{
tf.setText(String.valueOf(Math.E));
}
else{
tf.setText(tf.getText().trim() + str);
if(tf.getText().equals("0")){
tf.setText("0");
IfResult = true;
flag = true;
}
}
}
}
/**
* 小數點的事件監聽
*/
class Dot implements ActionListener{
public void actionPerformed(ActionEvent e) {
IfResult = false;
if(tf.getText().trim().indexOf(".") == -1){
tf.setText(tf.getText() + ".");
}
}
}
/**
* main方法
*/
public static void main(String[] args) {
new Calcutor().setVisible(true);
}
}

『陸』 用java語言編程實現計算器的基本功能

import java.awt.*;
import java.awt.event.*;

public class JSQ extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[19];
private String ss[] = { "1", "2", "3", "+", "-", "4", "5", "6", "*", "/",
"7", "8", "9", "0", "=", "%", "清空", "sqrt", "關閉", };
static double a;
static String s = "", str;// 定義變數 創建對像

public static void main(String args[]) {
(new JSQ()).frame();
}

public void frame() {
Frame fm = new Frame("計算器");
for (int i = 0; i <= 18; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i <= 17; i++) {
p2.add(b[i]);
} // 創建按鈕 並添加到P2
b[18].setBackground(Color.yellow);
txt = new TextField(16);
txt.setEditable(false);
for (int i = 0; i <= 18; i++) {
b[i].addActionListener(new buttonlistener());// 添加監聽器
}
b[18].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "North");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[18]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);// 都是些窗中設置 添加相關組件和監聽器
}

public void windowClosing(WindowEvent e) {
System.exit(0);// 退出系統
}

class buttonlistener implements ActionListener {// 編寫監聽器事件 通過按鍵得出給果
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel().equals("=")) {
if (s.equals("sqrt")) {
str = String.valueOf(a);
txt.setText(str);
s = "";
} else {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
}
} else if (btn.getLabel().equals("+")) {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel().equals("-")) {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel().equals("/")) {
jisuan();
txt.setText("");
s = "/";

} else if (btn.getLabel().equals("*")) {
jisuan();
txt.setText("");
s = "*";
} else if (btn.getLabel().equals("%")) {
jisuan();
txt.setText("");
s = "%";
} else if (btn.getLabel().equals("sqrt")) {
s = "sqrt";
jisuan();
txt.setText("");
} else {
txt.setText(txt.getText() + btn.getLabel());

if (btn.getLabel() == "清空")
txt.setText("");
}
}

public void jisuan() {// 編寫具體計算方法
if (s.equals("+"))
a += Double.parseDouble(txt.getText());
else if (s.equals("-"))
a -= Double.parseDouble(txt.getText());
else if (s.equals("*"))
a *= Double.parseDouble(txt.getText());
else if (s.equals("/"))
a /= Double.parseDouble(txt.getText());
else if (s.equals("%"))
a %= Double.parseDouble(txt.getText());
else if (s.equals("sqrt"))
a = Math.sqrt(Double.parseDouble(txt.getText()));
else
a = Double.parseDouble(txt.getText());
}
}
}

class close implements ActionListener {// 退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

『柒』 C語言編寫簡易計算器程序

C語言編寫計算器

  • 我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf

『捌』 請教一下如何編程一個簡易計算器

方法/步驟
1
打開Microsoft
Visual
Studio
2010,
新建名字為【計算器】的程序。
2
在新程序界面空白窗口上放置合適的控制項:包括,
顯示過程數字和結果數字的textbox控制項;
用於各數字輸入和計算方式輸入的按鈕,包括1,2,3,4,5,6,7,9,0和加減乘除等符號。
3
添加變數temp1(第一個數字)和pos(計算方式)
4
添加類addNum,在點擊數字按鈕時使用,用於在顯示屏上增加用戶輸入的數字。
添加數字按鈕0-9的代碼,分別調用類addNum。
添加【=】按鈕代碼
記錄第二個數字;
根據計算方式進行計算,顯示計算結果。
添加歸零按鈕的代碼,清空顯示屏,將計算結果歸零,將計算方式歸零。
生成exe文件進行測試
輸入第一個數字425
點擊【+】,輸入第二個數字897
點擊【=】,獲取計算結果1322
點擊【CE】,將數字歸零
輸入第一個數字852
點擊【*】,輸入第二個數字84
點擊【=】,獲取計算結果71568
15
同樣可以驗證減法和除法。

『玖』 用JAVA編寫一個計算器

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

importjavax.swing.SwingConstants;

{

/**

*

*/

=1L;

Resultresult=newResult();//定義text的面板

Number_Keynumber_key=newNumber_Key();//定義按鈕面板

//當點擊按鈕+、-、*、/時,com=true

booleancom=false;

//當i=0時說明是我們第一次輸入,字元串text不會累加

inti=0;

//存放text的內容

Stringtext="";

//存放點擊按鈕+、-、*、/之前的數值

doubledefbutton=0;

//+、-、*、/的代號分別為1,2,3,4

intsymbol=0;

//構造函數

Jisuanqi(){

super("WangJiao");//設定標題

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉窗體時退出程序

JPanelpane=newJPanel();//定義主面板

pane.setLayout(newBorderLayout());

setBounds(380,220,30,80);//前兩個參數是在屏幕上顯示的坐標,後兩個是大小

//替換圖標

ImageIconicon=newImageIcon("F:1.GIF");

//Jisuanqi.class.getResource("APPLE.GIF")

//);

setIconImage(icon.getImage());

pane.add(result,BorderLayout.NORTH);

pane.add(number_key,BorderLayout.CENTER);

pane.add(number_key.equal,BorderLayout.SOUTH);

number_key.one.addActionListener(this);//對1按鈕添加監聽事件

number_key.two.addActionListener(this);//對2按鈕添加監聽事件

number_key.three.addActionListener(this);//對3按鈕添加監聽事件

number_key.four.addActionListener(this);//對4按鈕添加監聽事件

number_key.five.addActionListener(this);//對5按鈕添加監聽事件

number_key.six.addActionListener(this);//對6按鈕添加監聽事件

number_key.seven.addActionListener(this);//對7按鈕添加監聽事件

number_key.eight.addActionListener(this);//對8按鈕添加監聽事件

number_key.nine.addActionListener(this);//對9按鈕添加監聽事件

number_key.zero.addActionListener(this);//對0按鈕添加監聽事件

number_key.ce.addActionListener(this);//對置零按鈕添加監聽事件

number_key.plus.addActionListener(this);//對+按鈕添加監聽事件

number_key.equal.addActionListener(this);//對=按鈕添加監聽事件

number_key.sub.addActionListener(this);//對-按鈕添加監聽事件

number_key.mul.addActionListener(this);//對*按鈕添加監聽事件

number_key.div.addActionListener(this);//對/按鈕添加監聽事件

number_key.point.addActionListener(this);//對.按鈕添加監聽事件

setContentPane(pane);

pack();//初始化窗體大小為正好盛放所有按鈕

}

//各個按鈕觸發的事件

publicvoidactionPerformed(ActionEvente){

/*

*如果是點擊數字按鈕那麼先要判斷是否在此之前點擊了+、-、*、/、=,如果是那麼com=true如果沒有com=

*false;或者是否點擊數字鍵,如果是i=1,如果沒有i=0;

*/

if(e.getSource()==number_key.one){

if(com||i==0){

result.text.setText("1");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"1");

}

}elseif(e.getSource()==number_key.two){

if(com||i==0){

result.text.setText("2");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"2");

}

}elseif(e.getSource()==number_key.three){

if(com||i==0){

result.text.setText("3");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"3");

}

}elseif(e.getSource()==number_key.four){

if(com||i==0){

result.text.setText("4");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"4");

}

}elseif(e.getSource()==number_key.five){

if(com||i==0){

result.text.setText("5");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"5");

}

}elseif(e.getSource()==number_key.six){

if(com||i==0){

result.text.setText("6");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"6");

}

}elseif(e.getSource()==number_key.seven){

if(com||i==0){

result.text.setText("7");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"7");

}

}elseif(e.getSource()==number_key.eight){

if(com||i==0){

result.text.setText("8");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"8");

}

}elseif(e.getSource()==number_key.nine){

if(com||i==0){

result.text.setText("9");

com=false;

i=1;

}else{

text=result.text.getText();

result.text.setText(text+"9");

}

}

/*

*對於0這個按鈕有一定的說法,在我的程序里不會出現如00000這樣的情況,我加了判斷條件就是

*如果text中的數值=0就要判斷在這個數值中是否有.存在?如果有那麼就在原來數值基礎之上添加0;否則保持原來的數值不變

*/

elseif(e.getSource()==number_key.zero){//result.text.getText()是得到text里內容的意思

if(com||i==0){

result.text.setText("0");

com=false;

i=1;

}else{

text=result.text.getText();

if(Float.parseFloat(text)>0||Float.parseFloat(text)<0){//Float.parseFloat(text)就是類型轉換了,下面都是一樣

result.text.setText(text+"0");

}else{

if(text.trim().indexOf(".")==-1){

result.text.setText(text);

}else{

result.text.setText(text+"0");

}

}

}

}elseif(e.getSource()==number_key.ce){

result.text.setText("0");

i=0;

com=true;

//text="";

defbutton=0;

}

/*

*本程序不會讓一個數值中出現2個以上的小數點.具體做法是:判斷是否已經存在.存在就不添加,不存在就添加.

*/

elseif(e.getSource()==number_key.point){

if(com||i==0){

result.text.setText("0.");

com=false;

i=1;

}else{

text=result.text.getText();

if(text.trim().indexOf(".")==-1){

result.text.setText(text+".");

}else{

result.text.setText(text);

}

}

}//獲得點擊+之前的數值

elseif(e.getSource()==number_key.plus){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=1;

}//獲得點擊-之前的數值

elseif(e.getSource()==number_key.sub){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=2;

}//獲得點擊*之前的數值

elseif(e.getSource()==number_key.mul){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol=3;

}//獲得點擊/之前的數值

elseif(e.getSource()==number_key.div){

com=true;

i=0;

defbutton=Double.parseDouble(result.text.getText());

symbol=4;

}elseif(e.getSource()==number_key.equal){

switch(symbol){

case1:{//計算加法

doublead=defbutton

+Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

case2:{//計算減法

doublead=defbutton

-Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i=0;

text="";

break;

}

case3:{//計算乘法

doublead=defbutton

*Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

case4:{//計算除法

doublead=defbutton

/Double.parseDouble(result.text.getText());

result.text.setText(ad+"");

i=0;

text="";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

publicstaticvoidmain(String[]args){

Jisuanqiloveyou=newJisuanqi();

loveyou.show();

}

}

//計算器數字按鈕定義面板

classNumber_KeyextendsJPanel{

/**

*

*/

=1L;

JButtonzero=newJButton("0");//數字鍵0

JButtonone=newJButton("1");//數字鍵1

JButtontwo=newJButton("2");//數字鍵2

JButtonthree=newJButton("3");//數字鍵3

JButtonfour=newJButton("4");//數字鍵4

JButtonfive=newJButton("5");//數字鍵5

JButtonsix=newJButton("6");//數字鍵6

JButtonseven=newJButton("7");//數字鍵7

JButtoneight=newJButton("8");//數字鍵8

JButtonnine=newJButton("9");//數字鍵9

JButtonplus=newJButton("+");

JButtonsub=newJButton("-");

JButtonmul=newJButton("*");

JButtondiv=newJButton("/");

JButtonequal=newJButton("=");

JButtonce=newJButton("清零");//置零鍵

JButtonpoint=newJButton(".");

Number_Key(){

setLayout(newGridLayout(4,4,1,1));//定義布局管理器為網格布局

setBackground(Color.blue);//設置背景顏色

//添加按鈕

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

//計算器顯示結果的窗體

classResultextendsJPanel{

/**

*

*/

=1L;

//text先是輸入和結果

JTextFieldtext=newJTextField("0");

@SuppressWarnings("deprecation")

Result(){//講輸入的數字或得到的結果在text的右邊顯示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false);//文本框不能編輯

setLayout(newBorderLayout());//設定布局管理器邊框布局

add(text,BorderLayout.CENTER);//text放置在窗體的中間

}

}

直接復制保存成Jisuanqi.java可以直接運行了

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:646
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:937
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:633
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:822
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:732
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1067
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:300
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:161
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:853
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:764