系统功能描述
Windows计算器是利用C#程序设计编写一个简易计算器程序,可以进行简单的加、减、乘、除运算,还可以实现对三角函数,对数、指数等高级运算的功能。
整个系统的功能结构图如图所示:
根据功能结构图,用户可以在窗体应用程序中,添加计算器窗体,建立windows应用程序的框架。因此,通过添加两个窗体控件Form1和Form2,分别用来表示科学计算器和普通计算器。然后用户添加Button控件,建立程序的主要界面。最后在每个按钮的代码段中,分别添加事件触发的处理代码。控件textBox1,显示输入的信息和显示处理后的结果。
系统功能分析及实现
控件表示用户和程序之间的图形化连接。控件可以提供或处理数据、接受用户输入、对事件做出响应或执行连接用户和应用程序的其他功能。窗体中的控件有很多,工具箱中的Windows窗体里包含了所有的windows标准控件。通过在属性窗口改变控件的属性可以改变控件的外观和特性。本项目需要用到以下控件:
(1) 按钮控件(Button):在Form1窗体中,添加30个Button控件,其中有10个数字按钮,16个运算按钮,一个用于计算的等号按钮,2个用于清空的按钮。在Form2窗体中,添加17个Button控件,其中有10个数字按钮,4个运算按钮,一个用于计算的等号按钮,一个用于清空的按钮。
(2) 文本框控件(TextBox):Form1和Form2分别添加一个TextBox控件,用于显示计算结果。
(3) 菜单栏控件(MenuStrip):在Form1和Form2中分别添加MenuStrip控件,并修改该控件中的ToolStripMenuItem菜单项的Text属性为机型转换。该控件可以实现两个窗体之间的相互转换。
(1)主窗体Form1.cs文件中的代码
using System;
using System.Windows.Forms;
namespace Windows计算器
{
public partial class Form1 : Form
{
private string Op;
private double Op1;
private double Op2;
private double result;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//数字键1
private void BtNumber1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键2
private void BtNumber2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键3
private void BtNumber3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键4
private void BtNumber4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键5
private void BtNumber5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键6
private void BtNumber6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键7
private void BtNumber7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键8
private void BtNumber8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键9
private void BtNumber9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//数字键0
private void BtNumber0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//加功能
private void BtAdd_Click(object sender, EventArgs e)
{
Op = "+";
Op2 = Op1;
textBox1.Text = "";
}
//减功能
private void BtSubtract_Click(object sender, EventArgs e)
{
Op = "-";
Op2 = Op1;
textBox1.Text = "";
}
//乘功能
private void BtMult_Click(object sender, EventArgs e)
{
Op = "*";
Op2 = Op1;
textBox1.Text = "";
}
//除功能
private void BtDivision_Click(object sender, EventArgs e)
{
Op = "/";
Op2 = Op1;
textBox1.Text = "";
}
//%
private void BtRemedial_Click(object sender, EventArgs e)
{
Op = "%";
Op2 = Op1;
textBox1.Text = "";
}
//Del功能
private void BtDel_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length >= 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
if (textBox1.Text.Length == 0)
{
textBox1.Text = "";
}
}
//小数点功能
private void BtComma_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf(".") < 0)
textBox1.Text += ((Button)sender).Text;
else
{
}
}
//等于功能
private void BtDebgyu_Click(object sender, EventArgs e)
{
switch (Op)
{
case "+":
result = Op2 + Op1;
break;
case "-":
result = Op2 - Op1;
break;
case "*":
result = Op2 * Op1;
break;
case "/":
if (Op2 == 0)
{
Console.WriteLine("错误,被除数不能为0!");
}
else
{
result = Op2 / Op1;
}
break;
case "%":
result = Op2 % Op1;
break;
case "^":
int m; double n;
m = 1; n = Op2;
while (m + 1 <= Op1)
{
n = n * Op2;
m++;
}
result = n;
break;
}
textBox1.Text = System.Convert.ToString(result);
}
//sin功能
private void Btsin_Click(object sender, EventArgs e)
{
Convert.ToDouble(Op1);
textBox1.Text = System.Convert.ToString(Math.Sin(Op1 * Math.PI / 180));
}
//con功能
private void Btcos_Click(object sender, EventArgs e)
{
Convert.ToDouble(Op1);
textBox1.Text = System.Convert.ToString(Math.Cos(Op1 * Math.PI / 180));
}
//tan功能
private void Bttan_Click(object sender, EventArgs e)
{
Convert.ToDouble(Op1);
textBox1.Text = System.Convert.ToString(Math.Tan(Op1 * Math.PI / 180));
}
//阶乘功能
private void BtFactorial_Click(object sender, EventArgs e)
{
int i, j;
i = 1; j = 1;
while (i <= Op1)
{
j *= i;
i++;
}
textBox1.Text = System.Convert.ToString(j);
}
//平方功能
private void BtSquare_Click(object sender, EventArgs e)
{
textBox1.Text = System.Convert.ToString(Op1 * Op1);
}
//立方功能
private void BtCube_Click(object sender, EventArgs e)
{
textBox1.Text = System.Convert.ToString(Op1 * Op1 * Op1);
}
//π
private void BtPI_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + Math.PI;
Op1 = System.Convert.ToDouble(textBox1.Text);
}
//清屏功能
private void BtClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
Op = "";
Op1 = 0;
Op2 = 0;
result = 0;
}
//幂运算
private void BtPower_Click(object sender, EventArgs e)
{
Op = "^";
Op2 = Op1;
textBox1.Text = "";
}
//10^x
private void BtIndex_Click(object sender, EventArgs e)
{
int a, b;
a = 1; b = 1;
while (a <= Op1)
{
b *= 10;
a++;
}
textBox1.Text = System.Convert.ToString(b);
}
//Log
private void BtLog_Click(object sender, EventArgs e)
{
textBox1.Text = System.Convert.ToString(Math.Log10(Op1));
}
//ln
private void BtLn_Click(object sender, EventArgs e)
{
textBox1.Text = System.Convert.ToString(Math.Log(Op1));
}
private void 机型转换ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 stu = new Form2();
this.Hide();
stu.ShowDialog();
this.Close();
}
}
}
(2)主窗体Form2.cs文件中的代码
using System;
using System.Windows.Forms;
namespace Windows计算器
{
public partial class Form2 : Form
{
private string Op;
private double Op1;
private double Op2;
private double result;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void BtNumber1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtNumber0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
Op1 = System.Convert.ToDouble(textBox1.Text);
}
private void BtComma_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf(".") < 0)
textBox1.Text += ((Button)sender).Text;
else
{
}
}
private void BtDebgyu_Click(object sender, EventArgs e)
{
switch (Op)
{
case "+":
result = Op2 + Op1;
break;
case "-":
result = Op2 - Op1;
break;
case "*":
result = Op2 * Op1;
break;
case "/":
result = Op2 / Op1;
break;
}
textBox1.Text = System.Convert.ToString(result);
}
private void BtAdd_Click(object sender, EventArgs e)
{
Op = "+";
Op2 = Op1;
textBox1.Text = "";
}
private void BtSubtract_Click(object sender, EventArgs e)
{
Op = "-";
Op2 = Op1;
textBox1.Text = "";
}
private void BtMult_Click(object sender, EventArgs e)
{
Op = "*";
Op2 = Op1;
textBox1.Text = "";
}
private void BtDivision_Click(object sender, EventArgs e)
{
Op = "/";
Op2 = Op1;
textBox1.Text = "";
}
private void BtClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
Op = "";
Op1 = 0;
Op2 = 0;
result = 0;
}
private void 机型转换ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 stu = new Form1();
this.Hide();
stu.ShowDialog();
this.Close();
}
}
}
(3)Program.cs代码
using System;
using System.Windows.Forms;
namespace Windows计算器
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}