您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页C++程序设计第三版(谭浩强)第四章习题答案

C++程序设计第三版(谭浩强)第四章习题答案

来源:二三娱乐


C++程序设计第三版(谭浩强)第四章习题答案

4.1 题

#include

using namespace std;

int main()

{int hcf(int,int);

int lcd(int,int,int);

int u,v,h,l;

cin>>u>>v;

h=hcf(u,v);

cout<<\"H.C.F=\"<l=lcd(u,v,h);

cout<<\"L.C.D=\"<return 0;

}

int hcf(int u,int v)

{int t,r;

if (v>u)

{t=u;u=v;v=t;}

while ((r=u%v)!=0)

{u=v;

v=r;}

return(v);

}

int lcd(int u,int v,int h)

{return(u*v/h);

}

4.2 题

#include

#include

using namespace std;

float x1,x2,disc,p,q;

int main()

{void greater_than_zero(float,float);

void equal_to_zero(float,float);

void smaller_than_zero(float,float);

float a,b,c;

cout<<\"input a,b,c:\";

cin>>a>>b>>c;

disc=b*b-4*a*c;

cout<<\"root:\"<if (disc>0)

{

greater_than_zero(a,b);

cout<<\"x1=\"<}

else if (disc==0)

{equal_to_zero(a,b);

cout<<\"x1=\"<}

else

{smaller_than_zero(a,b);

cout<<\"x1=\"<cout<<\"x2=\"<}

return 0;

}

void greater_than_zero(float a,float b) /* 定义一个函数,

用来求 disc>0 时方

程的根 */

{x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

}

void equal_to_zero(float a,float b) /* 定义一个函数,用

来求 disc=0 时方程

的根 */

{

x1=x2=(-b)/(2*a);

}

void smaller_than_zero(float a,float b) /* 定义一个函数,

用来求 disc<0 时方

程的根 */

{

p=-b/(2*a);

q=sqrt(-disc)/(2*a);

}

4.3 题

#include

using namespace std;

int main()

{int prime(int); /* 函数原型声明 */

int n;

cout<<\"input an integer:\";

cin>>n;

if (prime(n))

cout<else

cout<return 0;

}

int prime(int n)

{int flag=1,i;

for (i=2;iif (n%i==0)

flag=0;

return(flag);

}

4.4 题

#include

using namespace std;

int main()

{int fac(int);

int a,b,c,sum=0;

cout<<\"enter a,b,c:\";

cin>>a>>b>>c;

sum=sum+fac(a)+fac(b)+fac(c);

cout<return 0;

}

int fac(int n)

{int f=1;

for (int i=1;i<=n;i++)

f=f*i;

return f;

}

4.5 题

#include

#include

using namespace std;

int main()

{double e(double);

double x,sinh;

cout<<\"enter x:\";

cin>>x;

sinh=(e(x)+e(-x))/2;

cout<<\"sinh(\"<return 0;

}

double e(double x)

{return exp(x);}

4.6 题

#include

#include

using namespace std;

int main()

{double solut(double ,double ,double ,double );

double a,b,c,d;

cout<<\"input a,b,c,d:\";

cin>>a>>b>>c>>d;

cout<<\"x=\"<return 0;

}

double solut(double a,double b,double c,double d)

{double x=1,x0,f,f1;

do

{x0=x;

f=((a*x0+b)*x0+c)*x0+d;

f1=(3*a*x0+2*b)*x0+c;

x=x0-f/f1;

}

while(fabs(x-x0)>=1e-5);

return(x);

}

4.7 题

#include

#include

using namespace std;

int main()

{void godbaha(int);

int n;

cout<<\"input n:\";

cin>>n;

godbaha(n);

return 0;

}

void godbaha(int n)

{int prime(int);

int a,b;

for(a=3;a<=n/

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务