C#——《C#语言程序设计》实验报告——Windows桌面编程——简单的计算器(仿Windows 10计算器)

一、实验目的

  1. 熟悉使用WPF进行界面编程的基本过程;
  2. 掌握WPF布局、控件、事件的使用。

 

二、实验内容

  1. 运用WPF技术,模仿Windows 10系统中计算机器(Calculator)程序,开发一个类似程序。

C#——《C#语言程序设计》实验报告——Windows桌面编程——简单的计算器(仿Windows 10计算器)_第1张图片

本题作为综合练习,可在最后提交。

加分项:

1)计算机器功能的完整性;

2)是否使用了数据绑定、模板、样式等特性;

3)功能上的创新。

源代码

XAML 


    
        
            
                
            
        
        
        
        

CS 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using MahApps.Metro.Controls;
using System.Windows.Controls.Primitives;

namespace Homework19
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            B_MC.IsEnabled = false;
            B_M__1.IsEnabled = false;
            B_M__.IsEnabled = false;
            B_MR.IsEnabled = false;
        }

        //temp用于临时存储操作数,oper存储最近一次的运算符
        private double temp, op1, op2, memory, result;
        private string oper = null;
        //next用来表示是否要输入新的操作数
        private bool next;

        //连续计算时先计算前面的结果
        private void Aut()
        {
            op2 = Convert.ToDouble(Out_1.Text);
            switch (oper)
            {
                case "+":
                    result = op1 + op2;
                    break;
                case "-":
                    result = op1 - op2;
                    break;
                case "×":
                    result = op1 * op2;
                    break;
                case "÷":
                    result = op1 / op2;
                    break;
                default:
                    break;

            }
            Out_1.Text = Convert.ToString(result);
            //Out_2.Text = Convert.ToString(result);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //目标
            this.contextMenu.PlacementTarget = this.btnMenu;
            //位置
            this.contextMenu.Placement = PlacementMode.Top;
            //显示菜单
            this.contextMenu.IsOpen = true;
        }
        private void B_NUM_Click(object sender, RoutedEventArgs e)
        {
            Button numBtn = sender as Button;
            string numstr = numBtn.Content.ToString();
            if (numstr != "0")
            {
                if (Out_1.Text == "0" || next)
                {
                    Out_1.Text = numstr;
                    if (next)
                    {
                        next = false;
                    }
                }
                else
                    Out_1.Text += numstr;
            }
            else{
                if (Out_1.Text != "0")
                {
                    Out_1.Text += "0";
                }
                if (next)
                {
                    Out_1.Text = "0";
                    next = false;
                }
            }
        }

        private void B_C_Click(object sender, RoutedEventArgs e)
        {
            Out_1.Text = "0";
            Out_2.Text = "";
            oper = null;
        }

        private void B_CE_Click(object sender, RoutedEventArgs e)
        {
            Out_1.Text = "0";
            Out_2.Text = "";
            oper = null;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Out_2.Text += Out_1.Text + "+";
            if (oper != null)
            {
                Aut();
            }
            op1 = Convert.ToDouble(Out_1.Text);
            oper = "+";
            next = true;
        }

        private void B_sub_Click(object sender, RoutedEventArgs e)
        {
            Out_2.Text += Out_1.Text + "-";
            if (oper != null)
            {
                Aut();
            }
            op1 = Convert.ToDouble(Out_1.Text);
            oper = "-";
            next = true;
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Out_2.Text += Out_1.Text + "×";
            if (oper != null)
            {
                Aut();
            }
            op1 = Convert.ToDouble(Out_1.Text);
            oper = "×";
            next = true;
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            Out_2.Text += Out_1.Text + "÷";
            if (oper != null)
            {
                Aut();
            }
            op1 = Convert.ToDouble(Out_1.Text);
            oper = "÷";
            next = true;
        }

        private void B_delete_Click(object sender, RoutedEventArgs e)
        {
            if (!next)
            {
                if (Out_1.Text.Length == 1)
                {
                    Out_1.Text = "0";
                }
                else
                {
                    Out_1.Text = Out_1.Text.Substring(0, Out_1.Text.Length - 1);
                }
            }
        }

        private void B_reciprocal_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                temp = Convert.ToDouble(Out_1.Text);
                temp = 1 / temp;
                Out_1.Text = Convert.ToString(temp);
            }
            catch (Exception ee)
            {
                Out_1.Text = "除数不能为0";
            }
        }

        private void B_sq_Click(object sender, RoutedEventArgs e)
        {
            temp = Convert.ToDouble(Out_1.Text);
            temp = Math.Pow(temp, 2);
            Out_1.Text = Convert.ToString(temp);
        }

        private void B_cu_Click(object sender, RoutedEventArgs e)
        {
            temp = Convert.ToDouble(Out_1.Text);
            temp = Math.Pow(temp, 3);
            Out_1.Text = Convert.ToString(temp);
        }

        private void B_radical_Click(object sender, RoutedEventArgs e)
        {
            temp = Convert.ToDouble(Out_1.Text);
            temp = Math.Pow(temp, 0.5);
            Out_1.Text = Convert.ToString(temp);
        }

        private void B_per_Click(object sender, RoutedEventArgs e)
        {
            temp = Convert.ToDouble(Out_1.Text);
            temp = temp / 100;
            Out_1.Text = Convert.ToString(temp);
        }

        private void B_MR_Click(object sender, RoutedEventArgs e)
        {
            Out_1.Text = Convert.ToString(memory);
        }

        private void B_M__1_Click(object sender, RoutedEventArgs e)
        {
            memory += Convert.ToDouble(Out_1.Text);
        }

        private void B_M___Click(object sender, RoutedEventArgs e)
        {
            memory -= Convert.ToDouble(Out_1.Text);
        }

        private void B_MC_Click(object sender, RoutedEventArgs e)
        {
            memory = 0;
            B_MC.IsEnabled = false;
            B_M__1.IsEnabled = false;
            B_M__.IsEnabled = false;
            B_MR.IsEnabled = false;
        }

        private void B_MS_Click(object sender, RoutedEventArgs e)
        {
            memory = Convert.ToDouble(Out_1.Text);
            B_MC.IsEnabled = true;
            B_M__1.IsEnabled = true;
            B_M__.IsEnabled = true;
            B_MR.IsEnabled = true;
        }

        private void B_eq_Click(object sender, RoutedEventArgs e)
        {
            op2 = Convert.ToDouble(Out_1.Text);
            switch (oper)
            {
                case "+":
                    result = op1 + op2;
                    break;
                case "-":
                    result = op1 - op2;
                    break;
                case "×":
                    result = op1 * op2;
                    break;
                case "÷":
                    result = op1 / op2;
                    break;
                default:
                    break;

            }
            oper = null;
            Out_1.Text = Convert.ToString(result);
            Out_2.Text = "";
        }

        private void B_opp_Click(object sender, RoutedEventArgs e)
        {
            temp = Convert.ToDouble(Out_1.Text);
            if (temp != 0)
            {
                temp = 0 - temp;
            }
            Out_1.Text = Convert.ToString(temp);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Out_1.Text += ".";
        }
    }
}

 

运行结果

C#——《C#语言程序设计》实验报告——Windows桌面编程——简单的计算器(仿Windows 10计算器)_第2张图片

三、实验心得与体会

  1. 熟悉使用WPF进行界面编程的基本过程;
  2. 掌握WPF布局、控件、事件的使用。
  3. 熟悉使用数据绑定、模板、样式等特性;

源代码 

Windows 10 计算器 C++源代码 

https://github.com/Microsoft/calculator

仿Windows 10 计算器 C#源代码 

https://github.com/BuXianShan/Calculator

参考文章

https://blog.csdn.net/qq_43479622/article/details/91599602

https://blog.csdn.net/OneWord233/article/details/80755284

https://www.cnblogs.com/lonelyxmas/p/12117453.html

https://blog.csdn.net/weixin_42009898/article/details/80921151

https://www.cnblogs.com/ouyangkai/p/12759466.html

你可能感兴趣的:(#,C#)