The return of scanf The number of fields successfully converted and assigned int a =1, b =2, c =3; int n = scanf("%d %d %d", &a, &b, &c); printf("%d\n

Similar documents
CC213

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

3.1 num = 3 ch = 'C' 2

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

CC213

Microsoft Word - CPE考生使用手冊 docx

Guide to Install SATA Hard Disks

C++ 程式設計

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

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

2/80 2

C 1

科学计算的语言-FORTRAN95

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

C/C++ 语言 - 循环

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

untitled

K7VT2_QIG_v3

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

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

51 C 51 isp 10 C PCB C C C C KEIL

C

untitled

Windows XP

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

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

ebook14-4

untitled

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

Open topic Bellman-Ford算法与负环

穨control.PDF

Microsoft Word - template.doc

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

Microsoft PowerPoint - Lecture7II.ppt

Microsoft Word - ACL chapter02-5ed.docx

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

untitled

c_cpp

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

高中英文科教師甄試心得

ENGG1410-F Tutorial 6

2015年4月11日雅思阅读预测机经(新东方版)

untitled

Microsoft PowerPoint _代工實例-1

FY.DOC

概述

untitled

(Load Project) (Save Project) (OffLine Mode) (Help) Intel Hex Motor

SuperMap 系列产品介绍

IP505SM_manual_cn.doc

Microsoft PowerPoint - Eisenstein_ABET_Presentation_Beijing_Oct_2007-Chinese.ppt [兼容模式]

nooog

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

四川省普通高等学校

<4D F736F F D D342DA57CA7DEA447B14D2DA475B57BBB50BADEB27AC3FEB14DA447B8D5C344>

AL-M200 Series

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

Microsoft Word - Final Exam Review Packet.docx

(Microsoft Word - Motion Program \270\305\264\272\276\363 \307\245\301\366 \271\327 \270\361\302\367.doc)

Microsoft PowerPoint - ATF2015.ppt [相容模式]

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

三維空間之機械手臂虛擬實境模擬

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

我 把 我 觉 得 书 中 有 用 的 技 巧 还 有 时 间 安 排 都 穿 插 在 这 篇 攻 略 里 了, 所 以 只 要 买 韩 国 人 编 的 模 拟 卷 就 行, 难 度 设 置 还 比 较 好 ( 就 是 上 面 列 的 第 三, 第 四 本 ) 7. 还 有 就 是 由 Lin Lo

农银人寿发[2013]102号-4 农银寰宇至尊高端医疗保险条款

VHDL(Statements) (Sequential Statement) (Concurrent Statement) VHDL (Architecture)VHDL (PROCESS)(Sub-program) 2

C语言的应用.PDF

1505.indd

chap07.key

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

软件测试(TA07)第一学期考试

Microsoft PowerPoint - L17_Inheritance_v4.pptx

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

Logitech Wireless Combo MK45 English

蔡 氏 族 譜 序 2

untitled

TC35短信发送程序设计

CH01.indd

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>

RAID RAID 0 RAID 1 RAID 5 RAID * ( -1)* ( /2)* No Yes Yes Yes A. B. BIOS SATA C. RAID BIOS RAID ( ) D. SATA RAID/AHCI ( ) SATA M.2 SSD ( )

2 2 3 DLight CPU I/O DLight Oracle Solaris (DTrace) C/C++ Solaris DLight DTrace DLight DLight DLight C C++ Fortran CPU I/O DLight AM

提纲 1 2 OS Examples for 3

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

Microsoft Word doc

Microsoft Word - CX VMCO 3 easy step v1.doc

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

(baking powder) 1 ( ) ( ) 1 10g g (two level design, D-optimal) 32 1/2 fraction Two Level Fractional Factorial Design D-Optimal D

ch_code_infoaccess

穨1-林聖欽.doc

untitled

中山大學學位論文典藏

,,,,, : (5), (6),,, , 335, 126 : (61% ) 61 (18% ) 68 (21% ) (72% ) 18 (14% ) 17 (14% ) ,,,,,, 0. 3,, (7) (8)

國家圖書館典藏電子全文

华恒家庭网关方案

Microsoft PowerPoint - STU_EC_Ch08.ppt

Liao Mei-Yu Professor, Department of Chinese Literature, National Cheng Kung University Abstract Yao Ying was a government official in Taiwan for more

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Transcription:

Introduction to Computer and Program Design Lesson 2 Functions, scanf and EOF James C.C. Cheng Department of Computer Science National Chiao Tung University

The return of scanf The number of fields successfully converted and assigned int a =1, b =2, c =3; int n = scanf("%d %d %d", &a, &b, &c); printf("%d\n", n ); printf("%d, %d, %d", a, b, c); In the case of an input failure before any data could be successfully read, EOF is returned. 2

Multiple input int x = 0; while( scanf("%d", &x)!= EOF ){ EOF: end of file usually EOF is defined as (-1) It is a signal to tell the system that no more data can be read from a input source. In Windows, press Ctrl+Z and Enter In UNIX, Linux and Mac OS X, press Ctrl+D 3

Abnormal Input int x = 0; while( scanf("%d", &x)!= EOF ){ Please input a non-numeric line, ex: %T@#$@KI A non-stop loop occurred! Why? How to fix the problem? 4

Standard I/O Buffer Buffer: A memory block used to temporarily hold data while it is being moved from one place to another. There three I/O memory buffers in the computer FILE* stdin : For the standard input device (generally, a keyboard). FILE* stdout : For the standard output device (generally, the screen). FILE* stderr : For output error and warning messages to the standard output device FILE* is a pointer to point a stream I/O buffer 0x 26E81E00 FILE* stdin address data 26E81E00 04 A5 00 10 26E81E04 0B 00 56 4F 26E81E08 06 FF 00 00 26E81E0C B4 00 C7 FF 5

The Reading Mechanism of scanf -- 1 In the format string of scanf: Format specifiers (%): A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from stdin and stored in the locations pointed by the additional arguments. The basic usage of format specifier: %[*][l]type where type can be c, d, e, E, f, g, G, o, s, x, X EX: %d Read but ignored %*type int x = 0; while( scanf("%*d%d", &x)!= EOF ){ printf("%d\n", x); //? 6

The basic data types There are 13 basic data types in C Name Size (byte) Range char 1-128 to 127 unsigned char 1 0 to 255 short 2-32768 to 32767 unsigned short 2 0 to 65535 int 4-2 31 to 2 31-1 unsigned int 4 0 to 2 32-1 long 4-2 31 to 2 31-1 unsigned long 4 0 to 2 32-1 int64, long long 8-2 63 to 2 63 1 unsigned int64 8 0 to 2 64-1 float 4 ±(1.175494351e -38 to 3.402823466e 38 ) double 8 ±(2.2250738585072014e -308 to 1.7976931348623158e 308 ) long double 12 in DevC++, 8 in MSC The same as double In C++, bool represents boolean value, true or false. The size of bool depends on compiler, 1 byte in most case. 7

The type of format specifier type Qualifying Input Type of argument c Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char * d Decimal integer: Number optionally preceded with a + or int * - sign. Floating point: Decimal number containing a decimal e,e,f,g,g point, optionally preceded by a + or - sign and optionally float * folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 o Octal integer. int * s String of characters. This will read subsequent characters until a whitespace is found (whitespace char * characters are considered to be blank, newline and tab). u Unsigned decimal integer. unsigned int * x,x Hexadecimal integer. int * 8

The Reading Mechanism of scanf -- 2 It starts after one string line inputted. a string which the last key is \n In the format string of scanf: White-space characters, WC: blank (' '); tab ('\t'); or newline ('\n'). 一連串的 WC 可視為一個 WC 一個在 format string 的 WC 代表它可吸收零個以上的 WC 輸入 輸入數值型態的資料時, 前面可以鍵入零個以上的 WC. The non-wcs in format, except the % scanf 會從 stdin 依輸入順序讀取字元, 然後依序與 format string 的非 WC 字元比較. 若比較相同, 輸入字元 discarded, 繼續下一字元. 若比較不相同,scanf 失敗並結束,stdin 剩下的資料包含現在比較失敗的字元都會留在 stdin 9

Standard Input in C How to clear the stdin? getchar() read a character from stdin Usage: #include <stdio.h> int getchar( void ); Returns the input character Ex: int c = getchar(); Using getchar() to clear stdin int x=0; scanf("%d", &x); printf("%d\n", x); { int c; while((c=getchar())!='\n'&&c!=eof); // clear stdin printf("press enter to exit"); getchar(); // Waiting an Enter key 10

EOF in Windows Ctrl + z (^Z) in Windows: ^Z 要搭配 \n 才有意義按下 ^Z 之後可一連串按下任意鍵再按下 \n 皆等同按下 ^Z\n, 如 ^Z!@#!@$!$!@\n 等同 ^Z\n 甚至 ^Z 後面接一堆空格再加 \n 也等同 ^Z\n 他要能被當成 EOF, 之前除了一連串的 \n 或 ^Z, 不可按其它任何鍵 否則按下 \n 後是代表 Substitute 這個字元,ASCII code 是 26 當要成為 EOF 前,^Z 不產生任何 ASCII code 下列程式碼, ^Z\n 需按下兩次才能結束程式, 為什麼? int x=0, y=0; a white space while( scanf(" %d%d", &x, &y)!= EOF ){ printf("%d, %d\n", x, y); 11

EOF in Windows DO NOT let the last character of the format string be a WC int x=0; a white space while( scanf("%d ", &x)!= EOF ){ printf("%d\n", x); 12

Exercises 請根據指示輸入資料, 並解釋為何會出現畫面上的結果? int x=0, y=0; while( scanf("%d,%d", &x, &y)!= EOF ){ printf("%d, %d\n", x, y); int x=0, y=0; while( scanf("%d, %d", &x, &y)!= EOF ){ printf("%d, %d\n", x, y); 請輸入下列資料 : 1,2 3,4 5, 6 7, 8 9 10 int x=0, y=0; while( scanf("%d,%d", &x, &y)!= EOF ){ printf("%d, %d\n", x, y); 13

Function declaration Syntax: return type + function name + (parameters); where the parameters are: 1. nothing or void 2. typename1, typename2, typenamen 3. the names of parameters are not necessary Function name must be unique, except C++. Ex: int F1(void); int F2(); void F3(void); void F3(int); // In C++, OK, but error in C int F4(int, int, char); or int F4(int x, int y, char c); 14

Function declaration In C++, the function declaration must be placed before calling int main( void ){ int MyF(int); // declaration int x = 0; printf("%d\n", MyF(x)); return 0; int MyF(int n){ return n+1 ; // Function definition 15

Function definition Function definition Syntax: return type + function name + (parameters){ where the name of the parameters not be ignored. int MyF(int n){ return n+1 ; Function definition can not be placed in any block (except C++). 16

Function definition Declaration, definition and invocation. Which of following are correct? In C, all of them are correct implicit declaration and the function is assumed to return int In C++, the third is failed int MyF (int a); int MyF(int a){ return a+1; int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; int MyF (int a){ return a+1; int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; int MyF (int a); int MyF (int a); int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; int MyF (int a){ return a+1; int MyF (int a){ return a+1; int MyF (int a); int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; int MyF (int a); int MyF (int a){ return a+1; int MyF (int a){ return a+1; int main( void ){ int x = MyF (1); printf("1+1 = %d\n", x); return 0; 17

Function definition Default argument Only C++ provides default argument for functions A default argument is a value given in the declaration that the compiler automatically inserts if you don t provide a value in the function call. A default argument cannot be placed before non-default argument The declaration of a default argument either in global function declaration or in function definition. Local function declaration can has its own default argument list. void F1(int a, int b =10, int c = 2){ void F2(int a, int b =10, int c){ // Compiler error void F3(int a, int b =10, int c=5); // Global function declaration void F3(int a, int b =10, int c=5){ // Compiler error int main(){ F1(1, 2, 3); // a = 1; b= 2; c =3 F1(1); // a = 1; b= 10; c =2 void F1(int a, int b =20, int c = 30); // Local function declaration F1(1); // a = 1; b= 20; c =30 F1(1,,3); // Compiler error 18

練習題 1. 設計一個 function, 名稱為 CheckN int CheckN(int n); 若 n > 0 且任兩個連續的 digit 相加皆相同, 則 return 任兩個連續的 digit 之和, 反 之 return 0 10<=n<20 則 return 兩個 digit 之和,n<10 則 return0 注意, 不可用 字串處理方式 2. 設計一個 function, 名稱為 TileN int TileN(int RoomW, int RoomH, int TileW, int TileH); RoomW 及 RoomH: 分別代表房間的寬及長 ( 公分 ) TileW 及 TileH : 分別代表地磚的寬及長 ( 公分 ) 假設地磚不可分割, 所有地磚排列方式需一至, 則 TileN 會 return 該房間可貼的 最多地磚數量 3. 設計一個 function, 名稱為 IsPrime int IsPrime(int n); If n = ab, a <= b. Assuming that a >. n. 根據右列的理論來判斷 n 是否為質數 Then, n = ab >= aa > n causes contrary. Therefore, a <= n 若 n 為質數,return 1, 反之 return 0 19

練習題 4. 設計一個 function, 名稱為 ProductDigit unsigned long ProductDigit(unsigned long n); 給一個正整數 n, 請找一個最小的正整數 k( 即回傳值 ), 使得 k 所有 digit 的乘積會等於 n 若 k 不存在則回傳 -1 注意 overflow 問題! ex: n = 10 則 k = 25; n = 126 則 k = 279 ; n = 26 則 k = -1 5. 設計一個 function, 名稱為 PrimeSum void PrimeSum(int n); 給一個正整數 n, 若 n 可以被許多不重複的質數加總, 則列出這些質數之加總過程, 由大至小 答案可能不只一種, 列出其中一種即可 若不存在這樣的組合, 則只能顯示 X ex: n = 26 則顯示 13+11+5; n = 16 則顯示 13+3 或者 11+5 n = 6 則顯示 X 20