第四周项目5:程序分析

来源:互联网 发布:成都犀牛软件培训 编辑:程序博客网 时间:2024/04/30 09:10
/*   *Copyright (c)2016,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:qwe.cpp   *作    者:赵子琳   *完成日期:2016年3月28日   *版 本 号:v1.0   *   *问题描述:分析以下程序运行机制  */      问题(1)  #include <iostream>  using namespace std;  void fun(int k)  {      if(k > 0)          fun(k-1);      cout << k;  }    int main()  {      int w = 5;      fun(w);      cout << endl;      return 0;  } 

分析:


问题(2)  #include <iostream>  using namespace std;  void recur(char);  int main()  {      recur('0');      return 0;  }  void recur(char c)  {      cout << c;      if(c < '5')          recur(c+1);      cout << c;  }  

分析:

问题(3)  #include <iostream>  using namespace std; int fun2(int a,int b)  {      int c;      c = a*b%3;      return c;  }  int fun1(int &a,int &b)  {      int c;      a += a;      b += b;      c = fun2(a,b);      return c*c;  }  int main()  {      int x = 11,y = 19;      cout << fun1(x,y) << endl;      return 0;  }  

分析:

问题(4)  #include <iostream>  using namespace std;    const double pi = 3.1415926;  float area(float r = 6.5);  float volume(float h,float r = 6.5);  int main()  {      cout << area() << endl;      cout << area(7.5) << endl;      cout << volume(45.6) << endl;      cout << volume(34.2,10.4) << endl;      return 0;  }  float area(float r)  {      return pi*r*r;  }    float volume(float h,float r)  {      return pi*r*r*h;  }  

分析:

(1):去掉第四行的“=6.5”则在第一次调用area()函数时会因为无参数而错误。

(2):将第十四行改为“float area(float r = 6.5)”会出错,原因是在函数声明中已经指定了默认实参。

(3):将第五行“float h,float r = 6.5”改为“float h,float r”会出错,原因是在调用volume(45.6)时会少一个参数。

(4):将第五行改为“volume(float h = 0,float r = 6.5)”无错误,此时调用volume()时可以不传参。





0 0
原创粉丝点击