关于简易计算器的一个小问题
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace caculator { public partial class Form1 : Form { bool DataFlag = false; bool OperFlag = false; double dataNum1 = 0; double dataNum2 = 0; int Oper_Num = 0; public Form1() { InitializeComponent(); txt_show.KeyPress += new KeyPressEventHandler(txt_show_KeyPress); } //检验数字键 void txt_show_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar < 47 || e.KeyChar > 58) e.Handled = true; else e.Handled = false; } private void Form1_Load(object sender, EventArgs e) { } //backspace按键工作 private void btn_backsapce_Click(object sender, EventArgs e) { try { this.txt_show.Text = txt_show.Text.Substring(0, txt_show.Text.Length - 1); dataNum1 = 0; dataNum2 = 0; } catch (SystemException) { } } //C按键工作 private void btn_C_Click(object sender, EventArgs e) { txt_show.Text = txt_show.Text.Substring(0, 0); dataNum1 = 0; dataNum2 = 0; } //数字按键 private void btn_0_Click(object sender, EventArgs e) { DataNum_Click(0); } private void btn_1_Click(object sender, EventArgs e) { DataNum_Click(1); } private void btn_2_Click(object sender, EventArgs e) { DataNum_Click(2); } private void btn_3_Click(object sender, EventArgs e) { DataNum_Click(3); } private void btn_4_Click(object sender, EventArgs e) { DataNum_Click(4); } private void btn_5_Click(object sender, EventArgs e) { DataNum_Click(5); } private void btn_6_Click(object sender, EventArgs e) { DataNum_Click(6); } private void btn_7_Click(object sender, EventArgs e) { DataNum_Click(7); } private void btn_8_Click(object sender, EventArgs e) { DataNum_Click(8); } private void button9_Click(object sender, EventArgs e) { DataNum_Click(9); } //组合数字 private void DataNum_Click(int temp) { DataFlag = true; if (DataFlag) { if (!OperFlag) { dataNum1 = double.Parse(txt_show.Text + temp.ToString()); txt_show.Text = dataNum1.ToString(); } else { dataNum2 = double.Parse(txt_show.Text+temp.ToString()); txt_show.Text = dataNum2.ToString(); } } } //操作符处理 private void btn_add_Click(object sender, EventArgs e) { Oper_Num = 1; DataFlag = false; OperFlag = true; } private void btn_substract_Click(object sender, EventArgs e) { Oper_Num = 2; DataFlag = false; OperFlag = true; } private void btn_multiply_Click(object sender, EventArgs e) { Oper_Num = 3; DataFlag = false; OperFlag = true; } private void btn_divid_Click(object sender, EventArgs e) { Oper_Num = 4; DataFlag = false; OperFlag = true; } //等号求结果 private void btn_equal_Click(object sender, EventArgs e) { if (OperFlag) { calculate(dataNum1, dataNum2, Oper_Num); txt_show.Text = dataNum1.ToString(); dataNum2 = 0; OperFlag = false; } else { txt_show.Text = dataNum1.ToString(); } } private void calculate(double data1, double data2, int oper) { int oper_num = oper; switch(oper_num) { case 1:dataNum1=data1+data2;break; case 2:dataNum1=data1-data2;break; case 3:dataNum1=data1*data2;break; case 4:dataNum1=data1/data2;break; } } private void btn_dot_Click(object sender, EventArgs e) { char[] ch = txt_show.Text.ToCharArray(); for (int i = 0; i < ch.Length; i++)//这里数组的长度改成字符串的长度运行的时候就会报错 { if (ch[i] == '.') { txt_show.Text = "#####"; this.Close(); } txt_show.Text = txt_show.Text+ btn_dot.Text;//如果是输入1个数显示正确,但是输入两个以上就会输出两个点 } } } }
代码不完整,只有一小部分功能,现在有奇怪的是,我是输入小数点的时候,会出现两个小数点。希望大家指点下。