我在做个计算器,但我不知道怎么实现累加的功能!!!如:1+2+3=6 我要按等号,他才能得出结果.
我的计算器的代码如下...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 简单的计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double a = 0;
Double b = 0;
bool c = false;
string d;
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "1";
}
public void bainhao()
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "6";
}
private void button9_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "7";
}
private void button10_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "8";
}
private void button13_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "9";
}
private void button14_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "0";
if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf('.') < 0)
{
textBox1.Text += ".";
}
}
private void button7_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "+";
}
private void button8_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "-";
}
private void button11_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*";
}
private void button12_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "/";
}
private void button16_Click(object sender, EventArgs e)
{
switch (d)
{
case "+":
a = b + double.Parse(textBox1.Text);
break;
case "-":
a = b - double.Parse(textBox1.Text);
break;
case "*":
a = b * double.Parse(textBox1.Text);
break;
case "/":
a = b / double.Parse(textBox1.Text);
break;
case "leijia": for (double i = b; i <= double.Parse(textBox1.Text); i++)
{
a += i;
}
break;
}
textBox1.Text = a + "";
c = true; }
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button18_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "leijia";
}
}}
我的计算器的代码如下...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 简单的计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double a = 0;
Double b = 0;
bool c = false;
string d;
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "1";
}
public void bainhao()
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "6";
}
private void button9_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "7";
}
private void button10_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "8";
}
private void button13_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "9";
}
private void button14_Click(object sender, EventArgs e)
{
bainhao();
textBox1.Text += "0";
if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf('.') < 0)
{
textBox1.Text += ".";
}
}
private void button7_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "+";
}
private void button8_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "-";
}
private void button11_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*";
}
private void button12_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "/";
}
private void button16_Click(object sender, EventArgs e)
{
switch (d)
{
case "+":
a = b + double.Parse(textBox1.Text);
break;
case "-":
a = b - double.Parse(textBox1.Text);
break;
case "*":
a = b * double.Parse(textBox1.Text);
break;
case "/":
a = b / double.Parse(textBox1.Text);
break;
case "leijia": for (double i = b; i <= double.Parse(textBox1.Text); i++)
{
a += i;
}
break;
}
textBox1.Text = a + "";
c = true; }
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button18_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "leijia";
}
}}
给你个参考。
using System.Windows.Forms;
using System.Drawing;
public class win:Form { Button[] b = new Button[10];
Button bDot,bPlus,bSub,bMul,bDiv,bEqu,bClr;
Panel panCalc;
TextBox txtCalc;
Double dblAcc;
Double dblSec;
bool blnClear,blnFrstOpen;
String strOper;
public win() {
try {
this.Text="Calculator";
panCalc=new Panel();
txtCalc = new TextBox(); txtCalc.Location = new Point(10,10);
txtCalc.Size=new Size(150,10);
txtCalc.ReadOnly=true;
txtCalc.RightToLeft=RightToLeft.Yes;
panCalc.Size=new Size(200,200);
panCalc.BackColor=Color.Aqua;
panCalc.Controls.Add(txtCalc);
addButtons(panCalc);
this.Size=new Size(200,225);
this.Controls.Add(panCalc);
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
blnClear=false;
strOper=new String('=',1);
}
catch (Exception e) {
Console.WriteLine("error ...... " + e.StackTrace);
}
}
private void addButtons(Panel p)
{
for (int i=0;i<=9;i++)
{
b[i]=new Button();
b[i].Text=Convert.ToString(i);
b[i].Size=new Size(25,25);
b[i].BackColor=Color.White;
b[i].Click+=new EventHandler(btn_clk);
p.Controls.Add(b[i]);
}
b[0].Location=new Point(10,160);
b[1].Location=new Point(10,120);
b[4].Location=new Point(10,80);
b[7].Location=new Point(10,40);
b[2].Location=new Point(50,120);
b[5].Location=new Point(50,80);
b[8].Location=new Point(50,40);
b[3].Location=new Point(90,120);
b[6].Location=new Point(90,80);
b[9].Location=new Point(90,40);
bDot=new Button();
bDot.Size=new Size(25,25);
bDot.Location=new Point(50,160);
bDot.BackColor=Color.White;
bDot.Text=".";
bDot.Click+=new EventHandler(btn_clk);
bPlus=new Button();
bPlus.Size=new Size(25,25);
bPlus.Location=new Point(130,160);
bPlus.BackColor=Color.White;
bPlus.Text="+";
bPlus.Click+=new EventHandler(btn_Oper);
bSub=new Button();
bSub.Size=new Size(25,25);
bSub.Location=new Point(130,120);
bSub.BackColor=Color.White;
bSub.Text="-";
bSub.Click+=new EventHandler(btn_Oper);
bMul=new Button();
bMul.Size=new Size(25,25);
bMul.Location=new Point(130,80);
bMul.BackColor=Color.White;
bMul.Text="*";
bMul.Click+=new EventHandler(btn_Oper);
bDiv=new Button();
bDiv.Size=new Size(25,25);
bDiv.Location=new Point(130,40);
bDiv.BackColor=Color.White;
bDiv.Text="/";
bDiv.Click+=new EventHandler(btn_Oper);
bEqu=new Button();
bEqu.Size=new Size(25,25);
bEqu.Location=new Point(90,160);
bEqu.BackColor=Color.White;
bEqu.Text="=";
bEqu.Click+=new EventHandler(btn_equ);
bClr=new Button();
bClr.Size=new Size(20,45);
bClr.Location=new Point(170,40);
bClr.BackColor=Color.Orange;
bClr.Text="AC";
bClr.Click+=new EventHandler(btn_clr); p.Controls.Add(bDot);
p.Controls.Add(bPlus);
p.Controls.Add(bSub);
p.Controls.Add(bMul);
p.Controls.Add(bDiv);
p.Controls.Add(bEqu);
p.Controls.Add(bClr);
}
private void btn_clk(object obj,EventArgs ea) {
if(blnClear)
txtCalc.Text="";
Button b3=(Button)obj;
txtCalc.Text+=b3.Text;
if (txtCalc.Text==".")
txtCalc.Text="0.";
dblSec=Convert.ToDouble(txtCalc.Text);
blnClear=false;
}
private static void Main() {
Application.Run(new win());
}
private void btn_Oper(object obj,EventArgs ea)
{
Button tmp=(Button)obj;
strOper=tmp.Text;
if (blnFrstOpen)
dblAcc=dblSec;
else
calc(); blnFrstOpen=false;
blnClear=true;
} private void btn_clr(object obj,EventArgs ea) {
clear();
} private void btn_equ(object obj,EventArgs ea) {
calc();
}
private void calc() { switch(strOper) {
case "+":
dblAcc+=dblSec;
break;
case "-":
dblAcc-=dblSec;
break;
case "*":
dblAcc*=dblSec;
break;
case "/":
dblAcc/=dblSec;
break;
}
strOper="=";
blnFrstOpen=true;
txtCalc.Text=Convert.ToString(dblAcc);
dblSec=dblAcc;
}
private void clear() {
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
txtCalc.Text="";
txtCalc.Focus(); }
}