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

Similar documents
C

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

Microsoft Word - CPE考生使用手冊 docx

CC213

C/C++ - 文件IO

C/C++ - 函数

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

C C

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

C/C++ 语言 - 循环

新版 明解C言語入門編

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

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

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

c_cpp

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

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

02

nooog

CC213

untitled

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

新版 明解C++入門編

C/C++ - 数组与指针

C

untitled

FY.DOC

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

C++ 程式設計

CC213

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

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("%

C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

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

untitled

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C 1

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

untitled

第3章.doc

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

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

untitled

untitled

IO

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

新・解きながら学ぶJava

Strings

3.1 num = 3 ch = 'C' 2

untitled

Microsoft PowerPoint - string_kruse [兼容模式]

untitled

untitled

CHAPTER VC#


6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

untitled

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

《C语言程序设计》教材习题参考答案

責任政府與責任社會公共行政改革的方向與重點 學術研討會紀要

Ps22Pdf

extend

全国计算机技术与软件专业技术资格(水平)考试

208 中 南 大 学 学 报 ( 社 会 科 学 版 ) 2013 年 第 19 卷 第 6 期 节 目 录 上 卷 一 所 载 篇 名, 乃 总 目 录 中 篇 名 之 误, 正 文 卷 一 收 录 篇 名 为 月 支 使 者 玄 觉 杜 凝 妻 灌 国 婴 女 独 狐 及 吕 卿 均 五 篇

C

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

2015年计算机二级(C语言)模拟试题及答案(四)

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


华恒家庭网关方案

ebook39-5

<4D F736F F D20B2C43032B3B920B8EAAEC6ABACBA41BB50AAEDA5DCA6A12E646F63>

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

概述

Microsoft Word - 第3章.doc

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

第7章-并行计算.ppt

2015年下半年全国教师资格笔试《地理学科知识与教学能力》备考指导

Computer Architecture

科学计算的语言-FORTRAN95

Strings

数据结构与算法 - Python基础

untitled

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

Microsoft Word - 09.數學 docx

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

Microsoft Word - chap13.doc

甄選入學howhow作印刷校過版

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft Word - 01.DOC

<4D F736F F D20AE61AC462DBFAFADB9AD70B565BB50BB73A740B1D0AED7>

ebook14-4

FZUBRIDGE

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

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

untitled

Eclipse C C++, or

1505.indd

epub 33-8

Transcription:

C/C++ /

Table of contents 1. 2. 3. 4. 1

i # include <stdio.h> # include <string.h> // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int size, letters ; char name [40]; printf ("Hi! What s your first name?\n"); scanf ("%s", name ); printf ("%s, what s your weight in kilograms?\n ", name ); 2

ii } scanf ("%f", & weight ); size = sizeof name ; letters = strlen ( name ); volume = weight / DENSITY ; printf ("Well, %s, your volume is %2.2 f cubic meters.\n", name, volume ); printf (" Also, your first name has % d letters,\ n", letters ); printf (" and we have % d bytes to store in it.\ n ", size ); return 0; 3

Hi! What s your first name? xiaoping xiaoping, what s your weight in kilograms? 60 Well, xiaoping, your volume is 0. 06 cubic meters. Also, your first name has 8 letters, and we have 40 bytes to store in it. 4

string " Once more you open the door!" 5

C C char # include <stdio.h> int main ( void ) { char greeting [6] = { H, e, l, l, o, \0 }; printf (" Greeting message : "); printf ("%s\n", greeting ); return 0; } 6

C++ C++ C C C C++ C++ string C++ string 7

// C style string # include <iostream > using namespace std ; int main () { char greeting [6] = " Hello "; cout << " Greeting message : "; cout << greeting << endl ; return 0; } // C++ string class # include <iostream > # include <string > using namespace std ; int main ( void ) { string greeting = " Hello "; cout << " Greeting message : "; cout << greeting << endl ; return 0; } 8

C C \0 \0 0 ASCII 0 \0 1 9

: array 10

: array char name [40]; 40 char 10

: array char name [40]; 40 char [] name [] 40 char 10

C \0 char greeting [10] = { H, e, l, l, 0, \0 }; 11

C \0 char greeting [10] = { H, e, l, l, 0, \0 }; char greeting [10] = " Hello "; 11

# include <stdio.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); return 0; } 12

# include <stdio.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); return 0; } What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 12

# include < iostream > # include < string > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; return 0; } 13

# include < iostream > # include < string > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; return 0; } What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 13

C \0 name scanf() name & name %s scanf() C++ cin 14

"x" x x "x" "x" x \0 15

C char name[40] = "Hello"; sizeof name name 40 strlen(name) name \0 5 C++ string name = "Hello"; sizeof name name.size() \0 5 16

# include <stdio.h> # include <string.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); printf (" Your name of % ld letters occupises % ld memory cells.\n", strlen ( name ), sizeof name ); printf (" PRAISE has % ld letters and occupies % ld memory cells.\n", strlen ( PRAISE ), sizeof PRAISE ); return 0; } 17

What s your name? Xiaoping Hello, Xiaoping. What a super marvelous name! Your name of 8 letters occupied 40 memory cells. PRAISE has 30 letters and occpied 31 memory cells. 18

i # include < iostream > # include < string > # include < cstring > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; cout << " Your name of " << name. size () 19

ii } << " letters occupies " << sizeof name << " memory cells." << endl ; cout << " PRAISE has " << strlen ( PRAISE ) << " letters and occupies " << sizeof PRAISE << " memory celss." << endl ; return 0; 20

What s your name? Xiaoping Hello, Xiaoping. What s a super marvelous name! Your name of 8 letters occupies 32 memory cells. PRAISE has 30 letters and occupies 31 memory celss. 21

string.h strlen C (1) printf scanfs stdio.h (2) strlen string.h 22

printf printf printf 23

sizeof strlen name = "Morgan" sizeof name : 40 strlen ( name ): 6 M o r g a n \0 24

sizeof strlen sizeof PRAISE : 29 strlen ( PRAISE ): 28 sizeof 25

sizeof sizeof ( float ) sizeof ( char ) sizeof name sizeof 2. 15 sizeof ( name ) sizeof (2.15) 26

// circle1.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; radius = 1; area = 3. 1415926 * radius * radius ; circum = 2 * 3. 1415926 * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 27

// circle2.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; float pi = 3. 1415926; radius = 1; area = pi * radius * radius ; circum = 2 * pi * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 28

// circle3.c: # include <stdio.h> # define PI 3. 1415926 int main ( void ) { float radius, circum, area ; radius = 1; area = PI * radius * radius ; circum = 2 * PI * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 29

// circle4.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; const float PI = 3. 1415926; radius = 1.; area = PI * radius * radius ; circum = 2 * PI * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 30

# define NAME value 31

# define NAME value C 31

# define NAME value C #define " " # define BEEP \a # define TEE T # define ESC \033 # define OOPS " Now you have done it!" 31

# define B = 20 32

# define B = 20 B = 20 20 c = a + B; c = a + = 20; 32

33

# include <stdio.h> # define MAX (a,b) ((a) > (b)? (a) : (b)) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } 33

# include <stdio.h> # define MAX (a,b) ((a) > (b)? (a) : (b)) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } max (1, 2) = 2 1.0 + max (1.1+2.2, 3.0) = 4.30 33

# include <stdio.h> # define MAX (a,b) (a) > (b)? (a) : (b) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } 34

# include <stdio.h> # define MAX (a,b) (a) > (b)? (a) : (b) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } max (1, 2) = 2 1.0 + max (1.1+2.2, 3.0) = 3.30 34

# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", (x) * (x)); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } 35

# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", (x) * (x)); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } the square of 3 is 9. the square of 3+2 is 25. 35

# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", x * x); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } 36

# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", x * x); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } the square of 3 is 9. the square of 3+2 is 11. 36

# include <stdio.h> # define PRINT (a) \ do { \ printf ("%s: %d\n", #a, a); \ printf ("%d: %d\n", a, a); \ } while (0) ; int main ( void ) { PRINT (3) ; PRINT (3+2) ; return 0; } 37

# include <stdio.h> # define PRINT (a) \ do { \ printf ("%s: %d\n", #a, a); \ printf ("%d: %d\n", a, a); \ } while (0) ; int main ( void ) { PRINT (3) ; PRINT (3+2) ; return 0; } 3: 3 3: 3 3+2: 5 5: 5 37

# include <stdio.h> # define X(n) x##n # define PXN (n) printf ("x"#n" = %d\n", x##n); int main ( void ) { int X (1) = 12; PXN (1) ; int X (2) = 24; PXN (2) ; int X (3) = 36; PXN (3) ; return 0; } 38

# include <stdio.h> # define X(n) x##n # define PXN (n) printf ("x"#n" = %d\n", x##n); int main ( void ) { int X (1) = 12; PXN (1) ; int X (2) = 24; PXN (2) ; int X (3) = 36; PXN (3) ; return 0; } x1 = 12 x2 = 24 x3 = 36 38

\ 39

\ # ## # x #x ## #define X(n) x##n X(2) x2. #define CONS(a,b) (a##e##b) CONS(2,3) 2e3. 39

const C90 const const int MONTHS = 12; MONTHS 40

1: %a p- %A P- %c %d %e e- %E E- %f %g %f %e %e -4 %G %f %E %E -4 41

2: %i %d %o %p %s %x 0-f %X 0-F 42

printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) 43

printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) printf( "You look great in %s\n", color ); 43

printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) printf( "You look great in %s\n", color ); 43

% %% printf (" Once more you open the door!\n"); printf ("%s%d\n", " area = ", area ); printf ("%d%% = %f\n", 30, 0.3) ; 44

%d // width.c: # include <stdio.h> # define N 1000 int main ( void ) { printf ("*%d*\n", N); printf (" *%2 d*\n", N); printf (" *%10 d*\n", N); printf (" *% -10 d*\n", N); printf (" *%010 d*\n", N); return 0; } 45

%d // width.c: # include <stdio.h> # define N 1000 int main ( void ) { printf ("*%d*\n", N); printf (" *%2 d*\n", N); printf (" *%10 d*\n", N); printf (" *% -10 d*\n", N); printf (" *%010 d*\n", N); return 0; } *1000* *1000* * 1000* *1000 * * 0000001000* 45

%d %d 46

%d %d %md m m m 46

%d %d %md m m m %-md m m m 46

%d %0 md m m 0 m 47

// flags.c: # include <stdio.h> int main ( void ) { printf ("%x %X %#x %#X\n", 31, 31, 31, 31) ; printf ("*%d*\n", 42) ; printf ("*% d*\n", 42) ; printf ("*% d*\n", -42); printf (" *%5 d*\n", 6); printf (" *%5.3 d*\n", 6); printf (" *%05 d*\n", 6); printf (" *%05.3 d*\n", 6); return 0; } 48

%d 1f 1F 0 x1f 0 X1F *42* * 42* * -42* * 6* * 006* *00006* * 006* 49

%d % d 50

%d % d %m.nd, %0md, %0m.nd %5.3d %05d %05.3d 0 0 50

%x, %#x, %X, %#X %x, %#x, %X, %#X %x 1f %X 1F %#x 0x1f %#X 0X1F 51

%f, %e, %E // floats.c: # include <stdio.h> int main ( void ) { const double RENT = 3852. 42; printf ("*%f*\n", RENT ); printf ("*%e*\n", RENT ); printf (" *%4.2 f*\n", RENT ); printf (" *%3.1 f*\n", RENT ); printf (" *%10.3 f*\n", RENT ); printf (" *%10.3 e*\n", RENT ); printf (" *%10.3 E*\n", RENT ); printf (" *%+4.2 f*\n", RENT ); printf (" *% -10.2 f*\n", RENT ); printf (" *%010.2 f*\n", RENT ); printf (" *%10. f*\n", RENT ); printf (" *%.4 f*\n", RENT ); return 0; } 52

%f, %e, %E * 3852. 990000* *3.852990 e +03* *3852.99* *3853.0* * 3852.990* * 3.853 e +03* * 3.853 E +03* *+3852.99* *3852.99 * * 0003852. 99* 53

%f, %e, %E %m.nf %m.ne %m.ne m n 54

%f, %e, %E %m.nf %m.ne %m.ne m n %. nf n 54

%f, %e, %E %m.nf %m.ne %m.ne m n %. nf n %m.f m 54

%s // strings.c: # include <stdio.h> # define WORD " Hello World!" int main ( void ) { printf (" *%2 s*\n", WORD ); printf (" *%15 s*\n", WORD ); printf (" *%4.5 s*\n", WORD ); printf (" *% -15.5 s*\n", WORD ); printf (" *%.5 s*\n", WORD ); return 0; } 55

%s // strings.c: # include <stdio.h> # define WORD " Hello World!" int main ( void ) { printf (" *%2 s*\n", WORD ); printf (" *%15 s*\n", WORD ); printf (" *%4.5 s*\n", WORD ); printf (" *% -15.5 s*\n", WORD ); printf (" *%.5 s*\n", WORD ); return 0; } * Hello World!* * Hello World!* * Hello * * Hello * * Hello * 55

%f, %e, %E %s 56

%f, %e, %E %s %ms m m m 56

%f, %e, %E %s %ms m m m %m.ns m n m n m n n 56

%f, %e, %E %s %ms m m m %m.ns m n m n m n n %. ns n 56

printf() # include <stdio.h> int main ( void ) { int bph2o = 100; int rv; rv = printf (" Hello \n"); printf (" the printf function printed % d character.\n", rv); return 0; } 57

printf() # include <stdio.h> int main ( void ) { int bph2o = 100; int rv; rv = printf (" Hello \n"); printf (" the printf function printed % d character.\n", rv); return 0; } 100 C is water s boiling point. the printf function printed 32 character. printf() 57

printf() %n printf() %n %n 58

printf() %n printf() %n %n // printf_n.c: # include < stdio.h> int main ( void ) { int c1, c2; printf (" Hello Wuhan % nuniversity!%n\n", &c1, & c2); printf ("c1 = %d, c2 = %d\n", c1, c2); return 0; } 58

printf() %n printf() %n %n // printf_n.c: # include < stdio.h> int main ( void ) { int c1, c2; printf (" Hello Wuhan % nuniversity!%n\n", &c1, & c2); printf ("c1 = %d, c2 = %d\n", c1, c2); return 0; } Hello Wuhan University! c1 = 12, c2 = 23 58

printf() scanf() printf() scanf() 59

printf() scanf() printf() scanf() scanf() & scanf() & 59

scanf() // input.c: # include <stdio.h> int main ( void ) { int age ; double weight ; char name [20]; printf (" Enter your name, age and weight :\n"); scanf ("%s", name ); scanf ("%d, %lf", &age, & weight ); printf ("%s: %d %f\n", name, age, weight ); return 0; } 60

scanf() Enter your name, age and weight : Xiaoming 23 100 Xiaoming : 23 100.000000 61

scanf() scanf() %c printf() %f %e %E %g %G float double scanf() float double l 62

scanf %c %d %e, %f, %g, %a %E, %F, %G, %A %i %o %p 63

scanf %s %u %x, %X 64

scanf % 65

scanf % * %*d digit %10s hh signed char unsigned char % hhd %hhu ll long long unsigned long long %lld %llu 65

scanf() %hd, %hi %ho, %hx, %hu %ld, %li %lo, %lx, %lu %le, %lf, %lg %Le, %Lf, %Lg short unsigned short long unsigned long double long double 66

scanf() %hd, %hi %ho, %hx, %hu %ld, %li %lo, %lx, %lu %le, %lf, %lg %Le, %Lf, %Lg short unsigned short long unsigned long double long double %d, %i, %o %x int %e, %f, %g float 66

scanf() 67

scanf() scanf ("%d, %d", &n, &m); 67

scanf() scanf ("%d, %d", &n, &m); 12, 23 12, 23 12, 23 12, 23 67

scanf ("%d and %d", &n, &m); 68

scanf ("%d and %d", &n, &m); 12 and 23 12 and 23 12 and23 68

%c scanf ("%d%d",&n,&m); scanf ("%d %d",&n,&m); 69

%c scanf ("%c",&ch) scanf (" %c",&ch) 70

scanf() scanf() 0 (end of file) EOF EOF stdio.h -1 71

printf() * i // varwidth.c: # include <stdio.h> int main ( void ) { unsigned width, precision ; int number = 256; double weight = 123. 5; printf (" What field width?\n"); scanf ("%d", & width ); printf (" The number is :%* d\n", width, number ); printf (" Now enter a width and a precision :\ n") ; scanf ("%d %d", & width, & precision ); 72

printf() * ii } printf (" Weight =%*.* f\n", width, precision, weight ); return 0; 73

printf() * What field width? 6 The number is: 256 Now enter a width and a precision : 8 3 Weight = 123.500 74

scanf() * scanf() * % 75

scanf() * // skip2.c: # include <stdio.h> int main ( void ) { int n; printf (" Please enter three integers :\n"); scanf ("%*d %*d %d", &n); printf (" The last integer was %d\n", n); return 0; } 76

scanf() * // skip2.c: # include <stdio.h> int main ( void ) { int n; printf (" Please enter three integers :\n"); scanf ("%*d %*d %d", &n); printf (" The last integer was %d\n", n); return 0; } Please enter three integers : 10 20 30 The last integer was 30 76