#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double f(double); //函数声明
double xpoint(double, double); //函数声明
double root(double, double); //函数声明
int main( )
{
double x1,x2,f1,f2,x;
do
{
cout<<"input x1,x2:";
cin>>x1>>x2;
f1=f(x1);
f2=f(x2);
} while(f1*f2>=0);
x=root(x1,x2);
cout<<setiosflags(ios::fixed)<<setprecision(7);
//指定输出7位小数
cout<<"A root of equation is "<<x<<endl;
return 0;
}
double f(double x) //定义f函数,以实现f(x)
{
double y;
y=x*x*x-5*x*x+16*x-80;
return y;
}
double xpoint(double x1, double x2) //定义xpoint函数,求出弦与x轴交点
{
double y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); //在xpoint函数中调用f函数
return y;
}
double root(double x1, double x2) //定义root函数,求近似根
{
double x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2); //在root函数中调用xpoint函数
y=f(x); //在root函数中调用f函数
if (y*y1>0)
{
y1=y;
x1=x;
}
else
x2=x;
}while(fabs(y)>=0.00001);
return x;
}
input x1, x2:2.5 6.7↙ A root of equation is 5.0000000
int f(int x)
{
int y, z;
z=f(y); //在调用函数f的过程中,又要调用f函数
return (2*z);
}
age(5)=age(4)+2 age(4)=age(3)+2 age(3)=age(2)+2 age(2)=age(1)+2 age(1)=10
age(n)=10 (n=1) age(n)=age(n-1)+2 (n>1)
#include <iostream>
using namespace std;
int age(int);//函数声明
int main( )//主函数
{
cout<<age(5)<<endl;
return 0;
}
int age(int n)//求年龄的递归函数
{
int c; //用c作为存放年龄的变量
if(n==1) c=10; //当n=1时,年龄为10
else c=age(n-1)+2; //当n>1时,此人年龄是他前一个人的年龄加2
return c; //将年龄值带回主函数
}
18
n! = 1 (n=0, 1) n * (n-1)! (n>1)
#include <iostream>
using namespace std;
long fac(int);//函数声明
int main( )
{
int n;//n为需要求阶乘的整数
long y; //y为存放n!的变量
cout<<"please input an integer :"; //输入的提示
cin>>n; //输入n
y=fac(n);//调用fac函数以求n!
cout<<n<<"!="<<y<<endl; //输出n!的值
return 0;
}
long fac(int n) //递归函数
{
long f;
if(n<0)
{
cout<<"n<0,data error!"<<endl; //如果输入负数,报错并以-1作为返回值
f=-1;
}
else if (n==0||n==1) f=1; //0!和1!的值为1
else f=fac(n-1)*n;//n>1时,进行递归调用
return f;//将f的值作为函数值返回
}
please input an integer:10↙ 10!=3628800
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有