zoukankan      html  css  js  c++  java
  • C#之简易计算器设计

    在学完了C#的方法和数据类型之后,写了一个简易的计算器的界面。本次界面具备加减乘除求余等五项运算。不过存在一点缺陷就是无法判断输入数据的类型,是整数还是小数,由于目前所学知识有限,等学到以后再进行完善。

    本设计源代码如下:

    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;


    namespace MathsOperators
    {
         /// <summary>
         /// MainWindow.xaml 的交互逻辑
         /// </summary>
         public partial class MainWindow : Window
         {
             int OperandFlag=0;
             public MainWindow()
             {
                 InitializeComponent();
             }

            private void Quit_Click(object sender, RoutedEventArgs e)
             {
                 Close();//关闭WPF窗口
             }
             /*编写加法的方法*/
             private void AddStractValue()
             {
                 int Left = int.Parse(LeftNum.Text);
                 int Right = int.Parse(RightNum.Text);
                 int ShowResult = Left + Right;
                 Expresssion.Text ="("+ Left+")" + "+" +"("+ Right+")";
                 Result.Text = ShowResult.ToString();
             }
             /*编写减法的方法*/
             private void SubStractValue()
             {
                 int Left = int.Parse(LeftNum.Text);
                 int Right = int.Parse(RightNum.Text);
                 int ShowResult = Left - Right;
                 Expresssion.Text = "(" + Left + ")" + "-" + "(" + Right + ")";
                 Result.Text = ShowResult.ToString();
             }
                   /*编写乘法的方法*/
             private void MultStractValue()
             {
                 int Left = int.Parse(LeftNum.Text);
                 int Right = int.Parse(RightNum.Text);
                 int ShowResult = Left * Right;
                 Expresssion.Text = "(" + Left + ")" + "*" + "(" + Right + ")";
                 Result.Text = ShowResult.ToString();
             }
             /*编写除法的方法*/
             private void DivStractValue()
             {
                 int Left = int.Parse(LeftNum.Text);
                 int Right = int.Parse(RightNum.Text);
                 float ShowResult = (float)(Left) / Right;
                 Expresssion.Text = "(" + Left + ")" + "/" + "(" + Right + ")";
                 Result.Text = ShowResult.ToString();
             }
             /*编写求余法的方法*/
             private void RemStractValue()
             {
                 int Left = int.Parse(LeftNum.Text);
                 int Right = int.Parse(RightNum.Text);
                 int ShowResult = Left % Right;
                 Expresssion.Text = "(" + Left + ")" + "%" + "(" + Right + ")";
                 Result.Text = ShowResult.ToString();
             }
             private void Calculate_Click(object sender, RoutedEventArgs e)
             {
                 switch (OperandFlag)
                 {
                     case 1: AddStractValue();  break;
                     case 2: SubStractValue();  break;
                     case 3: MultStractValue(); break;
                     case 4: DivStractValue();  break;
                     case 5: RemStractValue();  break;
                     default: break;
                 }
               
             }

            private void Add_Checked(object sender, RoutedEventArgs e)
             {
                 OperandFlag = 1;
             }

            private void Sub_Checked(object sender, RoutedEventArgs e)
             {
                 OperandFlag = 2;
             }

            private void Mult_Checked(object sender, RoutedEventArgs e)
             {
                 OperandFlag = 3;
             }

            private void Div_Checked(object sender, RoutedEventArgs e)
             {
                 OperandFlag = 4;
             }
             private void Remain_Checked(object sender, RoutedEventArgs e)
             {
                 OperandFlag = 5;
             }
             private void LeftNum_TextChanged(object sender, TextChangedEventArgs e)
             {

            }
         }
    }

    本设计界面如下:

    image

    部分计算如下:

    99+12=111;

    image

    (99)-(-18)=117

    image

    99*(-18)=-1782

    image

    99/(-18)=-5.5

    image

    99%(-18)=9

    image

  • 相关阅读:
    linux查看CPU性能及工作状态的指令mpstat,vmstat,iostat,sar,top
    Linux vmstat命令实战详解
    dstat 性能监测工具
    sysstat 工具
    Linux命令详解----iostat
    Linux CPU实时监控mpstat命令详解
    Linux Top 命令解析 比较详细
    Linux统计/监控工具SAR详细介绍
    ubuntu 添加用户到已存在的组
    Ubuntu 14.04 使用速度极快的Genymotion 取代蜗牛速度的原生AVD模拟器
  • 原文地址:https://www.cnblogs.com/Mr-Wangblogs/p/9277405.html
Copyright © 2011-2022 走看看