📄 练习.txt
字号:
5.1
//*************************
//** ch5_1.cpp **
//*************************
#include <iostream.h>
#include <math.h>
void isprime(long);
void main()
{
long m;
cout<<"please input a number: "<<endl;
cin>>m;
isprime(m);
}
void isprime(long m)
{
double sqrtm=sqrt(m);
for(int i=2;i<=sqrtm;i++)
if(m%i==0)
break;
if(sqrtm<i)
cout<<m<<" is prime"<<endl;
else
cout<<m<<" isn't prime"<<endl;
}
5.2
//*************************
//** ch5_2.cpp **
//*************************
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
double f(double x);
double integral(double a,double b);
void main()
{
double a=0,b=1,s;
s=integral(a,b);
cout<<"The integral of f(x) from "
<<a<<" to "<<b<<" is "<<endl
<<setiosflags(ios::fixed)
<<setprecision(8)
<<setw(10)<<s<<endl;
}
double f(double x)
{
return exp(x)/(1+x*x);
}
double integral(double a,double b)
{
int n=1;
double h,Tn,T2n,In,I2n;
const double eps=1e-8;
h=b-a; //initializing
T2n=I2n=h*(f(a)+f(b))/2;
In=0;
while(fabs(I2n-In)>=eps)
{
Tn=T2n;
In=I2n;
double sigma=0.0; //sigma用来求和
for(int k=0;k<n;k++)
{
double x=a+(k+0.5)*h;
sigma+=f(x);
}
T2n=(Tn+h*sigma)/2.0;
I2n=(4*T2n-Tn)/3.0;
n*=2;
h/=2;
}
return I2n;
}
5.3
#include <iostream.h>
#include <iomanip.h>
void mul_t_title();
void total_mul_t();
void downt_mul_t();
void upt_mul_t();
void main()
{
total_mul_t();
cout<<endl;
downt_mul_t();
cout<<endl;
upt_mul_t();
cout<<endl;
cout<<"Thank you for viewing!"<<endl
<<"Bye!"<<endl;
}
void total_mul_t()
{
mul_t_title();
int i;
for(i=1;i<=9;i++)
{
cout<<setw(3)<<i;
for(int j=1;j<=9;j++)
{
cout<<setw(5)<<i*j;
}
cout<<endl;
}
}
void downt_mul_t()
{
mul_t_title();
int i;
for(i=1;i<=9;i++) //content
{
cout<<setw(3)<<i;
for(int j=1;j<=i;j++)
{
cout<<setw(5)<<i*j;
}
cout<<endl;
}
}
void upt_mul_t()
{
mul_t_title();
int i;
for(i=1;i<=9;i++) //content
{
cout<<setw(3)<<i;
for(int j=1;j<i;j++)
{
cout<<setw(5)<<" ";
}
for(;j<=9;j++)
{
cout<<setw(5)<<i*j;
}
cout<<endl;
}
}
void mul_t_title()
{
int i=1;
cout<<setw(3)<<"*";
for(;i<=9;i++)
{
cout<<setw(5)<<i;
}
cout<<endl;
for(i=1;i<=9;i++)
{
cout<<"………";
}
cout<<endl;
}
5.4
#include <iostream.h>
#include <iomanip.h>
void func();
int n=1;
void main()
{
static int x=5;
int y;
y=n;
cout<<"Main-- x="<<x
<<",y="<<y
<<",n="<<n<<endl;
func();
cout<<"Main-- x="<<x
<<",y="<<y
<<",n="<<n<<endl;
func();
}
void func()
{
static int x=4;
int y=10;
x+=2;
n+=10;
y+=n;
cout<<"Func-- x="<<x
<<",y="<<y
<<",n="<<n<<endl;
}
5.5
#include <iostream.h>
long fib(int);
void main()
{
int n,t;
cin>>n;
t=fib(n);
cout<<"t="<<t<<endl;
}
long fib(int n) //斐波那契数列第n项
{
if(n>2)
{
int a=1,b=1,t=0;
for(;(n-2)>=1;n--)
{
t=a+b;
a=b;
b=t;
}
return b;
}
else
return 1;
}
5.6
#include <iostream.h>
double p(int, double);
void main()
{
int n;
double x,s;
cin>>n>>x;
s=p(n,x);
cout<<"s="<<s<<endl;
}
double p(int n, double x)
{
if(n>=2)
return ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x)/n);
else if(n==1)
return x;
else if(n==0)
return 1;
else
cout<<"Error!"<<endl;;
}
5.7
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
double f(double);
void main()
{
double s,Xn,Xm;
Xn=Xm=3.14159/4;
s=f(Xn);
while(fabs(s)>1e-6)
{
Xn=Xm;
Xm=Xn+(cos(Xn)-Xn)/(sin(Xn)+1);
s=f(Xm);
}
cout<<"Solution:"<<endl
<<"Xm="
<<setiosflags(ios::fixed)
<<setprecision(6)<<Xm<<endl;
}
double f(double x)
{
return cos(x)-x;
}
5.8
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
void display(double);
void display(int);
void display(char);
void main()
{
double d=1.23;
int i=145;
char c='r';
float f=4.56;
short s=54;
display(d);
display(i);
display(c);
display(f);
display(s);
}
void display(double d)
{
cout<<"A double: "<<d<<endl;
}
void display(int i)
{
cout<<"A int: "<<i<<endl;
}
void display(char c)
{
cout<<"A char: "<<c<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -