实验一 计算器设计与实现
一、实验目的与要求
1.实验目的
锻炼可视化编程能力
2.实验要求
1、熟悉可视化编程环境(不限编程工具可使用Visual Studio、Java等)。
2、运用面向对象程序设计知识对实验题目进行分析和设计。
3、进行程序编写和调试工作。
二、实验内容
使用可视化编程环境下编写一个简易的计算器,能实现简单的加、减、乘、除四则运算,并加入特定的一些效果,如使计算器的颜色在红色,绿色和蓝色之间轮流变换等。完成诸如计算器对话框的创建、控件编辑、界面设计、关键功能的实现等工作。
- 实验方法
1、设计界面。创建一个对话框,在对话框中添加文本框用来显示计算结果,按钮:0-9、小数点、+、-、*、/、=等。
2、实现各个按钮的功能。
3、实现简单的算术四则运算语法分析,保证正确的计算结果。
4、实现颜色的变换。
四、详细的设计、调试和运行结果
1. 界面设计
- 模块设计
模块一:计算模块包括+、-、*、/、^(乘方)、%(取余)运算,利用switch进行选择。其中除法部分加入了除数为0的异常处理,利用MessageBox.Show弹出窗口。
public double Jisuan(double c, char t, double a)//计算
{
switch (t)
{
case '+':
return a + c;
case '-':
return a - c;
case '*':
return a * c;
case '/':
if (c == 0)//异常处理
{
MessageBox.Show("0不能作为除数!");
return 0;
}
else
return a / c;
case '%':
return a % c;
case '^':
return Math.Pow(a, c);
default:
return 0;
}
}
模块二:比较运算符优先级模块,使用一一列举比较,同时利用了数据结构关于运算符优先级的知识。
char Bijiao(char theta1, char theta2)//比较运算符优先级
{
if ((theta1 == '(' && theta2 == ')') || (theta1 == '#' && theta2 == '#'))// '#’为表达式结束符
{
return '=';// ‘(’与 ‘)’优先级相等,'#’与 '#’优先级相等
}
else if (theta1== '(' || theta1== '#' || theta2 == '(' || (theta1== '+' || theta1
//'#’,'('优先级最低。
== '-') && (theta2 == '*' || theta2 == '/' || theta2 == '^'))
//*,/,^的优先级高于+,-。
{
return '<';
}
else
return '>';
}
模块三:数字0-9,小数点按钮,使用了C#的语法如textBox1.Text += '2'。
private void button1_Click(object sender, EventArgs e)//数字1
{
textBox1.Text += '1';//加号的作用不是相加,而是为了将字符串连接起来。比如15就是1+5 ,不是相加的作用,下同
}
private void button2_Click(object sender, EventArgs e)//数字2
{
textBox1.Text += '2';
}
private void button3_Click(object sender, EventArgs e)//数字3
{
textBox1.Text += '3';
}
private void button4_Click(object sender, EventArgs e)//数字4
{
textBox1.Text += '4';
}
private void button5_Click(object sender, EventArgs e)//数字5
{
textBox1.Text += '5';
}
private void button6_Click(object sender, EventArgs e)//数字6
{
textBox1.Text += '6';
}
private void button7_Click(object sender, EventArgs e)//数字7
{
textBox1.Text += '7';
}
private void button8_Click(object sender, EventArgs e)//数字8
{
textBox1.Text += '8';
}
private void button9_Click(object sender, EventArgs e)//数字9
{
textBox1.Text += '9';
}
private void button10_Click(object sender, EventArgs e)//数字0
{
textBox1.Text += '0';
}
模块四:运算符+、-、*、/、^(pow)、%、( 、 )、= 按钮,使用了C#的语法如textBox1.Text += '+';与数字和小数点按钮类似。
private void button13_Click(object sender, EventArgs e)//加法
{
textBox1.Text += '+';
}
private void button14_Click(object sender, EventArgs e)//减法
{
textBox1.Text += '-';
}
private void button15_Click(object sender, EventArgs e)//乘法
{
textBox1.Text += '*';
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void button16_Click(object sender, EventArgs e)//除法
{
textBox1.Text += '/';
}
private void button17_Click(object sender, EventArgs e)//取余
{
textBox1.Text += '%';
}
private void button24_Click(object sender, EventArgs e)//乘方
{
textBox1.Text += '^';
}
private void button11_Click(object sender, EventArgs e)//左括号
{
textBox1.Text += '(';
}
private void button12_Click(object sender, EventArgs e)//右括号
{
textBox1.Text += ')';
}
private void button18_Click(object sender, EventArgs e)//小数点
{
textBox1.Text += '.';
}
模块五(关键模块):等号按钮实现。利用数据结构所讲的栈的知识进行优先级比较运算,关键是栈顶元素与输入的运算符进行优先级比较,详细过程体现在注释中。
private void button19_Click(object sender, EventArgs e)//等号
{
Stack<double> vs = new Stack<double>();//操作数栈
Stack<char> vvs = new Stack<char>();//运算符栈
string a = textBox1.Text + '=';//输入+“=”
string b = "";//初始为空
vvs.Push('=');//'='进运算符栈
for (int i = 0; i < a.Length; i++)
{
if (a[i] >= '0' && a[i] <= '9' || a[i] == '.')//输入为数字
{
b += a[i];//直接进行字符串连接
}
else if (a[i] == '=')//输入为'='
{
if (b != "")//b不为空
vs.Push(Convert.ToDouble(b));//b由string类型转换为double类型进操作数栈
while (vvs.Peek() != '=') //运算符栈顶不是'='
{
char t = vvs.Pop(); //运算符出栈
double d = vs.Pop();//操作数出栈
if (vs.Count() != 0)//操作数栈不空
{
double j = vs.Pop();//操作数出栈
vs.Push(Jisuan(d, t, j)); //运算结果入操作数栈
}
else//操作数栈空
{
MessageBox.Show("输入有问题");
textBox1.Text = "";//清空
}
}
}
else
{
if (b != "")
vs.Push(Convert.ToDouble(b));//b转换为double类型进操作数栈
switch (Bijiao(vvs.Peek(), a[i]))//操作数栈栈顶与输入的a[i]比较优先级
{
case '>'://操作数栈栈顶比a[i]优先级高
while (vvs.Count != 1 && vvs.Peek() != '(')//操作数数量不为1且运算符栈顶不为'='
{
char th = vvs.Pop(); //运算符出栈
double f = vs.Pop();//操作数出栈
double g = vs.Pop();//操作数出栈
vs.Push(Jisuan(f, th, g)); //运算结果入栈
}
if (vvs.Peek() == '(' && a[i] == ')')//左右括号匹配,出栈
{
vvs.Pop();
}
else
{
vvs.Push(a[i]);//否则继续入栈
}
break;
case '<'://操作数栈栈顶比a[i]优先级高
vvs.Push(a[i]);//a[i]入栈
break;
case '=':
while (vvs.Peek() != '=')//操作数栈栈顶与a[i]优先级相等
{
char th = vvs.Pop();//运算符出栈
double f = vs.Pop();//操作数出栈
double g = vs.Pop();//操作数出栈
vs.Push(Jisuan(f, th, g)); //运算结果入栈
}
break;
}
b = "";//b置空
}
}
textBox1.Text = "";
if (vs.Count() != 0)//操作数栈不空
{
textBox1.Text = vs.Peek().ToString();//textBox显示转换为string类型的操作数栈顶元素
}
}
模块六:清空模块包括textBox1中内容清空和颜色清除。主要是对各控件一一进行清空。
private void button20_Click(object sender, EventArgs e)//清空
{
textBox1.Text = "";//textBox1中内容清空
//颜色清除
this.BackColor = Color.FromName("Control");
this.textBox1.BackColor = Color.FromName("Window");
this.button1.BackColor = Color.FromName("Gainsboro");
this.button2.BackColor = Color.FromName("Gainsboro");
this.button3.BackColor = Color.FromName("Gainsboro");
this.button4.BackColor = Color.FromName("Gainsboro");
this.button5.BackColor = Color.FromName("Gainsboro");
this.button6.BackColor = Color.FromName("Gainsboro");
this.button7.BackColor = Color.FromName("Gainsboro");
this.button8.BackColor = Color.FromName("Gainsboro");
this.button9.BackColor = Color.FromName("Gainsboro");
this.button10.BackColor = Color.FromName("Gainsboro");
this.button11.BackColor = Color.FromName("Gainsboro");
this.button12.BackColor = Color.FromName("Gainsboro");
this.button13.BackColor = Color.FromName("Gainsboro");
this.button14.BackColor = Color.FromName("Gainsboro");
this.button15.BackColor = Color.FromName("Gainsboro");
this.button16.BackColor = Color.FromName("Gainsboro");
this.button17.BackColor = Color.FromName("Gainsboro");
this.button18.BackColor = Color.FromName("Gainsboro");
this.button19.BackColor = Color.FromName("Gainsboro");
this.button20.BackColor = Color.FromName("Gainsboro");
this.button21.BackColor = Color.FromName("Gainsboro");
this.button22.BackColor = Color.FromName("Gainsboro");
this.button23.BackColor = Color.FromName("Gainsboro");
this.button24.BackColor = Color.FromName("Gainsboro");
}
模块七:颜色变换模块包括红、绿、蓝三种颜色的变换,主要是对各控件一一进行颜色设置。
private void button21_Click(object sender, EventArgs e)//红色
{
this.BackColor = Color.FromName("Red");
this.textBox1.BackColor = Color.FromName("LavenderBlush");//textBox的颜色
this.button1.BackColor = Color.FromName("Red");
this.button2.BackColor = Color.FromName("Red");
this.button3.BackColor = Color.FromName("Red");
this.button4.BackColor = Color.FromName("Red");
this.button5.BackColor = Color.FromName("Red");
this.button6.BackColor = Color.FromName("Red");
this.button7.BackColor = Color.FromName("Red");
this.button8.BackColor = Color.FromName("Red");
this.button9.BackColor = Color.FromName("Red");
this.button10.BackColor = Color.FromName("Red");
this.button11.BackColor = Color.FromName("Red");
this.button12.BackColor = Color.FromName("Red");
this.button13.BackColor = Color.FromName("Red");
this.button14.BackColor = Color.FromName("Red");
this.button15.BackColor = Color.FromName("Red");
this.button16.BackColor = Color.FromName("Red");
this.button17.BackColor = Color.FromName("Red");
this.button18.BackColor = Color.FromName("Red");
this.button19.BackColor = Color.FromName("Red");
this.button20.BackColor = Color.FromName("Red");
this.button21.BackColor = Color.FromName("Red");
this.button22.BackColor = Color.FromName("Red");
this.button23.BackColor = Color.FromName("Red");
this.button24.BackColor = Color.FromName("Red");
}
private void button22_Click(object sender, EventArgs e)//绿色
{
this.BackColor = Color.FromName("Green");
this.textBox1.BackColor = Color.FromName("MintCream");
this.button1.BackColor = Color.FromName("Green");
this.button2.BackColor = Color.FromName("Green");
this.button3.BackColor = Color.FromName("Green");
this.button4.BackColor = Color.FromName("Green");
this.button5.BackColor = Color.FromName("Green");
this.button6.BackColor = Color.FromName("Green");
this.button7.BackColor = Color.FromName("Green");
this.button8.BackColor = Color.FromName("Green");
this.button9.BackColor = Color.FromName("Green");
this.button10.BackColor = Color.FromName("Green");
this.button11.BackColor = Color.FromName("Green");
this.button12.BackColor = Color.FromName("Green");
this.button13.BackColor = Color.FromName("Green");
this.button14.BackColor = Color.FromName("Green");
this.button15.BackColor = Color.FromName("Green");
this.button16.BackColor = Color.FromName("Green");
this.button17.BackColor = Color.FromName("Green");
this.button18.BackColor = Color.FromName("Green");
this.button19.BackColor = Color.FromName("Green");
this.button20.BackColor = Color.FromName("Green");
this.button21.BackColor = Color.FromName("Green");
this.button22.BackColor = Color.FromName("Green");
this.button23.BackColor = Color.FromName("Green");
this.button24.BackColor = Color.FromName("Green");
}
private void button23_Click(object sender, EventArgs e)//蓝色
{
this.BackColor = Color.FromName("Blue");
this.textBox1.BackColor = Color.FromName("Azure");
this.button1.BackColor = Color.FromName("Blue");
this.button2.BackColor = Color.FromName("Blue");
this.button3.BackColor = Color.FromName("Blue");
this.button4.BackColor = Color.FromName("Blue");
this.button5.BackColor = Color.FromName("Blue");
this.button6.BackColor = Color.FromName("Blue");
this.button7.BackColor = Color.FromName("Blue");
this.button8.BackColor = Color.FromName("Blue");
this.button9.BackColor = Color.FromName("Blue");
this.button10.BackColor = Color.FromName("Blue");
this.button11.BackColor = Color.FromName("Blue");
this.button12.BackColor = Color.FromName("Blue");
this.button13.BackColor = Color.FromName("Blue");
this.button14.BackColor = Color.FromName("Blue");
this.button15.BackColor = Color.FromName("Blue");
this.button16.BackColor = Color.FromName("Blue");
this.button17.BackColor = Color.FromName("Blue");
this.button18.BackColor = Color.FromName("Blue");
this.button19.BackColor = Color.FromName("Blue");
this.button20.BackColor = Color.FromName("Blue");
this.button21.BackColor = Color.FromName("Blue");
this.button22.BackColor = Color.FromName("Blue");
this.button23.BackColor = Color.FromName("Blue");
this.button24.BackColor = Color.FromName("Blue");
}
}
}
- 实现的主要功能或特色
本系统实现了如下功能:
(1)表达式的+、-、*、/运算,除此之外还添加了^(pow)(乘方)、%(取余)运算
+加法
-减法
*乘法
/除法
%取余
(2)小数点的实现
(3)混合运算优先级的处理
(4)括号的实现
(5)颜色变换
红色
绿色
蓝色
(6)除数为0的异常处理
(7)清空的实现
五.实验总结
1.由于第一次接触可视化编程,我对于VS软件及C#语言都很陌生,开始没有头绪。后来通过查阅资料、观看讲解视频,我对这些知识有了一定了解,再结合数据结构等专业知识编写了程序。经过不断修改和完善,完成了实验。该过程实属不易,让我学到了许多知识的同时,也磨练了意志。
2.计算器包括+、-、*、/、%、^几种运算,还添加了清空、颜色变换、优先级处理、小括号的实现等功能,还添加了异常处理,功能比较完善。
3.添加除数为0的异常处理时并未掌握如何给出提示,也是在查阅资料时学到利用MessageBox.Show("0不能作为除数!");解决这一问题。
4.实验中利用了本学期学习的数据结构中所讲的栈来解决优先级比较运算。
5.颜色变换和清空模块均是逐一列举操作,代码比较冗长。
6我在搜集资料过程中找到了更加全面的异常处理,包括两符号相邻和除号后面是0、字符串最后一个字母是符号、字符串首字母是符号、字符串开头是乘号或除号,另外还有退格操作。可供参考以对计算器功能进行完善。