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

Similar documents
C

C/C++ - 文件IO

新版 明解C言語入門編

C/C++ - 函数

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

CC213

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

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

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

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

C/C++ 语言 - 循环

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

CC213

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

untitled

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

C

新版 明解C++入門編

nooog

C 1

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

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

Ps22Pdf

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++;

数据结构与算法 - Python基础

C C

C/C++ - 数组与指针

C

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

FY.DOC

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

Microsoft Word - 第3章.doc

新・解きながら学ぶJava

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.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

untitled

untitled

c_cpp

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

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

C

Microsoft Word - CPE考生使用手冊 docx

extend

Microsoft PowerPoint - ds-1.ppt [兼容模式]

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

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

untitled

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

Ps22Pdf

CC213

untitled

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

Microsoft PowerPoint - C_Structure.ppt

5B_sasaki.pdf

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

ebook39-5

华恒家庭网关方案

Microsoft Word - 01.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

CHAPTER 1

untitled

Windows XP

Microsoft PowerPoint - 20-string-s.pptx

蔡 氏 族 譜 序 2

epub 33-8

立 志 于 打 造 最 贴 近 考 生 实 际 的 辅 导 书 计 算 机 考 研 之 数 据 结 构 高 分 笔 记 率 辉 编 著 周 伟 张 浩 审 核 讨 论 群 :

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

扉页

untitled

概述

C++ 程式設計

四川省普通高等学校

02

<D0A1B0E02E707562>

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

untitled

Microsoft Word doc

C语言的应用.PDF

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2


ebook8-30

untitled

例 度 讀 讀 不 不 來 念 來 了 讀 不 不 讀 不 讀行 利 了 說 更 了 讀

Generated by Unregistered Batch DOC TO PDF Converter , please register! 浙江大学 C 程序设计及实验 试题卷 学年春季学期考试时间 : 2003 年 6 月 20 日上午 8:3

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

LOVE IS OVER LOVE LOVE LOVE LOVE IS EVERYTHING LOVE LOVE LOVE LOVER'S QUEEN LYDIA MAYBE TOMORROW MEN'S TALK MY DEAR MY FRIEND MY OH MY MY SUMMER DREAM

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

余德浩诗词

Chapter 1 What is Programing Paradigm 1

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

第5章修改稿

ebook14-4

CHAPTER VC#

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

* RRB *

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

untitled

区 域 活 动 进 入 中 班 我 们 区 域 的 设 置 和 活 动 材 料 都 有 所 变 化, 同 时 也 吸 引 孩 子 们 积 极 的 参 与 学 习 操 作 区 的 新 材 料 他 们 最 喜 欢, 孩 子 们 用 立 方 块 进 行 推 理 操 作 用 扑 克 牌 进 行 接 龙 游

( 表 1) 學 校 基 本 資 料 學 校 類 型 新 竹 市 東 區 新 竹 國 小 班 級 數 55 校 址 新 竹 市 興 學 街 106 號 電 話 傳 真 網 址

Transcription:

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