您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 汽车理论 > 用C#做的一个计算器
用C#做的一个计算器1.usingSystem;2.usingSystem.Collections.Generic;3.usingSystem.Linq;4.usingSystem.Text;5.usingSystem.Windows;6.usingSystem.Windows.Controls;7.usingSystem.Windows.Data;8.usingSystem.Windows.Documents;9.usingSystem.Windows.Input;10.usingSystem.Windows.Media;11.usingSystem.Windows.Media.Imaging;12.usingSystem.Windows.Navigation;13.usingSystem.Windows.Shapes;14.15./************************************************/16./*******本程序供学习交流*******/17./*******作者:张骏南*******/18./*******最后修改时间*******/19./*******2011年3月10日21:03:17*******/20./************************************************/21.22.namespace计算器23.{24.///summary25.///Window1.xaml的交互逻辑26.////summary27.publicpartialclassWindow1:Window28.{29.privatedecimalm_dInput=0;30.privateintm_iPlaces=0;//数的小数位31.privateboolm_bIsFloat=false;//标记是否为小数32.privateboolm_bNewInt=false;//标记是否需要新的整数数33.privateboolm_bNewFloat=false;//标记是否需要新的浮点数34.privateCounterm_ctrCounter=newCounter();35.privateAboutBox1m_abtAboutBox;36.37.publicWindow1()38.{39.InitializeComponent();40.updateInput();41.}42.43.//输入更新结果显示框44.privatevoidupdateInput()45.{46.//去除末尾多于的047.if(m_bIsFloat)48.while(m_dInput.ToString().LastIndexOf('0')==m_dInput.ToString().Length-1&&m_dInput!=0)49.m_dInput=Convert.ToDecimal(m_dInput.ToString().Substring(0,m_dInput.ToString().Length-1));50.51.//在结果加上适当的小数点52.if(m_dInput.ToString().Contains(.))53.result.Text=m_dInput.ToString();54.else55.result.Text=m_dInput.ToString()+.;56.}57.58.#region数字单击事件59.60.privatevoidnumber_Click(objectsender,RoutedEventArgse)61.{62.newNumber();63.//确定是哪个数字被点击64.intinputNumber=Convert.ToInt32(sender.ToString().Substring(32,1));65.try66.{67.if(m_dInput=0)68.{69.if(m_bIsFloat)70.m_dInput+=(decimal)Math.Pow(10,-++m_iPlaces)*inputNumber;//求inputNumber乘10的-N次方71.else72.m_dInput=m_dInput*10+inputNumber;73.}74.else75.{76.if(m_bIsFloat)77.m_dInput-=(decimal)Math.Pow(10,-++m_iPlaces)*inputNumber;//求inputNumber乘10的-N次方78.else79.m_dInput=m_dInput*10-inputNumber;80.}81.}82.catch(OverflowException)83.{84.MessageBox.Show(输入的数超过范围,请重新输入,错误);85.buttonCE_Click(sender,e);86.}87.updateInput();88.}89.90.//数字0的处理与其他不同91.privatevoidnumber0_Click(objectsender,RoutedEventArgse)92.{93.if(m_bNewInt)94.{95.m_dInput=0;96.m_iPlaces=0;97.if(m_bNewFloat)98.{99.m_bIsFloat=true;100.m_bNewFloat=false;101.result.Text=0.;102.}103.else104.m_bIsFloat=false;105.m_bNewInt=false;106.}107.try108.{109.if(m_bIsFloat)110.{111.++m_iPlaces;112.result.Text+=0;113.}114.else115.{116.m_dInput*=10;117.updateInput();118.}119.}120.catch(OverflowException)121.{122.MessageBox.Show(输入的数超过范围,请重新输入,错误);123.buttonCE_Click(sender,e);124.}125.}126.127.#endregion128.129.//即将输入新的数130.privatevoidnewNumber()131.{132.if(m_bNewInt)133.{134.m_dInput=0;135.m_iPlaces=0;136.if(m_bNewFloat)137.m_bNewFloat=false;138.else139.m_bIsFloat=false;140.m_bNewInt=false;141.}142.}143.144.#region操作按钮的单击事件145.146.//清空按钮被单击,清空当前输入数据147.privatevoidbuttonCE_Click(objectsender,RoutedEventArgse)148.{149.m_bIsFloat=false;150.m_bNewFloat=false;151.m_bNewInt=false;152.m_dInput=0;153.m_iPlaces=0;154.updateInput();155.}156.157.//退位操作158.privatevoidbuttonBackspace_Click(objectsender,RoutedEventArgse)159.{160.//不需要新的数161.m_bNewInt=false;162.163.//判断是否为小数并退一位164.if(m_bIsFloat)165.--m_iPlaces;166.167.if(m_dInput.ToString().Length==1)168.{169.if(m_bIsFloat)170.{171.if(m_iPlaces==-1)172.{173.m_bIsFloat=false;174.m_iPlaces=0;175.}176.else177.{178.result.Text=result.Text.Substring(0,result.Text.Length-1);179.m_dInput=Convert.ToDecimal(result.Text);180.return;//结束方法181.}182.}183.else184.m_dInput=0;185.}186.else187.{188.if(m_bIsFloat)189.{190.if(m_iPlaces==-1)191.{192.m_bIsFloat=false;193.m_iPlaces=0;194.}195.else196.{197.result.Text=result.Text.Substring(0,result.Text.Length-1);198.m_dInput=Convert.ToDecimal(result.Text);199.return;200.}201.}202.else203.m_dInput=Convert.ToDecimal(m_dInput.ToString().Substring(0,m_dInput.ToString().Length-1));204.}205.updateInput();206.}207.208.//负号按钮被单击事件209.privatevoidminus_Click(objectsender,RoutedEventArgse)210.{211.m_dInput=-m_dInput;212.updateInput();213.}214.215.//小数点按钮被单击事件216.privatevoiddot_Click(objectsender,RoutedEventArgse)217.{218.if(m_bNewInt)219.m_bNewFloat=true;220.m_bIsFloat=true;221.}222.223.//重启按钮被单击事件,计算器重启224.privatevoidbuttonC_Click(objectsender,RoutedEventArgse)225.{226.m_ctrCounter.Clear();227.buttonCE_Click(sender,e);228.}229.230.//开方按钮被单击事件231.privatevoidoperatorSqrt_Click(objectsender,RoutedEventArgse)232.{233.m_dInput=(decimal)Math.Sqrt((double)m_dInput);234.updateInput();235.m_bNewInt=true;236.}237.238.//除法按钮被单击事件239.privatevoidoperatorDivide_Click(objectsender,RoutedEventArgse)240.{241.m_dInput=m_ctrCounter.Calculate(m_dInput,Counter.Operators.Divide);242.updateInput();243.m_bNewInt=true;244.}245.246.//乘法按钮被单击事件247.privatevoidoperatorMultiply_Click(objectsender,RoutedEventArgse)248.{249.m_dInput=m_ctrCounter.Calculate(m_dInput,Counter.Operators.Multiply);250.updateInput();251.m_bNewInt=true;252.}253.254.//减法按钮被单击事件255.privatevoidoperatorSubtract_Click(objectsender,RoutedEventArgse
本文标题:用C#做的一个计算器
链接地址:https://www.777doc.com/doc-4710434 .html