C/C++ - 字符输入输出和字符确认

Similar documents
C/C++语言 - 分支结构

C/C++ 语言 - 循环

C/C++ - 函数

C/C++语言 - 运算符、表达式和语句

C/C++ - 文件IO

C/C++语言 - C/C++数据

C/C++程序设计 - 字符串与格式化输入/输出

CC213

C

新・解きながら学ぶC言語

新・明解C言語入門編『索引』

CC213

C 语言 第八讲 字符输入输出与输入确认 张晓平 武汉大学数学与统计学院 2017 年 4 月 12 日

新版 明解C言語入門編

C C

C/C++ - 字符串与字符串函数

C

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

Microsoft Word - 第3章.doc

C 1

_汪_文前新ok[3.1].doc

untitled

プログラムの設計と実現II

14A 0.1%5% 14A 14A

(Chi)_.indb

穨_2_.PDF

FY.DOC

新・解きながら学ぶJava

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

nooog

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

CC213

第二章 臺灣客家族群民間信仰之發展

C/C++ - 数组与指针

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

南華大學數位論文

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

第 2 頁 (a) 擔 任 機 場 擴 建 統 籌 辦 總 監 的 首 席 政 府 工 程 師 職 位 第 3 點 ) ; (b) 擔 任 ( 機 場 擴 建 統 籌 辦 ) 的 首 長 級 丙 級 政 務 官 職 位 ; 以 及 (c) 擔 任 總 助 理 ( 機 場 擴 建 統 籌 辦 ) 的

cgn

39898.indb

穨ecr2_c.PDF

電腦相關罪行跨部門工作小組-報告書

i

发展党员工作手册

i

中医疗法(上).doc

香 港 舞 蹈 總 會    北 京 舞 蹈 學 院

一、

(As at 28

Microsoft Word - EDB Panel Paper 2016 (Chi)_finalr

二零零六年一月二十三日會議

马太亨利完整圣经注释—雅歌

厨房小知识(四)

妇女更年期保健.doc

小儿传染病防治(上)

<4D F736F F D B875B9B5A448ADFBBADEB27AA740B77EA4E2A5555FA95EAED6A641ADD75F2E646F63>

女性青春期保健(下).doc

避孕知识(下).doc

孕妇饮食调养(下).doc

禽畜饲料配制技术(一).doc

中老年保健必读(十一).doc

i

怎样使孩子更加聪明健康(七).doc

i

untitled


Ps22Pdf

Microsoft Word - CPE考生使用手冊 docx

C/C++ - 结构体、共用体、枚举体

新版 明解C++入門編

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

509 (ii) (iii) (iv) (v) 200, , , , C 57

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

尿路感染防治.doc

Microsoft Word - MP2018_Report_Chi _12Apr2012_.doc

南華大學數位論文

李天命的思考藝術

皮肤病防治.doc

性病防治

中国南北特色风味名菜 _一)

全唐诗24

untitled

心理障碍防治(下).doc

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

BC04 Module_antenna__ doc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

家庭用药指南(九).doc

第五条 非公开发行股票预案应当包括以下内容:

I

高 职 计 算 机 类 优 秀 教 材 书 目 * 序 号 书 号 (ISBN) 书 名 作 者 定 价 出 版 / 印 刷 日 期 ** 配 套 资 源 页 码 计 算 机 基 础 课 计 算 机 应 用 基 础 刘 升 贵 年 8 月

戲劇手冊.PDF


榫 卯 是 什 麼? 何 時 開 始 應 用 於 建 築 中? 38 中 國 傳 統 建 築 的 屋 頂 有 哪 幾 種 形 式? 40 大 內 高 手 的 大 內 指 什 麼? 42 街 坊 四 鄰 的 坊 和 街 分 別 指 什 麼? 44 北 京 四 合 院 的 典 型 格 局 是 怎 樣 的

3.1 num = 3 ch = 'C' 2

第一部分

untitled

儿童用药守则(上).doc

Untitiled

Transcription:

C/C++

Table of contents 1. 2. getchar() putchar() 3. (Buffer) 4. 5. 6. 7. 8. 1

2

3

1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read, increment line count 6 if a word has been read, increment word count 7 read next character 4

// while (( ch = getchar ())!= STOP ) {... } 5

// while (( ch = getchar ())!= STOP ) {... } 5

getchar() STOP 6

7

7

c!= && c!= \n && c!= \t! isspace (c) // # include <ctype.h> c == c == \n c == \t isspace (c) // # include <ctype.h> 8

( inword ) 1 inword 1 inword 0 1 // pseudo code 2 if c is not a whitespace and inword is false 3 set inword to true and count the word 4 if c is a white space and inword is true 5 set inword to false 9

i // wordcnt.c: # include <stdio.h> # include <ctype.h> # include < stdbool.h> # define STOP int main ( void ) { char c, prev ; long n_chars = 0L; int n_lines = 0, n_words = 0, p_lines = 0; bool inword = false ; printf (" Enter text ( to quit ):\n"); prev = \n ; while ((c = getchar ())!= STOP ) { n_chars ++; if ( c == \ n ) n_lines ++; 10

ii } if (! isspace (c) &&! inword ) { inword = true ; n_words ++; } if ( isspace ( c) && inword ) inword = false ; prev = c; } if ( prev!= \ n ) p_lines = 1; printf (" characters = % ld, words = %d, lines = %d, ", n_chars, n_words, n_lines ); printf (" partial lines = %d\n", p_lines ); return 0; 11

Enter text ( to quit ): Reason is a powerful servant but an inadequate master. characters = 56, words = 9, lines = 3, partial lines = 0 12

getchar() putchar()

getchar() putchar() 1 // echo.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char ch; 6 while (( ch = getchar ())!= # ) 7 putchar (ch); 8 return 0; 9 } 13

getchar() putchar() 1 // echo.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char ch; 6 while (( ch = getchar ())!= # ) 7 putchar (ch); 8 return 0; 9 } Hello world Hello world I am happy I am happy 13

(Buffer)

(Buffer) HHeelllloo wwoorrlldd [ enter ] II aamm hhaappppyy [ enter ] 14

(Buffer) 15

echo.c # # 16

EOF C getchar EOF (End Of File ) scanf() EOF EOF stdio.h # define EOF ( -1) 17

EOF -1 getchar() 0-127 0-255 -1 18

EOF // echo_eof.c # include <stdio.h> int main ( void ) { int ch; while (( ch = getchar ())!= EOF ) putchar (ch); return 0; } 19

EOF Hello world [ enter ] Hello world [ Ctrl +D] 20

EOF EOF Unix Ctrl+D Ctrl+Z Ctrl+Z 21

EOF // Linux or Mac OS Hello world [ enter ] Hello world [ Ctrl +D] 22

8.4

8.4 23

1-100 1 2 3 y yes n no 24

i // guess. c -- an inefficient and faulty number - guesser # include <stdio.h> int main ( void ) { int guess = 1; printf (" Pick an integer from 1 to 100. I will "); printf (" try to guess it.\ nrespond with "); printf (" a y if my guess is right and with "); printf ("\ nan n if it is wrong.\n"); printf ("Uh... is your number %d?\n", guess ); while ( getchar ()!= y ) printf ("Well, then, is it %d?\n", ++ guess ); printf ("I knew I could do it!\n"); return 0; } 25

Pick an integer from 1 to 100. I will try to guess it. Respond with a y if my guess is right and with an n if it is wrong. Uh... is your number 1? n Well, then, is it 2? Well, then, is it 3? n Well, then, is it 4? Well, then, is it 5? y I knew I could do it! 26

n Why? 27

n Why? 27

n Why? n n!= y Well, then, is it 2? \n \n!= y Well, then, is it 3? 27

while while ( getchar ()!= y ) { printf ("Well, then, is it %d?\n", ++ guess ); while ( getchar ()!= \n ) continue ; // skip rest of input line } no no way n 28

Pick an integer from 1 to 100. I will try to guess it. Respond with a y if my guess is right and with an n if it is wrong. Uh... is your number 1? n Well, then, is it 2? no Well, then, is it 3? no sir Well, then, is it 4? forget it Well, then, is it 5? y I knew I could do it! 29

30

1 int n; scanf ("%d", &n); // get first value while ( n >= 0) // detect out - of - range value { // process n scanf ("%d", &n); // get next value } 31

2 q scanf() scanf ("%d", &n) == 1 32

int n; while ( scanf ("%d", &n) == 1 && n >= 0) { // process n } while 33

scanf() getchar() 34

1000 1000 35

i /* checking. c -- validating input */ # include <stdio.h> # include < stdbool.h> // validate that input is an integer int get_int ( void ); // validate that range limits are valid bool bad_ limits ( int begin, int end, int low, int high ); // calculate the sum of the squares of the integer a through b double sum_squares ( int a, int b); int main ( void ) { const int MIN = -1000; 36

ii const int MAX = +1000; int start ; int stop ; double answer ; printf (" This program computes the sum of the " " squares of integers in a range.\ n" " The lower bound should not be less than " " -1000 and \ nthe upper bound should not " " be more than +1000.\ nenter the limits " "( enter 0 for both limits to quit ):\ n" " lower limit : "); start = get_ int (); 37

iii printf (" upper limit : "); stop = get_int (); while ( start!=0 stop!= 0) { if ( bad_limits ( start, stop, MIN, MAX )) printf (" Please try again.\n"); else { answer = sum_ squares ( start, stop ); printf (" The sum of the squares of the integers "); printf (" from %d to %d is %g\n", start, stop, answer ); } printf (" Enter the limits ( enter 0 for both " " limits to quit ):\n"); printf (" lower limit : "); 38

iv start = get_ int (); printf (" upper limit : "); stop = get_int (); } printf (" Done.\n"); return 0; } int get_int ( void ) { int input ; char ch; while ( scanf ("%d", & input )!= 1) { while (( ch = getchar ())!= \n ) putchar ( ch); // dispose of bad input printf (" is not an integer.\n"); 39

v printf (" Please enter an integer value, "); printf (" such as 25, -178, or 3: "); } return input ; } double sum_ squares ( int a, int b) { double total = 0; int i; for (i = a; i <= b; i ++) total += i * i; return total ; } bool bad_ limits ( int begin, int end, int low, int high ) { 40

vi bool not_ good = false ; if ( begin > end ) { printf ("%d isn t smaller than %d.\n", begin, end ); not_ good = true ; } if ( begin < low end < low ) { printf (" Values must be %d or greater.\n", low ); not_ good = true ; } if ( begin > high end > high ) { printf (" Values must be %d or less.\n", high ); 41

vii } not_ good = true ; } return not_ good ; 42

get_int() int input while while 43

bad_limits() 44

i This program computes the sum of the squares of integers in a range. The lower bound should not be less than -1000 and the upper bound should not be more than +1000. Enter the limits ( enter 0 for both limits to quit ): lower limit : 1q upper limit : q is not an integer. Please enter an integer value, such as 25, -178, or 3: 3 The sum of the squares of the integers from 1 to 3 is 14 Enter the limits ( enter 0 for both limits to quit ): lower limit : q q is not an integer. Please enter an integer value, such as 25, -178, or 3: 3 45

ii upper limit : 5 The sum of the squares of the integers from 3 to 5 is 50 Enter the limits ( enter 0 for both limits to quit ): lower limit : 4 upper limit : 3q 4 isn t smaller than 3. Please try again. Enter the limits ( enter 0 for both limits to quit ): lower limit : q is not an integer. Please enter an integer value, such as 25, -178, or 3: 0 upper limit : 0 Done. 46

main() get_int() badlimits() sum_squares() 47

C is 28 12. 4 C 1 i 2 s 3 4 2... 48

C getchar() while (( ch = getchar ())!= \n ) putchar (ch); 49

C 42 scanf() 50

C %c 4 char %s 4 2 %d 4 10 +2 = 42 int %f 42 float 51

Enter the letter of your choice : a. advice b. bell c. count q. quit 52

53

Enter the letter of your choice : a. advice b. bell c. count q. quit a[ enter ] Buy low, sell high. Enter the letter of your choice : a. advice b. bell c. count q. quit b[ enter ] Enter the letter of your choice : a. advice b. bell c. count q. quit c[ enter ] Count how far? Enter an integer : two [ enter ] two is not an integer. 54

Please enter an integer value, such as 25, -178, or 3: 5[ enter ] 1 2 3 4 5 Enter the letter of your choice : a. advice b. bell c. count q. quit q Bye. 55

i /* menu. c -- menu techniques */ # include <stdio.h> char get_choice ( void ); char get_first ( void ); int get_int ( void ); void count ( void ); int main ( void ) { int choice ; while ( ( choice = get_choice ())!= q ) { switch ( choice ) { case a : printf (" Buy low, sell high.\n"); break ; case b : putchar ( \a ); break ; case c : count (); break ; default : printf (" Program error!\n"); break ; } 56

ii } } printf (" Bye.\n"); return 0; void count ( void ) { int n, i; printf (" Count how far? Enter an integer :\n"); n = get_int (); for ( i = 1; i <= n; i ++) printf ("%d\n", i); while ( getchar ()!= \ n ) continue ; } char get_choice ( void ) 57

iii { } int ch; printf (" Enter the letter of your choice :\ n"); printf ("a. advice b. bell \n"); printf ("c. count q. quit \n"); ch = get_first (); while ( (ch < a ch > c ) && ch!= q ) { printf (" Please respond with a, b, c, or q.\n"); ch = get_first (); } return ch; char get_first ( void ) { int ch; 58

iv } ch = getchar (); while ( getchar ()!= \ n ) continue ; return ch; int get_int ( void ) { int input ; char ch; while ( scanf ("%d", & input )!= 1) { while (( ch = getchar ())!= \n ) putchar ( ch); // dispose of bad input printf (" is not an integer.\n"); printf (" Enter an integer value,\n"); printf (" such as 25, -178, or 3: "); } 59

v } return input ; 60

EOF 61

1-100 z 50 z 50 50 100 75 75 50 75 62 62

float 0 63

Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit a Enter first number : 22. 4 Enter second number : one one is not a number. Please enter a number, such as 2.5, -1.78 E8, or 3: 1 22. 40 + 1. 00 = 23. 40. 64

Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit d Enter first number : 1 Enter second number : 0 Enter a number other than 0: 0.2 1.00 / 0.20 = 5.00. Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit q Bye. 65