100分急求用c#窗体程序做一个简单的四则运算界面

2024-11-25 19:14:57
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Algorithm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            char[] symbol=new char[]{'+','-','*','/'};
            for (int i = 0; i < symbol.Length; i++)
            {
                comboBox1.Items.Add(symbol[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double n1, n2;
            n1 = Convert.ToDouble(textBox1.Text);
            n2 = Convert.ToDouble(textBox2.Text);

            textBox3.Text = _algorithm((char)comboBox1.SelectedItem, n1, n2);
            
        }

        private string _algorithm(char symbol,double n1, double n2)
        {
            string result="";
            switch (symbol)
            {
                case '+':
                    result = (n1 + n2).ToString();
                    break;
                case '-':
                    result = (n1 - n2).ToString();
                    break;
                case '*':
                    result = (n1 * n2).ToString();
                    break;
                case '/':
                    result = n2==0?"除数不能为零。":(n1 / n2).ToString();
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}

回答2:

已发 operation by 56lea

private void btn_get_Click(object sender, EventArgs e)
{
if (tb_n1.Text.Trim().Length < 1) return;
if (tb_n2.Text.Trim().Length < 1) return;
float a = Convert.ToSingle(tb_n1.Text);
float b = Convert.ToSingle(tb_n2.Text);
if(cb.Text=="+")tb_result.Text=(a+b).ToString();
else if (cb.Text == "-") tb_result.Text = (a -b).ToString();
else if (cb.Text == "*") tb_result.Text = (a * b).ToString();
else if (cb.Text == "/") tb_result.Text = (a / b).ToString();
}