0% found this document useful (0 votes)
14 views40 pages

2 - Variables and Basic Math Operation - Eng

1. Create menu items and assign them IDs. 2. Create a function to handle the menu selection. 3. Use a switch statement in the function to call different code based on the selected menu ID.

Uploaded by

gnvcsjvv7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

2 - Variables and Basic Math Operation - Eng

1. Create menu items and assign them IDs. 2. Create a function to handle the menu selection. 3. Use a switch statement in the function to call different code based on the selected menu ID.

Uploaded by

gnvcsjvv7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Variable ( 變數 ) and basic operation

• Composition of variable
• Variable declaration ( 變數宣告 )
• Basic data type( 資料型態 )
• Constant expression ( 常數表達 )
• Basic mathematical operation
• Data type conversion ( 型別轉換 )
• Special operation
AfxMessageBox()
• The simplest dialog to display the output in VC++
• Format
– AfxMessageBox(_T(“Text description”));
• e,g, AfxMessageBox(_T(“This is my first example”));
– AfxMessageBox(str);
• str is a variable, declared as Cstring. (str is changeable)
• e.g.
– CString str; // 宣告一變數 str, 為字串之資料型態
– str=_T(“This is my first example”); // 將等號右側字串指定給變數
str
– AfxMessageBox(str); // 輸出到對話框中
Add ”;” at the end of each statement

All variables must be declared


first before they are used.

%d: a format to display integer


Composition of Variable
• A variable is composed of English • Sum
letters, numbers and the symbol “_”. • Sum
• Uppercase and lowercase are regarded • Varaiable_1
as different variables • Dir_x
• dir_x
• System default words must not be used
• Dir_y
(e.g. for, if, int, double, float, while, etc.) • Dir_z
• Cannot start with number • State_of_art
• Only “_” can be used. All other symbols • StateOfArt

are not allowed. Wrong:


1_add
add/sd
Variable declaration
• int i, j, k; -
Blue color: system default words
-
Each variable must be assigned as a data type
-
Several variables can be declared together
• double sum; -
Add “;” at the end of each statement

• double K1, K2, K3;


• CString string; double
• FILE *fp;
pt1y,pt1z,pt2x,pt2y,pt2z,pt3x,pt3y,pt3z,pt4x,pt4y,pt4z;
double z_vector1,x_vector2,y_vector2,z_vector2;
double I1,J1,K1,I2,J2,K2;
• char Temp[80]; double a,length1,length2,product;
int b, dirx,diry,dirz,tempx,tempy,tempz;
double c;
double x3,y3,z3,xc,yc,zc;
double rc,f1,f2;
char t[10];
CString s1,s2,s3;

int i,j,k; double l,m,n;


Basic data type
• int integer( 整數 ) 123, 1, 345, etc.)
• char character( 字元 ) a, A, b, B, etc.
• float real, single precision ( 浮點單精度實數 ) 12.3456789
• double real, double precision ( 浮點雙精度實數 )
12.123456789123456
• CString string ( 字串 ) _T(“This is a book”)
• bool or BOOL Boolean ( 布林運算 ) True or False
Memory occupied by different kinds of data type:
– short 16 bits -32767 ~ 32768 (+-/2e15)
– unsigned short 16 bits 0~65535
– int 32 bits -21474836647~ 21474836648
Common
– unsigned int 32 bits 0 ~ 4294967295
– long 32 bits (same as int)
– unsigned long 32 bits (Same as unsigned int)

1 bit: 0 or 1, the basic unit in computer operation


1 byte=8 bits
– char 8 bits (256 characters)
Real number:
– float 32 bits 3.4(10e-38) ~ 3.4(10e38)
Effective decimal digits: 7
– double 64 bits 1.7(10e-308) ~ 1.7(10e308)
Effective decimal digits: 15
– long double 80 bits 3.4(10e-4932) ~ 1.1(10e4932)
Effective expressions

int i; int i, j, k; int i,j,k; int i, j; int k;


int j;
Add “;” at the end of each statement.
int k; Add “,” between variables for the declaration of multiple
variables

int i, I;
Uppercase and lowercase are regarded as different variables
int j, J; Don’t define such variables as the program can easily become wrong
int k, K;

int i1, i2;


int j1, j2;
int k1, k2;
Constant expression String

• int i; A method to display the output:


AfxMessageBox(_T(“This is a test”));
• char t[30];
Declaration
• float k; CString o2;
O2=_T(“This is a test”);
• CString str; AfxMessageBox(o2);
• i=5; Declared as a variable
• k=3.14159;
Assignment
• t=”Hello”;
• str=”Hello”+” everyone”;
Examples :
• int i, j, nTemps, nTempt;
• double u, v, us, vs, length, max, min;
• unsigned int ext2dChildNum=0;
• int i, m_WorkPlane;
• double WKX, WKY, WKZ, WKDX, WKDY, WKDZ;
• double LBx, LBy, LBz, RTx, RTy, RTz;
• CString m_Name;
• CString strFile1= _T("\\RevTemp1.RVE");
• FILE *fp;
• double xx, yy, zz, dd, hwidth, hlength;
• CString o2;
• CString strFile1= _T("\\RevTemp1.MEA");
• CString strFile5= _T("\\RevTemp5.MEA");
• BOOL bTemp, Flag_selected;
• long nTemp, dogNum=0;
• long MaxSelPtNum=0;
• int g, h, i, j, k, m, p, num;
• int selCurNum=0, selSurNum=0;
• double ang, chg, startang;
• double costh, sinth;
• int ProgressCount;
• int bb;
• FILE *fp;
• char Temp[128];
• int LineNow, PtNow;
• int nTemp;
• int i;
Array
• long ProgressCount;
• double MM[16], MP[16], MPt[16], DrawPt1[16];
• double x, ii, jj;
• long ProgressCount;
• int i, j, k, pp;
• CString
– String data type
– Use .Format to combine text description and
number
– e.g.
• CString o2;
• int i=2;
• o2.Format(_T(“This is my %d example”),i);
– %d: the format to display integer
– %f: the format to display single precision
– %lf: the format to display double precision
• CString xxx;
• xxx.Format();
• Question:
– Intend to output”I have 5 apples”, where 5 may be changed in terms of
a vvariable. How to write the codes?
• Answer
– CString test;
– int num;
– num=5;
– test.Format(_T(“I have %d apples”),num);
• More examples
– a.Format(_T(“%d + %d = %d”), i, j, k); //e.g.: 2 + 3 = 5
– b.Format(_T(“%d 除以 %d 等於 %f”), i, j, q); //5/2=2.5
• //Note : Variables can be declared in any place as long as they
should be declared before they are used.
• In general, some variables are declared first before the codes are
developed.
• But, during the developing of the codes, it may be necessary to
declare and put new variables before the new statement.
• It is a good practice to move all variables in the front once the
program is finished.

Put the variables in the front

Put the variable in front of the


statement that the variable is used
Operator
Meaning: Assign the content on the right to the
• Assignment ( 指派 ( 定 ) 運算子 ) = memory of the variable on the left

• Arithmetic ( 算術運算子 ) +, -, *, /, % Residual value

• Relational ( 比較 ( 關係 ) 運算子 ) >, <, >=, <=, ==, != Not equal

And, or, not


• Logic ( 羅輯運算子 ) &&, ||, !

• ++, --, sizeof Output the memory used for a data type

• i++ i=i+1
Special operators

• i-- i=i-1
Basic math operation
• sum=a+b; // 加
• sum=a-b; // 減
• s=a*b; // 乘
• s=1/b; // 除
• s=a*b%c; //%: 餘數
Data type conversion
It is sometimes necessary to force a variable
to be changed to another data type temporarily
• float s, t;
• int ii; /**********************
int i,j;
• s=(float)ii * t; double sum;
• s=(float) a / b; i = 1;
j = 2;
• s=(int) a / (int) b;
sum = i/j;
and
sum = (double)i/(double)j;
are different
*************************/
Special operator
• j++; j=j+1;
• j--; j=j-1;
• ++j; j=j+1; (seldom use)
• --j; j=j-1; (seldom use)
• a+=b; a=a+b;
• a/=b; a=a/b;
• a*=b; a=a * b;
• a%=b a=a%b;
• n=sizeof(char); n=1 (1 byte)
• m=sizeof(int); m=4 (4 bytes)
• mm=sizeof(double); mm=8 (8 bytes)
Several examples

Create a menu and a function for the menu

You might also like