1 / 20

Chapter 3

Chapter 3. C++ 的文字資料型別. 資料型別. char 存放字元碼或較小的整數     佔 1 個 byte. 整數型別. 基 本 資 料 型 別. int 存放整數資料,佔 2 個 bytes short int  short long int  long. 浮點數型別. float 存放浮點數 double  存放倍精確度浮點數 long double  存放比 double 更大更精確的浮點數. 資料型別 ( 續 ). 資料型別的數值範圍. 資料型別 ( 續 ).

Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 C++的文字資料型別

  2. 資料型別 char 存放字元碼或較小的整數     佔1個byte 整數型別 基 本 資 料 型 別 int 存放整數資料,佔2個bytes short int  short long int  long 浮點數型別 float存放浮點數 double 存放倍精確度浮點數 long double 存放比double 更大更精確的浮點數

  3. 資料型別(續) • 資料型別的數值範圍

  4. 資料型別(續)

  5. 混合算術運算和型別轉換 • 標準轉換 • 任何char, unsigned char或short的運算都將暫時看成是int的型別來進行運算 • 任何unsigned short的運算都將暫時看成是unsigned的型別來進行運算 • 若一運算子有混合型別的運算元,則都將以其中最強的型別來進行運算 • int < unsigned < long < unsigned long < float < double < long double

  6. 混合算術運算和型別轉換(續) • 指定變換型別 • 由指定運算子左邊的變數型別來決定運算式的最終型別 • x=20  int ,y  float , y=x  y=20.0 • x=20.0  float, y  int, y=x  y=20 • 直接指定轉換型別 • x=20.0  float, y  int, y=int(x) • x=20.0  float, y  int, y=(int)x

  7. 混合算術運算和型別轉換(續) • 列舉型別 • enum color { red, orange, yellow, green, blue, brown, black, white }; • red=0, orange=1, yellow=2, green=3, blue=4, brown=5, black=6,white=7 • enum color { red=5, orange, yellow, green=44, blue, brown, black, white }; • red=5, orange=6, yellow=7, green=44, blue=45, brown=46, black=47,white=48

  8. 基本輸入輸出 • 輸入輸出串流 • 輸出串流 cout • iostream.h • 語法 • 用途:將資料顯示在螢幕上 • 例如:cout<<國文成績=<<chin<<\n; cout<<<< << … ; 變數 運算式 字串 特殊控制碼 : 變數 運算式 字串 特殊控制碼 :

  9. 基本輸入輸出(續) • 輸入串流 cin • iostream.h • 語法 • 用途:由鍵盤輸入資料存至變數中 • 例如: cin>>x; cin>>變數>>變數>>… ;

  10. 基本輸入輸出(續) • 標準輸入輸出 • 標準輸出 printf • stdio.h • 語法 • 用途:將資料顯示在螢幕上 • 例如:printf("國文成績=%f ",chin); printf(格式控制字串,其他的引數);

  11. 基本輸入輸出(續) • 標準輸入 scanf • stdio.h • 語法 • 用途:由鍵盤輸入資料存至變數中 • 例如:scanf("%f“,&chin) scanf(格式控制字串,其他的引數);

  12. 標準輸入輸出的轉換指定詞

  13. 標準輸入輸出的轉換指定詞

  14. 範例程式 • 寫出一程式,允許使用者鍵入一數值,然後,顯示這數的平方值與立方值 請輸入任意數值:10 10的平方為:100 10的立方為:1000    螢幕輸出 聯想到的指令cin及scanf 聯想到的指令cout及printf ※紅字代表輸入,黑、藍字代表  輸出

  15. 看圖說故事的程式撰寫方法 第一步:首先將C的程式基本架構藉由Editor撰寫完成 #include <> #include <conio.h> void main() { getch(); }

  16. 看圖說故事的程式撰寫方法(續) 第二步:思考螢幕顯示的佈局 請輸入任意數值:10 10的平方為:100 10的立方為:1000

  17. 看圖說故事的程式撰寫方法(續) 第三步:安排對應於螢幕顯示的指令及敘述 cout, cin  iostream.h x  int 請輸入任意數值:10 10的平方為:100 10的立方為:1000 cout<<請輸入任意數值; cin>>x; cout<< 10的平方為:; cout<< x*x; cout<< \n ; //換行 cout<< 10的立方為:; cout<< x*x*x;

  18. 看圖說故事的程式撰寫方法(續) 第四步:整合敘述 cout<<請輸入任意數值; cin>>x; cout<< 10的平方為:<< x*x<< \n ; //換行 cout<< 10的立方為:<< x*x*x;

  19. 看圖說故事的程式撰寫方法(續) 第五步:填空 iostream.h #include < > #include <conio.h> void main() { getch(); } int x; cout<<請輸入任意數值; cin>>x; cout<< 10的平方為:<< x*x<< \n ; //換行 cout<< 10的立方為:<< x*x*x;

  20. 這樣寫也可以得到相同輸出結果 #include <stdio.h> #include <conio.h> main() { int x; printf(請輸入任意數值); scanf(%d,&x); printf(10的平方為:%d \n,x*x); /*換行*/ printf(10的立方為:%d,x*x*x); getch(); }

More Related