| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1022 人关注过本帖
标题:[求助]怎么办?
只看楼主 加入收藏
风霜
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:7
帖 子:242
专家分:0
注 册:2005-3-4
收藏
 问题点数:0 回复次数:2 
[求助]怎么办?
怎么样才能用C#编一个计算器程序?
搜索更多相关主题的帖子: 计算器 
2005-03-04 17:11
volte
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:69
帖 子:1167
专家分:1316
注 册:2004-12-19
收藏
得分:0 
这个你加上控件后。
再添加鼠标响应程序。
最后在程序中添加函数处理,例如加,减,乘,除等等。

大家都是朋友,有空就来坐坐!
2005-03-04 20:26
eastsnake
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2005-3-8
收藏
得分:0 

源代码如下,自己参考 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace calculator { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btn_clear; private System.Windows.Forms.Button button0; 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.TextBox textBox1; private System.Windows.Forms.Button btn_dot; private System.Windows.Forms.Button btn_equ; private System.Windows.Forms.Button btn_plus; private System.Windows.Forms.Button btn_minus; private System.Windows.Forms.Button btn_mul; private System.Windows.Forms.Button btn_div; private System.Windows.Forms.Button btn_log; private System.Windows.Forms.Button btn_exp; private System.Windows.Forms.Button btn_sqrt; private System.Windows.Forms.Button btn_abs;

//定义两个布尔变量 private bool blnclear; private bool blnfirstopen; private double dblfirst; private double dblsecond; private string stroper; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;

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

// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }

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

#region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btn_clear = new System.Windows.Forms.Button(); this.button0 = new System.Windows.Forms.Button(); 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.textBox1 = new System.Windows.Forms.TextBox(); this.btn_dot = new System.Windows.Forms.Button(); this.btn_equ = new System.Windows.Forms.Button(); this.btn_plus = new System.Windows.Forms.Button(); this.btn_minus = new System.Windows.Forms.Button(); this.btn_mul = new System.Windows.Forms.Button(); this.btn_div = new System.Windows.Forms.Button(); this.btn_log = new System.Windows.Forms.Button(); this.btn_exp = new System.Windows.Forms.Button(); this.btn_sqrt = new System.Windows.Forms.Button(); this.btn_abs = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btn_clear // this.btn_clear.Location = new System.Drawing.Point(16, 32); this.btn_clear.Name = "btn_clear"; this.btn_clear.Size = new System.Drawing.Size(40, 23); this.btn_clear.TabIndex = 0; this.btn_clear.Text = "C"; this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click); // // button0 // this.button0.Location = new System.Drawing.Point(16, 240); this.button0.Name = "button0"; this.button0.Size = new System.Drawing.Size(40, 23); this.button0.TabIndex = 1; this.button0.Text = "0"; this.button0.Click += new System.EventHandler(this.button_Click); // // button1 // this.button1.Location = new System.Drawing.Point(16, 188); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(40, 23); this.button1.TabIndex = 2; this.button1.Text = "1"; this.button1.Click += new System.EventHandler(this.button_Click); // // button2 // this.button2.Location = new System.Drawing.Point(98, 188); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(40, 23); this.button2.TabIndex = 3; this.button2.Text = "2"; this.button2.Click += new System.EventHandler(this.button_Click); // // button3 // this.button3.Location = new System.Drawing.Point(180, 188); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(40, 23); this.button3.TabIndex = 4; this.button3.Text = "3"; this.button3.Click += new System.EventHandler(this.button_Click); // // button4 // this.button4.Location = new System.Drawing.Point(16, 136); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(40, 23); this.button4.TabIndex = 5; this.button4.Text = "4"; this.button4.Click += new System.EventHandler(this.button_Click); // // button5 // this.button5.Location = new System.Drawing.Point(98, 136); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(40, 23); this.button5.TabIndex = 6; this.button5.Text = "5"; this.button5.Click += new System.EventHandler(this.button_Click); // // button6 // this.button6.Location = new System.Drawing.Point(180, 136); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(40, 23); this.button6.TabIndex = 7; this.button6.Text = "6"; this.button6.Click += new System.EventHandler(this.button_Click); // // button7 // this.button7.Location = new System.Drawing.Point(16, 84); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(40, 23); this.button7.TabIndex = 8; this.button7.Text = "7"; this.button7.Click += new System.EventHandler(this.button_Click); // // button8 // this.button8.Location = new System.Drawing.Point(98, 84); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(40, 23); this.button8.TabIndex = 9; this.button8.Text = "8"; this.button8.Click += new System.EventHandler(this.button_Click); // // button9 // this.button9.Location = new System.Drawing.Point(180, 84); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(40, 23); this.button9.TabIndex = 10; this.button9.Text = "9"; this.button9.Click += new System.EventHandler(this.button_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(88, 32); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(256, 21); this.textBox1.TabIndex = 11; this.textBox1.Text = ""; // // btn_dot // this.btn_dot.Location = new System.Drawing.Point(98, 240); this.btn_dot.Name = "btn_dot"; this.btn_dot.Size = new System.Drawing.Size(40, 23); this.btn_dot.TabIndex = 12; this.btn_dot.Text = "."; this.btn_dot.Click += new System.EventHandler(this.btn_dot_Click); // // btn_equ // this.btn_equ.Location = new System.Drawing.Point(180, 240); this.btn_equ.Name = "btn_equ"; this.btn_equ.Size = new System.Drawing.Size(40, 23); this.btn_equ.TabIndex = 13; this.btn_equ.Text = "="; this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click); // // btn_plus // this.btn_plus.Location = new System.Drawing.Point(262, 240); this.btn_plus.Name = "btn_plus"; this.btn_plus.Size = new System.Drawing.Size(40, 23); this.btn_plus.TabIndex = 14; this.btn_plus.Text = "+"; this.btn_plus.Click += new System.EventHandler(this.btn_plus_Click); // // btn_minus // this.btn_minus.Location = new System.Drawing.Point(262, 188); this.btn_minus.Name = "btn_minus"; this.btn_minus.Size = new System.Drawing.Size(40, 23); this.btn_minus.TabIndex = 15; this.btn_minus.Text = "-"; this.btn_minus.Click += new System.EventHandler(this.btn_plus_Click); // // btn_mul // this.btn_mul.Location = new System.Drawing.Point(262, 136); this.btn_mul.Name = "btn_mul"; this.btn_mul.Size = new System.Drawing.Size(40, 23); this.btn_mul.TabIndex = 16; this.btn_mul.Text = "*"; this.btn_mul.Click += new System.EventHandler(this.btn_plus_Click); // // btn_div // this.btn_div.Location = new System.Drawing.Point(262, 84); this.btn_div.Name = "btn_div"; this.btn_div.Size = new System.Drawing.Size(40, 23); this.btn_div.TabIndex = 17; this.btn_div.Text = "/"; this.btn_div.Click += new System.EventHandler(this.btn_plus_Click); // // btn_log // this.btn_log.Location = new System.Drawing.Point(344, 84); this.btn_log.Name = "btn_log"; this.btn_log.Size = new System.Drawing.Size(40, 23); this.btn_log.TabIndex = 18; this.btn_log.Text = "log"; // // btn_exp // this.btn_exp.Location = new System.Drawing.Point(344, 136); this.btn_exp.Name = "btn_exp"; this.btn_exp.Size = new System.Drawing.Size(40, 23); this.btn_exp.TabIndex = 19; this.btn_exp.Text = "exp"; // // btn_sqrt // this.btn_sqrt.Location = new System.Drawing.Point(344, 188); this.btn_sqrt.Name = "btn_sqrt"; this.btn_sqrt.Size = new System.Drawing.Size(40, 23); this.btn_sqrt.TabIndex = 20; this.btn_sqrt.Text = "sqrt"; // // btn_abs // this.btn_abs.Location = new System.Drawing.Point(344, 240); this.btn_abs.Name = "btn_abs"; this.btn_abs.Size = new System.Drawing.Size(40, 23); this.btn_abs.TabIndex = 21; this.btn_abs.Text = "abs"; this.btn_abs.Click += new System.EventHandler(this.btn_abs_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(400, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.btn_abs, this.btn_sqrt, this.btn_exp, this.btn_log, this.btn_div, this.btn_mul, this.btn_minus, this.btn_plus, this.btn_equ, this.btn_dot, this.textBox1, this.button9, this.button8, this.button7, this.button6, this.button5, this.button4, this.button3, this.button2, this.button1, this.button0, this.btn_clear}); this.Name = "Form1"; this.Text = "计算器"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false);

} #endregion

/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void append_num(int i) { if(blnclear) { if(blnfirstopen) { if(textBox1.Text=="") dblfirst=0; else dblfirst=Convert.ToDouble(textBox1.Text); } else dblsecond=Convert.ToDouble(textBox1.Text); textBox1.Text=""; blnclear=false;

} if(((i==0)&&(textBox1.Text==""))||(i!=0)) textBox1.Text=textBox1.Text+i.ToString(); }

private void Form1_Load(object sender, System.EventArgs e) { blnclear=true; blnfirstopen=true; }

private void button_Click(object sender, System.EventArgs e) { if(sender==button0) this.append_num(0); if(sender==button1) this.append_num(1); if(sender==button2) this.append_num(2); if(sender==button3) this.append_num(3); if(sender==button4) this.append_num(4); if(sender==button5) this.append_num(5); if(sender==button6) this.append_num(6); if(sender==button7) this.append_num(7); if(sender==button8) this.append_num(8); if(sender==button9) this.append_num(9); }

private void btn_clear_Click(object sender, System.EventArgs e) { dblfirst=0; dblsecond=0; blnfirstopen=true; blnclear=true; textBox1.Text=""; textBox1.Focus(); }

private void btn_dot_Click(object sender, System.EventArgs e) { if(textBox1.Text=="") textBox1.Text="0."; else textBox1.Text+="."; blnclear=false; }

private void btn_equ_Click(object sender, System.EventArgs e) { if(textBox1.Text!="") if(blnfirstopen==true) dblfirst=Convert.ToDouble(textBox1.Text); else dblsecond=Convert.ToDouble(textBox1.Text);

switch(stroper) { case"+": dblfirst+=dblsecond; break; case"-": dblfirst-=dblsecond; break; case "*": dblfirst*=dblsecond; break; case "/": dblfirst/=dblsecond; break; } stroper="="; blnfirstopen=false; textBox1.Text=Convert.ToString(dblfirst); blnclear=true; }

private void btn_plus_Click(object sender, System.EventArgs e) {

if(textBox1.Text!="") if(blnfirstopen==true) dblfirst=Convert.ToDouble(textBox1.Text); else dblsecond=Convert.ToDouble(textBox1.Text);

switch(stroper) { case"+": dblfirst+=dblsecond; break; case"-": dblfirst-=dblsecond; break; case "*": dblfirst*=dblsecond; break; case "/": dblfirst/=dblsecond; break; }

if(sender==btn_plus) stroper="+"; if(sender==btn_minus) stroper="-"; if(sender==btn_mul) stroper="*"; if(sender==btn_div) stroper="/"; textBox1.Text=Convert.ToString(dblfirst); blnfirstopen=false; blnclear=true; }

private void btn_abs_Click(object sender, System.EventArgs e) { } } }


程序员是男孩,语言是女孩; 每个男孩都希望能交往更多的女孩; 但是却没有一个男孩真正了解一个女孩; 男孩总是不能专心一个女孩,而女孩却总是在变~
2005-03-08 15:25
快速回复:[求助]怎么办?
数据加载中...
 
   



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

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