简单复数计算器

时间:2014-12-16 08:28:13
【文件属性】:

文件名称:简单复数计算器

文件大小:22KB

文件格式:RAR

更新时间:2014-12-16 08:28:13

复数加减乘除基本运算器

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 WindowsFormsApplication1 { public partial class Form1 : Form { public struct Complex { public double real; public double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } } Complex a, b, result = new Complex(0, 0); // 复数结构的声明 public Complex CPlus(Complex a, Complex b) { result = new Complex((a.real + b.real), (a.imag + b.imag)); return result; } //加法计算函数声明 public Complex CMinus(Complex a, Complex b) { result = new Complex((a.real - b.real), (a.imag - b.imag)); return result; } //减法计算函数声明 public Complex CMultiply(Complex a, Complex b) { result = new Complex((a.real * b.real - a.imag * b.imag), (a.real * b.imag + a.imag * b.real)); return result; } //乘法计算函数声明 public Complex CDivide(Complex a, Complex b) { result = new Complex((a.real * b.real + a.imag * b.imag) / (b.real * b.real + b.imag * b.imag), (b.real * a.imag - a.real * b.imag) / (b.real * b.real + b.imag * b.imag)); return result; } // 除法计算函数声明 public int flag = 0; //输入框判断标志,初始值为0 public string op="0"; //计算符号标志,初始值为“0” public string Equel ; // 是否清空判断标志 public Form1() { InitializeComponent(); } private void button0_Click(object sender, EventArgs e) //”0”~”9”、”.”、”i”等按键处理方法 { (即相应的输入框回显按钮字符) switch (flag) { case 1:a_real.Text += button0.Text; break; case 2: a_imag.Text += button0.Text; break; case 3: b_real.Text += button0.Text; break; case 4: b_imag. Text += button0.Text; break; case 5: textBox_input.Text += button0.Text; break; } } private void clear_Click(object sender, EventArgs e) //清零按键的处理 { a_real.Clear(); a_imag.Clear(); b_real.Clear(); b_imag.Clear(); output_real.Clear(); output_imag.Clear(); textBox_input.Clear(); result_real.Clear(); result_imag.Clear(); } private void button_1_Click(object sender, EventArgs e) //button1~5输入框是否可输入的控制按键处理 { flag = 1; if (equel == "=") a_real.Clear(); //判断是否该输入框需要清零 a_real.Enabled = true; a_imag.Enabled = false; b_real.Enabled = false; b_imag.Enabled = false; textBox_input.Enabled = false; } private void equal_Click(object sender, EventArgs e)// 等号按键处理方法 { a = new Complex(Convert.ToDouble(a_real.Text), Convert.ToDouble(a_imag.Text)); b = new Complex(Convert.ToDouble(b_real.Text), Convert.ToDouble(b_imag.Text)); //将输入的数据转换成双精度类型数据保存以计算 switch (op) //根据计算符号判定调用的函数进行计算 { case "+": CPlus(a, b); break; case "-": CMinus(a, b); break; case "*": CMultiply(a, b); break; case "/": if (b_real.Text == "0" && b_imag.Text == "0") MessageBox.Show("No zero!Please enter again!" + "denominator is" + b.real + "+(" + b.imag + ")i", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);//除法运算判断分母是否为0,若为0则提示错误 else { CDivide(a, b); } break; } result_real.Text = Convert.ToString(result.real); result_imag.Text = Convert.ToString(result.imag);/ / 回显当前计算的结果 equel = "="; flag = 0; //修改标志 } private void extract_Click(object sender, EventArgs e) //复数实部虚部提取按键方法 { String str = textBox_input.Text; string tem,temp; int i, j,m; int len = str.Length; bool k = true; //初始值赋值 i = str.IndexOf("i"); //判断i在字符串中的位置 for (j= 0; j< len - 1&& k; j++) { temp = Convert.ToString(str[j]); if (temp == "+" || temp == "-") k = false; } //判断第一个符号在字符串中的位置 j--; temp = Convert.ToString(str[j]); if (i == len - 1) // 当i在字符串最后时处理办法 { if (temp == "+")当第一符号为正时候处理办法 { if (j == len - 2) output_imag.Text = "1"; // 当虚部为i时,输出为1 else output_imag.Text = str.Substring(j + 1, len - j - 2); } Else //当符号为负时的处理方法 { if (j == 0) { for (m = 1; m< len - 1 && k; m++) { tem= Convert.ToString(str[m]); if (temp == "+" || temp == "-") k = false; } m--; tem= Convert.ToString(str[m]); if (tem == "+") output_imag.Text = str.Substring(m + 1, len - 2 - m); else { if (m == len - 2) output_imag.Text = "-1"; else output_imag.Text = str.Substring(m, len - 1 - m); } output_real.Text = str.Substring(0, j); } else //当第一个符号为负的处理办法 { if (j == len - 2) output_imag.Text = "-1"; //虚部为-i时,输出为-1 else output_imag.Text = str.Substring(j , len - j-1 ); } } output_real.Text = str.Substring(0, j); } else { if (temp == "+") { if (i ==0) output_imag.Text = "1"; else output_imag.Text = str.Substring(0, i); output_real.Text = str.Substring(j+1, len - j - 1); } else { if (j == 0) { if (j == i - 1) output_imag.Text = "-1"; else output_imag.Text = str.Substring(0, i); tem = Convert.ToString(str[i + 1]); if (tem == "+") output_real.Text = str.Substring(i + 2, len - 2 - i); else output_real.Text = str.Substring(i + 1, len - 1 - i); } else { if (i == 0) output_imag.Text = "1"; else output_imag.Text = str.Substring(0, i); output_real.Text = str.Substring(j, len - j); } } } } private void button10_Click(object sender, EventArgs e) // 退格键的处理方法 { switch (flag) // 用字串提取函数提取前length-1个长度的字符串 { case 1: a_real.Text = a_real.Text.Substring(0, a_real.Text.Length - 1); break; case 2: a_imag.Text = a_imag.Text.Substring(0, a_imag.Text.Length - 1); break; case 3: b_real.Text = b_real.Text.Substring(0, b_real.Text.Length - 1); break; case 4: b_imag.Text = b_imag.Text.Substring(0, b_imag.Text.Length - 1); break; case5:textBox_input.Text=textBox_input.Text.Substring(0,textBox_input.Text.Length - 1); break; } } } }


【文件预览】:
复数计算器
----WindowsFormsApplication1.vshost.exe.manifest(490B)
----WindowsFormsApplication1.exe(25KB)
----WindowsFormsApplication1.pdb(40KB)
----WindowsFormsApplication1.vshost.exe(14KB)

网友评论

  • 非常感谢,感觉挺有用!
  • 不错,就是太简单了
  • = =感覺代碼很複雜
  • 太简单了,不过还是感谢了