1 / 36

Chapter 9 - 格式化輸出與輸入

Chapter 9 - 格式化輸出與輸入. Outline 9.1 Introduction 9.2 資料串流 9.3 使用 printf 的格式化輸出 9.4 顯示整數 9.5 顯示浮點數 9.6 Printing Strings and Characters 9.7 其他轉換指定詞 9.8 顯示字串和字元 9.9 使用欄位寬度和精確度 9.10 在 printf 格式控制字串中使用旗標 9.11 使用 scanf 的格式化輸入. 9.1 Introduction. scanf printf

akasma
Télécharger la présentation

Chapter 9 - 格式化輸出與輸入

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 9 -格式化輸出與輸入 Outline 9.1 Introduction 9.2 資料串流 9.3 使用 printf 的格式化輸出 9.4 顯示整數 9.5 顯示浮點數 9.6 Printing Strings and Characters 9.7 其他轉換指定詞 9.8 顯示字串和字元 9.9 使用欄位寬度和精確度 9.10 在printf格式控制字串中使用旗標 9.11 使用 scanf的格式化輸入

  2. 9.1 Introduction • scanf • printf • gets, puts, getchar, putchar • <stdio.h>

  3. 9.3 使用printf 的格式化輸出 • printf • 精確的格式化輸出 • 轉換指定詞、旗標(flags)、欄位寬度(field widths)、精確度 • 功能 • 捨入、數字對齊、靠左/右對齊、精確插入字元常數、指數格式表示浮點數、十六進位格式、固定欄位寬度和精確度 • printf 格式 • printf(format-control-string, other-arguments); • format control string: 描述輸出格式 • other-arguments: 對應到 format-control-string中的每一種轉換指定 • 每個轉換指定者是以%開始,並且用轉換指定詞作結束

  4. 9.4 顯示整數

  5. 455 455 455 -455 32000 2000000000 707 455 4294966841 1c7 1C7

  6. 9.5 顯示浮點數 • 浮點數 • 包含小數點 (33.5) • 指數表示法(和科學記號一樣) • 150.3科學記號 : 1.503 x 10² • 150.3指數表示法 : 1.503E+02 (E表示指數) • 使用 e或 E • 轉換指定詞 • f–在小數點前面至少顯示一位 • g (or G) -顯示沒有補上零的 f或e格式(1.2300變成 1.23) • 如果數值轉換成指數記號後,該數的指數小於-4或是大於指定的精確度 (預設值為6 ),則會以e (或 E)表示

  7. 9.5 顯示浮點數

  8. 1.234568e+006 1.234568e+006 -1.234568e+006 1.234568E+006 1234567.890000 1.23457e+006 1.23457E+006

  9. 9.6 顯示字串和字元 • c • 顯示字元(char) • 無法顯示字串 • s • 需要指向字元 的指標作為引數 • 顯示所有字元一直到NULL (‘\0’)為止 • 無法顯示字元 • 注意 • 字元常數使用單引號 ('z') • 字串常數使用雙引號 “z” (實際包含兩個字元‘z’和'\0')

  10. A This is a string This is a string This is also a string

  11. 9.7 其他轉換指定詞 • p • 顯示指標值(位址) • n • 儲存目前 printf已經輸出的字元個數 • 以指向整數的指標作為引數 • 不會顯示任何東西 • % • 顯示百分比符號 • %% • 每次 printf呼叫會傳回一個值 • 輸出字元個數或 • 若錯誤傳回一個負值

  12. 9.7 其他轉換指定詞

  13. The value of ptr is 0012FF78 The address of x is 0012FF78 Total characters printed on this line: 38 This line has 28 characters 28 characters were printed Printing a % in a format control string

  14. 9.8 使用欄位寬度和精確度 • 欄位寬度 • 如果欄位寬度大於要顯示的資料,預設值為靠右對齊 • 如果要顯示的資料大於欄位寬度,則會自動增加欄位寬度 • 負號佔用一個位數 • 代表欄位寬度的整數加在%和轉換指定詞之間 • %4d–代表欄位寬度為4

  15. 9.8 使用欄位寬度和精確度 • 精確度 • 不同的資料型態有不同的意義 • 格式 • %.精確位數+轉換指定詞 • %.3f

  16. 9.8 使用欄位寬度和精確度 • 欄位寬度和精確度 • 可以一起使用 • %欄位寬度+.精確位數+轉換指定詞 %5.3f • 負的欄位寬度– left justified • 正的欄位寬度– right justified • 精確度必須為正數 • 可使用整數運算式來指定欄位寬度和精確度 • 加入星號 (*) 取代欄位寬度和精確度的位置 • Example: printf( "%*.*f", 7, 2, 98.736 );

  17. 1 12 123 1234 12345 -1 -12 -123 -1234 -12345

  18. Using precision for integers 0873 000000873 Using precision for floating-point numbers 123.945 1.239e+002 124 Using precision for strings Happy Birth

  19. 9.9 在printf格式控制字串中使用旗標 • 旗標(Flags) • 輔助格式化輸出 • 直接在%右邊放置旗標 • 可以同時使用多個旗標

  20. hello 7 a 1.230000 hello 7 a 1.230000

  21. 786 -786 +786 -786

  22. 547 -547

  23. 02623 0x593 0X593 1427 1427.00

  24. +00000452 000000452

  25. 9.10 字元常數和脫序字元的顯示

  26. 9.11 使用 scanf的格式化輸入

  27. 9.11 使用 scanf的格式化輸入

  28. 9.11 使用 scanf的格式化輸入 • scanf • 格式化輸入 • 功能 • 輸入所有型別的資料 • 輸入指定的字元 • 跳過某些指定的字元 • scanf格式 • scanf(format-control-string, other-arguments); • format-control-string • 描述輸入格式 • other-arguments • 指向變數的指標,這些變數會用來儲存輸入的資料

  29. 9.11 使用 scanf的格式化輸入 • 掃描集 • 由中括號[]括起來,並且前面加上百分比符號的字元 • 掃描輸入的字元,找出屬於此掃描集的字元 • 每次找到一個字元時,存放到掃描集對應的引數中 • 當遇到一個不包含在掃描集中的字元時,便停止輸入字元 • 反掃描集 • 使用 ^: [^aeiou] • 掃描不在掃描集中的字元 • 跳過某些指定的字元 • 將要跳過的字元放入格式控制字串中 • 或使用 *(設定禁止字元) • 跳過任何型態的字元,而不儲存

  30. Enter seven integers: -70 -70 070 0x70 70 70 70 The input displayed as decimal integers is: -70 -70 56 112 56 70 112

  31. Enter a string: Sunday The input was: the character "S" and the string "unday"

  32. Enter string: ooeeooahah The input was "ooeeooa"

  33. Enter a string: String The input was "Str"

  34. Enter a six digit integer: 123456 The integers input were 12 and 3456

  35. Enter a date in the form mm-dd-yyyy: 11-18-2003 month = 11 day = 18 year = 2003 Enter a date in the form mm/dd/yyyy: 11/18/2003 month = 11 day = 18 year = 2003

More Related