C#制作稍微复杂一点的计算器
计算器项目需求
一、界面设计
1.做一个显示屏
2.17个button按钮(0-9,±×÷=%,CE)
二、需要实现的功能
1.输入第一个数字
2.选择运算符号
3.输入第二个数字
4.按下等号计算结果,结果显示在显示屏上
三、实现步骤
1.先做界面
a. 显示屏 textbox / listbox / label
b. 按钮 使用17个button,button上的文本改成对应的符号
2.
补充,申请两个int变量,第一个num1,装第一个数字,第二个num2,装第二个数字
a.输入第一个数字,当点击一个数字按钮时,屏幕上显示一个,之前显示的数字在前面呆着
a1.添加数字按钮的点击事件
a2.事件触发,将按钮代表的数字显示textbox1的text
b.当输入符号的时候,清除屏幕,但是后台必须记录好第一个数字
b1.添加符号按钮的点击事件
b2.点击任何一个符号按钮,用一个变量装刚才输入的textbox1中的数字
c.输入第二个数字
d.按下等号按钮,显示屏上的改变成两个数字的运算结果
(小技巧:将textbox中的属性TabStop设置为False,运行初光标不会显示,也就是失焦)
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 Form5 : Form
{
public Form5()
{
InitializeComponent();
}
//1
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
//2
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
//3
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
//4
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
//5
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
//6
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
//7
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
//8
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
//9
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
//0
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
//装第一个数字的变量盒子
int a;
//装第二个数字的变量盒子
int b;
//装运算符
string f = "";
//方法,一个代码模块(如何生成方法,选中代码,右击选择——》重构——》点击提取方法——》重命名——》生成)
private void JS()
{
a = Convert.ToInt32(textBox1.Text);
textBox1.Text = "";
}
//+
private void button11_Click(object sender, EventArgs e)
{
JS();
f = "+";
}
//-
private void button12_Click(object sender, EventArgs e)
{
JS();
f = "-";
}
//x
private void button13_Click(object sender, EventArgs e)
{
JS();
f = "x";
}
//÷
private void button14_Click(object sender, EventArgs e)
{
JS();
f = "÷";
}
//%
private void button15_Click(object sender, EventArgs e)
{
JS();
f = "%";
}
//CE清除
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
//=按等号时接收num2中的内容
private void button17_Click(object sender, EventArgs e)
{
//记录第二个数字
b =int.Parse(textBox1.Text);
textBox1.Text = "";
if (f=="+")
{
//这里是拼接字符串,最终按下等号显示结果,例如:1+3=4,这种形式
textBox1.Text =a+"+"+b+"="+ (a + b).ToString();
}
if (f == "-")
{
textBox1.Text = a + "-" + b + "=" + (a - b).ToString();
}
if (f == "x")
{
textBox1.Text = a + "x" + b + "=" + (a * b).ToString();
}
if (f == "÷")
{
textBox1.Text = a + "÷" + b + "=" + (a / b).ToString();
}
if (f == "%")
{
textBox1.Text = a + "%" + b + "=" + (a % b).ToString();
}
}
}
}