思路:1.俩个文本框textbox
2.设计n各button,包括各个数字和运算符
3.设计textbook和button代码,实现相应功能
实现:1.单击按钮将其对应内容显示在文本框中
2.单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
3.单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单 击的符号进行运算将结果显示在第二个文本框中
4.单击C时将两个文本框中的内容清空kesh
仅可实现清空,输入等,最后计算有误(但是知识没弄懂,懒得做了,以后有用再回来看)
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 WinFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//声明2个变量
string type;//符号类型
double x;//第一个数
bool c = false;
private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '1';
}
private void button10_Click(object sender, EventArgs e)
{
if(c == true);
type = "-";
textBox1.Text += "-";
x = double.Parse(textBox1.Text);
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Text = "";//c删除全部
}
private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '6';
}
private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '2';
}
private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '3';
}
private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '4';
}
private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '5';
}
private void button1_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '7';
}
private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '8';
}
private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '9';
}
private void button12_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += '0';
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();
this.Text = "计算器";
textBox1.TabIndex = 0;//
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button15_Click(object sender, EventArgs e)
{
c = true;
type = "/";
textBox1.Text += "/";
x = double.Parse(textBox1.Text);
}
private void button16_Click(object sender, EventArgs e)
{
c = true;
type = "*";
textBox1.Text += "*";
x = double.Parse(textBox1.Text);
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
private void button11_Click(object sender, EventArgs e)
{
if(c==true)
type = "+";
textBox1.Text += "+";
x = double.Parse(textBox1 .Text);
}
//编写=
private void button17_Click(object sender, EventArgs e)
{
if (type == "+")
{
textBox1.Text = (x + textBox1.Text).ToString();
}
}
}
}