第七章 複習題

1. 函數原型,函數定義,函數調用


2.

a. void igor()

b. float tofu(int x)

c. double mpg(double a,double b)

d. long summation(long ar[],int n)

e. double doctor(const char*str)

f.  void ofcourse(boss b)

g string plot(map *m)

 

3.void (int ar[], int n,int a)

{

 for (int i=0;i<n;i++)

ar[i] = a;

}

4. void(int *start, int*end, int a)

{

int *pt;

for (pt = start; pt != end; pt++)

*pt = a;

}

5 double max( const double ar[], int size)

{

double temp = 0;

for(int i = 0; i<size;i++)

{

if (temp < ar[i])

temp = ar[i];

}

return temp;

}

6.

將const限定符用於指針,以防止指向的原始數據被修改。程序傳遞基本類型(如int或double)時,它將按值傳遞,以便函數使用副本。這樣,原始數據得到保護。


8.

int replace(char *str, char c1, char c2)

{

  int count =0;

While(*str != ’\n’)

{

if ((*str)==c1)

{

*str = c2;

count++;}

str++;

}

return count;

}


9. 指向字符串“pizza”第一個字符p的指針; c


10. 傳遞值就是函數調用時產生一個這個結構對象的副本,並對副本引用或操作;傳遞地址就是對這個結構本身引用或操作。強者更安全,但當結構對象比較大時,後者效率會更高。


11.

int fun (const char * ch);

void judge(int (*fun) (const char *ch))


12

a. void show(applicant a)

{

cout << a.name << endl;

cout << a.credt_ratings[0] << a.credit_ratings[1] << a.credit[2];

}

b.

void show(applicant *a)

{

cout << a->name << endl;

cout << a->credt_ratings[0] << a->credit_ratings[1] << a->credit_ratings[2];

 }


13.

void (*p1) (applicant *a) = f1;

const char (*p2) (const applicant *a1, const applicant *a2) = f2;

typedef void (*p_fun1) (applicant *a);

p_fun1 ap[3] ;

typedef const char (*p_fun2) (const applicant *a1, const applicant *a2);

p_fun2 pa[5];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章