计算器和画图板的设计
目录:
- 计算器设计
- 画图板设计
- 整体总结和分析
前言:
本次应课程要求要用C++设计计算器和画图程序,我会使用Microsoft Visual Studio 2010来完成该窗体应用程序,如果有写的不好的地方希望大家批评指正。
1.计算器设计
结构:
- 界面设计
- 函数模块设计
- 整体功能实现
首先,我们可以先创建一个一个基于CLR的窗体应用程序。这里我就从零开始讲解,这样大家可以更好的明白,同时自己也可以加深记忆。
我们打开Visual Studio 2010,先打开项目,创建一个CLR窗体应用程序,输入程序名称,选择保存路径,点击确定。如图:
打开项目后,将会出现以下的界面:
我们选择CLR中的窗体用用程序,这里一定要注意,千万别选错,不然就得不到想要的结果了。我这里创建了一个SpecialCalculator 名称,大家也可以创建别的名称,但最好是英文名,这样对编程更有利,也可以帮助大家学习英语。(ง •_•)ง
上面都创建好了,我们就出现如下的界面了:
接下来我们来实现整体界面设计,构建一个真正的计算器!!!
1.界面设计
我们都知道,要实现计算器,最基本的就是要有一个计算器的输入框和数字符号按钮,我们用鼠标点击数字、符号和公式,然后再点击=符号,输入框就可以计算出正确的答案。这样我们就实现了一个简单的计算器。
上面提到了输入框,按钮等等,以上我们可以称为组件,我们无非就是在窗体上添加自己想要的组件,然后给每个组件设置监听器,让他们去做不同的事情罢了。
这里我们就要打开关于组件的视图:工具箱||属性
所谓工具箱,就是存放工具的地方。里面包括所有我们要用到的组件,包括按钮,文本框,画图板,菜单栏,单选框等等。我们点击某个组件就可以在界面上进行绘制。如果我们要精确调整,这就涉及到组件的属性,属性自然说组件的特色,比如按钮的大小可以设置,按钮的背景颜色,按钮显示的文字的颜色,按钮的字体,视图的滚条设置等等,大家可以慢慢摸索。
接下来我们都知道组件的使用和属性了,我们可以简单设计一下界面。用到的组件有按钮、如下:
这里省略很多步骤,但这些步骤都一些简单的步骤,比如自己调整按钮的布局,在属性设置编辑框的显示文本,编辑框的滚条设计,还有就是按钮的前景色和背景色的设置。大家慢慢设计就可以制作一个美观的计算器,至于具体这么样大家可以有自己的想法。
2.函数模块设计
上面我们完成一个还算美观的界面,但是计算器是要用来计算的,而上面的按钮现在还只是摆设,我们因该要点击按钮时可以实现显示公式和算出结果。这里就涉及到对每个按钮添加相应的事件,我们可以称之为事件监听机制。
那么怎么实现事件监听呢?这里我们用鼠标双击我们需要添加监听器的组件,如下:
#pragma endregion
//界面
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
//编辑框
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
//标签
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
//按钮0
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
//这里我们让编辑框现实的字符串加上字符0
this->textBox1->Text=this->textBox1->Text+L"0";
}
接下来我们运行程序来看看点击O按钮的效果:
我们这里就完成字符的显示,接下类我们一气呵成,把其他的按钮都实现相应功能,完整的代码如下:
//------------------------------------------------------------------------------------------------------
//按钮0
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"0";
}
//按钮1
private: System::Void button17_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"1";
}
//按钮2
private: System::Void button12_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"2";
}
//按钮3
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"3";
}
//按钮4
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"4";
}
//按钮5
private: System::Void button13_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"5";
}
//按钮6
private: System::Void button16_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"6";
}
//按钮7
private: System::Void button15_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"7";
}
//按钮8
private: System::Void button14_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"8";
}
//按钮9
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"9";
}
//按钮.
private: System::Void button11_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L".";
}
//按钮/
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"/";
}
//按钮+
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"+";
}
//按钮-
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"-";
}
//按钮*
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"*";
}
//按钮(
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"(";
}
//按钮)
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L")";
}
//按钮Sin
private: System::Void button22_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"Sin";
}
//按钮Cos
private: System::Void button21_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"Cos";
}
//按钮Tan
private: System::Void button20_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"Tan";
}
//按钮In
private: System::Void button19_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"In";
}
//------------------------------------------------------------------------------------------------------
//按钮BackSpace(删除)
private: System::Void button27_Click(System::Object^ sender, System::EventArgs^ e) {
String^ s=this->textBox1->Text;
this->textBox1->Text=s->Remove(s->Length-1);
}
//按钮DelUp(清空)
private: System::Void button28_Click(System::Object^ sender, System::EventArgs^ e) {
String^ s=this->textBox1->Text;
this->textBox1->Text=s->Remove(0);
}
//按钮=输出-----------------------------------
private: System::Void button10_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox1->Text=this->textBox1->Text+L"=";
//调用计算函数(计算器的核心)
}
};
}
接下来我们键入公式,这里就需要根据公式来计算并显示答案,需要计算器的核心模块,计算函数函数构造:
1.首先,创建一个头文件来放置计算函数来供定义文件调用。
在出现的对话框中选中头文件(.h),并填写文件名,点击确定及创建一个头文件:
接下来就是编写函数代码:
using namespace System;//调用命名空间
//声名函数
String^ eatspaces(String^ str);//1.去掉输入公式中空格的函数
double expr(String^ str);//2.计算器的主函数计算+-
double term(String^ str,int^ index);//计算乘除函数
double number(String^ str,int^ index);//计算每一个数值
String^ extract(String^,int^);//抽取括号字符串函数
String^ exhsm(String^ int^);//取得三角函数字符串的函数
//定义函数-------------------------------------------------------------
//1.去掉输入公式中空格的函数
String^ eatspace(String^ str){
return str->Replace(L" ",L"");//注意第一个参数有空格,第二个没有
}
//这里我们可以简单了解一下Replace函数,这个大家可以去相应文档去查找,它有两个参数
//都是字符或字符串,这里显然是字符,意思是遍历传入的字符串并检查有无出现第一个数
//设置的字符,这个代表空字符,若出现空字符则用""代替,也就是去除。
//2.计算器的主函数,最后计算最基本的+,-
double expr(String^ str){
int^ index(0);//定义一个index指针变量,初始值为0
double value(term(str,index));//声名value双精度变量,以term
//函数作为参数,value可以理解为实时计算的过程值
while(*index<str->Length){
//判断index值是否大于传入公式的长度
switch(str[*index]){
//switch判断语句,按顺序判断传入公示的每一个字符
//+
case'+':
++(*index);
value+=term(str,index);
break;
case'-':
++(*index);
value+=term(str,index);
break;
default:
Console::WriteLine(L"There is an error!!!\n");
}
}
return value;
}
//定义先计算的乘除函数
double term(String^ str,int^ index){
double value(number(str,index));
while(*index<str->Length){
if(L'*'==str[*index]){
++(*index);
value*=number(str,index);
}
else if(L"/"==str[*index]){
++(*index);
value/=number(str,index);
}else{
break;
}
}
return value;
}
//返回每个表达式数字的函数
double number(String^ str,int^ index){
//初始化初值
double value(0.0);
double sign(