当前位置:首页 > 代码 > 正文

简单计算器的代码(简单计算器代码解释)

admin 发布:2022-12-19 18:17 147


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

本文目录一览:

简单的计算器代码

meta content="text/html; charset=utf-8" http-equiv="Content-Type" /

title/title

style type="text/css"

#box{width: 295px; margin: 0 auto; text-align: justify; border: 1px solid #ddd; padding: 15px;}

.d_num{display: inline-block; margin:  10px; width: 33px; height: 30px; border: 1px solid #ddd; text-align: center; line-height: 30px; cursor: pointer;}

.sum{margin: 0 10px 10px; line-height: 30px; font-size: 20px;}

#sum{width: 205px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;}

#process{width: 255px; height: 30px; padding: 0 5px; margin-left: 5px; font-size: 20px;}/style

p id="box"

p class="sum"

input id="process" name="process" type="text" value="0" //p

p class="sum"

SUM:input id="sum" name="sum" type="text" value="0" //p

p

span class="d_num" data-num="1"1/span span class="d_num" data-num="2"2/span span class="d_num" data-num="3"3/span span class="d_num" data-num="lt;-"lt;-/span span class="d_num" data-num="C"C/span/p

p

span class="d_num" data-num="4"4/span span class="d_num" data-num="5"5/span span class="d_num" data-num="6"6/span span class="d_num" data-num="*"*/span span class="d_num" data-num="/"//span/p

p

span class="d_num" data-num="7"7/span span class="d_num" data-num="8"8/span span class="d_num" data-num="9"9/span span class="d_num" data-num="+"+/span span class="d_num" data-num="-"-/span/p

p

span class="d_num" data-num="0"0/span span class="d_num" data-num="00"00/span span class="d_num" data-num="."./span span class="d_num" data-num="%"%/span span class="d_num" data-num="="=/span/p

/p

script type="text/javascript"

var $box = document.getElementById('box');

var $sum = document.getElementById('sum');

var $process = document.getElementById('process');

var sum = 0, process = '', num1 = '', num2 = '', temp = '', statu = false;

$box.onclick = function(e){

var event = window.event || e;

var ele = event.srcElement || event.target;

var _className = ele.className;

if(_className == 'd_num'){

var num = ele.getAttribute('data-num');//点击的.按钮对应的值

var NotNum = isNaN(num);

if(!NotNum || num == '.'){ //点击了数字

if(!statu){ //还没点击过符号

if(num1 == '0'){

num1 = '';

}

num1 += num;

process = num1;

}else{ //已经点击过符号

if(num2 == '0'){

num2 = '';

}

num2 += num;

process = num1 + temp + num2;

}

$process.value = process;

}

else{ //点击了符号

if(num1 == ''){//非法操作

return false;

}

if(num == 'C'){//归零

num1 = '';

num2 = '';

process = '';

temp = '';

sum = '';

$process.value = '0';

$sum.value = '0';

更多相关文章推荐:

简单计算器图形编程代码是什么?

//java版本的,你早晚能用到

package bag;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.SwingUtilities;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JButton;

import java.awt.Dimension;

public class SCul extends JFrame {

private static final long serialVersionUID = 1L;

public JTextField jTextField1;

private static String s1,s2,s,s3,s4;

private static double result;

public Component createComponents(){

final JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;

GridLayout gl1=new GridLayout(4,5);

JPanel panel1=new JPanel();

panel1.setLayout(gl1);

b1=new JButton("1"); b1.setText("1");b1.setForeground(Color.blue);

b2=new JButton("2"); b2.setText("2");b2.setForeground(Color.blue);

b3=new JButton("3"); b3.setText("3");b3.setForeground(Color.blue);

b6=new JButton("4"); b6.setText("4");b6.setForeground(Color.blue);

b7=new JButton("5"); b7.setText("5");b7.setForeground(Color.blue);

b8=new JButton("6"); b8.setText("6");b8.setForeground(Color.blue);

b11=new JButton("7"); b11.setText("7");b11.setForeground(Color.blue);

b12=new JButton("8"); b12.setText("8");b12.setForeground(Color.blue);

b13=new JButton("9"); b13.setText("9");b13.setForeground(Color.blue);

b16=new JButton("0"); b16.setText("0");b16.setForeground(Color.blue);

b4=new JButton("+"); b4.setText("+");b4.setForeground(Color.red);

b5=new JButton("C"); b5.setText("C");b5.setForeground(Color.red);

b9=new JButton("-"); b9.setText("-");b9.setForeground(Color.red);

b10=new JButton("退格"); b10.setText("退格");b10.setForeground(Color.red);

b14=new JButton("*"); b14.setText("*");b14.setForeground(Color.red);

b15=new JButton("1/x"); b15.setText("1/x");b15.setForeground(Color.blue);

b17=new JButton("+/-"); b17.setText("+/-");b17.setForeground(Color.blue);

b18=new JButton("."); b18.setText(".");b4.setForeground(Color.blue);

b19=new JButton("/"); b19.setText("/");b19.setForeground(Color.red);

b20=new JButton("="); b20.setText("=");b20.setForeground(Color.red);

//设置大小和颜色

b1.setSize(100, 100);

b2.setSize(100, 100);

b3.setSize(100, 100);

b4.setSize(100, 100);

b5.setSize(100, 100);

b6.setSize(100, 100);

b7.setSize(100, 100);

b8.setSize(100, 100);

b9.setSize(100, 100);

b10.setSize(100, 100);

b11.setSize(100, 100);

b12.setSize(100, 100);

b13.setSize(100, 100);

b14.setSize(100, 100);

b15.setSize(100, 100);

b16.setSize(100, 100);

b17.setSize(100, 100);

b18.setSize(100, 100);

b19.setSize(100, 100);

b20.setSize(100, 100);

panel1.add(b1);

panel1.add(b2);

panel1.add(b3);

panel1.add(b4);

panel1.add(b5);

panel1.add(b6);

panel1.add(b7);

panel1.add(b8);

panel1.add(b9);

panel1.add(b10);

panel1.add(b11);

panel1.add(b12);

panel1.add(b13);

panel1.add(b14);

panel1.add(b15);

panel1.add(b16);

panel1.add(b17);

panel1.add(b18);

panel1.add(b19);

panel1.add(b20);

panel1.setVisible(true);

b1.addActionListener(new ActionListener(){//按1

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b1.getText().toString()));

s=jTextField1.getText().toString();

}

});

b2.addActionListener(new ActionListener(){//按2

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b2.getText().toString()));

s=jTextField1.getText().toString();

}

});

b3.addActionListener(new ActionListener(){//按3

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b3.getText().toString()));

s=jTextField1.getText().toString();

}

});

b6.addActionListener(new ActionListener(){// 按4

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b6.getText().toString()));

s=jTextField1.getText().toString();

}

});

b7.addActionListener(new ActionListener(){//按5

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b7.getText().toString()));

s=jTextField1.getText().toString();

}

});

b8.addActionListener(new ActionListener(){//按6

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b8.getText().toString()));

s=jTextField1.getText().toString();

}

});

b11.addActionListener(new ActionListener(){//按7

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b11.getText().toString()));

s=jTextField1.getText().toString();

}

});

b12.addActionListener(new ActionListener(){//按8

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b12.getText().toString()));

s=jTextField1.getText().toString();

}

});

b13.addActionListener(new ActionListener(){//按9

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b13.getText().toString()));

s=jTextField1.getText().toString();

}

});

b16.addActionListener(new ActionListener(){//按0

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b16.getText().toString()));

s=jTextField1.getText().toString();

}

});

b4.addActionListener(new ActionListener(){//按+

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//将第一操作数保存至s1

s3="+";

jTextField1.setText(jTextField1.getText().concat(b4.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b9.addActionListener(new ActionListener(){//按-

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//将第一操作数保存至s1

s3="-";

jTextField1.setText(jTextField1.getText().concat(b9.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b14.addActionListener(new ActionListener(){//按*

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//将第一操作数保存至s1

s3="*";

jTextField1.setText(jTextField1.getText().concat(b14.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b19.addActionListener(new ActionListener(){//按/

public void actionPerformed(ActionEvent e){

if(s=="")

{

s="0";

jTextField1.setText(s);

}

else{

s1=s;//将第一操作数保存至s1

s3="/";

jTextField1.setText(jTextField1.getText().concat(b19.getText().toString()));

s=jTextField1.getText().toString();

}

}

});

b5.addActionListener(new ActionListener(){//按C清零

public void actionPerformed(ActionEvent e){

s="0";

jTextField1.setText(s);

}

});

b10.addActionListener(new ActionListener(){//按退格

public void actionPerformed(ActionEvent e){

int l=s.length();

if(l!=0)

s=s.substring(0, l-1);//删除一个字符

else

{

s="0";

s4="+";

}

jTextField1.setText(s);//将删除后的字符串显示到单行编辑框中

}

});

b15.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){//求倒数

if(s=="" || s=="0")

{

s="0";

jTextField1.setText("0");

}

else{

result=1/Double.parseDouble(s);

s=String.valueOf(result);

jTextField1.setText(s4+s);

}

}

});

b17.addActionListener(new ActionListener(){//按+/-

public void actionPerformed(ActionEvent e){

if(s=="")

{

jTextField1.setText("0");

}else{

if(s4=="+")

{ s4="-";

jTextField1.setText(s4+s);

}

else{

s4="+";

jTextField1.setText(s4+s);

}

}

}

});

b18.addActionListener(new ActionListener(){//按.

public void actionPerformed(ActionEvent e){

jTextField1.setText(jTextField1.getText().concat(b18.getText().toString()));

s=jTextField1.getText().toString();

}

});

b20.addActionListener(new ActionListener(){//按=求结果

public void actionPerformed(ActionEvent e){//求结果

if(s=="")

{

s="0";

s4="+";

s3="";

jTextField1.setText(s);

}

else if(s!=""){

int m1=s.indexOf(s3);

s1=s.substring(0, m1);

s2=s.substring(m1+1, s.length());

if(s4=="+"){

if(s3=="+")

result=Double.parseDouble(s1)+Double.parseDouble(s2);

if(s3=="-")

result=Double.parseDouble(s1)-Double.parseDouble(s2);

if(s3=="*")

result=Double.parseDouble(s1)*Double.parseDouble(s2);

if(s3=="/" s2!="0" s2!="")

result=Double.parseDouble(s1)/Double.parseDouble(s2);

if(s3=="/" (s2=="0" || s2==""))

result=00.00;

s=String.valueOf(result);

}

else if(s4=="-"){

if(s3=="+"){

result=Double.parseDouble(s2)-Double.parseDouble(s1);

s=String.valueOf(result);

}

if(s3=="-"){

result=Double.parseDouble(s1)+Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="*"){

result=Double.parseDouble(s1)*Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="/" (s2!="0" || s2!="")){

result=Double.parseDouble(s1)/Double.parseDouble(s2);

s="-"+String.valueOf(result);

}

if(s3=="/" s2=="0" s2=="")

{

s="00.00";

}

}

jTextField1.setText(s);

}

}

});

return panel1;

}

public static void main(String[] args) {

SCul cul1=new SCul();

JFrame frame1=new JFrame("计算器");

frame1.setBackground(Color.lightGray);

frame1.add(cul1.jTextField1,BorderLayout.NORTH);

frame1.setResizable(false);

Component contents=cul1.createComponents();

frame1.getContentPane().add(contents, BorderLayout.SOUTH);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame1.pack();

frame1.setVisible(true);

}

/**

* This is the default constructor

*/

public SCul() {

super();

jTextField1=new JTextField();

s="";

s1="";

s2="";

s3="";

s4="+";

jTextField1.setText(s);

jTextField1.setHorizontalAlignment(JTextField.RIGHT);

}

}

一个简单的计算器如图所示,使用“计算器”可以完成任意的的标准运算,“计算器”可用于基本的算术运算,比如加减运算等。

简易计算器代码

我的计算器 Option Explicit Dim FocusText As VB.TextBox Dim pd As Boolean Dim x As Double Dim y As Double Dim ch As Integer Private Sub Command1_Click(Index As Integer) On Error Resume Next FocusText.SetFocus If Index SendKeys Index ElseIf Index = 10 Then ch = Index - 10 x = Val(Text1.Text) Text1.Text = "" ElseIf Index = 14 Then y = Val(Text1.Text) Select Case ch Case 0 Text1.Text = x + y Case 1 Text1.Text = x - y Case 2 Text1.Text = x * y Case 3 Text1.Text = x / y End Select ElseIf Index = 15 Then FocusText.SetFocus SendKeys "{BS}" ElseIf Index = 16 Then FocusText.SetFocus SendKeys "." End If End Sub '防止输入多个小数点以及出数字外的其他字符 Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii 57) And KeyAscii 46 And KeyAscii 8 Then KeyAscii = 0 End If If pd = True And KeyAscii = 46 Then KeyAscii = 0 End If If pd = False And KeyAscii = 46 Then pd = True KeyAscii = 46 End If End Sub '防止 Ctrl + v 粘贴 Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyV And Shift = vbCtrlMask Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub '防止右键粘贴 Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbRightButton Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub Private Sub Text1_LostFocus() Set FocusText = Text1 End Sub 界面如下 0为Command1(0)1为Command1(1)依次类推 ←为Command1(15) 小数点为Command1(16) 贴子相关图片: 界面可以到

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

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/17083.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载