您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 简单复数计算器
  所属分类: C#
  开发工具:
  文件大小: 22kb
  下载次数: 0
  上传时间: 2011-11-26
  提 供 者: k4219*****
 详细说明: 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; } } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: