C#复杂计算器的实现
Posted 敲代码两年半的练习生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#复杂计算器的实现相关的知识,希望对你有一定的参考价值。
1 题目描述
复杂计算器的实现
2 源码详解
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Csharp9_1
{
public partial class MyCalculator : Form
{
double ShowResNumber = 0;
double InputNumber = 0;
bool flag = true;
string symbol;
public MyCalculator()
{
InitializeComponent();
}
private void MyCalculator_Load(object sender, EventArgs e)
{
}
private void Back_Click(object sender, EventArgs e)
{
NumberShow.Text = NumberShow.Text.Substring(0, NumberShow.Text.Length - 1);
}
private void CE_Click(object sender, EventArgs e)
{
NumberShow.Text = "";
}
private void Clear_Click(object sender, EventArgs e)
{
NumberShow.Text = "";
ShowResNumber = 0;
InputNumber = 0;
flag = true;
symbol = "";
}
private void Symbol_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "+-";
}
private void SignOfEvolution_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "sqrt";
}
private void Seven_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "7";
}
private void Eight_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "8";
}
private void Nine_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "9";
}
private void Divide_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "/";
}
private void Remainder_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "mod";
}
private void Four_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "4";
}
private void Five_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "5";
}
private void Six_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "6";
}
private void Multiply_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "*";
}
private void Reciprocal_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "1/x";
}
private void One_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "1";
}
private void Two_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "2";
}
private void Three_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "3";
}
private void Minus_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "-";
}
private void Zero_Click(object sender, EventArgs e)
{
if (flag == true)
{
NumberShow.Text = "";
flag = false;
}
NumberShow.Text += "0";
}
private void Point_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = ".";
}
private void Add_Click(object sender, EventArgs e)
{
flag = true;
InputNumber = double.Parse(NumberShow.Text);
symbol = "+";
}
private void Equal_Click(object sender, EventArgs e)
{
switch (symbol)
{
case "+":
ShowResNumber = InputNumber + double.Parse(NumberShow.Text);
break;
case "-":
ShowResNumber = InputNumber - double.Parse(NumberShow.Text);
break;
case "*":
ShowResNumber = InputNumber * double.Parse(NumberShow.Text);
break;
case "/":
if(InputNumber == 0 )
{
NumberShow.Text = "除数不能为0";
}
else
{
ShowResNumber = InputNumber / double.Parse(NumberShow.Text);
}
break;
case "sqrt":
if(InputNumber < 0 )
{
NumberShow.Text = "被开方数不能小于0";
}
else
{
ShowResNumber = Math.Sqrt(InputNumber);
}
break;
case "mod":
ShowResNumber = InputNumber % double.Parse(NumberShow.Text);
break;
case "1/x":
if (InputNumber == 0 )
{
NumberShow.Text = "除数不能为0";
}
else
{
ShowResNumber = 1 / InputNumber;
}
break;
case "+-":
ShowResNumber = -InputNumber;
break;
case ".":
ShowResNumber = double.Parse(InputNumber + "." + NumberShow.Text);
break;
}
NumberShow.Text = ShowResNumber + "";
flag = true;
}
}
}
3 实现效果
以上是关于C#复杂计算器的实现的主要内容,如果未能解决你的问题,请参考以下文章