2、计算器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private string yunsuanfu = "";
        private double sum;
        private bool JiLu = false;

        #region//数字键
        private void button1_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "1";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "1";
                }
                else
                {
                    textBox1.Text += "1";
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "2";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "2";
                }
                else
                {
                    textBox1.Text += "2";
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "3";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "3";
                }
                else
                {
                    textBox1.Text += "3";
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "4";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "4";
                }
                else
                {
                    textBox1.Text += "4";
                }
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "5";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "5";
                }
                else
                {
                    textBox1.Text += "5";
                }
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "6";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "6";
                }
                else
                {
                    textBox1.Text += "6";
                }
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "7";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "7";
                }
                else
                {
                    textBox1.Text += "7";
                }
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "8";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "8";
                }
                else
                {
                    textBox1.Text += "8";
                }
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "9";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "9";
                }
                else
                {
                    textBox1.Text += "9";
                }
            }
        }
        private void button0_Click(object sender, EventArgs e)
        {
            if (JiLu)
            {
                textBox1.Text = "0";
                JiLu = false;
            }
            else
            {
                if (textBox1.Text == "0")
                {
                    textBox1.Text = "0";
                }
                else
                {
                    textBox1.Text += "0";
                }
            }
        }
        private void buttondian_Click(object sender, EventArgs e)
        {
            if(textBox1.Text!="0")
            {
                if (textBox1.Text.Contains("."))
                {
                }
                else
                {
                 textBox1.Text += ".";
                }
            }
            else
            {
            }
        }
        #endregion

        #region//运算符
        private void buttonjia_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "+";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "+";
            JiLu = true;
        }
        private void buttonjian_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "-";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "-";
            JiLu = true;
        }
        private void buttoncheng_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "*";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "*";
            JiLu = true;
        }
        private void buttonchu_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "/";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "/";
            JiLu = true;
        }
        private void buttonmo_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "%";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "%";
            JiLu = true;
        }
        private void buttondeng_Click(object sender, EventArgs e)
        {
            textBox2.Text += textBox1.Text + "=";
            if (yunsuanfu == "")
            {
                sum = double.Parse(textBox1.Text);
            }
            else
            {
                if (yunsuanfu == "+")
                {
                    sum += double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "-")
                {
                    sum -= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "*")
                {
                    sum *= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "/")
                {
                    sum /= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "%")
                {
                    sum %= double.Parse(textBox1.Text);
                }
                else if (yunsuanfu == "=")
                {
                    textBox2.Text = "";
                    textBox1.Text = "0";
                    yunsuanfu = "";
                    sum = 0;
                }
                else
                {
                    sum = double.Parse(textBox1.Text);
                }
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "=";
            JiLu = true;
        }
        #endregion

        #region//其他运算符号
        private void buttondao_Click(object sender, EventArgs e)
        {
            textBox2.Text += "reciproc(" + textBox1.Text + ")";
            if (textBox1.Text == "" || textBox1.Text == "0")
            {
                textBox1.Text = "除数不能为0";
            }
            else
            {
                sum = 1/(double.Parse(textBox1.Text));
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "reciproc()";
            JiLu = true;
        }
        private void buttonfu_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox1.Text == "0")
            {
                textBox1.Text = "输入错误";
            }
            else
            {
                sum =-double.Parse(textBox1.Text);
            }
            textBox1.Text = sum.ToString();
            yunsuanfu = "±";
            JiLu = true;
        }
        private void buttongen_Click(object sender, EventArgs e)
        {
            textBox2.Text += "sqrt(" + textBox1.Text + ")";
            sum =Math.Sqrt(double.Parse(textBox1.Text));
            textBox1.Text = sum.ToString();
            yunsuanfu = "sqrt()";
            JiLu = true;
        }
        #endregion

        #region//删除键
        private void button10_Click(object sender, EventArgs e)
        {
            int a = textBox1.Text.Length;
            string b="";
            if (a - 1> 0)
            {
                b =textBox1.Text.Substring(0,a-1);
                textBox1.Text = b;
            }
            else
            {
                textBox1.Text = "0";
            }
        }
        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
        }
        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            textBox2.Text = "";
            sum = 0;
        }
        #endregion
    }
}
时间: 2024-10-10 07:50:04

2、计算器的相关文章

【自动化__GUI自动化】__java__Windows应用程序识别__计算器

一.代码如下 package www.woniu.gui.one; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.aw

bzoj2242: [SDOI2011]计算器.

2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 4353  Solved: 1679[Submit][Status][Discuss] Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数. In

j2ee-JSP之简单计算器

来源韩顺平.j2ee视频实战教程jsp第1讲(下集) -------------------------------------------------------------------------------------------------------- 简单计算器,可以控制输入的数(仅第一个数)不能为空且不能为字符串 myCal.jsp代码 1 <!--这是计算器的界面 --> 2 <!-- 可以控制输入的数不能为空且不能为字符串 --> 3 <%@ page co

通过键盘接收数值和字符,实现计算器功能。

import java.util.Scanner; /** * @author 蓝色以太 * 通过键盘接收数值和字符,实现计算器功能. */ public class Calculator { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数值:"); double num1=sc.nextDouble(); System.out

Android计算器APP练习(1)--- 界面

Android Studio 2.3.2 .参考文章:http://blog.csdn.net/like_program/article/details/51813632 1. 新建工程 MyCalculator,一路下一步,均采用默认值. 2. 按照要求修改三个xml文件内容.很多地方工程名字不一样,改成自己的名字. 3. 按照步骤修改Activity_main.XML文件,遇到问题: android:singleLine="false" 过时 暂时处理方式:删掉此行. 4. 按照步

一个简单的税利计算器(网页版)

嗯嗯,做一个简单的网页版的税率计算器,功能比较简单,但是相对比较实用.因为参考了一些其他作品,所以在计算汇率的时候习惯性的是以美元做单位.具体的功能有着较为详细的标注.仅供大家学习参考下. <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>JavaScript Loan Calculator</title>

反转波兰计算器

#include <iostream>using namespace std;typedef double stackEntry; const int overflow = 1;const int underflow = 2;const int success = 0; const int maxstack = 100;//栈的最大尺寸class stack{public: stack(); int pop(); int push(const stackEntry &item); in

JAVA编写的简单计算器

package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; impo

winform与面向对象应用做一个计算器12月28日

代码部分: public partial class 计算器 : Form { public 计算器() { InitializeComponent(); } private double sum = 0;//存放运算结果 private string biaodashi="";//用于存放除了刚点过的运算符的前面表达式部分 private string preyunsuanfu="";//用来存放上一步运算符,用于下次点运算符的时候算上一步的结果 private

LeetCode OJ:Basic Calculator(基础计算器)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is