第七周第五章函数

1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;

 

用无参函数来显示字符串

#include<iostream>
using namespace std;
void display()
{
   cout<<"This is an example."<<endl;
}

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


 定义有参函数求两数中最小值

#include<iostream>
using namespace std;

double min(double x,double y)
{
	return x<y?x:y;
}

int main()
{
	cout<<min(6.0,5.0)<<endl;
	return 0;
}


 

#include<iostream>
using namespace std;

double circleArea(double);
int main()
{
	double area=circleArea(5.0);
	cout<<"area="<<area<<endl;
	return 0;
}
double circleArea(double r)
{
	double pi=3.14;
	double area=pi*r*r;
	return area;
}


 计算a,b之和

#include<iostream>
using namespace std;

int sum(int x,int y)
{
	int temp;
	temp=x+y;
	return temp;
}
int main()
{
	int a,b,c;
	a=10;b=5;
	c=sum(a,b);
	cout<<a<<"+"<<b<<"="<<c<<endl;
	return 0;
}


 实参函数的顺序

#include<iostream>
using namespace std;

int ncomp(int i,int j)
{
	if(i>j)return 1;
	if(i==j)return 0;
	return -1;
}
int main()
{
	int k=2;
	int n=ncomp(k,++k);
	cout<<n;
	return 0;
}

结果为0
 

 

输入两整数,比较大小输出较大数

#include<iostream>
using namespace std;

int max(int u,int v)
{
	int w;
	w=u>v?u:v;
	return w;
}
int main()
{
	int a,b,c;
	cout<<"please input two numbers:";
	cin>>a>>b;
	c=max(a,b);
	cout<<"a="<<a<<"b="<<b<<endl;
	cout<<"max is"<<c<<endl;
	return 0;
}
	


 求10 的平方

#include<iostream>
using namespace std;

int sqr(int x)
{
	x=x*x;
	return x;
}
int main()
{
	int t=10;
	int s=sqr(t);
	cout<<"t="<<t<<'\t'
		<<"sqr("<<t<<")="<<s<<endl;
	return 0;
}


 值调用实现两个数据互换.

#include<iostream>
using namespace std;

void swap(int u,int v);
int main()
{
	int a=3;
	int b=4;
	cout<<"a="<<a<<"\tb="<<b<<endl;
	swap(a,b);
	cout<<"a="<<a<<"\tb="<<b<<endl;
	return 0;
}
void swap(int u,int v)
{
	int temp;
	temp=u;
	u=v;
	v=temp;
}
	


 

#include<iostream>
using namespace std;

void swap(int &u,int &v);
int main()
{
	int a=3;
	int b=4;
	cout<<"a="<<a<<"\tb="<<b<<endl;
	swap(a,b);
	cout<<"a="<<a<<"\tb="<<b<<endl;
	return 0;
}
void swap(int &u,int &v)
{
	int temp;
	temp=u;
	u=v;
	v=temp;
}


 RETURN的作用

#include<iostream>
using namespace std;

void display(int x,float y)
{
	cout<<x<<""<<y;
	return;

}
int main()
{
	float a;
	int b;
	cin>>b>>a;
	display(b,a);
	return 0;
}

 

不同类型参数的示例程序(求一元二次方程的根)

#include<iostream>
#include<cmath>
using namespace std;

void GetRoots(/*in*/double,/*in*/double,/*in*/double,/*out*/double&,/*out*/double&);
//函数GetRoots用于求一元二次方程的两个根;
//前三个形参为流入参数,是值调用,分别用于接收主调函数传递的三个系数值;
//后两个形参为流出参数,是引用调用,将计算后的两面三刀个根回传给主调函数。

int main()
{
	double a,b,c;
	double root1,root2;
	cout<<"input a,b,c:"<<endl;
	cin>>a>>b>>c;
	GetRoots(a,b,c,root1,root2);
	cout<<"root1="<<root1<<"rott2="<<root2<<endl;

	return 0;
}

void GetRoots(double a,double b,double c,double& root1,double& root2)
{
	double temp;
	temp=b*b-4.0*a*c;
	root1=(-b+sqrt(temp))/(2.0*a);
	root2=(-b-sqrt(temp))/(2.0*a);
}

 

递归求介乘

#include<iostream>
using namespace std;

float Factorial(int n);

int main()
{
	int a;
	float f;
	cout<<"input an integer number:";
	cin>>a;
	f=Factorial(a);
	cout<<a<<"!="<<f<<endl;

	return 0;
}

float Factorial(int n)
{
	float fact;
	if(n==0)
		fact=1;
	else
		fact=n*Factorial(n-1);

	return fact;
}



 

2.编程实现课本每一个编程习题。

3. 编程实现输入两个4X5矩阵和5X3矩阵,定义函数并在主函数中调用计算它们的积。

4.编程计算S[n]=1!+21+3!+...n!。要求定义两个函数,一个计算n!,一个计算s[n],在后一个函数中调用前一个函数。然后在主程序中输入数n的值,然后调用定义函数输出结果。

5.编写一个函数,输入一个十六进制数,输出相应的十进制数。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值