• Calculator之WPF应用程序


    C++写出计算器之后,我又用C#重写了计算器。算法较V1.20变化不大,关键这回我用的是WPF,UI升级。看图大家就知道了。

    这是我做的第一个WPF应用程序,谨以此纪念。

    结尾同样附源码下载。

    ///MainWindow.xaml.cs

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

     

    namespace Calculator

    {

        /// <summary>

        /// MainWindow.xaml 的交互逻辑

        /// </summary>

        public partial class MainWindow : Window

        {

            bool bEquation, bNumber, bDot;

            double a, b, c, answer;

            string m, n;

            int cOperator;

            public MainWindow()

            {

               

                InitializeComponent();

            }

            private void Startup()

            {//初始化所有变量

                txtNumber.Text = "0";

                txtFormula.Text = "";

                a = b = c = answer = 0;

                m = n = "\0";

                bEquation = false;

                bNumber = true;

                bDot = false;

                cOperator = 0;

            }

     

     

            private void Input(string s)

            {//输入函数

                switch (s)

                {

                    case ".":

                        if (bDot == false && bNumber)

                        {

                            txtNumber.Text += ".";

                        }

                        bDot = true;

                        //bEquation=false;

                        bNumber = true;

                        break;

     

     

                    case "0":

                    case "1":

                    case "2":

                    case "3":

                    case "4":

                    case "5":

                    case "6":

                    case "7":

                    case "8":

                    case "9":

                        if (bEquation)

                        {

                            txtFormula.Text = "";

                            txtNumber.Text = s.ToString();

                        }

                        else if (cOperator != 0 && !bNumber)

                        {

                            txtNumber.Text = s;

                        }

                        else

                        {

                            if (txtNumber.Text == "0") txtNumber.Text = s;

                            else txtNumber.Text += s;

                        }

                        bEquation = false;

                        bNumber = true;                   

                        break;

     

                    case "+":

                    case "-":

                    case "*":

                    case "/":

                        if (bEquation)

                        {//如果刚刚按了等号按钮

                            txtFormula.Text = txtNumber.Text;

                            txtFormula.Text += s;

                            a = Convert.ToDouble(txtNumber.Text);

                            m = s;

                            bEquation = false;

                            cOperator = 1;

                            break;

                        }

                        if (cOperator == 0)

                        {//第一回按运算符按钮

                            a = Convert.ToDouble(txtNumber.Text);

                            m = s;

                        }

                        else if (cOperator == 1)

                        {//第二回按按钮

                            b = Convert.ToDouble(txtNumber.Text);

                            n = s;

                        }

                        else

                        {//第三回按按钮

                            c = Convert.ToDouble(txtNumber.Text);

                            Count();

                            c = 0;

                            n = s;

                        }

                        txtFormula.Text += txtNumber.Text;//txtNumber的数据添加到txtFormula

                        txtFormula.Text += s;//运算符添加到txtFormula

                        bEquation = false;

                        bNumber = false;

                        cOperator++;

                        bDot = false;

                        break;

     

     

                    case "=":

                        if (bEquation)

                        {

                            break;

                        }

                        bEquation = true;

                        bNumber = false;

                        txtFormula.Text += txtNumber.Text;

                        txtFormula.Text += "=";

                        if (cOperator == 0) { break; }

                        else if (cOperator == 1) { b = Convert.ToDouble(txtNumber.Text); }

                        else { c = Convert.ToDouble(txtNumber.Text); }

                        Count();

                        answer = a;

                        txtNumber.Text = answer.ToString();

                        cOperator = 0;

                        a = b = c = 0;

                        m = n = "0";

                        break;

     

     

                    case "CE":

                        Startup();

                        break;

                }

            }

     

     

            private void Count()

            {//计算函数

                switch (n)

                {

                    case "+":

                    case "-":

                        switch (m)

                        {

                            case "+": a = a + b; b = c; m = n; break;

                            case "-": a = a - b; b = c; m = n; break;

                            case "*": a = a * b; b = c; m = n; break;

                            case "/": a = a / b; b = c; m = n; break;

                        }

                        break;

                    case "*":

                        if (m == "/") { a = a / b; b = c; m = n; break; }

                        else { b = b * c; break; }

                    case "/":

                        if (m == "/") { a = a / b; b = c; m = n; break; }

                        else { b = b / c; break; }

                    case "\0": break;

                }

                if (bEquation)

                {//如果bEquation为真,则执行如下运算

                    switch (m)

                    {

                        case "+": a = a + b; break;

                        case "-": a = a - b; break;

                        case "*": a = a * b; break;

                        case "/": a = a / b; break;

                        case "\0": txtFormula.Text += "Error!"; break;

                    }

                }

            }

     

     

     

            private void Calculator_Loaded(object sender, RoutedEventArgs e)

            {

                Startup();

            }

     

            private void btn0_Click(object sender, RoutedEventArgs e)

            {

                Input("0");

            }

     

            private void btn1_Click(object sender, RoutedEventArgs e)

            {

                Input("1");

            }

     

            private void btn2_Click(object sender, RoutedEventArgs e)

            {

                Input("2");

            }

     

            private void btn3_Click(object sender, RoutedEventArgs e)

            {

                Input("3");

            }

     

            private void btn4_Click(object sender, RoutedEventArgs e)

            {

                Input("4");

            }

     

            private void btn5_Click(object sender, RoutedEventArgs e)

            {

                Input("5");

            }

     

            private void btn6_Click(object sender, RoutedEventArgs e)

            {

                Input("6");

            }

     

            private void btn7_Click(object sender, RoutedEventArgs e)

            {

                Input("7");

            }

     

            private void btn8_Click(object sender, RoutedEventArgs e)

            {

                Input("8");

            }

     

            private void btn9_Click(object sender, RoutedEventArgs e)

            {

                Input("9");

            }

     

     

            private void btnDot_Click(object sender, RoutedEventArgs e)

            {

                Input(".");

            }

     

            private void btnPlus_Click(object sender, RoutedEventArgs e)

            {

                Input("+");

            }

     

            private void btnSubtration_Click(object sender, RoutedEventArgs e)

            {

                Input("-");

            }

     

            private void btnMultiplication_Click(object sender, RoutedEventArgs e)

            {

                Input("*");

            }

     

            private void btnDevide_Click(object sender, RoutedEventArgs e)

            {

                Input("/");

            }

     

            private void btnEqual_Click(object sender, RoutedEventArgs e)

            {

                Input("=");

            }

     

            private void btnInitialize_Click(object sender, RoutedEventArgs e)

            {

                Input("CE");

            }

        }

    }

     

    xaml不再列出,先看的朋友直接下源码吧。

    Calculator0417CSWPF  http://cid-c96c6ec8ad8dcfea.office.live.com/browse.aspx/Program/Calculator

    知识共享许可协议
    作品Wave创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    ActiveReports 报表应用教程 (3)---图表报表
    ActiveReports 报表应用教程 (4)---分栏报表
    ActiveReports 报表应用教程 (5)---解密电子商务领域首张电子发票的诞生(套打报表)
    hdu4467 Graph
    ActiveReports 报表应用教程 (6)---分组报表
    [leetcode]Search a 2D Matrix
    参加百度开放云编程马拉松后一点总结
    CAS服务器配置
    Tomcat 的 SSL 配置
    在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM
  • 原文地址:https://www.cnblogs.com/sanwave/p/2019186.html
Copyright © 2020-2023  润新知