- 程序7.5例题
long double result = 1.0
for (n=numbers,p = picks;p>0;n--,p--)
result=result*n/p
//循环不是将所有分子相乘,而是分别算每轮的结果。这样的好处:中间项不会很大,交替进行乘除运算的策略可以防止中间结果超出最大的浮点数。
- 程序7.6
sizeof 数组名 显示整个数组的长度 (例:int c[8] => sizeof c = 8*4 (int 占4个字节))
sizeof 指针 显示地址长度(例: int *c => sizeof c = 8(64位/32位有关))
-
cin >> temp; if(!cin) { cin.clear(); //清除cin缓存区数据流 while(cin.get() != '\n') //直到用户输入回车才可报错 continue; cout << "bad input" }
cin.clear()是用来更改cin的状态标示符的,cin在接收到错误的输入的时候,会设置状态位good。如果good位不为1,则cin不接受输入,直接跳过。如果下次输入前状态位没有改变那么即使清除了缓冲区数据流也无法输入。所以清除缓冲区之前必须要cin.clear()。cin.sync()的作用是清除输入缓冲区全部的内容。
-
void change( const in *t ){}
参数前加 const 则函数无法修改t中的内容,只可读
-
c++ 禁止 将const的地址付给常规指针;允许const的地址 赋给const指针;
- cout 若程序有错,没有endl或‘\n’,则不打印;
cout流在内存中对应开辟了一个缓冲区,用来存放流中的数据,当向cout流插 人一个endl时,不论缓冲区是否已满,都立即输出流中所有数据,然后插入一个换行符, 并刷新流(清空缓冲区)。注意如果插人一个换行符”\n“(如cout<<a<<"\n"),则只输出和换行,而不刷新cout流(但并不是所有编译系统都体现出这一区别)。
- 不要将空指针赋值给函数做参数
# include <iostream>
int sum_arr(int arr[],int);
unsigned int c_in_char(const char * ,char);
char * buildstr(char, int);
using namespace std;
struct times
{
int hour;
int mins;
};
void sum( times* ,times* , times*);
void show_time(times*);
int main()
{
times start = { 1 , 25 };
times end = { 2 , 50 } ;
times *summ ; // 应该times *summm = new times();
cout << "1111\n" ;
sum(&start,&end,summ); //未赋值传给参数,并希望其返回,错误!!!!
show_time(summ);
}
void sum(times *a , times *b,times *all)
{
int al;
//times *all;
cout << a << "222\n";
cout << a->hour;
cout << "333\n";
al=(a->hour+b->hour)*60+a->mins+b->mins;
cout << al << endl;
all->hour = al/60;
all->mins = al%60;
cout << all->hour << endl;
//return all;
}
void show_time(times *a)
{
cout << "all time is " <<a->hour << ":" << a->mins <<endl;
}
-
const double * f1(const double ar[],int n);
const double * f2(const double [],int );
const double * f3(const double *,int );const double *(*p1)(const double * ,int) = &f1; //p1指向函数的指针
const double *(*pa[3])(const double *,int) = {f1,f2,f3}; //指向函数的指针数组;
const double *(*(*pd)[3])(const double *, int) = &pa // 指向函数的指针数组的指针;
const double * pdb = (*pd)[1](av,3); //pdb是指向函数的指针;
auto pc=&pa;//可用auto 自动处理;
- typedef double real //创建别名;
- typedef const double *(*p1)(const double * ,int) ;
p1 pc=&f1;