Ecrire une calculatrice en langage C #



Les pièces jointes du système informatique d' exploitation Mimic Windows sont la conception de style , pour concevoir les éléments de menu de base ( touche de raccourci, touche de raccourci , etc. et la ligne de séparation )

        

 

 

Exigences de fonction : réalisez les touches de fonction comme indiqué dans la figure ci-dessous

 



La méthode de paramétrage du raccourci clavier est la suivante:

 

Utilisez d'abord l'élément de formulaire pour créer le formulaire comme indiqué dans la figure

Cliquez avec le bouton droit sur l'élément du menu déroulant

 

Cliquez sur modifier l'élément pour définir le raccourci clavier

 

Cliquez sur la collection DropdownItems

Réglez le raccourci clavier ici, et ainsi de suite;

 

 

 

Une fois tous les réglages terminés, comme indiqué

 

 

Cliquez ensuite sur le formulaire du calculateur

La zone de texte d'affichage initiale est vide; ajoutez la surveillance d'opérande pour 0-9 et le point décimal,

Ajoutez la surveillance des opérateurs pour les opérateurs et ajoutez des écouteurs pour la suppression, le retour arrière et le vide respectivement;

Le code entier est le suivant:

<span style="font-family:Comic Sans MS;font-size:14px;">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //保存用户所按的运算符
        private string s;
        //保存用户输入的运算数
        private double x, y;
        //运算符按钮对象
        private Button btn;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        private void buttond_Click(object sender, EventArgs e)
        {
            btn = (Button)sender;
            textBox1.Text += btn.Text;
        }
        private void buttonop_Click(object sender, EventArgs e)
        {
            btn = (Button)sender;
            if(btn.Name!="btn_Equal")
            {
                x = Convert.ToDouble(textBox1.Text);
                textBox1.Text = "";
                s = btn.Name;
                switch (s)
                {
                    case "btn_Recip":
                        textBox1.Text = "1/" + x.ToString() + "=" + (1/x).ToString();
                        break;
                    case "btn_Sqrt":
                        textBox1.Text = "√" + x.ToString() + "=" + Math.Sqrt(x).ToString();
                        break;
                }
            }
            else
            {
                y = Convert.ToDouble(textBox1.Text);
                switch(s)
                {
                    case"btn_Add":
                        textBox1.Text = x.ToString()+"+"+y.ToString()+"="+(x + y).ToString();
                        break;
                    case "btn_Sub":
                        textBox1.Text = x.ToString() + "-" + y.ToString() + "=" + (x - y).ToString();
                        break;
                    case "btn_Mul":
                        textBox1.Text = x.ToString() + "*" + y.ToString() + "=" + (x * y).ToString();
                        break;
                    case "btn_Div":
                        if (y == 0)
                        {
                            MessageBox.Show("除零错误!!!", "信息提示", MessageBoxButtons.OK);
                        }
                        else
                            textBox1.Text = x.ToString() + "/" + y.ToString() + "=" + (x / y).ToString();
                        break;
                    case "btn_Mod":
                        textBox1.Text = x.ToString() + "%" + y.ToString() + "=" + (x % y).ToString();
                        break;
                }
            }
        }
        private void changeSign(object sender, EventArgs e)        
        {            
            double storNum;             
            if (textBox1.Text.Length > 0)  
            {
                storNum = double.Parse(textBox1.Text);                 
                storNum *= -1;               
                textBox1.Text = storNum.ToString();           
            }            
            btn_Equal.Select();       
        }
        private void btnDeleteSign_Clicked(object sender, EventArgs e)
        {
             
        }
        private void btnDeleteAll_Clicked(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        private void btnDelete1_Clicked(object sender, EventArgs e)
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            if(this.textBox1.Text=="")
            {
                this.textBox1.Text = "";
            }
        }
    }
}</span>

Le résultat final de l'exécution est le suivant:

 


Je suppose que tu aimes

Origine blog.csdn.net/dream_18/article/details/51713400
conseillé
Classement