「简单计算器的实现java」简单计算器的实现用c语言

博主:adminadmin 2022-11-22 11:58:05 65

今天给各位分享简单计算器的实现java的知识,其中也会对简单计算器的实现用c语言进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用JAVA编写一个简单的计算器,要求如下:

然后 通过输入 显示结果,比如说:

以下是上图计算器的代码:

package Computer;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Stack;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Count extends JApplet implements ActionListener

{

/**

*

*/

private static final long serialVersionUID = 1L;

private JTextField textField = new JTextField("请输入");

String operator = "";//操作

String input = "";//输入的 式子

boolean flag =  true;

//  boolean flag1 = true;

//  boolean flag2 = true;

public void init()//覆写Applet里边的init方法

{

Container C = getContentPane();

JButton b[] = new JButton[16];

JPanel panel = new JPanel();

C.add(textField, BorderLayout.NORTH);

C.add(panel,BorderLayout.CENTER);

panel.setLayout(new GridLayout(4, 4,5,5));

String name[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置 按钮

for(int i=0;i16;i++)//添加按钮

{

b[i] = new JButton(name[i]);

b[i].setBackground(new Color(192,192,192));

b[i].setForeground(Color.BLUE);//数字键 设置为 蓝颜色

if(i%4==3)

b[i].setForeground(Color.RED);

b[i].setFont(new Font("宋体",Font.PLAIN,16));//设置字体格式

panel.add(b[i]);

b[i].addActionListener(this);

}

b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色

b[13].setForeground(Color.RED);

}

public void actionPerformed(ActionEvent e)

{

int cnt = 0;

String actionCommand = e.getActionCommand();

if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))

input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子

else if(actionCommand.equals("C"))

input = "";

else if(actionCommand.equals("="))//当监听到等号时,则处理 input

{

input+= "="+compute(input);

textField.setText(input);

input="";

cnt = 1;

}

else

input += actionCommand;//数字为了避免多位数的输入 不需要加空格

if(cnt==0)

textField.setText(input);

}

private String compute(String input)//即1237 的 样例

{

String str[];

str = input.split(" ");

StackDouble s = new StackDouble();

double m = Double.parseDouble(str[0]);

s.push(m);

for(int i=1;istr.length;i++)

{

if(i%2==1)

{

if(str[i].compareTo("+")==0)

{

double help = Double.parseDouble(str[i+1]);

s.push(help);

}

if(str[i].compareTo("-")==0)

{

double help = Double.parseDouble(str[i+1]);

s.push(-help);

}

if(str[i].compareTo("*")==0)

{

double help = Double.parseDouble(str[i+1]);

double ans = s.peek();//取出栈顶元素

s.pop();//消栈

ans*=help;

s.push(ans);

}

if(str[i].compareTo("/")==0)

{

double help = Double.parseDouble(str[i+1]);

double ans = s.peek();

s.pop();

ans/=help;

s.push(ans);

}

}

}

double ans = 0d;

while(!s.isEmpty())

{

ans+=s.peek();

s.pop();

}

String result = String.valueOf(ans);

return result;

}

public static void main(String args[])

{

JFrame frame = new JFrame("Count");

Count applet = new Count();

frame.getContentPane().add(applet, BorderLayout.CENTER);

applet.init();//applet的init方法

applet.start();//线程开始

frame.setSize(350, 400);//设置窗口大小

frame.setVisible(true);//设置窗口可见

}

}

编写java程序简单计算器

主要涉及的知识点: 类的写法, 以及方法的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.

参考代码如下

//Number类

class Number {

private int n1;//私有的整型数据成员n1

private int n2;//私有的整型数据成员n2

// 通过构造函数给n1和n2赋值

public Number(int n1, int n2) {

this.n1 = n1;

this.n2 = n2;

}

// 加法

public int addition() {

return n1 + n2;

}

// 减法

public int subtration() {

return n1 - n2;

}

// 乘法

public int multiplication() {

return n1 * n2;

}

// 除法 (可能除不尽,所以使用double作为返回类型)

public double division() {

return n1 * 1.0 / n2; // 通过n1*1.0 把计算结果转换成double类型.

}

}

//Exam4 类

public class Exam4{

public static void main(String[] args) {

Number number=new Number(15, 6);//创建Number类的对象

//下面的是调用方法得到返回值进行输出显示

System.out.println("加法"+number.addition());

System.out.println("减法"+number.subtration());

System.out.println("乘法"+number.multiplication());

System.out.println("除法"+number.division());

}

}

用java实现一个简单的计算器。

/*

* @(#)JCalculator.java 1.00 06/17/2015

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/**

* A simple calculator program.

* pI saw this program in a QQ group, and help a friend correct it./p

*

* @author Singyuen Yip

* @version 1.00 12/29/2009

* @see JFrame

* @see ActionListener

*/

public class JCalculator extends JFrame implements ActionListener {

/**

* Serial Version UID

*/

private static final long serialVersionUID = -169068472193786457L;

/**

* This class help close the Window.

* @author Singyuen Yip

*

*/

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

int i;

// Strings for Digit Operator buttons.

private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",

"2", "3", "-", ".", "0", "=", "+" };

// Build buttons.

JButton[] buttons = new JButton[str.length];

// For cancel or reset.

JButton reset = new JButton("CE");

// Build the text field to show the result.

JTextField display = new JTextField("0");

/**

* Constructor without parameters.

*/

public JCalculator() {

super("Calculator");

// Add a panel.

JPanel panel1 = new JPanel(new GridLayout(4, 4));

// panel1.setLayout(new GridLayout(4,4));

for (i = 0; i  str.length; i++) {

buttons[i] = new JButton(str[i]);

panel1.add(buttons[i]);

}

JPanel panel2 = new JPanel(new BorderLayout());

// panel2.setLayout(new BorderLayout());

panel2.add("Center", display);

panel2.add("East", reset);

// JPanel panel3 = new Panel();

getContentPane().setLayout(new BorderLayout());

getContentPane().add("North", panel2);

getContentPane().add("Center", panel1);

// Add action listener for each digit operator button.

for (i = 0; i  str.length; i++)

buttons[i].addActionListener(this);

// Add listener for "reset" button.

reset.addActionListener(this);

// Add listener for "display" button.

display.addActionListener(this);

// The "close" button "X".

addWindowListener(new WindowCloser());

// Initialize the window size.

setSize(800, 800);

// Show the window.

// show(); Using show() while JDK version is below 1.5.

setVisible(true);

// Fit the certain size.

pack();

}

public void actionPerformed(ActionEvent e) {

Object target = e.getSource();

String label = e.getActionCommand();

if (target == reset)

handleReset();

else if ("0123456789.".indexOf(label) 0)

handleNumber(label);

else

handleOperator(label);

}

// Is the first digit pressed?

boolean isFirstDigit = true;

/**

* Number handling.

* @param key the key of the button.

*/

public void handleNumber(String key) {

if (isFirstDigit)

display.setText(key);

else if ((key.equals(".")) (display.getText().indexOf(".") 0))

display.setText(display.getText() + ".");

else if (!key.equals("."))

display.setText(display.getText() + key);

isFirstDigit = false;

}

/**

* Reset the calculator.

*/

public void handleReset() {

display.setText("0");

isFirstDigit = true;

operator = "=";

}

double number = 0.0;

String operator = "=";

/**

* Handling the operation.

* @param key pressed operator's key.

*/

public void handleOperator(String key) {

if (operator.equals("+"))

number += Double.valueOf(display.getText());

else if (operator.equals("-"))

number -= Double.valueOf(display.getText());

else if (operator.equals("*"))

number *= Double.valueOf(display.getText());

else if (operator.equals("/"))

number /= Double.valueOf(display.getText());

else if (operator.equals("="))

number = Double.valueOf(display.getText());

display.setText(String.valueOf(number));

operator = key;

isFirstDigit = true;

}

public static void main(String[] args) {

new JCalculator();

}

}

运行界面:

java 创建一个简单的计算器

创建U.java,代码如下:

package pack;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class U implements ActionListener {

public static String str = "";

public static String a = "";

public static String b = "";

public static String k = "";

public void actionPerformed(ActionEvent e) {

String w = e.getActionCommand();//字

if (k.equals("")) {

a += w;

F.show.setText(a);

} else {

b += w;

F.show.setText(b);

}

}

}

创建Qing.java,代码:

package pack;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Qing implements ActionListener {

public void actionPerformed(ActionEvent e) {

U.a = "";

U.b = "";

U.k = "";

F.show.setText("");

}

}

创建 Fu.java,代码:

package pack;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Fu implements ActionListener {

public void actionPerformed(ActionEvent e) {

String a = e.getActionCommand();//字

U.k = a;

}

}

创建F.java,代码:

package pack;

import java.awt.*;

import javax.swing.*;

public class F {

JFrame frame = new JFrame("计算机");

JPanel pl = new JPanel();

JPanel p2 = new JPanel();

static JTextField show = new JTextField();

static JButton b0 = new JButton("0");

static JButton b1 = new JButton("1");

static JButton b2 = new JButton("2");

static JButton b3 = new JButton("3");

static JButton b4 = new JButton("4");

static JButton b5 = new JButton("5");

static JButton b6 = new JButton("6");

static JButton b7 = new JButton("7");

static JButton b8 = new JButton("8");

static JButton b9 = new JButton("9");

JButton bjia = new JButton("+");

JButton bjian = new JButton("-");

JButton bcheng = new JButton("*");

JButton bchu = new JButton("/");

JButton bdian = new JButton(".");

JButton bdeng = new JButton("=");

JButton bqingchu = new JButton("清除");

public void y() {

pl.setLayout(new GridLayout(1, 1));

pl.add(show);

}

public void p() {

b1.addActionListener(new U());

b2.addActionListener(new U());

b3.addActionListener(new U());

b4.addActionListener(new U());

b5.addActionListener(new U());

b6.addActionListener(new U());

b7.addActionListener(new U());

b8.addActionListener(new U());

b9.addActionListener(new U());

b0.addActionListener(new U());

bjia.addActionListener(new Fu());

bjian.addActionListener(new Fu());

bcheng.addActionListener(new Fu());

bchu.addActionListener(new Fu());

bdeng.addActionListener(new Deng());

bqingchu.addActionListener(new Qing());

p2.setLayout(new GridLayout(6, 3));

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.add(b7);

p2.add(b8);

p2.add(b9);

p2.add(b0);

p2.add(bjia);

p2.add(bjian);

p2.add(bcheng);

p2.add(bchu);

p2.add(bdian);

p2.add(bqingchu);

p2.add(bdeng);

}

public void o() {

frame.setLayout(new BorderLayout());

frame.add(pl, BorderLayout.NORTH);

frame.add(p2, BorderLayout.CENTER);

frame.setSize(400, 300);

frame.setVisible(true);

}

public static void main(String[] args) {

F f = new F();

f.y();

f.p();

f.o();

}

}

创建Deng.java,代码:

package pack;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Deng implements ActionListener {

public void actionPerformed(ActionEvent e) {

int a = Integer.parseInt(U.a);

int b = Integer.parseInt(U.b);

int c = 0;

if (U.k.equals("+")) {

c = a + b;

} else

if (U.k.equals("-")) {

c = a - b;

} else

if (U.k.equals("*")) {

c = a * b;

} else

if (U.k.equals("/")) {

c = a / b;

} else {

}

String d = String.valueOf(c);

F.show.setText(d);

U.a = d;

U.b = "";

U.k = "";

}

}

我试过了,可以运行

简单计算器的实现java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于简单计算器的实现用c语言、简单计算器的实现java的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。