1.函数声明和函数定义
拿段代码举例哈:
#include <iostream> using namespace std; int add(int, int); int main() { int x, y; cin >> x >> y; cout << add(x, y); return 0; } int add(int a, int b) { return a+b; }
函数声明,在我看来便是在系统中声明一个函数,告诉系统从现在开始,有了这个函数,可以对它进行调用,如上述函数中最上方的int add,即是声明add函数;
而函数定义,则是向系统具体阐释这个函数是做什么的,功能是什么,如上述函数中最后的int add,是告诉系统add函数要求a+b的值,而我觉得,int main说白了也是对main函数的一种定义;
还有,函数声明可以和函数定义结合在一起,即将函数声明删去,将函数定义移至上方主函数前即可。
2.实参形参,函数参数与返回值的作用
还是上面那串代码:
#include <iostream> using namespace std; int add(int, int); int main() { int x, y; cin >> x >> y; cout << add(x, y); return 0; } int add(int a, int b) { return a+b; }
形参,即形式参数,只是走个形式,在我看来就是一个一次性参数,只有在定义它的这个函数中,需要的时候,才会给它分配空间,使用结束即释放空间,这个函数结束后在主函数及其他函数中无法使用,正如上述代码中的a,b,add函数结束后就无法再使用;
实参,是一个固定的值,可以是常量,变量,函数,表达式等等,一般是先通过赋值,输入等方法获得固定值,再传给形参,就如代码中的x和y,通过输入获得固定值,再传给形参a和b;
函数的参数,就是函数从外界主函数中获得解决这个函数问题需要的条件的桥梁,代码中的a和b就是获得输入值的途径;
函数的返回值,就是函数解决的问题的答案,通过这个返回值传回给主函数进行进一步加工处理,代码中就是通过返回值a+b将答案返回主函数。
3.值传递
#include <iostream>
using namespace std;
void swao(int,int);
int main()
{
int x=1, y=2;
cout << "x= " << x <<< ", " << "y= " << y << endl;
swap(x,y);
cout << "x= " << x << ", "<< "y= " << y;
return 0;
)
void swap(int a, int b) { int m; m = a; a = b; b = m; )
结果: x= 1, y= 2
x= 1, y= 2
像代码中a=b之类的都属于直接的值传递,是直接将一个值通过赋值传给另一个变量,并不会改变函数外的原值;
#include <iostream>
using namespace std;
void swao(int *,int *);
int main()
{
int *x=1, *y=2;
cout << "x= " << x <<< ", " << "y= " << y << endl;
swap(*x,*y);
cout << "x= " << x << ", "<< "y= " << y;
return 0;
)
void swap(int *a, int *b)
{
int m;
m = a;
a = b;
b = m;
)
结果: x= 1, y= 2
x= 2, y= 1
间接传递则是可以改变原变量的位置来改变它的值,可以影响函数外的原值;
2-28
//if else #include <iostream> using namespace std; void fun(); int main() { fun(); return 0; } void fun() { char ch; while(ch!='Q') { cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>ch; if(ch=='A'){ cout<<"数据已经增加。"<<endl; continue; } else if(ch=='D'){ cout<<"数据已经删除。"<<endl; continue; } else if(ch=='S'){ cout<<"数据已经排序。"<<endl; continue; } else if(ch=='Q'){ cout<<"输入结束"<<endl; break; } else{ cout<<"输入错误,请重新输入"<<endl; continue; } } }
//switch #include <iostream> using namespace std; void choose(); int main() { choose(); return 0; } void choose() { char ch; while(ch!='Q') { cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>ch; switch (ch){ case 'A': cout<<"数据已经增加。"<<endl; break; case 'D': cout<<"数据已经删除。"<<endl; break; case 'S': cout<<"数据已经排序。"<<endl; break; case 'Q': cout<<"输入结束。"<<endl; break; default: cout<<"输入错误,请重新输入"<<endl; break; } } }
2-29
判断素数:一个素数只有1和它本身两个因数,所以从2开始只有它本身一个因数。那么从2开始检索它的第一个因数,若为它本身,则它是素数,反之则不是;
//for #include <iostream> #include <cmath> using namespace std; void search(); int main() { search(); return 0; } void search() { int i,j; for(i=2; i<100; i++) { for(j=2; j<=i; j++) if(i%j==0) break; if(j==i) cout<<i<<' '; else continue; } }
//while #include <iostream> #include <cmath> using namespace std; void search(); int main() { search(); return 0; } void search() { int i=1,j; while(i++ < 100) { j=1; while(++j <= i){ if(i%j==0) break;} if(j==i) cout<<i<<' '; else continue; } }
//do while #include <iostream> #include <cmath> using namespace std; void search(); int main() { search(); return 0; } void search() { int i=2,j; do { j=2; do{ if(i%j==0) break; }while(j++ < i); if(j==i) cout<<i<<' '; else continue; }while(i++ < 100); }
显而易见,for循环最快,while循环次之,dowhile循环最慢。
2-32
//do while #include <iostream> #include <cstdlib> using namespace std; void game(); int main() { game(); return 0; } void game() { int target, answer; target = 1 + rand()%100; cout << "Please enter the number you guess: "; do{ cin >> answer; if(answer == target) cout << "Correct!"; else if(answer < target) cout << "Please bigger!" << endl << "Enter again: "; else cout << "Please smaller!" << endl << "Enter again: "; }while(answer != target); }
//while #include <iostream> #include <cstdlib> using namespace std; void game(); int main() { game(); return 0; } void game() { int target, answer = -1; target = 1 + rand()%100; cout << "Please enter the number you guess: "; while(answer != target) { cin >> answer; if(answer < target) { cout << "Please bigger!" << endl << "Enter again: "; continue; } else if(answer > target) { cout << "Please smaller!" << endl << "Enter again: "; continue; } } cout << "Correct!"; }
3-34
思路:高中时做过类似的摸球的概率计算,一共n个球,若摸可能不同颜色的,第一次n种可能,第二次n-1种,···,第x次n-(x-1)种,由于是1次摸完,所以再除去顺序,除以x!,得到结果
//摸球游戏 #include <iostream> using namespace std; void moqiu(); int main() { moqiu(); return 0; } void moqiu() { int sum=1, aim, total; cout << "The total number of kinds is: "; cin >> total; cout << endl << "The number of balls you choose is: "; cin >> aim; for(int i=1; i <= aim; i++ ) { sum *= (total-(i-1)); sum /= i;//除去摸球顺序影响 } cout << endl << "The number of posibility is: " << sum; }