C/C++
Table of contents 1. 2. 3. 4. 5. 6. 7. 8. 1
C C (struct) C 2
C C (struct) C 2
i // book.c: # include < stdio.h> # define MAX_ TITLE 41 # define MAX_ AUTHOR 31 struct book { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; }; int main ( void ) { struct book book ; printf (" Please enter the book title.\ n"); gets ( book. title ); printf (" Please enter the author.\n"); 3
ii } gets ( book. author ); printf (" Now enter the price.\n"); scanf ("%f", & book. price ); printf ("%s by %s: %.2 f\n", book. title, book. author, book. price ); return 0; 4
Please enter the book title. C primer plus Please enter the author. Stephan Prata Now enter the price. 90 C primer plus by Stephan Prata : 90. 00 5
struct book { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; }; struct book 3 (member) 6
(structure declaration) struct book { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; }; 7
(structure declaration) struct book { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; }; char float 7
struct book struct book book ; book book C 8
9
typedef typedef typedef struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } Book ; 10
typedef typedef typedef struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } Book ; Book book ; 10
i // book1.c: # include < stdio.h> # define MAX_ TITLE 41 # define MAX_ AUTHOR 31 typedef struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } Book ; int main ( void ) { Book book ; printf (" Please enter the book title.\ n"); gets ( book. title ); printf (" Please enter the author.\n"); 11
ii } gets ( book. author ); printf (" Now enter the price.\n"); scanf ("%f", & book. price ); printf ("%s by %s: %.2 f\n", book. title, book. author, book. price ); return 0; 12
typedef struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } Book ; Book book ; Book book MAX_TITLE char MAX_AUTHOR char float book 13
Book int float Book Book book1, book2, * pbook ; 14
Book book ; struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } book ; 15
struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } book ; 16
Book library = { "C primer plus ", " Stephan Prata ", 80 }; 17
18
. 18
. book.price book price float book.price.title.author.price book 18
C99 Book price Book surprise = {. price = 20. 50}; Book gift = {. price = 40.50,. author = " Dennis M. Ritchie ",. title = " The C programming language " }; 19
i // manybook.h: # include < stdio.h> # define MAX_ TITLE 41 # define MAX_ AUTHOR 31 # define MAX_ BOOK 100 typedef struct { char title [ MAX_ TITLE ]; char author [ MAX_ AUTHOR ]; float price ; } Book ; 20
i // manybook.c: # include " manybook.h" int main ( void ) { Book book [ MAX_BOOK ]; int count = 0; int i; printf (" Enter the book title.\n"); printf (" Press [ enter ] at the start of a line to stop.\n"); while ( count < MAX_ BOOK && gets ( book [ count ]. title )!= NULL && book [ count ]. title [0]!= \0 ) { printf (" Enter the author.\n"); gets ( book [ count ]. author ); printf (" Enter the price.\n"); 21
ii } scanf ("%f", & book [ count ++]. price ); while ( getchar ()!= \n ) continue ; if ( count < MAX_ BOOK ) printf (" Enter the next title.\n"); } if ( count > 0) { printf (" Here is the list of your book :\ n"); for ( i = 0; i < count ; i ++) printf ("%s by %s: %.2 f\n", book [i]. title, book [i]. author, book [i]. price ); } else { printf ("No book? Too bad!\n"); } return 0; 22
Enter the book title. Press [ enter ] at the start of a line to stop. C primer plus [ enter ] Enter the author. Stephan Prata [ enter ] Enter the price. 80[ enter ] Enter the next title. C programing language [ enter ] Enter the author. Dennis Ritchie [ enter ] Enter the price. 40[ enter ] Enter the next title. 23
Here is the list of your book : C primer plus by Stephan Prata : 80. 00 C programing language by Dennis Ritchie : 40. 00 24
Book book [ MAX_BOOK ]; MAX_BOOK Book book Book 25
book [0]. price ; book [4]. title ; book [2]. title [4]; // 1 price // 5 title // 3 title // 5 26
John 27
i // friend.h # include < stdio.h> # define LEN 20 const char * msgs [5] = { " Thank your for the wonderful evening, ", " You certainly prove that a ", " is a special kind of guy. We must get together ", " over a delicous ", " and have a few laughs " }; typedef struct { char first [ LEN ]; char last [ LEN ]; } Name ; typedef struct { Name name ; 28
ii char favfood [ LEN ]; char job [ LEN ]; float income ; } Guy ; 29
i // friend.c # include " friend.h" int main ( void ) { Guy guy = { {" Ewen ", " Villard "}, " grilled salmon ", " personality coach ", 158812. 0 }; printf (" Dear %s, \n\n", guy. name. first ); printf ("%s%s.\n", msgs [0], guy. name. first ); printf ("%s%s\n", msgs [1], guy. job ); printf ("%s\n", msgs [2]) ; printf ("%s%s%s", msgs [3], guy. favfood, msgs [4]) ; if ( guy. income > 150000. 0) 30
ii } puts ("!!"); else if ( guy. income > 75000. 0) puts ("!"); else puts ("."); printf ("\n %40 s%s\n", " ", " See you soon, "); printf (" %40 s%s\n", " ", " John "); return 0; 31
Dear Ewen, Thank your for the wonderful evening, Ewen. You certainly prove that a personality coach is a special kind of guy. We must get together over a delicous grilled salmonand have a few laughs!! See you soon, John 32
guy. name. first 33
1. 2. C 3. 34
Guy * him ; him Guy john Guy him = & john ; 35
Guy * him ; him Guy john Guy him = & john ; & 35
fellow Guy fellow [5]; fellow[0] him fellow[0] him = & fellow [0]; 36
him fellow[0] 1. -> him -> income is fellow [0]. income if him == & fellow [0] him him->income him 2.. fellow [0]. income == (* him ). income * 37
him fellow[0] fellow [0]. income == (* him ). income == him -> income 38
1. 2. 3. 39
int, char, float, double 40
int, char, float, double 40
// student.h # include <stdio.h> # define LEN 50 typedef struct { char name [ LEN ]; int math, phys, chem ; } Student ; 41
# include " student.h" int total ( int math, int phys, int chem ); int main ( void ) { Student std = {" Zhang San ", 90, 85, 80}; printf (" Total score of %s is %d.\n", std.name, total ( std.math, std.phys, std. chem )); } int total ( int math, int phys, int chem ) { return math + phys + chem ; } 42
// student2.c # include " student.h" int total ( const Student *); int main ( void ) { Student std = {" Zhang San ", 90, 85, 80}; printf (" Total score of %s is %d.\n", std.name, total (& std )); } int total ( const Student * std ) { return std -> math + std -> phys + std -> chem ; } 43
// student3.c # include " student.h" int total ( const Student ); int main ( void ) { Student std = {" Zhang San ", 90, 85, 80}; printf (" Total score of %s is %d.\n", std.name, total ( std )); } int total ( const Student std ) { return std. math + std. phys + std. chem ; } 44
C n_data o_data o_data = n_data ; o_data n_data 45
Name name1 = {" Ruthie ", " George "}; Name name2 = name1 ; 46
C 47
i // names.h # include <stdio.h> # include <string.h> typedef struct { char first [20]; char last [20]; int letters ; } Name ; void getinfo ( Name *); void makeinfo ( Name *); void showinfo ( const Name *); 48
i // info.c # include " names.h" void getinfo ( Name * pname ) { puts (" Enter your first name."); gets ( pname -> first ); puts (" Enter your last name."); gets ( pname -> last ); } void makeinfo ( Name * pname ) { pname -> letters = ( int ) strlen ( pname -> first ) + ( int ) strlen ( pname -> last ); } void showinfo ( const Name * pname ) { 49
ii } printf ("%s %s, your name contains %d letters.\n", pname -> first, pname ->last, pname -> letters ); 50
i // main.c # include " names.h" int main ( void ) { Name person ; getinfo (& person ); makeinfo (& person ); showinfo (& person ); return 0; } 51
Enter your first name. Stepha Enter your last name. Prata Stephan Prata, your name contains 12 letters. 52
53
i // names.h # include <stdio.h> # include <string.h> typedef struct { char first [20]; char last [20]; int letters ; } Name ; Name getinfo ( void ); Name makeinfo ( Name ); void showinfo ( Name ); 54
i // info.c # include " names.h" Name getinfo ( void ) { Name name ; puts (" Enter your first name."); gets ( name. first ); puts (" Enter your last name."); gets ( name. last ); return name ; } Name makeinfo ( Name name ) { name. letters = ( int ) strlen ( name. first ) + ( int ) strlen ( name. last ); return name ; 55
ii } void showinfo ( Name name ) { printf ("%s %s, your name contains %d letters.\n", name. first, name.last, name. letters ); } 56
i // main.c # include " names.h" int main ( void ) { Name person ; person = getinfo (); person = makeinfo ( person ); showinfo ( person ); return 0; } 57
58
1. C 2. const 59
1. 2. 1. C 2. 60
61
i # include <stdio.h> # include <math.h> typedef struct { double val [3]; } Vec ; Vec add ( Vec a, Vec b) { Vec c; for ( int i = 0; i < 3; i ++) c. val [i] = a. val [i] + b. val [i]; return c; } Vec sub ( Vec a, Vec b) { Vec c; for ( int i = 0; i < 3; i ++) 62
ii c. val [i] = a. val [i] - b. val [i]; return c; } double dot ( Vec a, Vec b) { return a. val [0]* b. val [0] + a. val [1]* b. val [1] + a. val [2]* b. val [2]; } double norm ( Vec a) { return sqrt ( dot (a, a)); } void show ( Vec a) { 63
iii } printf (" <%.2f, %.2f, %.2f >\n", a. val [0], a. val [1], a. val [2]) ; 64
# include " vector.h" int main ( void ) { Vec a = {1.0, 1.0, 1.0}; Vec b = {1.0, 2.0, 3.0}; Vec c; c = add (a, b); printf ("a + b = "); show (c); c = sub (a, b); printf ("a - b = "); show (c); printf ("(a, b) = %.2 f\n", dot (a, b)); printf (" a = %.2 f\n", norm (a)); return 0; } 65
const 66
typedef struct { char first [20]; char last [20]; } Name ; typedef struct { char * first ; char * last ; } pname ; 67
typedef struct { char first [20]; char last [20]; } Name ; typedef struct { char * first ; char * last ; } pname ; 67
Name name1 = {" Stephan ", " Prata "}; pname name2 = {" Dennis ", " Ritche "}; printf ("%s %s", name1. first, name2. last ); 68
Name name1 40 pname name2 16 32 4 64 8 pname 69
Name boy ; pname girl ; puts (" Enter the last name of the boy "); scanf ("%s", boy. last ); puts (" Enter the last name of the girl "); scanf ("%s", girl. last ); // 70
boy boy last girl scanf() girl.last 71
malloc() 72
malloc()
malloc() malloc() malloc() 73
malloc() i // names.h # include <stdio.h> # include <stdlib.h> # include <string.h> typedef struct { char * first ; char * last ; int letters ; } Name ; void getinfo ( Name *); void makeinfo ( Name *); void showinfo ( const Name *); void cleanup ( Name *); 74
malloc() i // info.c # include " names.h" void getinfo ( Name * pname ) { char temp [81]; puts (" Enter your first name."); gets ( temp ); pname -> first = ( char *) malloc ( strlen ( temp ) +1) ; strcpy ( pname -> first, temp ); puts (" Enter your last name."); gets ( temp ); pname -> last = ( char *) malloc ( strlen ( temp ) +1) ; strcpy ( pname ->last, temp ); } void makeinfo ( Name * pname ) 75
malloc() ii { } pname -> letters = ( int ) strlen ( pname -> first ) + ( int ) strlen ( pname -> last ); void showinfo ( const Name * pname ) { printf ("%s %s, your name contains %d letters.\n", pname -> first, pname ->last, pname -> letters ); } void cleanup ( Name * pname ) { free ( pname -> first ); free ( pname -> last ); } 76
malloc() // main.c # include " names.h" int main ( void ) { Name person ; getinfo (& person ); makeinfo (& person ); showinfo (& person ); cleanup (& person ); return 0; } 77
malloc() malloc() malloc() free() cleanup() 78
(union) 79
typedef union { int digit ; double bigfl ; char letter ; } Hold ; int double char 80
3 hold Hold fit ; Hold save [10]; Hold * pu; fit double 8 save 10 8 hold 81
1. 2. 3. C99 Hold vala ; vala. letter = R ; Hold valb = vala ; Hold valc = {88}; Hold vald = {. bigfl = 118. 2}; 82
Hold fit ; fit. digit = 23; // 23 fit 2 fit. bigfl = 2.0; // 23 2.0 8 fit. letter = h ; // 2.0 h 1 83
-> Hold * pu; Hold fit ; pu = & fit ; x = pu -> digit ; // x = fit.digit fit. letter = A ; flnum = 3.2 * fit. bigfl ; // 84
typedef struct { char socsecurity [ 20];... } Owner ; typedef struct { char name [40]; char headquarters [ 40];... } LeaseCompany ; 85
typedef union { Owner owncar ; LeaseCompany leasecar ; } Info ; typedef struct { char make [15]; int status ; // 0 = 1 = Info info ;... } CarInfo ; 86
honda CarInfo honda.status 0 ( ) honda.info.owncar.socsecurity; honda.status 1 ( ) honda.info.leasecar.name. 87
(enumerated type) enum enum int int 88
typedef enum { red, orange, yellow, green, blue } Spectrum ; Spectrum color ; Spectrum Spectrum color Spectrum 89
int c; color = blue ; if ( color == yellow )... for ( color = red ; color <= blue ; color ++)... Spectrum 0 5 90
printf (" red = %d, orange = %d\n", red, orange ); red = 0, orange = 1 red 0 1 5 91
0, 1, 2 nina 3 enum kids { nippy, slats, skippy, nina, liz }; 92
typedef enum { low = 100, medium = 500, high = 2000 } Level ; typedef enum { cat, lynx = 10, puma, tiger } Animal ; cat 0 lynx puma tiger 10 11 12 93
enum red blue 0 1 94
enum i # include <stdio.h> # include <string.h> # include < stdbool.h> typedef enum { red, orange, yellow, green, blue, violet } Spectrum ; const char * colors [] = { " red ", " orange ", " yellow ", " green ", " blue ", " violet " }; # define LEN 30 int main ( void ) { char choice [ LEN ]; Spectrum color ; bool color_ is_ found = false ; puts (" Enter a color ( empty line to quit ):"); while ( gets ( choice )!= NULL && choice [0]!= \0 ) { 95
enum ii for ( color = red ; color <= violet ; color ++) { if ( strcmp ( choice, colors [ color ]) == 0) { color_ is_ found = true ; break ; } } if ( color_is_found ) switch ( color ) { case red : puts (" Roes are red."); break ; case orange : puts (" Poppies are orange."); break ; case yellow : puts (" Sunflowers are yellow."); break ; case green : puts (" Grass is green."); break ; case blue : puts (" Bluebells are blue."); break ; case violet : puts (" Violets are violet."); break ; } 96
enum iii } else printf ("I don t know about the color %s.\n", choice ); color_ is_ found = false ; puts (" Next color, please ( empty line to quit ): "); } puts (" Goodbye!"); return 0; 97
enum i Enter a color ( empty line to quit ): orange Poppies are orange. Next color, please ( empty line to quit ): blue Bluebells are blue. Next color, please ( empty line to quit ): red Roes are red. Next color, please ( empty line to quit ): sdf I don t know about the color sdf. Next color, please ( empty line to quit ): Goodbye! 98
typedef
typedef typedef #define #define typedef typedef typedef #define 99
typedef typedef unsigned char BYTE ; BYTE x, y [10], * z; unsigned char BYTE BYTE typedef 100
typedef typedef char * STRING ; STRING char STRING name, sign ; char * name, * sign ; 101
typedef # define STRING char *; STRING name, sign ; char * name, sign ; 102
typedef typedef typedef struct complex { float real ; float imag ; } COMPLEX ; COMPLEX struct complex typedef 103
typedef typedef typedef struct { double x; double y; } vector ; 104
typedef vector v1 = {3.0, 6. 0}; vector v2; v2 = v1; struct { double x; double y; } v1 = {3.0, 6. 0}; struct { double x; double y; } v2; v2 = v1; 105
typedef typedef typedef typedef char (* FRPTC ()) [5]; FRPTC 5 char 106
typedef typedef 107
int board [8][8]; //int int ** ptr ; // int int * risk [10]; // 10 int int (* rusk ) [10]; // 10 int int * oof [3][4]; // 3x4 int int (* uuf ) [3][4]; // 3X4 int int (* uof [3]) [4]; // 3 // 4 int 108
char * fump (); // char char (* frump ) (); // char char (* flump [3]) (); // 3 char 109
typedef int arr5 [5] typedef arr5 * p_ arr5 ; typedef p_ arr5 arrp10 [ 10]; arr5 togs ; //togs 5 int p_arr5 p2; //p2 5 int arrp10 ap; //ap 10 // 5 int 110
int * int * f ( int a, int b); 111
i # include <stdio.h> # include <stdlib.h> int * f( int a, int b); int main ( void ) { int * q = NULL ; printf (" Memory address of q = %p\n", q); q = f(1, 2); printf (" Memory address of q = %p\n", q); printf ("*q = %d \n", *q); return 0; } int * f( int a, int b) { int * p = ( int *) malloc ( sizeof ( int )); 112
ii } printf (" Memory address of p = %p\n", p); *p = a + b; printf ("*p = %d \n", *p); return p; 113
Memory address of q = ( nil ) Memory address of p = 0 x5627e8f93670 *p = 3 Memory address of q = 0 x5627e8f93670 *q = 3 114
int (* f) ( int a, int b); * 115
i int 116
i # include <stdio.h> # include <stdlib.h> int max ( int a, int b); int min ( int a, int b); int (*f) (int, int ); int main ( void ) { f = max ; printf (" The max value is %d \n", (*f)(1, 2)); f = min ; printf (" The min value is %d \n", (*f)(1, 2)); return 0; } int max ( int a, int b) 117
ii { return (a > b? a : b); } int min ( int a, int b) { return (a < b? a : b); } 118
The max value is 2 The min value is 1 119
i double 120
i // fun_ptr1.h # include <stdio.h> # include <stdlib.h> # include <math.h> void Add ( double a, double b) { printf (" %8.3 f + %8.3 f = %8.3 f\n", a, b, a + b) ; } void Sub ( double a, double b) { printf (" %8.3 f - %8.3 f = %8.3 f\n", a, b, a - b) ; } 121
ii void Mul ( double a, double b) { printf (" %8.3 f * %8.3 f = %8.3 f\n", a, b, a * b) ; } void Div ( double a, double b) { if ( fabs (b) < 1.e -12) { printf (" Denominator is nearly zero!\n"); exit ( EXIT_FAILURE ); } printf (" %8.3 f / %8.3 f = %8.3 f\n", a, b, a / b) ; } 122
i // fun_ptr1.c # include " fun_ptr1.h" int main () { void (* mathop []) ( double, double ) = {Add, Sub, Mul, Div }; int choice ; double a = 15.0, b = 3. 0; printf (" Enter choice : 0 for add ; 1 for substract \n"); printf (" 2 for multiply ; 3 for division \n"); scanf ("%d", & choice ); if ( choice > 3) return 0; 123
ii } (* mathop [ choice ])(a, b); return 0; 124
i double 125
i // fun_ptr2.h # include <stdio.h> # include <stdlib.h> # include <math.h> double Add ( double a, double b) { return a + b; } double Sub ( double a, double b) { return a - b; } double Mul ( double a, double b) { 126
ii return a * b; } double Div ( double a, double b) { if ( abs (b) < 1.e -12) { printf (" Denominator is nearly zero!\n"); exit ( EXIT_FAILURE ); } return a / b; } double domath ( double a, double b, double (* mathop )( double, double ) ) { return (* mathop )(a, b); } 127
iii 128
i // fun_ptr2.c # include " fun_ptr2.h" int main ( void ) { printf (" Add gives : %8.3 f\n", domath (10., 2., Add )); printf (" Sub gives : %8.3 f\n", domath (10., 2., Sub )); printf (" Mul gives : %8.3 f\n", domath (10., 2., Mul )); printf (" Div gives : %8.3 f\n", domath (10., 0., Div )); return 0; } 129