//************************
//* 4_9(3).chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<i;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<" ";
}
for (j=i;j<10;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
}
//************************
//* 4_10.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int total(int a);
void main()
{
int n,m;
cout<<"please input years:"<<endl;
cin>>n;
m=total(n);
cout<<"the total of cows:"<<m<<endl;
}
int total(int a)
{
int b=0;
int i;
i=a/4;
b=int(pow(2,i));
return b;
}
第五章
//************************
//* 5_1.cpp *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int isprime(long n);
void main()
{
long a,b,l=0;
cout<<"please input two numbers:/n";
cin>>a>>b;
cout<<"primes from "<<a<<" to "<<b<<"is:/n";
if(a%2==0)
a++;
for(long m=a;m<=b;m+=2)
{
if(isprime(m))
{
if(l++%10==0)
cout<<endl;
cout<<setw(5)<<m;
}
}
cout<<endl;
}
int isprime(long n)
{
//int sqrtn=int(sqrt(n));
int i,j=0;
for(i=2;i<n/*sqrtn*/;i++)
{
if(n%i==0)
{
j=0;
break;
}
j=1;
}
return j;
}
//************************
//* 5_2.cpp *
//* 计算定积分 *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
double f(double);
double integral(double,double);
void main()
{
double m,n,c;
cout<<"please input two point:"<<endl;
cin>>m>>n;
c=integral(m,n);
cout<<endl<<"the integral of f(x) from"
<<m<<" to "<<n<<"is /n"
<<setiosflags(ios::fixed)
<<setprecision(8)
<<setw(10)<<c<<endl;
}
double integral(double a,double b)
{
int n=1;
double h,Tn,T2n,In,I2n;
const double eps=1e-8;
h=b-a;
T2n=I2n=h*(f(a)+f(b))/2;
In=0;
while(fabs(I2n-In)>=eps)
{
Tn=T2n;
In=I2n;
double sigma=0.0;
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;
}
double f(double x)
{
return exp(x)/(1+x*x);
}
//************************
//* 5_3.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void p1();
void p2();
void p3();
void main()
{
int a;
cout<<"please input print sty(1-all,2-down,3-up):"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:p1();break;
case 2:p2();break;
case 3:p3();break;
}
}
void p1()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<10;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
}
void p2()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<i+1;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
return;
}
void p3()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<i;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<" ";
}
for (j=i;j<10;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
return;
}
//************************
//* 5_4 *
//* *
//************************
Main--x=5,y=1,n=1
Func--x=6,y=21,n=11
Main--x=5,y=1,n=11
Func--x=8,y=21,n=31
//************************
//* 5_5.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int fib(int);
void main()
{
int n;
int m;
cin>>n;
m=fib(n);
cout<<"fib("<<n<<""<<"="<<m<<endl;
}
int fib(int a)
{
if(a>2)
{
int tp1=1;
int tp2=1;
int temp=0;
int b;
for(b=3;b<(a+1);b++)
{
temp=tp1+tp2;
tp1=tp2;
tp2=temp;
}
return tp2;
}
else return 1;
}
//************************
//* 5_6.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
double poly(int,double);
void main()
{
int n;
double x;
double m;
cout<<"please input n and x"<<endl;
cin>>n>>x;
m=poly(n,x);
cout<<"poly("<<n<<","<<x<<""<<"="<<m<<endl;
}
double poly(int a,double b)
{
if(a>1)
{
return ((2*a-1)*b*poly((a-1),b)-(a-1)*poly((a-2),b))/2;
}
else
{
if (a=1)
return b;
else
return 1;
}
}