东北大学C++实验报告2
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
运行结果:
2.
编写重载函数Max1可分别求两个整数,三个整数,两个双精度数,三个双精度数的最大值。
分别编写四个同名函数max1,实现函数重载,在main()函数中测试函数功能。程序名:lab3_2.cpp
源代码:
#include<iostream>
using namespace std;
int max(int a,int b)
}
}
运行结果:
思考题
1.如何利用系统函数来实现一些常用的功能?
使用之前首先包含头文件,然后在代码中直接使用。
2.如何查找系统函数?
上网查找或者翻阅书籍。
3.如何实现函数重载?
改变参数的类型或者改变参数的个数
4.如何编写一个递归程序?
找到函数的递归规律,并在结束时加上结束条件。
2.编写重载函数Max1可分别求两个整数,三个整数,两个双精度数,三个双精度数的最大值。
3.使用系统函数pow(x,y)计算xy的值,注意包含头文件math.h
4.用递归的方法编写函数求Fibonacci级数,观察递归调用的过程
三、实验内容
1.
编写一个函数把华氏温度转换为摄氏温度,转换公式如下C=(F-32)*5/9
return 0;
}
运行结果:
4.
用递归的方法编写函数求Fibonacci级数,观察递归调用的过程
编写递归函数int fib(int n),在主程序中输入n的值,调用fib函数计算Fibonacci级数。
公式为fib(n)=fib(n-1)+fib(n-2),n>2;
fib(1)=fib(2)=1。
使用if语句判断函数的出口,在程序中用cout语句输出提示信息。程序名:lab3_5.cpp
源代码:
#include<iostream>
using namespace std;
int Fibonacci(int x);
int main()
{
int a,f;
cout<<"请输入你要计算的斐波那契数列的元数:"<<endl;
else
cout<<c<<"is the max."<<endl;
return 0;
}
float max(float a,float b)
{
if(a>b)
cout<<a<<"is the max."<<endl;
else
cout<<b<<"is the max."<<endl;
return 0;
}
float max(float a,float b,float c)
int main()
{
float c,f;
cout<<"请输入需要转换的温度:";
cin>>c;
f=Convert(c);
cout<<"摄氏温度为:"<<f<<endl;
return 0;
}
float Convert(float x)//将华氏温度转换为摄氏温度
{
return ((x-32)*5/9);
C++实验报告
学号:20154377姓名: 罗艺博 班级:计算机1508班
一、实验目的
1.掌握函数的定义和调用方法
2.练习重载函数的使用
3.练习使用系统函数
4.学习使用VC++的debug调试功能,使用step into追踪到函数内部
二、实验要求
1.编写一个函数把华氏温度转换为摄氏温度,转换公式如下C=(F-32)*5/9
cin>>a;
f=Fibonacci(a);
cout<<"斐波那契数列的值是:"<<f<<endl;
return 0;
}
int Fibonacci(int x)
{
if(x<3)
{
return 1;//递归的法求值
}
else
{
return (Fibonacci(x-1)+Fibonacci(x-2));
{
float d;
if(a>b)
d=a;
else
d=b;
if(d>c)
cout<<d<<"is the max."<<endl;
else
cout<<c<<"is the max."<<endl;
return 0;
}
int main()
{
int a=1,b=2,c=3;
float d=4,e=5,f=6;
编写函数float Convert(float TempFer),参数和返回值都为float类型,实现算法C=(F-32)*5/9,在main()函数中实现输入、输出。程序名:lab2_1.cpp。
源程序:
#include<iostream>
using namespace std;
float Convert(float x);
源代码:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x,y,power;
cout<<"请输入两个整数x,y"<<endl;
cin>>x>>y;
power=pow(x,y); //调用库函数求次方数
cout<<x<<"的"<<y<<"次方是:"<<power<<endl;
max(a,b);
max(a,b,c);
max(d,e);
max(d,e,f);
return 0;
}
运行结果:
3.
使用系统函数pow(x,y)计算xy的值,注意包含头文件math.h
在main()函数中提示输入两个整数x、y,使用cin语句得到x、y的值,调用pow(x,y)函数计算x的y次幂的结果,再显示出来。程序名:lab3_4.cpp
{
if(a>b)
cout<<a<<"is the max."<<endl;
else
cout<<b<<"is the max."<<endl;
return 0;
}
int max(int a,int b,int c)
{
int d;
if(a>b)
d=a;
else
d=b;
if(d>c)
cout<<d<<"is the max."<<endl;
运行结果:
2.
编写重载函数Max1可分别求两个整数,三个整数,两个双精度数,三个双精度数的最大值。
分别编写四个同名函数max1,实现函数重载,在main()函数中测试函数功能。程序名:lab3_2.cpp
源代码:
#include<iostream>
using namespace std;
int max(int a,int b)
}
}
运行结果:
思考题
1.如何利用系统函数来实现一些常用的功能?
使用之前首先包含头文件,然后在代码中直接使用。
2.如何查找系统函数?
上网查找或者翻阅书籍。
3.如何实现函数重载?
改变参数的类型或者改变参数的个数
4.如何编写一个递归程序?
找到函数的递归规律,并在结束时加上结束条件。
2.编写重载函数Max1可分别求两个整数,三个整数,两个双精度数,三个双精度数的最大值。
3.使用系统函数pow(x,y)计算xy的值,注意包含头文件math.h
4.用递归的方法编写函数求Fibonacci级数,观察递归调用的过程
三、实验内容
1.
编写一个函数把华氏温度转换为摄氏温度,转换公式如下C=(F-32)*5/9
return 0;
}
运行结果:
4.
用递归的方法编写函数求Fibonacci级数,观察递归调用的过程
编写递归函数int fib(int n),在主程序中输入n的值,调用fib函数计算Fibonacci级数。
公式为fib(n)=fib(n-1)+fib(n-2),n>2;
fib(1)=fib(2)=1。
使用if语句判断函数的出口,在程序中用cout语句输出提示信息。程序名:lab3_5.cpp
源代码:
#include<iostream>
using namespace std;
int Fibonacci(int x);
int main()
{
int a,f;
cout<<"请输入你要计算的斐波那契数列的元数:"<<endl;
else
cout<<c<<"is the max."<<endl;
return 0;
}
float max(float a,float b)
{
if(a>b)
cout<<a<<"is the max."<<endl;
else
cout<<b<<"is the max."<<endl;
return 0;
}
float max(float a,float b,float c)
int main()
{
float c,f;
cout<<"请输入需要转换的温度:";
cin>>c;
f=Convert(c);
cout<<"摄氏温度为:"<<f<<endl;
return 0;
}
float Convert(float x)//将华氏温度转换为摄氏温度
{
return ((x-32)*5/9);
C++实验报告
学号:20154377姓名: 罗艺博 班级:计算机1508班
一、实验目的
1.掌握函数的定义和调用方法
2.练习重载函数的使用
3.练习使用系统函数
4.学习使用VC++的debug调试功能,使用step into追踪到函数内部
二、实验要求
1.编写一个函数把华氏温度转换为摄氏温度,转换公式如下C=(F-32)*5/9
cin>>a;
f=Fibonacci(a);
cout<<"斐波那契数列的值是:"<<f<<endl;
return 0;
}
int Fibonacci(int x)
{
if(x<3)
{
return 1;//递归的法求值
}
else
{
return (Fibonacci(x-1)+Fibonacci(x-2));
{
float d;
if(a>b)
d=a;
else
d=b;
if(d>c)
cout<<d<<"is the max."<<endl;
else
cout<<c<<"is the max."<<endl;
return 0;
}
int main()
{
int a=1,b=2,c=3;
float d=4,e=5,f=6;
编写函数float Convert(float TempFer),参数和返回值都为float类型,实现算法C=(F-32)*5/9,在main()函数中实现输入、输出。程序名:lab2_1.cpp。
源程序:
#include<iostream>
using namespace std;
float Convert(float x);
源代码:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x,y,power;
cout<<"请输入两个整数x,y"<<endl;
cin>>x>>y;
power=pow(x,y); //调用库函数求次方数
cout<<x<<"的"<<y<<"次方是:"<<power<<endl;
max(a,b);
max(a,b,c);
max(d,e);
max(d,e,f);
return 0;
}
运行结果:
3.
使用系统函数pow(x,y)计算xy的值,注意包含头文件math.h
在main()函数中提示输入两个整数x、y,使用cin语句得到x、y的值,调用pow(x,y)函数计算x的y次幂的结果,再显示出来。程序名:lab3_4.cpp
{
if(a>b)
cout<<a<<"is the max."<<endl;
else
cout<<b<<"is the max."<<endl;
return 0;
}
int max(int a,int b,int c)
{
int d;
if(a>b)
d=a;
else
d=b;
if(d>c)
cout<<d<<"is the max."<<endl;