zoukankan      html  css  js  c++  java
  • 计算器简单封装和ASP.net

    封装:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 计算器
    {
        class Richnone
        {
            public string fuhao;//计算符号
            public double result;//计算结果
    
    
            private double x;//第一个数
            public double X
            {
                get { return x; }
                set { x = value; }
            }
            private double y;//第二个数
            public double Y
            {
                get { return y; }
                set { y = value; }
            }
            public void Add()//加法
            {
                if (fuhao=="+")
                {
                    result = X + Y;
                }
            }
            public void Sub()//减法
            {
                if (fuhao == "-")
                {
                    result = X - Y;
                }
            }
            public void Mul()//乘法
            {
                if (fuhao == "*")
                {
                    result = X * Y;
                }
            }
            public void Div()//除法
            {
                if (fuhao == "/")
                {
                    result = X / Y;
                }
            }
           
        }
    }



    Form1代码


      private void Form1_Load(object sender, EventArgs e)
            {
                if (File.Exists(path))
                {
                    this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                    open.Enabled = true;
                }
                save.Enabled = true;
            }
    
            private void open_Click(object sender, EventArgs e)
            {
                OpenFileDialog TxTOPenDialog = new OpenFileDialog();
                TxTOPenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOPenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOPenDialog.FileName;
                    this.richTextBox1.LoadFile(TxTOPenDialog.FileName, RichTextBoxStreamType.RichText);
                    open.Enabled = true;
                    save.Enabled = true;
                    MessageBox.Show(" 读取成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Show();
                }
            }
    
    
    
            private void save_Click(object sender, EventArgs e)
            {
                SaveFileDialog TxTSaveDialog = new SaveFileDialog();
                TxTSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                richTextBox1.Text = textBox2.Text;
                if (File.Exists(path))
                {
                    this.richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
                    MessageBox.Show(" 保存成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    save.Enabled = false;
                }
                else
                {
                    if (TxTSaveDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.richTextBox1.SaveFile(TxTSaveDialog.FileName, RichTextBoxStreamType.RichText);
                        MessageBox.Show(" 保存成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.richTextBox1.Clear();
                        save.Enabled = false;
                    }
                }
            }
    
    
     private void add_Click(object sender, EventArgs e)
            {
                SUAN.Text = "+";
            }
    
            private void sub_Click(object sender, EventArgs e)
            {
                SUAN.Text = "-";
            }
    
            private void mul_Click(object sender, EventArgs e)
            {
                SUAN.Text = "*";
            }
            private void div_Click_1(object sender, EventArgs e)
            {
                SUAN.Text = "/";
            }
    
           //实例对象
                Richnone ric = new Richnone();
                ric.X = double .Parse( richTextBox1.Text);//第一个数
                ric.Y = double.Parse(textBox2.Text);//第二个数
                ric.fuhao = SUAN.Text;//运算符号
                ric.result = result;//结果
                ric.Add();//加法
                ric.Sub();//减法
                ric.Mul();//乘法
                ric.Div();//除法
                //int sum;
                //sum = int.Parse(richTextBox1.Text) + int.Parse(textBox2.Text);
    
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox5.Text == ric.result.ToString())
                    {
                        right++;
    
                        MessageBox.Show("回答正确!");
                    }
                    else
                    {
                        
                        MessageBox.Show("回答错误!");
                    }
                    Count++;
                    richTextBox1.Clear();
                    textBox2.Clear();
                    textBox5.Clear();
    
                }
                
            }截图




    ASP.net


    后台代码
     protected void Button1_Click(object sender, EventArgs e)
        {
            ric.X =double .Parse( TextBox1.Text);
            ric.Y =double .Parse( TextBox3.Text);
            ric.fuhao = DropDownList1.SelectedValue;
            ric.result = result;
            ric.Add();
            ric.Sub();
            ric.Mul();
            ric.Div();
            if (TextBox4.Text == ric.result.ToString())
            {
                Response.Write("<script>alert('回答正确')</script>");
            }
            else
            {
                Response.Write("<script>alert('回答错误')</script>");
            }
        }
        protected void DropDownList1_TextChanged(object sender, EventArgs e)
        {
            string fuhao = DropDownList1.SelectedValue;
            switch (fuhao)
            {
                case"+":
                    DropDownList1.SelectedValue = "+";
                    break;
                case"-":
                    DropDownList1.SelectedValue = "-";
                    break;
                case"*":
                    DropDownList1.SelectedValue = "*";
                    break;
                case"/":
                    DropDownList1.SelectedValue = "/";
                    break;
                default:
                    break;
            }
        }



    封装
    public class Richnone
    {
        public string fuhao;//计算符号
        public double result;
    
    
        private double x;//第一个数
        public double X
        {
            get { return x; }
            set { x = value; }
        }
        private double y;//第二个数
        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public void Add()//加法
        {
            if (fuhao == "+")
            {
                result = X + Y;
            }
        }
        public void Sub()
        {
            if (fuhao == "-")
            {
                result = X - Y;
            }
        }
        public void Mul()
        {
            if (fuhao == "*")
            {
                result = X * Y;
            }
        }
        public void Div()
        {
            if (fuhao == "/")
            {
                result = X / Y;
            }
        }
           
    }



    截图




    
    
    









     
  • 相关阅读:
    吃素到底能不能减肥? 生活至上,美容至尚!
    女性生理期如何清洁? 生活至上,美容至尚!
    怎么去大蒜味(一定要看拉) 生活至上,美容至尚!
    排毒减肥几大绝招让你轻松扫除油脂 生活至上,美容至尚!
    常用食物排毒护身 排除毒素 一身轻松 生活至上,美容至尚!
    生活家健康饮食:巧用食醋让女人内外兼修 生活至上,美容至尚!
    冬季护肤必看!冬季巧防皮肤干燥 生活至上,美容至尚!
    喝牛奶要注意的一些事情 生活至上,美容至尚!
    夏天如何盘头发(附图解) 生活至上,美容至尚!
    黑眼圈的去除办法和草方 生活至上,美容至尚!
  • 原文地址:https://www.cnblogs.com/smj0630/p/5004925.html
Copyright © 2011-2022 走看看