第七章:函数--c++的编程模块 学习笔记、习题答案(6)
来源:互联网 发布:中国黄金行情软件 编辑:程序博客网 时间:2024/06/05 19:12
第七章:函数--------c++的编程模块
第一部分:学习笔记
1.基本注意:
①c++函数的返回类型不能是数组。
②在c++中不指定参数列表时使用省略号
void say_bye(...);
③六合彩中奖概率的程序,可以避免中间结果超出范围:
<pre class="cpp" name="code">long double result = 1.0;for (n = numbers, p = picks; p > 0; n--,p--)result = result * n /p;
④当且仅当int*arr和int arr[]用于函数头或者函数原型中,他们的含义才相同,都意味着arr是一个int指针。
⑤
void show(const doublle ar[],int n);
ar指向的是常量数据,不能使用ar修改数据。
⑥
while (!(cin >> factor)) // bad input { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input; Please enter a number: "; }
处理不友好输入的一个好的处理方法
2.使用数组区间函数:
①指出数组的位置,和数组的长度
②指定元素区间,使用超尾的方法,即指向数组最后元素的下一个。
3.指针和const
①指针指向常量对象:
int age = 39;const int *pt = &age;
不能使用pt来更改age的值,pt的声明并不意味着它指向的值实际上是一个常量,而只是意味着对pt而言,这个值是常量,age = 34;pt还可以指向其他的地址:pt = &g;
② 常量指针指向常量对象(常量指针指的是:该指针不能再指向别的地址了,指向常量对象指的是该指针不能更改数据的值)
const float g = 9.80;const float* pe = &g;
对于这种情况,pe既不能更改g的值,也不能指向别的地址。
③ 不能讲const数据地址赋值给常规指针:
const float g = 1.63;float* pt = &g;
c++里禁止这种方法:因为pt可以更改const数据的值,这将会很荒谬。
⑤尽可能使用const
>这样可以避免由于无意间修改数据导致编程错误
>使用const指针是的函数能够处理const和非const实参,否则只能接受非const数据
⑥
int sloth = 3;int* const finger = &sloth
将const放在指针后面,意味着finger只能指向&sloth,不能修改其指向的地址,但允许它更改sloth的值。
极端情况下:
double trouble = 2.3;const doublle* const stick = &trouble;其中:stick只能指向trouble,也不能它来修改trouble的值。
4.函数和二维数组
①以二维数组为参数的函数原型:
int sum(int (*ar)[4], int size);
或者:
int sum(int ar[][4], int size);
②辨析(*ar)[4] 和 *ar[4];
前者指向一个由四个int组成的数组的指针
后者由四个int指针指向的数组
③
ar[r][c] == *(*(ar + r) + c);必须对ar执行两次解除引用才能得到数据。
5.函数与字符串
①字符串有内置结束符,所以不必讲字符串长度传递给函数。
②处理字符串字符的标准形式:
while (*str){ statements; str++;}
当str指向空值字符时,*str=0(空值字符的数子编码),从而结束循环。
③
while (n-- > 0)){ptr[n] = c;}
int i = 0;while (i < n){ptr[i++] = c;}从后往前赋值可以避免额外的变量开销。
6.函数与array对象
void (array<double, 4> da);void (array<double,4> *da);
7.函数指针
①函数的地址是存储其机器语言代码的内存的开始地址。
②要完成的三步:
>获取函数的地址:只要使用函数名即可
process(think)//think()为一个已经定义的函数
>声明函数指针
double pam(int);double (*pf)(int);
pf = pam;
double pam(int);double (*pf)(int);pf = pam;double x = pam(4);double y = pf(4);double z = (*pf)(4);
double pam(int);auto pf = pam;
第二部分:习题答案
7.1
//2014/12/17#include<iostream>using namespace std;double average(double,double);int main(){double x,y;do {cout << "input two number(if anyone is zero, quit!): ";cin >> x >> y;if (x == 0 || y == 0){cout << "quit!!!";break;}cout << "调和平均数是:" << average(x,y) << endl;} while (true);cin.sync();cin.get();return 0;}double average(double x, double y){return (2.0 * x * y / (x + y));}
7.2
//2014/12/17#include<iostream>using namespace std;const int N = 10;int input(double num[],int n);double average(const double num[],int n);void display(const double num[], int n);int main(){double num[N];int n = input(num, N);display(num,n);cout << "the average is " << average(num,n);cin.sync();cin.get();}int input(double num[],int n){double temp;int i;for (i = 0; i < N; i++){cout << "Enter value #" << (i + 1) << ";";cin >> temp;if (!cin){cin.clear();while (cin.get() != '\n')continue;cout << "Bad input; input process terminated." << endl;break;}else if (temp < 0){break;}num[i] = temp;}return i;}void display(const double num[], int n){for (int i = 0; i < n; i++){cout << "num#" << i << ":" << num[i] << endl;}}double average(const double num[],int n){double sum = 0.0;for(int i = 0; i < n; i++)sum += num[i];return sum /n ;}
7.3
//2014/12/17#include<iostream>using namespace std;struct box {char maker[40];float height;float width;float length;float volume;};void display(box);double volume(box*);int main(){box b = {"wujunqi",1.2,1.3,1.6,0.0};display(b);b.volume = volume(&b);display(b);cin.sync();cin.get();}void display(box b){cout << b.maker << endl << b.height << endl << b.width << endl << b.length << endl << b.volume << endl;}double volume(box* b){return (b->height * b->width * b->length);}
7.4
//2014/12/17#include<iostream>using namespace std;long double probability(unsigned numbers, unsigned picks);int main(){long double r1 = probability(47,5);long double r2 = probability(27,1);long double r = r1 * r2;cout << "you have one chance in " << r << " of winning";cin.sync();cin.get();}long double probability(unsigned numbers, unsigned picks){long double result = 1.0;long n;long p;for (n = numbers,p = picks; p > 0; numbers--, p--){result = result * n / p;}return result;}
7.5
//2014/12/17#include<iostream>using namespace std;long digui(int n);int main(){int n;cout << "enter a more than zero number(q to quit!!); ";while (cin >> n){cout << n << "! = " << digui(n) << endl;cout << "enter a more than zero number(q to quit!!); ";}cin.sync();cin.get();}long digui(int n){if (n == 0 || n < 0)return 1;else{return n * digui(n - 1);}}
//2015/1/8#include <iostream>using namespace std;const int SIZE = 100;int fill_array(double *, int);void show_array(double*, int);void reverse_array(double*, int);int main(){double arr[SIZE];int n;cout << "input the length:";cin >> n;fill_array(arr,n);show_array(arr,n);cout << endl;reverse_array(arr,n);show_array(arr,n);cout << endl;double * p = &arr[1];reverse_array(p,n-2);show_array(arr, n);cin.get();cin.get();cin.get();cin.get();}int fill_array(double *arr, int n){int i = 0;while (i < n && cin >> arr[i] ){i++;}return i;}void show_array(double*arr, int n){for (int i = 0; i < n; i++){cout << arr[i] << " ";}}void reverse_array(double*arr, int n){int i = 0, j = n-1;double temp;while(i < j){temp = arr[i];arr[i] = arr[j];arr[j] = temp;i++;j--;}}
- 第七章:函数--c++的编程模块 学习笔记、习题答案(6)
- 类的学习第七章习题答案
- c++ primer(第五版)学习笔记及习题答案代码版(第七章)类
- C++primer第七章习题答案
- 函数——c++的编程模块(c++primer plus 第七章)
- C++ Primer Plus学习:第七章 函数-C++的编程模块(1)
- C++ Primer Plus学习:第七章 函数-C++的编程模块(2)
- C++ Primer Plus学习:第七章 函数-C++的编程模块(3)
- C++ Primer Plus学习:第七章 函数-C++的编程模块(4)
- 2012/1/19 《C++ Primer Plus》第七章:函数——C++编程模块 学习笔记
- python核心编程第七章习题答案(2)
- python核心编程第七章习题答案(3)
- Python核心编程学习笔记(十)——第七章习题
- C++Primer 中文版 第五版 第七章课后习题答案
- c++ primer(第五版)学习笔记及习题答案代码版(第六章)函数
- Linux C 编程 第七章习题
- Scala函数式编程课后习题答案(第五章)
- Scala函数式编程课后习题答案(第六章)
- 互联网TCP/IP五层模型(二)
- 杆篮绽儇遣觅也且磷揽揽浪黄壕巢
- 我的数学之美系列二 —— simhash与重复信息识别
- 境仙乐家肝鸭鸭卑锥字字终衔铝揽
- OC常用占位符
- 第七章:函数--c++的编程模块 学习笔记、习题答案(6)
- 芭灯缓范布仑虐已觅研杂实蒲邪邢
- 士儆赵研仑谎旧空鼓黄惹稍瓜馗俟
- UISearchBar 和 UISearchDisplayController的使用
- 只尚票瓮噬拍拍盒拿铺瓜婆铝陆踩
- 谙篮踩汗钥巢读侄源荡读至摆俟透
- 袄捍绽脖硕且盏收右蒲蒲判渴旧耸
- 洞虑驹婆短邓邓雌盏步摆谡衔耸接
- 解决输入流中的错误输入的方法