实验四 函数(C++)

一、实验目的与要求

1、掌握函数的定义、声明的方法;

2、掌握函数的编写要求;

3、掌握函数的调用方法;

4、掌握函数参数的传递方法;

5、掌握变量的作用域;

6、掌握多文件编程方法。

二、实验内容

1、输入自然数m和n:

(1)求他们的最大公约数(或称最大公因数)。

 要求输入、输出在主函数中进行,求公约数由函数实现。

  (2)在函数中求最大公约数与最小公倍数。(提示:使用引用参数)

2、指出程序的局部与全局变量,程序输出结果如何:

若main函数改为{cout<<i<<j;p();cout<<"j="<<j;p();return 0;}编译后会有何问题,说明产生该问题的理由;修改后输出结果。

程序如下:

#include <iostream>
using namespace std;
int j=40;
void p()
{
    int i=5;
    static int j=5;
    i++;
    j++;
    cout << “i is “<< i <<endl;
    cout << “j is “<< j <<endl;
}

int main()
{
    p();
    p();
    return 0;
}

3、编写程序满足:

声明一个函数,判断一个整数是否为素数,使用如下函数头:

bool is_prime(int num) ,如果num是素数函数返回true,否则返回false;

利用函数is_prime找出前200个素数,并按每行10个输出:

    2     3      5      7    11    13    17    19    23    29

4、编程实现摄氏温度到华氏温度的转换:

编写一个头文件,包含下面两个函数:

double celsius_to_fah(double cel)    //摄氏温度到华氏温度

double fahrenheit_to_cels(double fah) //华氏温度到摄氏温度

实现头文件,并编写测试程序,调用函数显示如下结果:

Celsius    Fahrenheit     |   Fahrenheit       Celsius

40.0       105.0        |   120.0            48.89

39.0       102.0        |   110.0            43.33

……       ……         |   ……             ……

31.0        87.8        |   30.0             -1.11

(测试程序为主模块,即main( )函数所在的CPP文件,头文件mytemperature.h只有函数声明;函数定义写在另一CPP文件mytemperature.cpp)

5、创建名为mytriangle.h的头文件,包括:

bool is_valid(double side1,double side2,double side3)

double_area(double side1,double side2, double side3)

面积=sqrt(s(s-side1)(s-side2)(s-side3))

其中s=(side1+side2+side3)/2

写测试程序:读取三角形三边长,如输入合法,计算面积,否则输出错误信息。

(测试程序为主模块,即main( )函数所在的CPP文件,头文件mytriangle.h只有函数声明;函数定义写在另一CPP文件mytriangle.cpp)

4与5选一个完成

 

【实验思考题】

1. 本实验中函数中返回的值为什么与函数类型一致?

2. 本实验中主函数调用函数时采用的是何种传递方式?

三、实验步骤、算法与结果分析

1、求最大公约是与最小公倍数

1.1 流程图

实验四 函数(C++)实验四 函数(C++)

1.2 程序

#include <iostream>
using namespace std;
//求最大公约数
int gcd(int m,int n)
{
	int k=1;
	int i=1;
	while (i<=m&&i<=n)
	{
		if (m%i==0&&n%i==0)
			k=i;
		i++;
	}
	return k;
}
//求最小公倍数
int lcm(int m,int n)
{
	int k=1;
	for (int i = 2;i<=m&&i<=n;i++)
	{
		if (m%i==0&&n%i==0)
			k=i;
	}
	int h;
	h=m*n/k;
	return h;
}
int main()
{
	cout<<"Please enter two nums : "<<endl;
	int m , n;
	cout<<"m = ";
	cin>>m;
	cout<<"n = ";
	cin>>n;
	cout<<m<<" and "<<n<<" greatest common divisor gcd :  "<<gcd(m,n)<<endl;
    cout<<m<<" and "<<n<<" lowest common multiple LCM : "<<lcm(m,n)<<endl;

	return 0;
} 

1.3 结果

实验四 函数(C++)

2、指出程序的局部与全局变量,程序输出结果如何

修改前代码:

#include<iostream>
using namespace std;
int j = 40;
void p()
{
	int i=5;
	static int j=5;
	i++;
	j++;
	cout<<"i is "<<i<<endl;
	cout<<"j is "<<j<<endl;
}
int main()
{
	p();
	p();

	return 0;
}

修改前结果:

实验四 函数(C++)

修改后代码:

#include<iostream>
using namespace std;
int j = 40;
void p()
{
	int i=5;
	static int j=5;
	i++;
	j++;
	cout<<"i is "<<i<<endl;
	cout<<"j is "<<j<<endl;
}
int main()
{
	cout<<j<<endl;
	p();
	cout<<"j = "<<j<<endl;
	p();

	return 0;
}

修改后结果:

实验四 函数(C++)

3、判断素数并输出前两百个素数

3.1 流程图

判断素数:

实验四 函数(C++)

输出前两百个素数:

实验四 函数(C++)

3.2 程序

#include<iostream>
#include<iomanip>
using namespace std;
//判断是否为素数
bool is_prime(int number)
{
	for(int divisor = 2;divisor <=number/2;divisor++)
	{
		if(number%divisor==0)
		{
			return false;
		}
	}
	return true;
}
//按要求输出指定个数素数
void printPrimeNumber(int numofprime)
{
	const int NUMBER_OF_PRIMES =200;
	const int LINE = 10;
	int count = 0;
	int number = 2;
	while (count<numofprime)
	{
		if(is_prime(number))
		{
			count++;
		if(count  % LINE==0)
		{
			cout<<setw(5)<<number<<endl;
		}
		else
			cout<<setw(5)<<number;
		}
		number++;
	}
}

int main()
{
	cout<<"The first 200 prime numbers are \n";
	printPrimeNumber(200);

	return 0;
}

3.3 结果

实验四 函数(C++)

4、写测试程序:读取三角形三边长,如输入合法,计算面积,否则输出错误信息。

4.1 流程图

实验四 函数(C++)

4.2 程序

//mytriangle.h头文件:
//mytriangle.h
#include <cmath>
bool is_valid(double side1,double side2,double side3)

{
	if((side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1))
		return true;
}
double double_area(double side1,double side2,double side3)
{
	double s,area;
	s=(side1+side2+side3)/2.0;
	area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
	return area;
}
//主函数:
#include<iostream>
#include"mytriangle.h"
using namespace std;
int main()
{   double side1,side2, side3;
	cout<<"Please enter three sides : "<<endl;
	cin>>side1>>side2>>side3;
	if(is_valid(side1,side2,side3))
		cout<<"The area is "<<double_area(side1,side2,side3)<<endl;
	else
		cout<<"The sides are illegal ."<<endl;
	
	return 0;
}

4.3 结果

实验四 函数(C++)

实验四 函数(C++)

实验四 函数(C++)

四、实验思考

1、本实验中函数中返回的值为什么与函数类型一致?

        函数本没有类型,只是为了识别返回值的类型,才在声明中在函数前面也加了这个类型,一遍编译程序把返回值以这个指定类型来处理。所以,函数中返回值的类型只能与函数声明时的类型一致。

2、本实验中主函数调用函数时采用的是何种传递方式?

      传用参数的传递方式。

 

相关文章: