计算器需求分析
1.界面分析
做一个显示屏
17个按钮(0~9 +-*/% =ce)
2.需要实现的功能
选择第一个数字
选择运算符
选择第二个数字
按下等号进行计算,结果显示在显示屏中
3.实现步骤
1)先做界面
a.显示屏:textbox,listbox,label
b.使用17个button ,button上的文本对应数字符号
2)
补充:申请两个int类型变量,第一个num1装第一个数字, 第二个num2装第二个数字,再申请一个变量string用来装
运算符
a.输入第一个数字,点一个数字按钮,屏幕上显示一个,之前显示的数字在前面待着
a1. 添加按钮的click事件
a2,事件触发,将按钮代表的数字显示在textbox中
b.当输入符号的时候,清除屏幕,但是后台必须记录第一个数字
b1. 添加符号按钮的click事件
b2. 当点击任何一个符号按钮用一个变量装刚才的textbox中的数字
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 _1228计算器_复杂版_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//1
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text+"1";
// textBox1.Text = button1.Text;
}
//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 num1;
//这是装第二个数字的变量盒子
int num2;
//这个盒子装运算符号
string fun;
//+
private void button11_Click(object sender, EventArgs e)
{
jisuan();
fun = "+";
}
//提取方法,方法是一个代码模块,(右击,选择快速操作和重构,点击“提取方法”,自由取名,调用该方法)
private void jisuan()
{
num1 = Convert.ToInt32(textBox1.Text);
textBox1.Text = "";
}
//-
private void button12_Click(object sender, EventArgs e)
{
jisuan();
fun = "-";
}
//*
private void button13_Click(object sender, EventArgs e)
{
jisuan();
fun = "*";
}
//÷
private void button14_Click(object sender, EventArgs e)
{
jisuan();
fun = "/";
}
//%
private void button15_Click(object sender, EventArgs e)
{
jisuan();
fun = "%";
}
//=
private void button16_Click(object sender, EventArgs e)
{
num2 = int.Parse(textBox1.Text);
textBox1.Text = "";
if (fun=="+")
{
textBox1.Text = num1+"+"+ num2+"="+(num1 + num2).ToString();
}
if (fun=="-")
{
textBox1.Text = num1 + "-" + num2 + "=" + (num1 - num2).ToString();
}
if (fun=="*")
{
textBox1.Text = num1 + "×" + num2 + "=" + (num1 * num2).ToString();
}
if (fun=="/")
{
textBox1.Text = num1 + "÷" + num2 + "=" + (num1 / num2).ToString();
}
if (fun=="%")
{
textBox1.Text = num1 + "%" + num2 + "=" + (num1 % num2).ToString();
}
}
//ce
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}