科学计算器c#
时间: 2023-07-10 21:10:28 浏览: 53
好的,你需要一个科学计算器的C#代码,是吗?以下是一个简单的科学计算器的C#代码示例,你可以根据自己的需求进行修改或扩展。
```c#
using System;
using System.Windows.Forms;
namespace ScientificCalculator
{
public partial class Form1 : Form
{
double memory;
string operation;
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
if (isOperationPerformed)
{
textBox_Result.Clear();
isOperationPerformed = false;
}
Button button = (Button)sender;
if (button.Text == ".")
{
if (!textBox_Result.Text.Contains("."))
{
textBox_Result.Text = textBox_Result.Text + button.Text;
}
}
else
{
textBox_Result.Text = textBox_Result.Text + button.Text;
}
}
private void operator_Click(object sender, EventArgs e)
{
if (memory != 0)
{
button_Equals.PerformClick();
operation = ((Button)sender).Text;
memory = double.Parse(textBox_Result.Text);
isOperationPerformed = true;
}
else
{
operation = ((Button)sender).Text;
memory = double.Parse(textBox_Result.Text);
isOperationPerformed = true;
}
}
private void button_Clear_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
}
private void button_ClearEntry_Click(object sender, EventArgs e)
{
textBox_Result.Text = "0";
memory = 0;
}
private void button_Equals_Click(object sender, EventArgs e)
{
switch (operation)
{
case "+":
textBox_Result.Text = (memory + double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (memory - double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (memory * double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (memory / double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
memory = double.Parse(textBox_Result.Text);
isOperationPerformed = true;
}
private void button_SquareRoot_Click(object sender, EventArgs e)
{
double sqrt = Math.Sqrt(double.Parse(textBox_Result.Text));
textBox_Result.Text = sqrt.ToString();
memory = sqrt;
}
private void button_Square_Click(object sender, EventArgs e)
{
double square = Math.Pow(double.Parse(textBox_Result.Text), 2);
textBox_Result.Text = square.ToString();
memory = square;
}
private void button_Inverse_Click(object sender, EventArgs e)
{
double inverse = 1 / double.Parse(textBox_Result.Text);
textBox_Result.Text = inverse.ToString();
memory = inverse;
}
}
}
```
这是一个基本的科学计算器的C#代码,它可以执行加、减、乘、除、平方根、平方和倒数等操作。你可以根据自己的需求进行修改或扩展。