2 - Variables and Basic Math Operation - Eng
2 - Variables and Basic Math Operation - Eng
• 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
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;
• ++, --, 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