作业基本信息
这个作业属于哪个课程 | https://bbs.csdn.net/forums/ssynkqtd-05 |
---|---|
这个作业要求在哪里 | https://bbs.csdn.net/topics/617294583 |
这个作业的目标 | 完成一个具有可视化界面的计算器 |
Gitcode项目地址
项目功能展示
PSP表格
PSP | Persona Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 20 |
• Estimate | • 估计这个任务需要多少时间 | 30 | 20 |
Development | 开发 | 460 | 760 |
• Analysis | • 需求分析 (包括学习新技术) | 30 | 20 |
• Design Spec | • 生成设计文档 | 30 | 35 |
• Design Review | • 设计复审 | 30 | 20 |
• Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 10 | 10 |
• Design | • 具体设计 | 30 | 45 |
• Coding | • 具体编码 | 300 | 420 |
• Code Review | • 代码复审 | 30 | 30 |
• Test | • 测试(自我测试,修改代码,提交修改) | 120 | 180 |
Reporting | 报告 | 60 | 40 |
• Test Repor | • 测试报告 | 30 | 20 |
• Size Measurement | • 计算工作量 | 10 | 10 |
• Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 20 | 210 |
合计 | 550 | 820 |
解题思路描述
要求实现一个具有图形化界面的计算器,在实现普通的加减乘除四则运算的基础上,进一步实现科学计算器的相关内容,包括次方、幂、三角函数等相关操作。
由于本次实践要求进行图形化开发,个人选择使用C#以及.NET进行开发
解题思路如下:
1.创建WPF项目,利用Text Box、Button等控件,创建GUI界面,并配置各个控件的属性。
2.通过触发各控件的事件进行表达式输入、运算以及结果输出。
3.表达式结果计算方法:首先扫描表达式,将表达式分割成多个子项,并将子项中隐含的操作数以及操作符补全,例如输入2sin(1)+23,将通过自己编写的分割函数将该表达式补全并分割为2sin(1)、2*3两个子项。接着通过堆栈方式,将各个子项转化为后缀表达式并计算。最后将各个子项相加即为最终结果。
4.在实现复杂表达式的分割时,需要对可能存在的特殊情况进行特判并处理,以便对各个子项求值。
接口设计和实现过程
1.通过工具箱生成TextBox和Button控件,使用XAML语言修改布局,双击控件进入控件的触发事件函数,完成各事件的响应。
2.设计item_divide()函数,将需处理的表达式传入该函数,进行子项分割。
3.设计Caculate()函数,将存放子项的字符串数组传入该函数,进行结果运算。
核心代码
表达式分割:
public static void item_divide(string input, out string[] items, out int sum)
{
string expression = "";
items = new string[233];
sum = 0;
char temp;
input += "#";
int couples = 0;
for (int i = 0; input[i] != '#'; i++)
{
temp = input[i];
if(temp >= '0' && temp <= '9' && (input[i+1] >= 'a' && input[i+1] <= 'z' || input[i+1] == '('))
{
expression = expression + temp + '*';
}
else if(temp >= 'a' && temp <= 'z' && input[i + 1] >= 'a' && input[i + 1] <= 'z')
{
string alpha = "";
alpha += temp;
while (input[i+1] >= 'a' && input[i+1] <= 'z')
{
alpha += input[i + 1];
i++;
}
while(alpha.Length != 0)
{
switch(alpha)
{
case "sin":
expression += "sin";
alpha = "";
break;
case "cos":
expression += "cos";
alpha = "";
break;
case "tan":
expression += "tan";
alpha = "";
break;
case "abs":
expression += "abs";
alpha = "";
break;
case "lg":
expression += "lg";
alpha = "";
break;
}
}
}
else if(temp == '(')
{
expression += temp;
couples++;
if (input[i+1] == '-' && (input[i+2] >= 'a' && input[i+2] <= 'z'))
{
expression += "0-1*";
i++;
}
else if (input[i + 1] == '-' && (input[i + 2] >= '0' && input[i + 2] <= '9'))
{
expression += "0-";
i++;
}
}
else if(temp == ')')
{
expression += temp;
couples--;
}
else
{
if(couples == 0 && (temp == '+' || temp == '-'))
{
items[sum] = expression;
sum++;
expression = Convert.ToString(temp);
}
else
{
expression += temp;
}
}
}
items[sum] = expression;
sum++;
}
子项结果计算:
public static double Caculate(string[] items, int sum)
{
Stack<double> data = new Stack<double>();
Stack<string> ope = new Stack<string>();
double result = 0;
char temp;
string alpha;
int flag = 0;
while(sum > 0)
{
alpha = "";
items[--sum] += '#';
data.Clear();
ope.Clear();
data.Push(0); // 栈底元素为0,便于处理负数
for(int i = 0; items[sum][i] != '#'; i++)
{
temp = items[sum][i];
alpha += temp;
if(temp >= '0' && temp <= '9')
{
while (items[sum][i] != '#')
{
if (items[sum][i+1] >= '0' && items[sum][i+1] <= '9' || items[sum][i+1] == '.')
{
alpha += items[sum][i + 1];
i++;
}
else
{
break;
}
}
data.Push(Convert.ToDouble(alpha));
}
else if(temp >='a' && temp <= 'z')
{
while (items[sum][i] != '#')
{
if (items[sum][i+1] >= 'a' && items[sum][i+1] <= 'z')
{
alpha += items[sum][i+1];
i++;
}
else
{
break;
}
}
ope.Push(alpha);
}
else if(temp == ')')
{
string now_ope;
while (ope.Count() != 0 && !ope.Peek().Equals("("))
{
now_ope = ope.Pop();
if (now_ope.Equals("+"))
{
data.Push(data.Pop() + data.Pop());
}
else if (now_ope.Equals("-"))
{
data.Push(-data.Pop() + data.Pop()); // 注意操作数顺序
}
else if (now_ope.Equals("*"))
{
data.Push(data.Pop() * data.Pop());
}
else if (now_ope.Equals("/")) // 注意操作数顺序
{
data.Push(1/data.Pop() * data.Pop());
}
else if (now_ope.Equals("^")) // 注意操作数顺序
{
double second_num = data.Pop();
data.Push(Math.Pow(data.Pop(), second_num));
}
else if (now_ope.Equals("sin"))
{
data.Push(Math.Sin(data.Pop()));
}
else if (now_ope.Equals("cos"))
{
data.Push(Math.Cos(data.Pop()));
}
else if (now_ope.Equals("tan"))
{
data.Push(Math.Tan(data.Pop()));
}
else if (now_ope.Equals("abs"))
{
data.Push(Math.Abs(data.Pop()));
}
else if (now_ope.Equals("lg"))
{
data.Push(Math.Log2(data.Pop()));
}
}
if(ope.Count() != 0 && ope.Peek().Equals("("))
{
ope.Pop();
}
}
else
{
switch (alpha)
{
case "+":
case "-":
while(ope.Count != 0 && !ope.Peek().Equals("("))
{
string now_ope;
now_ope = ope.Pop();
if (now_ope.Equals("+"))
{
data.Push(data.Pop() + data.Pop());
}
else if (now_ope.Equals("-"))
{
data.Push(-data.Pop() + data.Pop()); // 注意操作数顺序
}
else if (now_ope.Equals("*"))
{
data.Push(data.Pop() * data.Pop());
}
else if (now_ope.Equals("/")) // 注意操作数顺序
{
data.Push(1 / data.Pop() * data.Pop());
}
else if (now_ope.Equals("^")) // 注意操作数顺序
{
double second_num = data.Pop();
data.Push(Math.Pow(data.Pop(), second_num));
}
else if (now_ope.Equals("sin"))
{
data.Push(Math.Sin(data.Pop()));
}
else if (now_ope.Equals("cos"))
{
data.Push(Math.Cos(data.Pop()));
}
else if (now_ope.Equals("tan"))
{
data.Push(Math.Tan(data.Pop()));
}
else if (now_ope.Equals("abs"))
{
data.Push(Math.Abs(data.Pop()));
}
else if (now_ope.Equals("lg"))
{
data.Push(Math.Log2(data.Pop()));
}
}
ope.Push(alpha);
break;
case "*":
case "/":
while(ope.Count != 0 && ope.Peek() != "(")
{
string now_ope;
now_ope = ope.Pop();
if (now_ope.Equals("*"))
{
data.Push(data.Pop() * data.Pop());
}
else if (now_ope.Equals("/")) // 注意操作数顺序
{
data.Push(1 / data.Pop() * data.Pop());
}
else if (now_ope.Equals("^")) // 注意操作数顺序
{
double second_num = data.Pop();
data.Push(Math.Pow(data.Pop(), second_num));
}
else if (now_ope.Equals("sin"))
{
data.Push(Math.Sin(data.Pop()));
}
else if (now_ope.Equals("cos"))
{
data.Push(Math.Cos(data.Pop()));
}
else if (now_ope.Equals("tan"))
{
data.Push(Math.Tan(data.Pop()));
}
else if (now_ope.Equals("abs"))
{
data.Push(Math.Abs(data.Pop()));
}
else if (now_ope.Equals("lg"))
{
data.Push(Math.Log2(data.Pop()));
}
}
ope.Push(alpha);
break;
case "^":
ope.Push(alpha);
break;
case "(":
ope.Push(alpha);
break;
}
}
alpha = "";
}
while(ope.Count != 0)
{
alpha = ope.Pop();
if (alpha.Equals("+"))
{
data.Push(data.Pop() + data.Pop());
}
else if(alpha.Equals("-"))
{
data.Push(-data.Pop() + data.Pop());
}
else if (alpha.Equals("*"))
{
data.Push(-data.Pop() * data.Pop());
}
else if (alpha.Equals("/"))
{
data.Push(1/data.Pop() * data.Pop());
}
else if (alpha.Equals("^"))
{
double second_num = data.Pop();
data.Push(Math.Pow(data.Pop(), second_num));
}
else if (alpha.Equals("sin"))
{
data.Push(Math.Sin(data.Pop()));
}
else if (alpha.Equals("cos"))
{
data.Push(Math.Cos(data.Pop()));
}
else if (alpha.Equals("tan"))
{
data.Push(Math.Tan(data.Pop()));
}
else if (alpha.Equals("abs"))
{
data.Push(Math.Abs(data.Pop()));
}
else if (alpha.Equals("lg"))
{
data.Push(Math.Log2(data.Pop()));
}
}
result += data.Peek();
}
return result;
}
性能改进
通过括号匹配原则,按照各运算符优先级完成三角函数、自然对数、绝对值的嵌套运算,对负号进行判定,区分负数与减法;补全表达式中隐含的运算数以及运算符。
单元测试
单元测试代码:
public void CaculateTest()
{
string[] items1 = { "(sin(0.5*2))^2", "(cos(0.5/0.5))^2" };
if (MainWindow.Caculate(items1, 2) != 1)
{
Assert.Fail();
}
string[] items2 = { "(4*sin(1))^2", "(4*cos(1))^2" };
if(MainWindow.Caculate(items2, 2) != 16)
{
Assert.Fail();
}
string[] items3 = { "-1*tan(2*cos(-1))+1" , "tan(2*cos(-1))" };
if (MainWindow.Caculate(items3, 2) != 1)
{
Assert.Fail();
}
double ans4 = Math.Tan(Math.Sin(Math.Cos(1)));
string[] items4 = { "tan(sin(cos(1)))" };
if( MainWindow.Caculate(items4, 1) != ans4)
{
Assert.Fail();
}
string[] items5 = { "abs(lg(1/2))" };
if (MainWindow.Caculate(items5, 1) != 1)
{
Assert.Fail();
}
}
测试结果:
心路历程与收获
本次作业是个人第一次接触图形化开发,本次实践也让我学习了如何进行单元测试以及如何将程序打包成可执行文件。在实现作业要求的计算器的运算功能时,我选择利用栈,将中缀表达式转化为后缀表达式进行运算,由于要实现的运算功能较多,在优先级设置方面一开始没有考虑周全,在进行单元测试时也发现有部分特殊输入没有考虑到。在跟舍友同学交流过程中,逐步完善功能,也算是对关于栈的数据结构做了一点简单的复习。