Write Calculator program in C #

Source: Internet
Author: User

First, design ideas

A comparison of the basic Windows Form program written in C #, which implements the basic mathematical operations such as addition, subtraction, multiplication, and other tasks. Mainly through the program to learn Vs.net

Programming environment, and Windows Form programs. Mainly for beginners

We have two parts to implement the program,

The first part. Program Interface

1, the following control table

control type Name Text
Form Calcform Calculator
button Button1 0
.....
Button10 9
Bdot . (decimal point) decimal point button
Bplus + (plus) plus button
BSub -(minus) minus button
Bmul * (multiplication) multiplication button
Bdiv /(Division) Division Button
Bequ = (equal sign) equals button
Bclr AC Purge button
Textbox Txtcalc (null value) to display input and output results

Part II, program structure

1, define the following variables

Double dblAcc; //运算数A
Double dblSec; //运算数B
bool blnClear,blnFrstOpen;//布尔类型用来判断清除与否,以及第一个显示字符
String strOper;//通过获取strOper的值来决定运算+,-,*,/,=

2, use the following methods to implement the action of the button

Example: Bdot.click+=net EventHandler (BTN_CLK);//eventhandler class is an event representative class that registers the processing of an event.

The first parameter is the object type, pointing to the objects that emit the event;

The second parameter is the EventArgs type, which contains data about this event

3, use the following methods to determine operations and operations

private void calc(){
switch(strOper){
 case "+":
  dblAcc+=dblSec;//加法运算
  break;
 case "-":
  dblAcc-=dblSec;//减法运算
  break;
 case "*":
  dblAcc*=dblSec;//乘法运算
  break;
 case "/":
  dblAcc/=dblSec;//除法运算
  break;
}
strOper="=";//等号运算
blnFrstOpen=true;
txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符型,并输出结果
dblSec=dblAcc;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.