项目名称:计算器程序
项目概述:
1 编程环境:Microsoft Visual Studio 2008;
2 运行环境:Microsoft Visual Studio 2008;
3 主要功能:浮点数间的加、减、乘、除及混合运算,括号功能,清零键。
一、 功能及界面形式的截图
(1)主界面:
功能说明:除 清零键和“=”键外,可通过其他所有按键改变现实栏的显示值
(2)特别功能说明:含括号的浮点数的加减乘除四则混合运算:
算式的输入:
得到结果:
二、 主要算法思想说明
1. 参数设置:
private double a;//存储出栈的第一个数字
private double b;//存储出栈的第二个数字
private string pres="";//存储之前读入的字符串
private string curs="";//存储当前读入的字符串
private Stack<double> num;//存储读入字符串中转化后的浮点数
private Stack<char> ope;//存储读入字符串中的操作符以及括号
2 主要函数设置:
public Form1();//窗体生成
private void InitMembers()//初始化参数列表
private void button5_Click(object sender, EventArgs e)//Button 0
private void button4_Click(object sender, EventArgs e)//Button 1
private void button9_Click(object sender, EventArgs e)//Button 2
private void button14_Click(object sender, EventArgs e)//Button 3
private void button3_Click(object sender, EventArgs e)// Button4
private void button8_Click(object sender, EventArgs e)//Button 5
private void button13_Click(object sender, EventArgs e)//Button 6
private void button2_Click(object sender, EventArgs e)//Button 7
private void button7_Click(object sender, EventArgs e)//Button 8
private void button12_Click(object sender, EventArgs e)//Button 9
private void button10_Click(object sender, EventArgs e)//Button'.'
private void button6_Click(object sender, EventArgs e)//Button '('
private void button11_Click(object sender, EventArgs e)//Button')'
private void button18_Click(object sender, EventArgs e)//Button'+'
private void button19_Click(object sender, EventArgs e)//Button'-'
private void button16_Click(object sender, EventArgs e)//Button'*'
private void button17_Click(object sender, EventArgs e)//Button'/'
private void button15_Click(object sender, EventArgs e)//Button'='
3.算法设计阐述:
主要运用了栈的思想。首先通过按键的方式在窗体界面用textBox接收传入的
运算式的字符串(可能包括数字字符、小数点、运算符字符、括号,可在窗体
的显示栏同步显示),同时用 string 类型的变量 pres 存储,当运算式输入结
束后,按等号Button时,调用等号Button对应的函数(即
button15_Click(object sender, EventArgs e)),进行计算(这个是计算
器实现的关键部分都包含在这个函数里)。首先定义了一个char类型变量o,
‘初始化值为 e’,然后给pres自定义加一个结束字符,作为运算式结束的标志,
“用 pres+=‘e’;”语句实现。定义int 类型变量 l 存储字符串pres的长度。用for
循环对数组pres进行扫描,在for循环中以加一个while循环获取字符串中的数
字字符,临时存储在string 变量 curs 中,每次进入while 循环前将 curs置为
空,在循环结束后在if语句中将该字符数字的字符串转化为浮点数后,进入到
存储数字的num栈当中。
For循环的条件:
for (int i = 0; i < l; i++) //对pres 字符串进行扫描