C#学习之七---计算器应用程序

在《 Visual C#  2010 从入门到精通》的例子中有个计算器的应用程序---Maths Operators


里面用到了几个新的控件,在这里作一下记录,并讲讲自己多这个应用程序的理解:

RadioButton 和 StackPanel

正题:

StackPanel用来摆放RadioButton

RadioButton用来作选择,单击选中.

如下图:



if ((bool)addition.IsChecked)
    addValues();
else if ((bool)subtraction.IsChecked)
    subtractValues();
else if ((bool)multiplication.IsChecked)
    multiplyValues();
else if ((bool)division.IsChecked)
    divideValues();
else if ((bool)remainder.IsChecked)
    remainderValues();

用来判断RadioButton是否选中.


这是整体程序:


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 MathsOperators
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void calculateClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((bool)addition.IsChecked)
                    addValues();
                else if ((bool)subtraction.IsChecked)
                    subtractValues();
                else if ((bool)multiplication.IsChecked)
                    multiplyValues();
                else if ((bool)division.IsChecked)
                    divideValues();
                else if ((bool)remainder.IsChecked)
                    remainderValues();
                if (!((bool)addition.IsChecked || (bool)subtraction.IsChecked || (bool)multiplication.IsChecked 
			|| (bool)division.IsChecked || (bool)remainder.IsChecked))
                {
                    expression.Text = "请选择运算法则!";
                }
            }
            catch (Exception caught)
            {
                expression.Text = "异常!!!";
                result.Text = caught.Message;
                
            }
        }
   
        private void addValues()
        {

            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            
            outcome = lhs + rhs;
            expression.Text = lhsOperand.Text + " + " + rhsOperand.Text;
            result.Text = outcome.ToString();
        }

        private void subtractValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs - rhs;
            expression.Text = lhsOperand.Text + " - " + rhsOperand.Text;
            result.Text = outcome.ToString();
        }

        private void multiplyValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs * rhs;
            expression.Text = lhsOperand.Text + " * " + rhsOperand.Text;
            result.Text = outcome.ToString();
        }

        private void divideValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs / rhs;
            expression.Text = lhsOperand.Text + " / " + rhsOperand.Text;
            result.Text = outcome.ToString();
        }

        private void remainderValues()
        {
            int lhs = int.Parse(lhsOperand.Text);
            int rhs = int.Parse(rhsOperand.Text);
            int outcome;
            outcome = lhs % rhs;
            expression.Text = lhsOperand.Text + " % " + rhsOperand.Text;
            result.Text = outcome.ToString();
        }

        private void quitClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

 
    }
}


流程是判断哪种预算法则来进行相应的运算服务程序.

try 和catch来扑捉异常!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值