这个作业属于哪个课程 | https://bbs.csdn.net/forums/ssynkqtd-05 |
---|---|
这个作业要求在哪里 | https://bbs.csdn.net/topics/617294583 |
这个作业的目标 | 完成一个可视化计算器 |
其他参考文献 | https://www.cnblogs.com/xinz/archive/2011/11/20/2255830.html |
1.界面展示
2.PSP表格
PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 10 | 10 |
• Estimate | • 估计这个任务需要多少时间 | 10 | 10 |
Development | 开发 | 300 | 240 |
• Analysis | • 需求分析 (包括学习新技术) | 300 | 240 |
• Design Spec | • 生成设计文档 | 20 | 20 |
• Design Review | • 设计复审 | 5 | 5 |
• Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 30 | 60 |
• Design | • 具体设计 | 60 | 60 |
• Coding | • 具体编码 | 120 | 120 |
• Code Review | • 代码复审 | 60 | 60 |
• Test | • 测试(自我测试,修改代码,提交修改) | 180 | 180 |
Reporting | 报告 | 120 | 120 |
• Test Repor | • 测试报告 | 60 | 60 |
• Size Measurement | • 计算工作量 | 20 | 10 |
• Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 30 | 20 |
合计 | 1215 |
3.解题思路描述
刚开始拿到题目后,先查找了相似的事例,分析其设计语言与设计思路作为参考,由于本人的设计经验不足以及时间等原因,最终选择了C#WinForm来实现可视化计算器。
在C#WinForm中使用工具箱可以在窗口中编辑设计文本框与按键,较为直观与方便。
4.设计与实现过程
设计思路
将输入分为数字,运算符与其他功能,点击数字按钮,将数字打印到文本框中,点击运算符按钮,储存运算符与文本框中的数,并清空文本框。 点击“=”时,将储存的数与运算符进行运算后打印到文本框中。使用Math类实现指数,对数,三角函数等运算。
清空:清空文本框,将所有相关变量初始化
删除:读取文本,打印文本长度-1到文本框。
部分代码
使用的变量:
double num1 = 0;
double num2 = 0;
double result= 0;
string typ; //运算符类型
bool eq = false; // 运算符判断
bool chushu_flag =true; //除数判断
按键绑定:
private void Form1_Load(object sender, EventArgs e)
{
this.b2.Click += new System.EventHandler(this.B1_Click);
this.b3.Click += new System.EventHandler(this.B1_Click);
this.b4.Click += new System.EventHandler(this.B1_Click);
this.b5.Click += new System.EventHandler(this.B1_Click);
this.b6.Click += new System.EventHandler(this.B1_Click);
this.b7.Click += new System.EventHandler(this.B1_Click);
this.b8.Click += new System.EventHandler(this.B1_Click);
this.b9.Click += new System.EventHandler(this.B1_Click);
this.b0.Click += new System.EventHandler(this.B1_Click);
this.b_jia.Click += new System.EventHandler(this.B1_Click);
this.b_jian.Click += new System.EventHandler(this.B1_Click);
this.b_cheng.Click += new System.EventHandler(this.B1_Click);
this.b_chu.Click += new System.EventHandler(this.B1_Click);
this.b_dian.Click += new System.EventHandler(this.B1_Click);
this.b_yv.Click += new System.EventHandler(this.B1_Click);
}
数字打印与运算符输入:
private void B1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string txt = btn.Text;
if (txt == "+" || txt == "-" || txt == "*" || txt == "/"||txt=="%")
{
mode(txt);
}
else//0-9,.
{
string tem = textBox1.Text;
if (tem == "0") tem = null;
tem = tem + txt;
textBox1.Text = tem;
}
}
void mode(string mode)
{
if (eq == true)
{
num1 = result;
textBox1.Text = null;
}
else if (typ != null)
{
if(textBox1.Text != "")
{
num2 = double.Parse(textBox1.Text);
Ji_suan();
num1 = result;
textBox1.Text = null;
}
}
else
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = null;
}
if (chushu_flag == false)
{
textBox2.Text = " 除数不能为0!";
textBox1.Text = "0";
chushu_flag = true;
typ = null;
}
else
{
textBox2.Text = num1 + mode;
eq = false;
typ = mode;
}
}
运算函数:
private void Ji_suan ()
{
switch (typ)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0)
{
chushu_flag = false;
return;
}
result = num1 / num2;
break;
case "%":
result = num1 % num2;
break;
case "^":
result = Math.Pow(num1 , num2);
break;
case "√":
result = num1 * (Math.Sqrt(num2));
break;
case "sin":
result = num1 * (Math.Sin(num2));
break;
case "cos":
result = num1 * (Math.Cos(num2));
break;
case "tan":
result = num1 * (Math.Tan(num2));
break;
case "ln":
result = num1 * (Math.Log(num2, Math.E));
break;
case "lg":
result = num1 * (Math.Log(num2,10));
break;
default:
break;
}
}
等于:
private void b_deng_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string txt = btn.Text;
if (eq == true)
num1 = result;
else
{
num2 = double.Parse(textBox1.Text);
eq = true;
}
if (typ == null)
textBox2.Text = num2 + "=";
else
textBox2.Text = num1 + typ + num2 + "=";
Ji_suan();
textBox1.Text = result.ToString();
if(chushu_flag==false)
{
textBox2.Text = " 除数不能为0!";
chushu_flag = true;
}
}
清空与删除:
private void b_cl_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
result = 0;
eq = false;
typ = null;
textBox2.Text = "";
textBox1.Text = "0";
}
private void b_del_Click(object sender, EventArgs e)
{
string tem = textBox1.Text;
if (tem.Length > 0)
{
tem = tem.Substring(0, tem.Length - 1);
}
textBox1.Text = tem;
}
5.程序性能改进
确定思路后,先完成了含有有基本运算功能的计算器
由于此文本框只能打印数字,不能打印运算符(技术不足,容易报错),不够直观方便,因此参考了手机APP的计算器界面进行改进,增加新的文本框2,用于打印储存过的数字与运算符
改进了运算符按键,在进行多个数运算时,能起到类似“等于”的作用,将文本框2的部分运算后作为第一个数。
以此为基础,利用Math类添加指数,对数,三角函数等运算功能。
6.单元测试
void B1_Click(object sender, EventArgs e)
{
if (txt == "+" || txt == "-" || txt == "*" || txt == "/" || txt == "%")
{
mode(txt);
}
}
void mode(string mode)
{
if (eq == true)
{
num1 = result;
t1 = null;
}
else if (typ != null)
{
if (t1 != "")
{
num2 = double.Parse(t1);
Ji_suan();
num1 = result;
t1 = null;
}
}
else
{
num1 = double.Parse(t1);
t1 = null;
}
if (chushu_flag == false)
{
t2 = " 除数不能为0!";
t1 = "0";
chushu_flag = true;
typ = null;
}
else
{
t2 = num1 + mode;
eq = false;
typ = mode;
}
}
void b_deng_Click(object sender, EventArgs e)
{
if (eq == true)
num1 = result;
else
{
num2 = double.Parse(t1);
eq = true;
}
if (typ == null)
t2 = num2 + "=";
else
t2 = num1 + typ + num2 + "=";
Ji_suan();
t1 = result.ToString();
if (chushu_flag == false)
{
t2 = " 除数不能为0!";
chushu_flag = true;
}
}
void Ji_suan()
{
switch (typ)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0)
{
chushu_flag = false;
return;
}
result = num1 / num2;
break;
case "%":
result = num1 % num2;
break;
case "^":
result = Math.Pow(num1, num2);
break;
case "√":
result = num1 * (Math.Sqrt(num2));
break;
case "sin":
result = num1 * (Math.Sin(num2));
break;
case "cos":
result = num1 * (Math.Cos(num2));
break;
case "tan":
result = num1 * (Math.Tan(num2));
break;
case "ln":
result = num1 * (Math.Log(num2, Math.E));
break;
case "lg":
result = num1 * (Math.Log(num2, 10));
break;
default:
break;
}
}
7.心路历程与收获
在本次作业中,我对对可视化界面的设计有了初步的认识,熟悉了c#的相关语法,学习了windows窗体应用的使用,学习了前端界面的设计,提高了自己的编程技能,增强了发现与解决问题的能力,也意识自己早期规划与整体意识的不足。对软件工程有了更深刻的认识,将在今后更加努力。