Click here to load reader

基本資料型態

Embed Size (px)

DESCRIPTION

3. 基本資料型態. 能盡物之性,則可以贊天地之化育。 可以贊天地之化育,則可以與天地矣。 《 中庸 ﹒ 第二十一章 》. 基本資料型態. 3.1  整數和浮點數 3.2  變數和常數 3.3  算術運算 3.4  標準數學函數的運算 3.5  邏輯值及其運算 3.6  字元與字串 3.7  位元處理運算. C++ 的資料型態. 3.1 整數. 整數 (integer values) 是所有不具小數點的數值。例如: 6 -3728 0 +248571 -12 +0 005 4L. - PowerPoint PPT Presentation

Citation preview

  • 3

  • 3.13.2 3.33.43.53.63.7

  • C++

  • 3.1 (integer values)

    6-37280+248571-12+00054L

  • long int816 unsigned int ()48U// unsigned int75UL// unsigned long372L// long75l// long (l 1)012 // 128 = 100x12// 1216 = 18

  • Int.cpp

    // Int.cpp#include using namespace std;int main() { cout

  • Int.cpp

    48U : 4875UL : 75372L : 37275l : 75012 : 100x12 : 18

  • (floating point numbers)

    36.0-7.382+3.92L0.00+4.03.7f3.7F

  • (exponential notation)eexponentEe

  • float long double4.7 // double48.0F// float48.0f// float3.75L// long double4.26e12 // double4.26e+12L// long double (+)4.26E-12 // double

  • Float.cpp

    // Float.cpp#include using namespace std;// ----- --------------------------int main() { cout

  • Float.cpp

    4.7: 4.748.0F: 4848.0f: 483.75L: 3.754.26e12: 4.26e+124.26e+12L: 4.26e+124.26E-12: 4.26e-12

  • 3.2 (variables) (constants) (declaration)

  • Int Age;floatHeight;AgeHeight (definition)

  • floata;

    00010100

    10001000

    01101101

    01010001

    0065FDFC

  • short longintshortlonglong int Ia;long Ib;shortint Ic;short Id;

  • float double float double

    float Fa;// Fa double Db;// Db

  • Average.cpp

    // Average.cpp#include using namespace std;// ---------------------------int main(){int Number = 3;float a, b;float c = 5.6;float Average;a = 7.8;b=3.9; Average = (a + b + c) / Number; cout

  • (multiple declarations)float a, b;float a;float b;

  • cfloat c = 5.6;

  • (constant) 1.2.

  • const double TaxRate = 0.17;const float Inch2Meter = 0.024;const int Max = 1024;

  • lvalue () const int Max = 1024;Max = 2048;// !

  • (arithmetic operators)7

  • expressionx < 15.8x = y + sin( z )( x + y ) * 0.8 z

  • statementC++ x = a + b * sin( c ) ;;b++;y = ( b > 10.0 ) ;x = c + 5.0 ;

  • (1/2)1.

    2.x = a + b * ( c + 6.5 ) +c * 105.8 4.0;x = a + b * ( c + 6.5 ) + c * 105.8 4.0;

  • (2/2)3. (operator) x=a+b;x = a + b;

  • (assignment statement):Value = expression;x = a + b;(1) expression(2) Value

  • (operand) (operator)x * yxy (operand)* (operator)

  • (unary operator) (binary operator) *, / %

  • ---x xx y xy

  • 6 + 4 * 8 % 3 * 6 - 56 + ((4 * 8) % 3) * 6 - 5

  • C++

    1/2 03/7 015/2 7

  • (modulus operator) %1%2 13%7 315%2 1

  • (increment operator) (decrement operator)

    N = N + 1N++ ++NN = N - 1N-- --NM = M + 1M++ ++MM = M 1M-- --M

  • ++ -- (prefix)M = ++i;i = i + 1; M = i;

  • ++ -- (postfix) M = i++; M = i;i = i + 1;

  • M = i++ * 5; M = i*5; i++;M = ++i * 5; i++;M = i*5;

  • (assignment operators)

    ()Num = Num + Inc;um += Inc;x = x * TaxRage;x *= TaxRate;y = y / Ratio;y /= Ratio;M = M % N;M %= N;

  • rvalue lvalue1. rvaluelvalueright value () left value () rvalue=rvalue2. compilerlvalue3. rvaluelvalue5 = x ;( b = 10.0 ) = a ;

  • - Convert.cpp

    // Convert.cpp#include using namespace std;// ---------------------------int main(){ int a = 5; float x; x = a + 3.8; cout

  • (implicit type conversions)

    (expression)a + 3.8ffloata + 3.8double

  • C++ 1.2.3. (assignment operation)lvalue

  • C++ () ; ();x = float (a) + 3.8f ;x = (float) a + 3.8f ;

  • sizeof () sizeof()

  • Size.cpp

    // Size.cpp#include #include using namespace std;// ---------------- -------------------------int main(){ int a = 5; cout

  • Size.cpp

    Size of int is: 4 bytesSize of short is: 2 bytesSize of (3.8f + a) is: 4 bytes

  • 3.4 C++

  • Power3.cpp x3

    // Power3.cpp#include #include using namespace std;// ------------ ----------------------int main(){ double x ; cout > x; cout

  • Power3.cpp

    4.83: 110.592

  • (arguments) (Overloading)

    pow()

    x3

    (x,3)

  • -- abs(x) fabs(x)

    double x = -253.0427;cout

  • 3.5 (Boolean data)truefalse C++ 0 true

  • (Relational Operations) (relational expression)x < 3.81 ( x 3.8 ) 0 ( x 3.8 )

  • (relational operators )

    x > 10.0= b==x == y!=x != y

  • C++ (logical operators)

    &&AND( a > b ) && ( c > d )||OR( x > 8.2 ) || ( y < 0.0 )!NOT! ( x < 0.0 )

  • ! ++ -- -()-!a-(!a)* / %a*b/c( a * b ) / c+ - ()a*bc( a * b ) c< >=a+b

  • (1)a * b c < d(2)y = ( b > 10.0 ) ;

  • (De Morgan' s law)

    ( ORAND)

    not (A and B)(not A) or (not B)not (A or B)(not A) and (not B)

  • !( x = a && y != b && z != c )

    ( x != a || y = b || z = c)

  • bool : BoolCheck.cpp

    // BoolCheck.cpp#include using namespace std;// ----- --------------------------int main(){ bool b1, b2, b3, b4; b1= true; b2= false; b3= (3>1); b4= (3

  • BoolCheck.cpp

    b1 = 1b2 = 0b3 = 1b4 = 0Size of bool is : 1 byteSize of b1 is : 1 byte

  • 1. A~Za~z2.0~93. + $ - @ ! 128 'a''A' '$' '+' '5'

  • ASCIIASCIIAmerican Standard Code for Information Interchangea 01100001b 01100010Q = 8110 = 010100012

  • char Ch = 'f' ;// ch f

    cout

  • BIG-5Unicode ()16

  • escape sequences\ (escape character)'\b' '\t' '\\'

  • Escape Sequencecode\n00001010(newline)\t00001001(tab)\b00001000(back)\f00001100(form feed)\r00001101(return)\\01011100 \\00100111 \00100010 \000000000

  • (string) "

    "c+b=""""The answer is: ""\nCorrect""C:\\C++\\Examples\\File.cpp

    '\0'

  • byte "c+b="// 5 ""// 5 "Good! "// 6 "\nAnswer: " //9 (bytes)

  • "Good!"

    Good!\01 byte1 byte1 byte1 byte1 byte1 byte

  • ""

    \02 bytes2 bytes1 byte

  • // StringSize.cppStringSize.cpp// ----- --------------------------int main(){ cout

  • cout
  • 3.7

    ~ (complement)>&AND^XOR|OR

  • ~ (complement)>&AND^XOR|OR

  • AND AND

    a01000001b01100010a & b01000000

  • XOR XOR

    a01000001b01100010a ^ b00100011

  • OR OR

    a01000001b01100010a | b01100011

  • ~ ()

    a01000001~a10111110

  • XN X >> N; XN 0 2 2

    a10011010a >> 200100110

  • X

  • BitOp.cpp

    // BitOp.cpp#include using namespace std;// ----- ------------------------------------int main(){ short Ia = 0x5678, Ib = 0x12ff; char C1 = 'A' ; cout