简易计算器--计应192西--第六组--辛海博
1先设计界面(按钮、文本框(一个显示算式,一个显示结果))布局
2单击按钮将其对应内容显示在文本框中
3单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
4单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单击的符号进行运算将结果显示在第一个文本框中
5单击C时将两个文本框中的内容清空
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 Test_Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//声明三个变量
string type; //符号类型
double x;//装第一个数(按符号(+-×÷%)时textbox1中的数字)
double y;//装第二个数(按等号时textbox1中的数字)
bool c=false;
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();//窗体居中显示
this.Text = "计算器";
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
textBox1.ReadOnly = true;//文本框只读
textBox2.TabIndex = 0;//光标焦点在textbox2中
}
private void button1_Click(object sender, EventArgs e)
{
if (c==true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "1";
textBox2.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "2";
textBox2.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "3";
textBox2.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "4";
textBox2.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "5";
textBox2.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "6";
textBox2.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "7";
textBox2.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "8";
textBox2.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "9";
textBox2.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "0";
textBox2.Text += "0";
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
textBox2.Text += ".";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
private void button13_Click(object sender, EventArgs e)
{
c = true;
type = "+";
textBox2.Text += "+";
x = double.Parse(textBox1.Text);
}
private void button14_Click(object sender, EventArgs e)
{
c = true;
type = "-";
textBox2.Text += "-";
x = double.Parse(textBox1.Text);
}
private void button15_Click(object sender, EventArgs e)
{
c = true;
type = "×";
textBox2.Text += "×";
x = double.Parse(textBox1.Text);
}
private void button16_Click(object sender, EventArgs e)
{
c = true;
type = "÷";
textBox2.Text += "÷";
x = double.Parse(textBox1.Text);
}
private void button18_Click(object sender, EventArgs e)
{
c = true;
type = "%";
textBox2.Text += "%";
x = double.Parse(textBox1.Text);
}
private void button17_Click(object sender, EventArgs e)
{
y = double.Parse(textBox1.Text);
//法一
while (type=="+")
{
textBox1.Text = (x + y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "-")
{
textBox1.Text = (x - y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "×")
{
textBox1.Text = (x * y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "÷")
{
if (y!=0)
{
textBox1.Text = (x / y).ToString();
textBox2.Text += "=" + textBox1.Text;
}
else
{
MessageBox.Show("请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
textBox1.Text = "";
textBox2.Text = "";
}
return;
}
while (type == "%")
{
textBox1.Text = (x % y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
//法二:
//if (type=="+")
//{
// textBox1.Text=(x + y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="-")
//{
// textBox1.Text = (x - y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="×")
//{
// textBox1.Text = (x * y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="÷")
//{
// textBox1.Text = (x / y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="%")
//{
// textBox1.Text = (x % y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
}
}
}
PSP 各个阶段 |
实际记录 (分钟) |
计划:明确需求和其他因素,估计以下的各个任务需要多少时间 |
|
开发(包括下面8项子任务) |
|
. 需求分析(包括学习新技术、新工具的时间) |
100 |
. 生成设计文档(整体框架的设计,各模块的接口,用时序图,快速原型等方法) |
50 |
. 设计复审 (和同事审核设计文档,或者自己复审) |
30 |
.代码规范(为目前的开发制定或选择合适的规范) |
20 |
.具体设计(用伪代码,流程图等方法来设计具体模块) |
50 |
.具体编码 |
100 |
.代码复审 |
30 |
.测试(自我测试,修改代码,提交修改) |
20 |
总共花费的时间(分钟) |
400 |