翻出 初学C#时候写的一个计算器:初学者可以看看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string Op1, Op2;
bool HasDecimal;
int NumCount;
string LastInput;
int OpFlag;
String tempReadout;
private void Form1_Load(object sender, EventArgs e)
{
HasDecimal = false;
NumCount = 0;
LastInput = "NONE";
OpFlag = 0;
textBox1.Text = "0.";
}
public void InputNum(int num)
{
if (LastInput != "NUMS")
{
textBox1.Text = "0";
HasDecimal = false;
}
if (HasDecimal)
{
textBox1.Text =textBox1.Text + num;
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = num+"." ;
}
else
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + num+".";
}
}
if (LastInput == "NEG")
{
textBox1.Text ="-"+textBox1.Text;
}
LastInput = "NUMS";
}
private void Decimal_Click(object sender, EventArgs e)
{
if (LastInput == "NEG")
{
textBox1.Text = "-0.";
}
else if (LastInput != "NUMS")
{
textBox1.Text = "0.";
}
HasDecimal = true;
LastInput = "NUMS";
}
private void button1_Click(object sender, EventArgs e)
{
InputNum(1);
}
private void button2_Click(object sender, EventArgs e)
{
InputNum(2);
}
private void button3_Click(object sender, EventArgs e)
{
InputNum(3);
}
private void button4_Click(object sender, EventArgs e)
{
InputNum(4);
}
private void button5_Click(object sender, EventArgs e)
{
InputNum(5);
}
private void button6_Click(object sender, EventArgs e)
{
InputNum(6);
}
private void button7_Click(object sender, EventArgs e)
{
InputNum(7);
}
private void button8_Click(object sender, EventArgs e)
{
InputNum(8);
}
private void button9_Click(object sender, EventArgs e)
{
InputNum(9);
}
private void button0_Click(object sender, EventArgs e)
{
InputNum(0);
}
private void Operator(int op)
{
tempReadout = textBox1.Text;
if (LastInput == "NUMS")
{
NumCount += 1;
}
string Myop;
Myop = NumCount.ToString();
switch (NumCount)
{
case 0:
if (op == 2 && LastInput != "NEG")
{
textBox1.Text = "-" + textBox1.Text+".";
LastInput = "NEG";
}
break;
case 1:
Op1 = textBox1.Text;
if (op == 2 && LastInput != "NUMS"&&OpFlag !=5)
{
textBox1.Text = "-" ;
LastInput = "NEG";
}
break;
case 2:
Op2=tempReadout ;
switch (OpFlag )
{
case 1:
Op1=((Double.Parse (Op1)+Double.Parse (Op2 )).ToString ());
break;
case 2:
Op1=((Double.Parse (Op1)-Double.Parse (Op2 )).ToString ());
break;
case 3:
Op1=((Double.Parse (Op1)*Double.Parse (Op2 )).ToString ());
break;
case 4:
if (Double.Parse(Op2) != 0)
Op1=((Double.Parse (Op1)/Double.Parse (Op2 )).ToString ());
break;
case 5:
Op1 = Op2;
break;
}
textBox1.Text = Op1;
NumCount = 1;
break;
}
if(LastInput !="NGS")
{
LastInput = "OPS";
OpFlag = op;
}
}
private void button13_Click(object sender, EventArgs e)
{
Operator(1);
}
private void button14_Click(object sender, EventArgs e)
{
Operator(2);
}
private void button15_Click(object sender, EventArgs e)
{
Operator(3);
}
private void button16_Click(object sender, EventArgs e)
{
Operator(4);
}
private void button17_Click(object sender, EventArgs e)
{
Operator(5);
}
private void buttonCLS_Click(object sender, EventArgs e)
{
textBox1.Text = "0.";
Op1 = "";
Op2 = "";
Form1_Load(this,new EventArgs ());
}
private void buttonBack_Click(object sender, EventArgs e)
{
textBox1.Text = "0.";
HasDecimal = false;
LastInput = "CE";
}
private void button10_Click(object sender, EventArgs e)
{
if (LastInput == "NUMS"&&textBox1.Text != "" && textBox1.Text != "0." && textBox1.Text != string.Empty && textBox1.Text != null && textBox1.Text.Length >1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 2) + ".";
if (textBox1.Text.Length == 1)
{
textBox1.Text = "0.";
}
else
{
}
}
else
{
return;
}
LastInput = "NUMS";
}
}
}