设计简单的计算器,实现两个数的加、减、乘、除、求幂等计算。

(1)新建Windows应用程序MyApplicaton3,添加控件,其中文本框name属性分别为:(txtnum1、xtnum2)。
设计简单的计算器,实现两个数的加、减、乘、除、求幂等计算。_第1张图片
(2)5个按钮事件的代码为:
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 MyApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double num1, num2;
Double result;
//加法
private void button1_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(txtnum1.Text);
num2 = Convert.ToDouble(txtnum2.Text);
result = num1 + num2;
label3.Text = label3.Text + “\n” + result.ToString();
}
//减法
private void button2_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(txtnum1.Text);
num2 = Convert.ToDouble(txtnum2.Text);
result = num1 - num2;
label3.Text = label3.Text + “\n” + result.ToString();
}
//乘法
private void button3_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(txtnum1.Text);
num2 = Convert.ToDouble(txtnum2.Text);
result = num1 * num2;
label3.Text = label3.Text + “\n” + result.ToString();
}
//除法
private void button4_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(txtnum1.Text);
num2 = Convert.ToDouble(txtnum2.Text);
result = num1 / num2;
label3.Text = label3.Text + “\n” + result.ToString();
}
//求幂
private void button5_Click(object sender, EventArgs e)
{
num1 = Convert.ToDouble(txtnum1.Text);
num2 = Convert.ToDouble(txtnum2.Text);
result = Math.Pow(num1, num2);
label3.Text = label3.Text + “\n” + result.ToString();
}
}
}

(3)运行结果:
设计简单的计算器,实现两个数的加、减、乘、除、求幂等计算。_第2张图片

你可能感兴趣的:(C#——vs2010,c#)