| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1343 人关注过本帖
标题:[求助]一个简单计算器的问题!
只看楼主 加入收藏
qingxisong
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-2-16
收藏
 问题点数:0 回复次数:4 
[求助]一个简单计算器的问题!

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();
}
//public Single res;//存储结果
public string op;//存储操作符
public Single num1;//存储前数字
public Single num2;//存储操作符后数字
private Single res;
private void btn数字1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (textBox1.Text != "0")
{
textBox1.Text += btn.Text;
}
else
{
textBox1.Text = "";
textBox1.Text += btn.Text;
}

}
。。。。。
数字0;数字2-数字9;
。。。。。
private void btn清零_Click(object sender, EventArgs e)
{
textBox1.Text = "";
res = 0;
num1 = 0;
}

public void btn加法_Click(object sender, EventArgs e)
{
num1=Convert.ToSingle(textBox1.Text);
Button btn = (Button)sender;
op = btn.Text;
res += num1;
num1 = 0;
textBox1.Text = Convert.ToString(num1);
}

private void btn等于_Click(object sender, EventArgs e)
{
// num1 = Convert.ToSingle(n);
num2 = Convert.ToSingle(textBox1.Text);
switch (op)
{
case"+":
{
res += num2;
break;
}
case "-":
{
this.res -= num2;
break;
}
case "*":
{
res *= num2;
break;
}
case "/":
{
res/= num2;
break;
}
}
textBox1.Text = Convert.ToString(res);
}

private void btn减法_Click(object sender, EventArgs e)
{
num1 = Convert.ToSingle(textBox1.Text);
Button btn = (Button)sender;
op = btn.Text;
res -= num1;
num1 = 0;
textBox1.Text = Convert.ToString(num1);
}

private void btn乘法_Click(object sender, EventArgs e)
{

num1 = Convert.ToSingle(textBox1.Text);
Button btn = (Button)sender;
op = btn.Text;
res *= num1;
num1 = 0;
textBox1.Text = Convert.ToString(num1);
}

private void btn除法_Click(object sender, EventArgs e)
{
num1 = Convert.ToSingle(textBox1.Text);
Button btn = (Button)sender;
op = btn.Text;
if (num1 != 0)
res /= num1;
else
return;
num1 = 0;
textBox1.Text = Convert.ToString(num1);
}
}
}

问题在于:不能进行混合运算(0-9);
请高手们帮小弟解决一下 啊,万分感激!

搜索更多相关主题的帖子: 计算器 using System public Single 
2006-03-18 18:51
dazhi_
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2006-3-10
收藏
得分:0 
能不能把原码传上来,看的晕的,没个介面,代码也不全,意思也没看明白

2006-03-19 11:54
dazhi_
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2006-3-10
收藏
得分:0 
是用文件压缩完传上来,大家在帮你看。

2006-03-19 11:55
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

我写的计算器,你可以参考一下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 计算器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Calculate : System.Windows.Forms.Form
{
//记录第一个操作数
protected double dNum;
//记录操作符
protected char cOperators;
//是否为第一次输入
protected bool bNumBegins;
//点击小数点后几位
protected long iDone;
//是否点击了小数点
protected bool bDone;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button0;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button subButton;
private System.Windows.Forms.Button mulButton;
private System.Windows.Forms.Button divButton;
private System.Windows.Forms.Button conButton;
private System.Windows.Forms.Button doneButton;
private System.Windows.Forms.Button clearButton;
private System.Windows.Forms.Label operatorLabel;
private System.Windows.Forms.Label txtOutput;
private System.Windows.Forms.Button asButton;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Calculate()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//以下非系统自动生成代码,用于加入事件委托
EventHandler eh=new EventHandler(this.Number_Click);
KeyEventHandler eh1=new KeyEventHandler(this.allKeys_Down);
MouseEventHandler eh2=new MouseEventHandler(this.allMouse_Up);
//数字按钮0——9的事件委托
button0.Click+=eh;
button0.MouseUp+=eh2;
button1.Click+=eh;
button1.MouseUp+=eh2;
button2.Click+=eh;
button2.MouseUp+=eh2;
button3.Click+=eh;
button3.MouseUp+=eh2;
button4.Click+=eh;
button4.MouseUp+=eh2;
button5.Click+=eh;
button5.MouseUp+=eh2;
button6.Click+=eh;
button6.MouseUp+=eh2;
button7.Click+=eh;
button7.MouseUp+=eh2;
button8.Click+=eh;
button8.MouseUp+=eh2;
button9.Click+=eh;
button9.MouseUp+=eh2;

eh=new EventHandler(this.Operators_Click);
//符号按钮(+、-、*、/、.、C、=)的事件委托
addButton.Click+=eh;
addButton.MouseUp+=eh2;
subButton.Click+=eh;
subButton.MouseUp+=eh2;
mulButton.Click+=eh;
mulButton.MouseUp+=eh2;
divButton.Click+=eh;
divButton.MouseUp+=eh2;
asButton.MouseUp+=eh2;
clearButton.MouseUp+=eh2;
conButton.Click+=eh;
conButton.KeyDown+=eh1;
conButton.MouseUp+=eh2;
doneButton.MouseUp+=eh2;

InitNumber(); //初始化变量及控件
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-03-20 11:52
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button0 = new System.Windows.Forms.Button();
this.addButton = new System.Windows.Forms.Button();
this.subButton = new System.Windows.Forms.Button();
this.mulButton = new System.Windows.Forms.Button();
this.divButton = new System.Windows.Forms.Button();
this.conButton = new System.Windows.Forms.Button();
this.doneButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.operatorLabel = new System.Windows.Forms.Label();
this.txtOutput = new System.Windows.Forms.Label();
this.asButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 24);
this.button1.TabIndex = 1;
this.button1.Text = "1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 24);
this.button2.TabIndex = 2;
this.button2.Text = "2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(104, 80);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(32, 24);
this.button3.TabIndex = 3;
this.button3.Text = "3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(24, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(32, 24);
this.button4.TabIndex = 4;
this.button4.Text = "4";
//
// button5
//
this.button5.Location = new System.Drawing.Point(64, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 24);
this.button5.TabIndex = 5;
this.button5.Text = "5";
//
// button6
//
this.button6.Location = new System.Drawing.Point(104, 112);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 24);
this.button6.TabIndex = 6;
this.button6.Text = "6";
//
// button7
//
this.button7.Location = new System.Drawing.Point(24, 144);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 24);
this.button7.TabIndex = 7;
this.button7.Text = "7";
//
// button8
//
this.button8.Location = new System.Drawing.Point(64, 144);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 24);
this.button8.TabIndex = 8;
this.button8.Text = "8";
//
// button9
//
this.button9.Location = new System.Drawing.Point(104, 144);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 24);
this.button9.TabIndex = 9;
this.button9.Text = "9";
//
// button0
//
this.button0.Location = new System.Drawing.Point(24, 176);
this.button0.Name = "button0";
this.button0.Size = new System.Drawing.Size(32, 23);
this.button0.TabIndex = 10;
this.button0.Text = "0";
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(144, 80);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(48, 24);
this.addButton.TabIndex = 11;
this.addButton.Text = "+";
//
// subButton
//
this.subButton.Location = new System.Drawing.Point(144, 112);
this.subButton.Name = "subButton";
this.subButton.Size = new System.Drawing.Size(48, 24);
this.subButton.TabIndex = 12;
this.subButton.Text = "-";
//
// mulButton
//
this.mulButton.Location = new System.Drawing.Point(144, 144);
this.mulButton.Name = "mulButton";
this.mulButton.Size = new System.Drawing.Size(48, 24);
this.mulButton.TabIndex = 13;
this.mulButton.Text = "*";
//
// divButton
//
this.divButton.Location = new System.Drawing.Point(144, 176);
this.divButton.Name = "divButton";
this.divButton.Size = new System.Drawing.Size(48, 24);
this.divButton.TabIndex = 14;
this.divButton.Text = "/";
//
// conButton
//
this.conButton.Location = new System.Drawing.Point(104, 176);
this.conButton.Name = "conButton";
this.conButton.Size = new System.Drawing.Size(32, 24);
this.conButton.TabIndex = 1;
this.conButton.Text = "=";
//
// doneButton
//
this.doneButton.Location = new System.Drawing.Point(64, 176);
this.doneButton.Name = "doneButton";
this.doneButton.Size = new System.Drawing.Size(32, 24);
this.doneButton.TabIndex = 16;
this.doneButton.Text = ".";
this.doneButton.Click += new System.EventHandler(this.doneButton_Click);
//
// clearButton
//
this.clearButton.ForeColor = System.Drawing.Color.Red;
this.clearButton.Location = new System.Drawing.Point(24, 208);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(112, 23);
this.clearButton.TabIndex = 17;
this.clearButton.Text = "C";
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
//
// operatorLabel
//
this.operatorLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.operatorLabel.Location = new System.Drawing.Point(24, 48);
this.operatorLabel.Name = "operatorLabel";
this.operatorLabel.Size = new System.Drawing.Size(32, 23);
this.operatorLabel.TabIndex = 18;
this.operatorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtOutput
//
this.txtOutput.BackColor = System.Drawing.Color.White;
this.txtOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtOutput.Location = new System.Drawing.Point(24, 16);
this.txtOutput.Name = "txtOutput";
this.txtOutput.Size = new System.Drawing.Size(168, 23);
this.txtOutput.TabIndex = 19;
this.txtOutput.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// asButton
//
this.asButton.Location = new System.Drawing.Point(144, 208);
this.asButton.Name = "asButton";
this.asButton.Size = new System.Drawing.Size(48, 23);
this.asButton.TabIndex = 20;
this.asButton.Text = "+/-";
this.asButton.Click += new System.EventHandler(this.asButton_Click);
//
// Calculate
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(216, 245);
this.Controls.Add(this.asButton);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.operatorLabel);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.doneButton);
this.Controls.Add(this.conButton);
this.Controls.Add(this.divButton);
this.Controls.Add(this.mulButton);
this.Controls.Add(this.subButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.button0);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Calculate";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Calculate());
}
//数字按钮事件方法
private void Number_Click(Object sender,EventArgs e)
{
double iCur,i; //用于记录文本框中的数字及用户点击的按钮(事件源)数字
if(txtOutput.Text=="Error")
txtOutput.Text="0";
if(bDone) //如果用户点击了小数点
iDone++; //记录小数点后的位数
try
{
iCur=double.Parse(txtOutput.Text); //得到文本框中的数字
i=double.Parse(((Button)sender).Text); //得到按钮(事件源)的标签
//如果是第一次点击数字
if(bNumBegins)
{
iCur=i; //存储用户点击的按钮(事件源)的标签
bNumBegins=false;
}
else
{
if(iCur>=0)
{
//如果小数点后位数为0,说明用户输入的是整数
if(iDone==0)
checked{iCur=(iCur*10)+i;}
else
{
long iC=1;
for(long u=1;u<=iDone;u++)
iC*=10; //得到小数点后的位数的倍数,即有几位则除几个10
checked{iCur=iCur+i/iC;}
}
}
else
{
InitNumber();
bNumBegins=false;
txtOutput.Text=i.ToString();
return;
}
}
txtOutput.Text=iCur.ToString();
}
catch{}
}
private void allMouse_Up(Object sender,MouseEventArgs e)
{
conButton.Focus();
}
//所有按钮的键盘事件
private void allKeys_Down(Object sender,KeyEventArgs e)
{
switch(e.KeyValue)
{
case 96:
Number_Click(button0,e);
break;
case 97:
Number_Click(button1,e);
break;
case 98:
Number_Click(button2,e);
break;
case 99:
Number_Click(button3,e);
break;
case 100:
Number_Click(button4,e);
break;
case 101:
Number_Click(button5,e);
break;
case 102:
Number_Click(button6,e);
break;
case 103:
Number_Click(button7,e);
break;
case 104:
Number_Click(button8,e);
break;
case 105:
Number_Click(button9,e);
break;
case 107:
Operators_Click(addButton,e);
break;
case 109:
Operators_Click(subButton,e);
break;
case 106:
Operators_Click(mulButton,e);
break;
case 111:
Operators_Click(divButton,e);
break;
case 110:
doneButton_Click(doneButton,e);
break;
case 13:
Operators_Click(conButton,e);
break;
case 67:
clearButton_Click(clearButton,e);
break;
}
}
//符号按钮的事件方法
private void Operators_Click(Object sender,EventArgs e)
{
double iCur,iResult; //得到文本框中的数字及计算结果
char op=((Button)sender).Text[0]; //得到按钮(事件源)的标签,由于标签为字符串,所以要得到该字符串的第一个字符Text[0]
try
{
iCur=double.Parse(txtOutput.Text);
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
try
{
//遍历符号
switch(cOperators)
{
case '+':
checked{iResult=dNum+iCur;}
break;
case '-':
checked{iResult=dNum-iCur;}
break;
case '*':
checked{iResult=dNum*iCur;}
break;
case '/':
checked{iResult=dNum/iCur;}
break;
//以上都不是,所以肯定是等于号
default:
iResult=iCur;
break;
}
txtOutput.Text=iResult.ToString();
dNum=iResult;
cOperators=op;
bNumBegins=true;
bDone=false;
iDone=0;
operatorLabel.Text=cOperators.ToString();
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
}
//初始化变量及控件
private void InitNumber()
{
dNum=0;
txtOutput.Text="0";
bNumBegins=true;
cOperators='=';
iDone=0;
bDone=false;
operatorLabel.Text="";
}
//小数点按钮的事件
private void doneButton_Click(object sender, System.EventArgs e)
{
//看文本框中的数字是否已有小数点,如果没有则执行
if(txtOutput.Text.IndexOf(".")==-1)
{
txtOutput.Text+=".";
bDone=true; //已点击小数点
bNumBegins=false; //第一个数设置为非
}
}
//清除按钮事件方法
private void clearButton_Click(object sender, System.EventArgs e)
{
InitNumber(); //回到初始状态
}

private void asButton_Click(object sender, System.EventArgs e)
{
try
{
double d=double.Parse(txtOutput.Text);
if(d!=0)
{
d=d*(-1);
}
txtOutput.Text=d.ToString();
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
}

}
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-03-20 11:52
快速回复:[求助]一个简单计算器的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038119 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved