c# winform 程序实现 复数基本运算

来源:互联网 发布:java 内存泄漏检测工具 编辑:程序博客网 时间:2024/05/16 17:28

不多废话,直接源代码:

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 Complex
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox_operation.SelectedIndex = 0;
        }

        private void button_calc_Click(object sender, EventArgs e)
        {
            int real1 = 0, real2 = 0;
            int image1 = 0, image2 = 0;
            string opera="";

            try
            {
                real1 = Convert.ToInt32(this.textBox_real1.Text);
                image1 = Convert.ToInt32(this.textBox_image1.Text);
                real2 = Convert.ToInt32(this.textBox_real2.Text);
                image2 = Convert.ToInt32(this.textBox_image2.Text);
                opera = Convert.ToString(this.comboBox_operation.SelectedItem);
            }
            catch
            {
                MessageBox.Show("请输入正确形式的数据!", "错误提示");
            }

            switch (opera)
            {
                case "+":
                    this.textBox_result.Text = (real1 + real2) + ((image1 + image2) >= 0 ? "+" + (image1 + image2) + "i" : (image1 + image2) + "i");
                    break;
                case "-":
                    this.textBox_result.Text = (real1 - real2) + ((image1 - image2) >= 0 ? "+" + (image1 - image2) + "i" : (image1 - image2) + "i");
                    break;
                case "*":
                    this.textBox_result.Text = (real1 * real2 - image1 * image2) + ((real1 * image2 + real2 * image1) >= 0 ? "+" + (real1 * image2 + real2 * image1) + "i" : (real1 * image2 + real2 * image1) + "i");
                    break;
            }
        }
    }
}


程序运行结果: