C#学习过程中,遇到一个作业:仿照windows自带计算器编写一个窗体应用程序。经过学习、编写、不断试错和优化,做出了一个满意度较高的成品。
程序界面与功能如下:
要能够实现按钮的输入操作,在这一步,可以将多个按钮的Click函数合并,用一个函数来控制多个不同但功能类似的按钮。
private void Numbtn_Click(object sender, EventArgs e)//数字部分
{
Button btn = (Button)sender;
if (output)//连等结束后的处理
{
left_num = right_num = result = 0;
textBox1.Text = "";
flag = ' ';
output = false;
}
if (btn.Text.Equals("0"))
{
if (start || textBox2.Text == "0")//计算结束后重新输入,输入框清零
{
textBox2.Text = "";
textBox2.Text += "0";
put0 = false;
}
if (put0)//只有允许输出0时才能输出0
{
textBox2.Text += "0";
}
}
else
{
if (start || textBox2.Text == "0")
{
textBox2.Text = "";
start = false;
}
textBox2.Text += btn.Text;//输入数字
if (flag != ' ')//若有运算符,则能够进行运算
end = true;
put0 = true;//输入数字后可以输入多个0
}
}
数字输入部分功能:
(1)连等后数字重新输入
(2)计算结束后数字重新输入
(3)添加数字
private void Flagbtn_Click(object sender, EventArgs e)//符号部分
{
Button btn = (Button)sender;
if (flag != '+' && textBox2.Text == "")//符号变换
{
if (!(string.IsNullOrEmpty(textBox1.Text)))//判断非空
{
int temp = textBox1.Text.Length;
textBox1.Text = textBox1.Text.Substring(0, temp - 1);
textBox1.Text += btn.Text;
Flagchange(btn.Text);
return;
}
}
if (end)//计算
{
right_num = Convert.ToDouble(textBox2.Text);
Numcal();
textBox1.Text = Convert.ToString(result) + btn.Text;
Flagchange(btn.Text);
start = true;
end = false;
}
else//添加符号
{
checkPoint();
left_num = Convert.ToDouble(textBox2.Text);
textBox1.Text = textBox2.Text + btn.Text;
Flagchange(btn.Text);
output = false;
start = true;
}
}
符号输入部分功能:
(1)已经输入符号,需要变换符号。例如: 已经输入”6+“,想要改为”6-“,故而在已经输入过符号后再次输入符号
(2)连续计算。例如6+3+4。由于本程序为仿照windows简易计算器功能,故不具备括号或优先级计算的功能,仅能实现输入第二个+号时,自动计算前面式子并输出答案的功能
(3)添加符号。如果未输入符号,则添加按钮相应符号
private void button18_Click(object sender, EventArgs e)//等号部分
{
checkPoint();
if (flag == ' ')//无运算的等号
{
textBox1.Text = textBox2.Text + "=";
start = true;
return;
}
if (output)//处理连等
{
textBox1.Text = left_num.ToString() + textBox1.Text.Substring(textBox1.Text.Length - right_num.ToString().Length - 2, right_num.ToString().Length + 2);
Numcal();
start = true;
end = false;
put0 = false;
return;
}
//计算
right_num = Convert.ToDouble(textBox2.Text);
textBox1.Text += textBox2.Text + "=";
Numcal();
start = true;
end = false;
output = true;
put0 = false;
}
等号输入部分功能:
(1)无运算的等号
(2)连等处理
(3)等号的计算
剩余根号、百分号、倒数、正负号、清零、删除部分较为简单,故不详细介绍。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Button = System.Windows.Forms.Button;
namespace Calculator
{
public partial class Calculation : Form
{
public Calculation()
{
InitializeComponent();
}
double left_num = 0, right_num = 0, result = 0;
bool start = true, end = false, put0 = false, output = false;
char flag = ' ';//根据加减乘除的选择控制运算
public void Flagchange(string btn_flag)//控制符号
{
if(btn_flag == "+")
{
flag = '+';
}
else if(btn_flag == "-")
{
flag = '-';
}
else if(btn_flag == "×")
{
flag = '*';
}
else if(btn_flag == "÷")
{
flag = '/';
}
}
public void Numcal()//计算
{
if (flag == '+')
result = left_num + right_num;
else if (flag == '-')
result = left_num - right_num;
else if (flag == '*')
result = left_num * right_num;
else if (flag == '/')
result = left_num / right_num;
left_num = result;
textBox2.Text = Convert.ToString(result);
}
public void checkPoint()//检查小数点后有无数字
{
if (textBox2.Text.Contains("."))
{
try
{
Convert.ToInt32(textBox2.Text[textBox2.Text.IndexOf(".") + 1]);
}
catch
{
textBox2.Text = textBox2.Text.Replace(".", "");
}
}
}
private void Numbtn_Click(object sender, EventArgs e)//数字部分
{
Button btn = (Button)sender;
if (output)//连等结束后的处理
{
left_num = right_num = result = 0;
textBox1.Text = "";
flag = ' ';
output = false;
}
if (btn.Text.Equals("0"))
{
if (start || textBox2.Text == "0")//计算结束后重新输入,输入框清零
{
textBox2.Text = "";
textBox2.Text += "0";
put0 = false;
}
if (put0)//只有允许输出0时才能输出0
{
textBox2.Text += "0";
}
}
else
{
if (start || textBox2.Text == "0")
{
textBox2.Text = "";
start = false;
}
textBox2.Text += btn.Text;//输入数字
if (flag != ' ')//若有运算符,则能够进行运算
end = true;
put0 = true;//输入数字后可以输入多个0
}
}
private void button11_Click(object sender, EventArgs e)//小数点部分
{
if (start)
{
textBox2.Text = "0";
}
if (!textBox2.Text.Contains("."))//只有不存在小数点时才允许输入,防止小数点过多
{
textBox2.Text += ".";
start = false;
}
}
private void Flagbtn_Click(object sender, EventArgs e)//符号部分
{
Button btn = (Button)sender;
if (flag != '+' && textBox2.Text == "")//符号变换
{
if (!(string.IsNullOrEmpty(textBox1.Text)))//判断非空
{
int temp = textBox1.Text.Length;
textBox1.Text = textBox1.Text.Substring(0, temp - 1);
textBox1.Text += btn.Text;
Flagchange(btn.Text);
return;
}
}
if (end)//计算
{
right_num = Convert.ToDouble(textBox2.Text);
Numcal();
textBox1.Text = Convert.ToString(result) + btn.Text;
Flagchange(btn.Text);
start = true;
end = false;
}
else//添加符号
{
checkPoint();
left_num = Convert.ToDouble(textBox2.Text);
textBox1.Text = textBox2.Text + btn.Text;
Flagchange(btn.Text);
output = false;
start = true;
}
}
private void button16_Click(object sender, EventArgs e)//百分号部分
{
checkPoint();
textBox2.Text = Convert.ToString(Convert.ToDouble(textBox2.Text) * left_num / 100);
}
private void button17_Click(object sender, EventArgs e)//倒数部分
{
checkPoint();
textBox1.Text = "1/(" + textBox2.Text + ")";
textBox2.Text = Convert.ToString(1 / (Convert.ToDouble(textBox2.Text)));
left_num = result;
start = true;
put0 = false;
end = false;
}
private void button18_Click(object sender, EventArgs e)//等号部分
{
checkPoint();
if (flag == ' ')//无运算的等号
{
textBox1.Text = textBox2.Text + "=";
start = true;
return;
}
if (output)//处理连等
{
textBox1.Text = left_num.ToString() + textBox1.Text.Substring(textBox1.Text.Length - right_num.ToString().Length - 2, right_num.ToString().Length + 2);
Numcal();
start = true;
end = false;
put0 = false;
return;
}
//计算
right_num = Convert.ToDouble(textBox2.Text);
textBox1.Text += textBox2.Text + "=";
Numcal();
start = true;
end = false;
output = true;
put0 = false;
}
private void button19_Click(object sender, EventArgs e)//根号部分
{
checkPoint();
textBox1.Text = "√(" + textBox2.Text + ")";
textBox2.Text = Convert.ToString(Math.Sqrt(Convert.ToDouble(textBox2.Text)));
left_num = result;
start = true;
put0 = false;
end = false;
}
private void button20_Click(object sender, EventArgs e)//正负号部分
{
checkPoint();
textBox2.Text = Convert.ToString(Convert.ToDouble(textBox2.Text) * -1);//只需乘以-1即可(感谢zjt)
}
private void button21_Click(object sender, EventArgs e)//CE部分
{
textBox2.Text = "0";
put0 = false;
start = true;
end = false;
}
private void button22_Click(object sender, EventArgs e)//C部分
{
left_num = right_num = result = 0;
textBox1.Text = "";
textBox2.Text = "0";
flag = ' ';
start = true;
put0 = false;
end = false;
}
private void button23_Click(object sender, EventArgs e)//Delete部分
{
if (!start)//计算完成不允许删除
{
if (textBox2.Text != "0")
{
int temp = textBox2.Text.Length;
textBox2.Text = textBox2.Text.Substring(0, temp - 1);
if (textBox2.Text == "")
{
textBox2.Text = "0";
put0 = false;
start = true;
}
}
}
else
{
textBox1.Text = "";
}
}
}
}
再给大家分享一下笔者在编写程序过程中遇到的一些bug,各位也可据此测试一下自己的程序并进行相关优化。