文档详情

C++上机实验报告实验二.docx

发布:2017-02-04约3.02千字共6页下载文档
文本预览下载声明
实验二实验目的1.掌握函数的定义和调用方法2.练习重载函数的使用3.练习使用系统函数4.学习使用VC++的debug调试功能,使用step into追踪到函数内部实验要求1.编写一个函数把华氏温度转换为摄氏温度,转换公式如下: C=(F-32)*5/92.编写重载函数Max1可分别求两个整数,三个整数,两个双精度数,三个双精度数的最大值。3.使用系统函数pow(x,y)计算xy的值,注意包含头文件math.h 4.用递归的方法编写函数求Fibonacci级数,观察递归调用的过程实验内容1.编写函数float Convert(float TempFer),参数和返回值都为float类型,实现算法C=(F-32)*5/9,在main()函数中实现输入、输出。程序名:lab3_1.cpp。2.分别编写四个同名函数max1,实现函数重载,在main()函数中测试函数功能。程序名:lab3_2.cpp 3.在main()函数中提示输入两个整数x、y,使用cin语句得到x、y的值,调用pow(x,y)函数计算x的y次幂的结果,再显示出来。程序名:lab3_4.cpp 。4.编写递归函数int fib(int n),在主程序中输入n的值,调用fib函数计算Fibonacci级数。公式为 fib(n)=fib(n-1)+fib(n-2),n2; fib(1)=fib(2)=1。使用if语句判断函数的出口,在程序中用cout语句输出提示信息。程序名:lab3_5.cpp 5.debug调试操作步骤如下:1)选择菜单命令Build | Start Debug | Step in,或按下快捷键F11,系统进入单步执行状态,程序开始运行,并出现一个DOS窗口,此时Visual Studio中光标停在main()函数的入口处。2)把光标移到语句answer=fib(n)前,从Debug菜单或Debug工具栏中单击Run to Cursor,在程序运行的DOS窗口中按提示输入数字10,这时回到Visual Studio中,光标停在第11行,观察一下n的值(观察方法见实验2)。3)从Debug菜单或Debug工具栏中单击Step Into,程序进入fib函数,观察一下n的值,把光标移到语句return(fib(n-2)+fib(n-1))前,从Debug菜单或Debug工具栏中单击Run to Cursor,再单击Step Into,程序递归调用fib函数,又进入fib函数,观察一下n的值。4)继续执行程序,参照上述的方法,观察程序的执行顺序,加深对函数调用和递归调用的理解。5)再试试Debug菜单栏中的菜单项,熟悉Debug的各种方法。4.源程序1.#includeiostreamusing namespace std;main(){float Convert(float );float tempF,tempC;coutPlease input the temperature F:endl;cintempF;tempC=Convert(tempF);couttemperature C is tempCendl;}float Convert(float tempFar){float tempCel;tempCel=(tempFar-32)*5/9;return(tempCel);}2.#includeiostreamusing namespace std;int Max1(int a,int b){if(ab)return(a);elsereturn(b);}double Max1(double a,double b){if(ab)return(a);elsereturn(b);}int Max1(int a,int b,int c){if(ab){if(ac)return(a);elsereturn(c);}else if(bc)return(b);elsereturn(c);}double Max1(double a,double b,double c){if(ab){if(ac)return(a);elsereturn(c);}else if(bc)return(b);elsereturn(c);}void main(){int a,b,c;double e,d,f;coutPlease input two integers:endl;cinab;coutThe max number is Max1(a,b)endl;coutPlease input three integers:endl;cinabc;coutThe max number is Max1(a,b,c)endl;coutPlease input two doubles:endl;c
显示全部
相似文档