2 電子書-程式語言-10小時學c++ 語言

Click here to load reader

Transcript of 2 電子書-程式語言-10小時學c++ 語言

10 C

Sep.22,1997

10 C []

1997 6 3

1

C Turbo C C

page 2 4 10 13 21 32 40 46 55

2

C

C Turbo C

C / Turbo C Turbo C Turbo C PATH PATH=C:\TC;C:\DOS;...... TC Turbo C PATH DOS TC Turbo C TC (Edit)(Compile)(Link)(Debug) (File)... CD C TC Del BS (C ) F3 F10 File Load Load File Name *.C * ? TC 3

Enter F2 F10 File Save F10 File Write to _ Enter Turbo C TC (pass 1) (pass 2)(.exe) PC Ctrl + F9 F10 Run Run TC TC TC TC Alt + F5 F10 Run User screen TC Turbo C Alt + X F10 File Quit Turbo C TC Verify NONAME.C not saved. Save? (Y/N) Y N New Name

4

C C hello.c 1#include 2main() 3{ 4 5} Hello! printf("Hello!");

#include (prototype)(struct)(constant) C (declear) C INCLUDE\*.H #include INCLUDE\stdio.h stdio.h (standard I/O) printf() .h #include #include main() main() C int main() int main(int) void main(void),

5

{ } main() { } printf("Hello!"); C (;) C (space)(tab) main printf C printf() PrintF() printf printf() TC printf printf Ctrl + F1 Help Help TC Help printf: formatted output to stdout int printf(const char *format, ...); Prototype in stdio.h Print formats a variable number of arguments according to the format, and sends the output In the event of error, it returns EOF. See also ecvt puts fprintf scanf putc vprintf printf printf #include stdout byte EOF ecvt,fprintf,putc, puts,scanf,vprintf stdout printf

to stdout. Returns the number of bytes output.

6

TC Ctrl + F1 TC Help printf int printf(const char *format, ...); const

const char *format ... );

format printf format printf( "()" ,

C (") %d %d 10 () %d

arith.c 1#include 2void main(void) 3{ 4 5 6 7 8} 8 + 2 = 10 8 - 2 = 6 8 * 2 = 16 8 / 2 = 4 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); printf("%d - %d = %d\n", 8 , 2 , 8-2 ); printf("%d * %d = %d\n", 8 , 2 , 8*2 ); printf("%d / %d = %d\n", 8 , 2 , 8/2 );

7

printf("%d + %d = %d\n",

8

,

2

,

8+2 );

() 3 %d 8 , 2 , 8+2 8 + 2 = 10 () \n \n 8 + 2 = 108 - 2 = 68 * 2 = 168 / 2 = 4

C ============ + * / ============ ============ ++ -% ============ ( ) ===================== ===================== 1 1 ===================== 6 2 8 2 ========== ++i --i ========== i 1 i 1 0 ==========

=====================

============== 4 + 2 4 - 2 4 * 2 4 / 2 ============== i++ i-4 % 2 ============== ==============

==========

8

C ** 2 ** 3 2 3 ^ 2 ^ 8 2 8 C C pow() pow( 2 , 3 ) 2 3 C ( ) C [ ] { } ( )C = = = = = 62 ( 1 + 2 * ( 3 + 4 ) ) * 5 - 6 * 7 / 2 + 8 ( 1 + 2 * ( ( 15 75 75 7 ) ) * 5 - 6 * 7 / 2 + 8 ) * 5 - 6 * 7 / 2 + 8 42 21 / 2 + 8 + 8

(Comments) Keyin () C /* */ C /* */ /* */ " C++ //.

comments.c or comments.cpp 9

1#include 2void main(void) 3{

/* prototype : printf() */

// main program

4/* */ 5/* printf("%d + %d = %d\n", 8 , 2 , 8+2 ); 6/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); 7 8 9*/ 10} // end of program printf("%d * %d = %d\n", 8 , 2 , 8*2 ); printf("%d / %d = %d\n", 8 , 2 , 8/2 ); // division */

10

(Nested Comments) nestcom0.c 1#include 2void main(void) 3{ 4/* */ 5/* 6 8 9 10*/ 11} printf("%d + %d = %d\n", 8 , 2 , 8+2 ); */ printf("%d * %d = %d\n", 8 , 2 , 8*2 ); printf("%d / %d = %d\n", 8 , 2 , 8/2 ); 7/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); /* prototype : printf() */

/* */ Compile 5 /* 10 */ 5 10 7 /* 7 */ 7 Turbo C 5 /* 7 */ 10 */ Compile F10 Options Compiler Source Nested comments Off On nestcom1.c printf(); nestcom2.c 11

Off

printf(); /* */

nestcom3.c /* */ nestcom1.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11} printf("%d + %d = %d\n", 8 , 2 , 8+2 ); printf("%d - %d = %d\n", 8 , 2 , 8-2 ); printf("%d * %d = %d\n", 8 , 2 , 8*2 ); printf("%d / %d = %d\n", 8 , 2 , 8/2 ); /* 8 - 2 = 6 /* 8 / 2 = 4 /* */ /* prototype : printf() */ /* 8 + 2 = 10 */ */ */ /* 8 * 2 = 16 */

nestcom2.c 1#include 2void main(void) 3{ 4/* */ 5 6/* printf("%d + %d = %d\n", 8 , 2 , 8+2 ); */ 7/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); */ 8/* printf("%d * %d = %d\n", 8 , 2 , 8*2 ); */ 9/* printf("%d / %d = %d\n", 8 , 2 , 8/2 ); */ 10 11} /* 8 - 2 = 6 /* 8 / 2 = 4 /* prototype : printf() */ /* 8 + 2 = 10 */ */ */ /* 8 * 2 = 16 */

12

nestcom3.c 1#include 2void main(void) 3{ 4/* */ 5/* 6 7 8 9 10*/ 11} printf("%d + %d = %d\n", 8 , 2 , 8+2 ); printf("%d - %d = %d\n", 8 , 2 , 8-2 ); printf("%d * %d = %d\n", 8 , 2 , 8*2 ); printf("%d / %d = %d\n", 8 , 2 , 8/2 ); /* 8 - 2 = 6 /* 8 / 2 = 4 /* prototype : printf() */ /* 8 + 2 = 10 */ */ */ /* 8 * 2 = 16 */

13

C (constant)(variable) 0,1,2 1 10 type Char, C (A Z a z)( _ ) 0 9 32 TC if switch goto char void short typedef auto interrupt sizeof _AH _AL14

int, long, float, double

etc.

else default return int near struct const

for case long far union static cdecl

do break float unsigned huge enum

while continue double

register signed

volatile extern pascal _cs asm _CS

_AX

_BX _CX _DX _SI

_BH _CH _DH _DI

_BL _CL _DL _BP

_ds _es _ss _SP

_DS _ES _SS

1000 32767 2147483647 (float) C 1 [, 2 [,...]] ; int long char char NumberOfStudent; MoneyInBank, interest; EndOfString; OneStudentName[9]; /* */ /* */ /* */ /* */ /* */

float RateOfInterest;

(initial value) 1= 1 [, 2= 2 [,...]] ; int long char char NumberOfStudent=60; MoneyInBank=1000000L; EndOfString='\0'; OneStudentName[9]="";15

/* */ /* */ /* in % */ /* */ /* */

float RateOfInterest=5.0;

1000000 L 1000000 C TC TC TC L L char byte ' 'A' 'a' '0' '\101''\141''\60' '\0'

'\x41''\x61''\x30''\x0' '\0' "123""ABC""abc" 4 bytes strlen() ( '\0' ) int StringLen=strlen("123"); #include StringLen 3 strlen() = = (); 16

C = C == PI = 3.1415926; r = 4; area = PI * r * r ; var.c #include void main(void) { } 10 + 2 = 12 10 - 2 = 8 10 * 2 = 20 10 / 2 = 517

int i,j; i = 10; j = 2; printf("%d + %d = %d\n", i , j , i+j ); printf("%d - %d = %d\n", i , j , i-j ); printf("%d * %d = %d\n", i , j , i*j ); printf("%d / %d = %d\n", i , j , i/j ); i = 20; j = 2; printf("%d + %d = %d\n", i , j , i+j ); printf("%d - %d = %d\n", i , j , i-j ); printf("%d * %d = %d\n", i , j , i*j ); printf("%d / %d = %d\n", i , j , i/j );

20 + 2 = 22 20 - 2 = 18 20 * 2 = 40 20 / 2 = 10

: (Global Variable): (Local Variable): (Static Variable):

18

printf() printf printf printf : printf("" , 1 , 2 , ... ); % \ % % % printf %c %d %ld %f %lf %Lf %s

\ '\x41' A 19

C Dec Hex \n \t \b \a \r \f \\ \' \" \xHH *%% 10 0x0A 9 0x09(1+8n) 8 0x08 7 0x07

13 0x0D 12 0x0C 92 0x5C \ 39 0x27 ' 34 0x22 " 0xHH 0xHH 37 0x25 %

% printf % %5d 5 %12ld 12 printf %8.3f 8 3 8 - ( 3 + 1 ) = 4 printf printf %-5d 5 5

20

(int)(long) % % + + w d w ld int long w

(float)(double)(long double) % % % + + + w . p f w . p lf w . p Lf p 6 float double long double

w

print.c 1#include 2void main(void) 3{21

4 5 6 7 8 9 10 11 12 13}

printf("|%ld|\n", printf("|%5ld|\n", printf("|%d|\n", printf("|%5d|\n", printf("|%-5d|\n", printf("|%f|\n", printf("|%9f|\n",

123456 ); 123456 ); 123 123 123 ); ); );

12.345 ); 12.345 );

printf("|%9.2f|\n", 12.345 ); printf("|%-9.2f|\n",12.345 );

|123456| |123456| |123| | 123| | 5 3 5 3 TC 6 9 9 2 9 2 |123 123456 32767 , %ld 5 TC

|12.345000| |12.345000| | 12.35| | |12.35

scanf() C scanf keyboard scanf scanf("" , & 1 , & 2 , ... ); scanf printf scanf printf scanf 22

%c %d %ld *%D %f %lf %Lf %s

%F %D scanf() printf() scanf (pointer) i C &i &i i i () char c; int i; long l; float f; double d; long double ld; char str[80]; int a[100]; long b[100]; float c[100]; /* () */ /* /* /* */ */ */ /* str[80] str */ /* a[100] /* b[100] /* c[100] a b c */ */ */ /* /* /* /* /* */ */ */ */ */ /* c /* i /* l /* f /* d &c &i &l &f &d */ */ */ */ */

/* */

/* ld &ld */

var.c scanf io.c 23

1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11 12 13} Enter 2 integers:20 4 Now, I find that ... 20 + 4 = 24 20 - 4 = 16 20 * 4 = 80 20 / 4 = 5 i = 20 , j = 4 printf("Enter 2 integers:"); scanf("%d %d", &i, &j ); printf("Now, I find that ...\n"); printf("%d + %d = %d\n", i , j , i+j ); printf("%d - %d = %d\n", i , j , i-j ); printf("%d * %d = %d\n", i , j , i*j ); printf("%d / %d = %d\n", i , j , i/j ); int i,j;

/* i j &i,&j */

scanf ((tab)) scanf i j Enter scanf scanf gets() gets gets atoi() atol() atof()

24

()() f2c.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10} Enter the temperature in F : 100 100 degrees in F is 37 degrees in C. printf("Enter the temperature in F : "); scanf("%d", &f ); c = ( f - 32 ) * 5 / 9 ; /* */ int f,c; /* f &f */

printf("%d degrees in F is %d degrees in C.", f, c);

()() C = ( F - 32 ) * 5 / 9 F = C * 9 / 5 + 32 "" age.c 1#include 2void main(void) 3{ 4 5 6 7 printf("Enter the age of you : "); scanf("%f", &years );25

/* years &years */

float years, days;

8 9 10}

days = years * 365.25 ; printf("You are %f days old.", days );

/* */

Enter the age of you : 28.5 You are 10409.625000 days old.

getche() scanf() Enter Enter getche() getche ch = getche(); getche() ch getche() ASCII code.c 1#include 2#include 3void main(void) 4{ 5 6 7 8 9 10}26

/* printf() */ /* getche() */ /* getche() */

char ch; ch = getche();

printf(" -- You typed %c.\n", ch ); printf("Character %c has ASCII code %d.\n", ch, ch );

A -- You typed A. Character A has ASCII code 65.

ASCII 9 %c ch %d ch

10

Help Help printf: formatted output to stdout int printf(const char *format, ...); Prototype in stdio.h Print formats a variable number of arguments according to the format, and sends the output In the event of error, it returns EOF. See also ecvt puts fprintf scanf putc vprintf stdout printf #include byte EOF

to stdout. Returns the number of bytes output. stdout

Help 27

type [flag] none + # f e g E G c s % p n X d i o u x

Format Specifiers % [flags] [width] [.prec] [F|N|h|l] type Format of Output signed decimal int signed decimal int unsigned octal int unsigned decimal int in printf = unsigned hexdecimal int lowercase; in scanf = hexadecimal int in printf = unsigned hexdecimal int floating point [-]dddd.ddd floating point [-]d.ddd e [+/-]ddd format e or f based on precision same as e except E for exponent same as g except E for exponent single character print characters till '\0' or [.prec] the % character pointer: near - YYYY; far - XXXX:YYYY stores count of characters written so far in the location pointed to by input argument What it Specifies right-justify, pad 0 or blank to left left-justify, pad spaces to right always begin with + or print sign for negative values only convert using alternate form: c,s,d,i,u o x or X no effect 0 prepended to nonzero arg 0x or 0X prepended to arg28

format [ ] d i o u x 16 () scanf 16 scanf 16 X 16 () f 314.159 e 3.14159e2 g f e E e E G g E c s '\0' % % p YYYY XXXX:YYYY n [] 0 + #

uppercase; in scanf = hexadecimal long

blank

c,s,d,i,u o 0 0 x,X 0x 0X

[width] [.prec] Modifier L F N h l l .n * none .0 n 0n *

e, E, f g or G

always use decimal point same as above but no trailing zeros

e,E,f g,G [] n * 0

Effect on Output at least n characters, blank-padded at least n characters, 0 left fill next argument from list is width Effect on Output default precision d,i,o,u,x e, E, f default precision no decimal point

0n 0

[.] e,E,f .0 d,i,o,u,x .n n .* F N h l l ( scanf) L

at most n characters next argument from list is precision How arg is Interpreted arg is far pointer arg is near pointer d,i,o,u,x,X arg is short int d,i,o,u,x,X arg is long int e, E, f, g, G (scanf only) e,E,f,g,G arg is long double arg is double

29

Help scanf: performs formatted input from stdin int scanf(const char *format, ...); Prototype in stdio.h Returns the number of input fields processed successfully. the format and places the results in the memory locations pointed to by the arguments. See also atof getc cscanf printf fscanf sscanf vsscanf stdin scanf #include It processes input according to

vfscanf vscanf

Help getch: gets character from console, no echoing getche: gets character from the console and int getch(void); int getche(void); Prototype in conio.h Both functions return the character read. Characters are available immediately - no buffering of whole lines. Special keys such as function keys and arrow keys are represented by a two character sequence: a zero character followed by the scan code for the key pressed. 30

getch getche #include getch getche 0

echoes to screen

See also

getpass kbhit getchar

cgets ungetch getc

cscanf putch

31

scanf Garbage In, Garbage Out ) ( C ( Flow Chart ) 6

32

___________ __________

__________ a, b, c __________ a, b, c a, b, c

a,b,c

F = 100; C =(F-32)*5/9;

Yes No

a>=b Yes

Yes No 33

No

c=0; c=5; a b

Yes No

c=5; c=0;

scanf("%d %d", a>=b Yes No &a,&b);

a>=b Yes b>=c Yes c=1;

No

No

34

a>=b Yes 35

No

a b a b a b a b a b C > < >= b a=b a b a= 0 No Yes if-else if() { 1 } else () { 1 } { 2 } { 2 } 2 1 max1.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11} Enter 2 integer : 7 5 The maximum of 7 and 5 is 7. printf("Enter 2 integer :"); scanf("%d %d", &i, &j); if( i > j ) max = i; else max = j; printf("The maximum of %d and %d is %d.\n", i, j, max ); int i, j, max; /* printf(),scanf() */ if-else if 39 if-else C else if-else else if-else 20 15 10 if-else if if( 1) { 1 } else if( 2) { 2 } else if( 3) { 3 } else if... else { n } ( 1) { 1 }; ( 2) { 2 }; ( 3) { 3 }; ... { n } Yes 1 40 No No Yes 2 Yes 3 ... Yes ... No No n police.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11 12 printf("Enter the speed = "); scanf("%d", &speed ); if( speed < 60 ) printf("Too slow, speed up!\n"); else if( speed < 90 ) printf("Good day, boss.\n"); else if( speed < 100 )41 /* printf(),scanf() */ int speed;13 14 15 16}printf("Too fast, slow down!\n"); else printf("I will give you a ticket!\n"); Enter the speed = 50 Too slow, speed up! Enter the speed = 80 Good day, boss. Enter the speed = 95 Too fast, slow down! Enter the speed = 120 I will give you a ticket! 12 8 13 9 if( speed < 100 ) printf("Too fast, slow down!\n"); else if( speed < 90 ) printf("Good day, boss.\n"); else if( speed < 60 ) printf("Too slow, speed up!\n"); else printf("I will give you a ticket!\n"); speed < 100 Too fast, slow down! I will give you a ticket! speed >= 100 42 60 Yes 60 No 60 90 Yes No 90 100 No 100 Yes 60 90 90 100 100 43 calc.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20} Enter number, operator, number 4 + 8 4.000000 + 8.000000 = 12.000000 Enter number, operator, number 5 * 7 5.000000 + 7.000000 = 35.000000 ^C } for(;;) { printf("Enter number, operator, number\n"); scanf("%f %c %f", &num1, &op, &num2); if( op == '+' ) printf("%f + %f = %f\n", num1, num2, num1+num2); else if( op == '-' ) printf("%f + %f = %f\n", num1, num2, num1-num2); else if( op == '*' ) printf("%f + %f = %f\n", num1, num2, num1*num2); else if( op == '/' ) printf("%f + %f = %f\n", num1, num2, num1/num2); float num1,num2; char op; /* printf(),scanf() */ + - * / 44 7 Ctrl + Break Ctrl + C C && AND exp1 && exp2 exp1 exp2 || OR exp1 || exp2 exp1 exp2 exp1 exp2 ! NOT !exp1 exp1 exp1 ( sum = 5 + 3 ) 8 ( ( sum = 5 + 3 ) tcc yes2.c yes.exe yes2.exe 48 for C for for for for ( ; ; ) { } for for 1. 2. { } 3. { } 2 for 49 for for ( i = 0 ; i < 100 ; i = i+1 ) printf("i = %3d\n", i ); for ( i = 0 , j = 0 ; i < 100 ; i = i+1 , j = j+2 ) printf("i = %3d j = %3d\n", i, j ); for for ( ; ; ) { } for 1. 2. { } 2 3. { } 50 for for for ( ; ; ) { } for for { } (delay) ( ; ) 51for ( ; ) ASCII ascii.c 1#include 2void main(void) 3{ 4 5 6 7 8} 32= 42=* 52=4 ... 252=* 253=* 254=* 255= 33=! 43=+ 53=5 34=" 44=, 54=6 35=# 45=55=7 36=$ 46=. 56=8 37=% 47=/ 57=9 38=& 48=0 58=: 39=' 49=1 59=; for( i = 32 ; i < 256 ; i = i+1 ) printf("%3d=%c\t", i, i ); int i; /* printf() */ ... ... ... ... for( i = 32 ; ======== i < 256 ; i = i+1 ) ======== ========= i = i + 1 i /= 2 i = i / 2 i = i - 1 i += 2 i = i + 2 i++ C i -= 2 i *= 2 i-- 1 i = i - 2 i = i * 2 += -= n! n!.c52 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11 12 13 14 15 16 17} Enter the value of n to compute n! : 5 n! = 5x4x3x2x1 = 120 } printf(" = %ld", fact); printf("Enter the value of n to compute n! : "); scanf("%d", &n ); printf("%d! = %d", n, n ); fact = n; for( n = n-1 ; n >0 ; n-- ) { fact *= n; printf("x%d", n); long fact; int n; /* printf(),scanf() */ n n! = n * (n-1) * (n-2) * ... * 2 * 1 fact n 5 9 n 5 10 fact = n; fact = 5 n n-1 4 n n-1 13 fact *= n fact = fact * n n 1 fact n fact n 5 4 5 n 5 5! = 5 5! = 553 1 5*4 2 5*4*3 3 5*4*3*2 4 5*4*3*2*1 5*4*3*2*1 4 3 2 1 1 5! = 5x4 5! = 5x4x3 5! = 5x4x3x2 5! = 5x4x3x2x1 5! = 5x4x3x2x1 = 120 for for for for (nest) ; ... for( ... ; ... ; ... ) { } } if ; ... ; ... for( ... ; ... ; ... ) ; ... {54 } } ... } } for(...;...;...) { for(...;...;...) { ... for(...;...;...) { ... } } ... for(...;...;...) { ... for(...;...;...) { for(...;...;...) { ... 55 99.c 1#include 2void main(void) 3{ 4 5 6 7 8 9 10 11 12} 1x1= 1 2x1= 2 3x1= 3 4x1= 4 5x1= 5 6x1= 6 7x1= 7 8x1= 8 9x1= 9 1x2= 2 2x2= 4 3x2= 6 4x2= 8 5x2=10 6x2=12 7x2=14 8x2=16 9x2=18 1x3= 3 2x3= 6 3x3= 9 4x3=12 5x3=15 6x3=18 7x3=21 8x3=24 9x3=27 1x4= 4 2x4= 8 3x4=12 4x4=16 5x4=20 6x4=24 7x4=28 8x4=32 9x4=36 1x5= 5 2x5=10 3x5=15 4x5=20 5x5=25 6x5=30 7x5=35 8x5=40 9x5=45 1x6= 6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 7x6=42 8x6=48 9x6=54 1x7= 7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 8x7=56 9x7=63 1x8= 8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 9x8=72 1x9= 9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 } for( i=1 ; i