C#之簡易計算器設計

在學完了C#的方法和數據類型以後,寫了一個簡易的計算器的界面。本次界面具有加減乘除求餘等五項運算。不過存在一點缺陷就是沒法判斷輸入數據的類型,是整數仍是小數,因爲目前所學知識有限,等學到之後再進行完善。ui

本設計源代碼以下:spa

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();
         }3d

        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;
             }
           
         }blog

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

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

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

        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)
         {class

        }
     }
}
object

本設計界面以下:

image

部分計算以下:

99+12=111;

image

(99)-(-18)=117

image

99*(-18)=-1782

image

99/(-18)=-5.5

image

99%(-18)=9

image

相關文章
相關標籤/搜索