计算器--计应192(西)-第六组-郑团超

实现思路

1、先去除字符串内的空格。

2、然后按照括号优先的规则先用正则找出最里层的括号。

3、去除括号后把括号里的内容传入计算的函数。

4、在计算函数内用递归对传入的内容按照先乘除后加减的顺序进行计算和替换,直到得到一个数字。

5、最后把计算的结果返回赋值并对整个字符串进行替换。

6、在把新的字符串传入进行寻找括号的计算。

7、如果已经没有括号了就直接把该字符串传入计算。

8、直到最后如果已经找不到加减乘除这些计算符号,得到的就是最后的结果。

 

流程图

 

 

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

. 生成设计文档(整体框架的设计,各模块的接口,用时序图,快速原型等方法)

 25

. 设计复审 (和同事审核设计文档,或者自己复审)

15 

.代码规范(为目前的开发制定或选择合适的规范)

30 

.具体设计(用伪代码,流程图等方法来设计具体模块)

35 

.具体编码

20 

.代码复审

 15

.测试(自我测试,修改代码,提交修改)

 10

总共花费的时间(分钟)

260

 

 

 

 

PSP 各个阶段

 预估时间

(分钟)

实际记录

(分钟)

计划:明确需求和其他因素,估计以下的各个任务需要多少时间 

 

 

开发(包括下面8项子任务)

 

 

. 分析(包括学习新技术、新工具的时间)

 

 

. 生成设计文档(整体框架的设计,各模块的接口,用时序图,快速原型等方法)

 

 

. 设计复审 (和同事审核设计文档,或者自己复审)

 

 

.代码规范(为目前的开发制定或选择合适的规范)

 

 

.具体设计(用伪代码,流程图等方法来设计具体模块)

 

 

.具体编码

 

 

.代码复审

 

 

.测试(自我测试,修改代码,提交修改)

 

 

总共花费的时间(分钟)

 

 
posted @ 2021-04-11 17:53  计应192西六组  阅读(58)  评论(0编辑  收藏  举报