- Windows程序设计与架构
- 蔺华 汤春林 蔡兴旺编著
- 1391字
- 2020-08-28 17:44:33
2.2 案例分析2 创建简单计算器程序
2.2.1 案例描述
本节介绍如何创建一个简单的计算器应用程序。
2.2.2 案例分析
创建Windows窗体应用程序的基本步骤。
2.2.3 案例实现与技巧
① 首先,运行Visual Studio 2005,新建一个Windows应用程序,命名为calculate。
② 向窗体中添加Button按钮控件,显示数字0~9,并设置控件的ForeColor属性为Blue。布局如图2-6所示。
图2-6 控件布局
③ 向窗体添加Button按钮控件,运算符“+”、“-”、“*”、“/”、“.”、“-/+”、“C”、“=”,并设置控件的ForeColor属性为Red。
④ 向窗体添加TextBox控件用于显示数据,并将其TextAlign属性设置为“Right”。
⑤ 在Form1类中对下列数据进行定义和初始化,代码如下所示。
private int opMain = 0; // 1(加法) 2(减法) 3(乘法) 4(除法) private double mainNum1 = 0; // 存储第一个数 private double mainNum2 = 0; // 存储第二个数 private bool isSecond = false; // 用来判断输入的是第一个还是第二个数 private bool isDone = false; // 用来判断是否按了等于按钮 private bool isDecimal = false; // 用了判断是否有小数
⑥ 在类中增加set Text方法,代码如下所示:
public void setText(String textset) { if (textset.Equals("clear")) //If the user hits the clear button { txt_result.Text = ""; //Clear the text and reset the boolean variables isDone = false; isSecond = false; isDecimal = false; } else { if (isSecond) //Determine if the number being entered is the begining of the //second number. If it is { txt_result.Text = textset; //Start the text over and set the first # to //what the user enters isSecond = false; //So Calculator knows to continue the # rather than making //a new one isDecimal = false; } else { if (isDone) //isDone lets the program know that the user just hit "=" and //if they press another # to start a new number { txt_result.Text = textset; isDone = false; //Set isDone to false so that the number just started //is added on to and a new # is not started } else txt_result.Text += textset; //Simply add on to the existing #. } } btn_equals.Select(); //Set the focus back to the "=" button } public void Calc(double num1,double num2,int op) { double answer = 0;//Initialize answer to 0 switch (op) //Determine which operation to perform depending on the value of "op" { case 1: answer = num1 + num2; break; case 2: answer = num1 - num2; break; case 3: answer = num1 * num2; break; case 4: answer = num1 / num2; break; } setText(answer.ToString()); //Show the answer in the textbox; } private void doEquals() { mainNum2 = double.Parse(txt_result.Text); //Set the value of the second number setText("clear"); //Clear the textbox Calc(mainNum1,mainNum2,opMain); //Call the Calc method isDone = true; //Set isDone to true so that if another # is pressed,the program //will begin a new number } private void changeSign() { double storNum; //Variable to store value of number if (txt_result.Text.Length > 0) //If there is a number { storNum = double.Parse(txt_result.Text); //Store its value storNum *= -1; //multiply by negative 1 txt_result.Text = storNum.ToString(); //put it in the textbox } btn_equals.Select(); //Set focus to "=" button } private void setOperator(int operation) { if (txt_result.TextLength> 0)//Make sure that the user entered a number { opMain = operation; //Store the operation mainNum1 = double.Parse(txt_result.Text); //Store the value of the first number isSecond = true; //Let the program know to begin the second number isDone = false; //If a operator button is pressed before a new number,let //the program know to start a new number btn_equals.Select(); //Set the focus to the equals button } } private void setDecimal() { if (!isDecimal)//Check for existing decimal { setText("."); //Add decimal isDecimal = true; //Let program know decimal has been added } btn_equals.Select(); //Set focus to "=" button } public void filterKeys(int keyCode) { switch (keyCode) { case 96: setText("0"); break; case 97: setText("1"); break; case 98: setText("2"); break; case 99: setText("3"); break; case 100: setText("4"); break; case 101: setText("5"); break; case 102: setText("6"); break; case 103: setText("7"); break; case 104: setText("8"); break; case 105: setText("9"); break; case 67: setText("clear"); break; case 107: setOperator(1); break; case 109: setOperator(2); break; case 106: setOperator(3); break; case 111: setOperator(4); break; case 110: setDecimal(); break; } } private void Calculator1_KeyDown(object sender,System.Windows.Forms. KeyEventArgs e) { filterKeys(e.KeyValue); }
⑦ 双击Button控件0~9,分别添加如下事件处理程序。
setText("0"); setText("1"); setText("2"); setText("3"); setText("4"); setText("5"); setText("6"); setText("7"); setText("8"); setText("9");
⑧ 双击“+”号按钮,添加如下事件处理程序。
setOperator(1);
⑨ 双击“-”号按钮,添加如下事件处理程序。
setOperator(2);
⑩ 双击“*”号按钮,添加如下事件处理程序。
setOperator(3);
⑪ 双击“=”号按钮,添加如下事件处理程序。
doEquals();
⑫ 双击“.”号按钮。添加如下事件处理程序。
setDecimal();
⑬ 双击“C”按钮,添加如下事件处理程序。
isSecond = false; setText("clear");
⑭ 程序运行结果如图2-7所示。
图2-7 程序运行结果