简单计算器 C# 学习

C# 源代码

https://git.oschina.net/363451039/Jisuanqi

实现加减乘除 平方开放倒数 键盘输入
历史记录查询和删除

未实现记忆加减功能

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 WindowsFormsApplication3
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /*连等功能
        ///存储最近输入的数字e_Num和最近的操作e_Op
        ///button CE 保存e_Num,而button C 清空e_Num和e_Op
        double e_Num = 0;
        double e_Res=0;
        string e_Op="";
        //*/
        string op="";//运算符
        double num1=0;
        double num2=0;
        double res=0;
        int iOp = 0;//运算符位置
        bool newOp = true;  //开始新的运算
        bool addOp = false;
        //键入数字
        private void button_Click_Num(object sender, EventArgs e)
        {
            if (newOp)
            {
                textNumBox.Text = "";
                newOp = false;
                addOp = false;
                /*
                e_Num = num2;
                e_Res=res;
                e_Op=op;
                //*/
            }
            ///*  获取button值,赋值给textNumBox.Ttext
            ///多个button的Click事件同时命名为button_Click_Num
            ///实现批量的button_Click_Num的操作
            Button tempNum = (Button) sender;
            textNumBox.Text += tempNum.Text.ToString();
            //*/
        }

        private void button_Click_Op(object sender, EventArgs e)
        {

            Button tempOp = (Button)sender;
            operater(tempOp.Text.ToString());
        }
        private void operater(string oper)
        {
            if (addOp)
            {
                textNumBox.Text = Convert.ToString(res) + oper;
                newOp = false;
            }
            else
            {
                textNumBox.Text += oper;
            }
            op=oper;
            //e_Op=tempOp.Text.Tostring();
            //e_num1=textNumBox.Text.Substring(0, textNumBox.Text.Length - 1);
            //e_num2=textNumBox.Text.SkipWhile<char>(char i,result<char,i==‘+‘||i==‘-‘||i==‘ב||i==‘÷‘>predicate);//并不会用该函数
        }
        private void operater(char oper)
        {
            if (addOp)
            {
                newOp = false;
            }
            op = Convert.ToString(oper);
            //e_Op=tempOp.Text.Tostring();
            //e_num1=textNumBox.Text.Substring(0, textNumBox.Text.Length - 1);
            //e_num2=textNumBox.Text.SkipWhile<char>(char i,result<char,i==‘+‘||i==‘-‘||i==‘ב||i==‘÷‘>predicate);//并不会用该函数
        }

        private void button_Click_Del(object sender, EventArgs e)
        {
            if (textNumBox.Text.Length > 0)
            {
                textNumBox.Text = textNumBox.Text.Substring(0, textNumBox.Text.Length - 1);
                newOp = false;
            }
        }

        private void button_Click_CE(object sender, EventArgs e)
        {
            textNumBox.Text = "";
            newOp = true;
        }

        private void button_Click_C(object sender, EventArgs e)
        {
            textNumBox.Text = "";
            /*
            e_Num = 0;
            e_Op = "";
            //*/
        }
        private void button_Click_Result(object sender, EventArgs e)
        {
            try
            {
                //if (newOp)    不要在result里判断是否是重新开始的计算,而是更新textNumBox.Text
                //{
                    result();
                    res = Operate(op);
                    newOp = true;
                    addOp = true;
                    Common.historylist.Add(textNumBox.Text);

                //}
                //else
                //{
                //    result(res);
                //    res = Operate(op);
                //}
            }
            catch (Exception exc)
            {
                textNumBox.Text= "Error";
                newOp = false;
                addOp = true;
            }
        }
        private void result()
        {
            iOp = textNumBox.Text.IndexOf(op);
            num1 = Convert.ToDouble(textNumBox.Text.Substring(0, iOp));
            num2 = Convert.ToDouble(textNumBox.Text.Substring(iOp + 1, textNumBox.Text.Length - iOp - 1));//-iOp,第二个参数是substring起始之后多少个

        }
        //private void result(double oldnum)
        //{
        //    iOp=textNumBox.Text.LastIndexOf(op);
        //    num1 = oldnum;
        //    num2 = Convert.ToDouble(textNumBox.Text.Substring(iOp + 1, textNumBox.Text.Length - iOp - 1));
        //}
        private double Operate(string opp)
        {
            double ress;
            if (op == "+")
            {
                ress = num1 + num2;
                textNumBox.Text += "=" + ress;
            }
            else if (op == "-")
            {
                ress = num1 - num2;
                textNumBox.Text += "=" + ress;
            }
            else if (op == "×")
            {
                ress = num1 * num2;
                textNumBox.Text += "=" + ress;
            }
            else if (op == "÷")
            {

                ress = num1 / num2;
                textNumBox.Text += "=" + ress;
            }
            else
            {
                ress = 0;
            }
            return ress;
        }

        private void button_Click_fushu(object sender, EventArgs e)
        {
            textNumBox.Text += "-";
            newOp = false;
            Common.historylist.Add(textNumBox.Text);
        }

        private void button_Click_pingfanggen(object sender, EventArgs e)
        {
            double numtemp;
            try
            {
                numtemp = Convert.ToDouble(textNumBox.Text);
            }
            catch (System.FormatException)  //不能转化为double的异常下用res结果计算
            {
                numtemp = res;
            }
            res = Math.Sqrt(numtemp);
            textNumBox.Text = "√(" + numtemp + ")=" + res;
            newOp = true;
            Common.historylist.Add(textNumBox.Text);
        }

        private void button_Click_daoshu(object sender, EventArgs e)
        {
            double numtemp;
            try
            {
                numtemp = Convert.ToDouble(textNumBox.Text);
            }
            catch (System.FormatException)  //不能转化为double的异常下用res结果计算
            {
                numtemp = res;
            }
            res = 1/numtemp;
            textNumBox.Text = "1/(" + numtemp + ")=" + Convert.ToString(res);
            newOp = true;
            Common.historylist.Add(textNumBox.Text);
        }

        private void Click_About(object sender, EventArgs e)
        {
            About aboutbox = new About();   //打开新的窗口,要先new 方法
            aboutbox.ShowDialog();
        }

        private void Click_History(object sender, EventArgs e)
        {
            History historybox = new History();   //打开新的窗口,要先new 方法
            historybox.ShowDialog();
        }

        private void Click_PasteNow(object sender, EventArgs e)
        {
            textNumBox.Text += Clipboard.GetText();
        }

        private void Click_CopyNow(object sender, EventArgs e)
        {
            Clipboard.SetText(textNumBox.Text);
        }

        bool flag = true;
        private void KeyPress_EqueKey(object sender, KeyPressEventArgs e)
        {

            switch (e.KeyChar)
            {
                case ‘=‘:
                    {
                        if (flag)
                        {
                            flag = false;
                            try
                            {
                                //if (newOp)    不要在result里判断是否是重新开始的计算,而是更新textNumBox.Text
                                {
                                    //textNumBox.Text += "=";
                                    //result();
                                    //if (textNumBox.Text.Length > 0)
                                    //{
                                    //    textNumBox.Text = textNumBox.Text.Substring(0, textNumBox.Text.Length - 2);
                                    //    newOp = false;
                                    //}
                                    iOp = textNumBox.Text.IndexOf(op);
                                    int op0 = textNumBox.Text[0];//(0);
                                    int op1 = textNumBox.Text[1];
                                    num1 = Convert.ToDouble(textNumBox.Text.Substring(0, iOp));
                                    //string temp = textNumBox.Text.Substring(iOp + 1, textNumBox.Text.Length - iOp-1);
                                    num2 = Convert.ToDouble(textNumBox.Text.Substring(iOp + 1, textNumBox.Text.Length - iOp - 1));//-iOp,第二个参数是substring起始之后多少个

                                    res = Operate(op);
                                    newOp = true;
                                    addOp = true;
                                    Common.historylist.Add(textNumBox.Text);

                                }
                                //else
                                //{
                                //    result(res);
                                //    res = Operate(op);
                                //}
                            }
                            catch (Exception exc)
                            {
                                textNumBox.Text = "Error";
                                newOp = false;
                                addOp = true;
                            }
                        }
                        else
                        {
                            flag = true;
                            //textNumBox.Text.Substring(1, textNumBox.Text.Length - 1);
                        }
                        break;
                    }
                case ‘+‘:
                    {
                        operater(‘+‘);
                        break;
                    }
                case ‘-‘:
                    {
                        operater(‘-‘);
                        break;
                    }
                case ‘*‘:
                    {
                        operater(‘ב);
                        break;
                    }
                case ‘/‘:
                    {
                        operater(‘÷‘);
                        break;
                    }
            }
        }
    }

    //历史记录传递
    public class Common
    {
        public static List<string> historylist = new List<string>();
        public static int a = 0;
    }
}
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 WindowsFormsApplication3
{
    public partial class History : Form
    {
        public History()
        {
            InitializeComponent();
        }

        private void button_Click_Copy(object sender, EventArgs e)
        {
            Clipboard.SetText(listBox1.Items[listBox1.SelectedIndex].ToString());
        }

        private void button_Click_Delete(object sender, EventArgs e)
        {
            //listBox1.ClearSelected();
            //listBox1.Items.Clear(); 清除全部
            int sel = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(sel);
        }

        private void History_Load(object sender, EventArgs e)
        {
            foreach (string a in Common.historylist)
            {
                listBox1.Items.Add(a);
            }
        }
    }
}
时间: 2024-11-08 20:28:06

简单计算器 C# 学习的相关文章

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att

j2ee-JSP之简单计算器

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

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

[Java.web]简单计算器

项目的  WebRoot 目录下的 calculator.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>计算结果</title> </head> <body> <jsp:us

NOIP19:简单计算器

/* 1.4编程基础之逻辑表达式与条件分支 19:简单计算器 总时间限制: 1000ms 内存限制: 65536kB 描述 一个最简单的计算器,支持+, -, *, / 四种运算.仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围. 输入 输入只有一行,共有三个参数,其中第1.2个参数为整数,第3个参数为操作符(+,-,*,/). 输出 输出只有一行,一个整数,为运算结果.然而: 1. 如果出现除数为0的情况,则输出:Divided by zero! 2. 如果出现无效的操作符

hdoj 1237 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14512    Accepted Submission(s): 4920 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

JS中简单的this学习

我在学习JS初期,在使用this的时候经常出现问题,当然就是在现在,也有一些场景不能很好的明白this到底指代的是什么?看下面一个例子: ? 1 2 3 4 5 6 7 8 9 10 var x = 10;    var foo = {        x: 20,        bar: function() {            alert(this.x);        }    }    var bar = foo.bar;    foo.bar();    //20    bar()

Shell 实现简单计算器功能

Shell 实现简单计算器功能,脚本如下: [[email protected] scripts]# cat jisuan.sh #!/bin/bash print_usage(){     printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"     exit 1 } #判断传入的参数是不是3个 if [ $# -ne 3 ]   then     print_usage fi firstnum=$1 secondnum=$3 op=$2 #对传入的参数进

Javascript 实现简单计算器实例代码

Javascript 实现简单计算器实例代码 这篇文章主要介绍了Javascript 实现简单计算器实例代码的相关资料,需要的朋友可以参考下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63