下面C#windows程序实现一个简单的计算器,要求能够实现基本的加、减、乘、除功能,并以这个“计算器”为基础,进一步考虑计算器的更多功能设计.
时间: 2024-02-03 19:13:30 浏览: 34
以下是一个简单的 C# Windows 程序实现计算器的例子:
```csharp
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
double resultValue = 0;
string operatorClicked = "";
bool isOperatorClicked = false;
public Form1()
{
InitializeComponent();
}
private void number_Click(object sender, EventArgs e)
{
if ((resultBox.Text == "0") || (isOperatorClicked))
{
resultBox.Clear();
}
isOperatorClicked = false;
Button button = (Button)sender;
if (button.Text == ".")
{
if (!resultBox.Text.Contains("."))
{
resultBox.Text += button.Text;
}
}
else
{
resultBox.Text += button.Text;
}
}
private void operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultValue != 0)
{
equalButton.PerformClick();
operatorClicked = button.Text;
label1.Text = resultValue + "" + operatorClicked;
isOperatorClicked = true;
}
else
{
operatorClicked = button.Text;
resultValue = Double.Parse(resultBox.Text);
label1.Text = resultValue + "" + operatorClicked;
isOperatorClicked = true;
}
}
private void clearButton_Click(object sender, EventArgs e)
{
resultBox.Text = "0";
resultValue = 0;
label1.Text = "";
}
private void equalButton_Click(object sender, EventArgs e)
{
switch (operatorClicked)
{
case "+":
resultBox.Text = (resultValue + Double.Parse(resultBox.Text)).ToString();
break;
case "-":
resultBox.Text = (resultValue - Double.Parse(resultBox.Text)).ToString();
break;
case "*":
resultBox.Text = (resultValue * Double.Parse(resultBox.Text)).ToString();
break;
case "/":
resultBox.Text = (resultValue / Double.Parse(resultBox.Text)).ToString();
break;
default:
break;
}
resultValue = Double.Parse(resultBox.Text);
label1.Text = "";
}
}
}
```
在这个程序中,我们定义了一个 `resultValue` 变量来保存当前的结果值,以及一个 `operatorClicked` 变量来保存当前的操作符。当用户点击数字按钮时,这个程序将数字添加到 `resultBox` 控件中,当用户点击操作符按钮时,程序将执行前一个操作符,然后将当前操作符保存到 `operatorClicked` 变量中。当用户点击等于号按钮时,程序将根据当前的操作符执行计算,并将结果显示在 `resultBox` 控件中。
这个计算器只实现了加、减、乘、除四种基本运算,如果想要增加更多的功能,可以考虑添加以下功能:
1. 添加取模、求幂、开方等高级函数。
2. 添加支持括号的运算。
3. 添加历史记录功能,记录用户之前进行过的计算。
4. 添加单位转换功能,比如将长度、重量、温度等转换为不同的单位。
5. 添加复数运算功能。
6. 添加图形计算功能,比如计算圆的面积和周长。
以上是一些计算器可扩展的功能,您可以根据自己的需求和兴趣来进行扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)