第七章:函数--------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--;
}
}