第七周作业

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

例1

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

例2

#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;
}

例3

#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;
}

例4

#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;
}

例5

#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;
}

这个c++系统是从右到左的处理顺序

例6

#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 5"<<c<<endl;
	return 0;
}


例7

#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'
		<<"sqt("<<t<<")="<<s<<endl;

	return 0;
}

例8

#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;
}

例9

#include<iostream>
using namespace std;

void swap(int&u,int &);

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 = v;
	v=u;
	u=temp;
}

例10

#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;

}

例11



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

void GetRoots(/*in*/double,/*in*/double,/*in*/double,
			  /*out*/double&,/*out*/double&);
int main()
{
    double a,b,c;
	double root1,root2;
    cout<<"输入方程的三个系数a,b,c:"<<endl;
	cin>>a>>b>>c;
	GetRoots(a,b,c,root1,root2);
	cout<<"root1= "<<root1<<"root2 ="<<root2<<endl;

	return 0;

}
void GetRoots(/*in*/double a,/*in*/double b,/*in*/double c,
			  /*out*/double&root1,/*out*/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);
	
}

例12

#include<iostream>
using namespace std;

void swap(/*inout*/int&u,/*inout*/int &);

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 = v;
	v=u;
	u=temp;
}

例13

#include<iostream>
using namespace std;

long f2(int);
long f1(int p)
{
    int k;
    long r;
    k=p*p;
    r=f2(k);
	
	return r;
}
long f2(int q)
{
    long fact = 1;
    for(int i=1;i<=q;i++)
	   fact*=i;
    return fact;
}
int main()
{
    int i;
    long sum=0;
    for(i=2;i<=3;i++)
	    sum+=f1(i);
    cout<<"sum ="<<sum<<endl;
	return 0;
}

例14

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

float f(float x);
float root(float x1,float x2);
float point(float x1,float x2);

int main()
{
    float x1,x2,y1,y2,x;
    do
	{
	    cout<<"请输入根所在的范围:";
		cin>>x1>>x2;
		y1=f(x1);
		y2=f(x2);
		cout<<"两端点的值为["<<y1<<","<<y2<<"]"<<endl;

	}while(y1*y2>=0);
	x=root(x1,x2);
	cout<<"在"<<x1<<"与"<<x2<<"之间,方程的解为"<<x<<endl;

	return 0;
   
}
float f(float x)
{
    return(x*x*x-4*x*x+6*x-10);
}
float root(float x1,float x2)
{
    float y1,x,y;
	y1=f(x1);
	do
	{
	    x=point(x1,x2);
		y=f(x);
		if(y*y1>0)
		{
		   y1=y;x1=x;
		}
		else x2=x;
	}while(fabs(y)>=0.0001);

    return x;

}
float point(float x1,float x2)
{
    float y;
    y =(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));

	return y;
}

例15

#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;
}

例16

#include<iostream>
using namespace std;

const N=8;
long fibo(int n);

int main()
{
   long f=fibo(N);
   cout<<"Fibonacci数列第8项的值为:"<<f<<endl;

   return 0;
}
long fibo(int n)
{
   if(n==1)return 1L;
   else if(n==2)return 1L;
   else
	   return fibo(n-1)+fibo(n-2);

}

例17

#include<iostream>
using namespace std;

int i;
int main()
{
   i=5;
   {
      int i;
	  i=7;        
	  cout<<"内层i ="<<i<<endl;
   } 
   cout<<"外层i ="<<i<<endl;

   return 0;
}

例18

#include<iostream>
using namespace std;

int i=1;
int main()                  
{
    cout<<"i= "<<i<<endl;
	int i=5;
    cout<<"i= "<<i<<endl;
    {
	   int i=7;
	   cout<<"i= "<<i<<endl;
	   cout<<"i= "<<::i<<endl;
	}
	cout<<"i= "<<i<<endl;
	cout<<"i= "<<::i<<endl;

	return 0;
}



例19

#include<iostream>
using namespace std;

int max(int x,int y)
{
    int z;
	z=x>y?x:y;
	return z;
}
int main()
{
   extern int a,b;
   cout<<max(a,b)<<endl;

   return 0;
}
int a=13,b=-8;

例20

#include<iostream>
using namespace std;

void sort(int);
void echoa();
int a[5] = {6,3,9,8,2};
int main()
{
    echoa();
	sort(5);
	echoa();
    
	return 0;
}
void echoa()
{
    for(int i=0;i<5;i++)
		cout<<a[i]<<'\t';
	cout<<endl;
}
void sort(int n)
{
    int i,j,min,temp;
	for(i=0;i<n-1;i++)
	{
	   min=1;
	   for(j=i+1;j<n;j++)
		   if(a[j]<a[min])
			   min=j;
	   temp=a[i];a[i]=a[min];a[min]=temp;
	}

}

例21

#include<iostream>
using namespace std;

int a=3,b=5;

int main()
{
   int a=8;
   int c;
   c=a>b?a:b;
   cout<<c<<endl;

   return 0;
}

例22

#include<iostream>
using namespace std;

int f(int a)
{
   auto int b=0;
   static int c=3;
   b++;
   c++;
   return (a+b+c);
}
int main()
{
    int a=2,i;
	for(i=0;i<3;i++)
	   cout<<f(a)<<endl;
	
	return 0;
}

例23

#include<iostream>
using namespace std;

int fun(int);

int main()
{
    int i,k;
	cout<<"请输入一个非负整数:";
	cin>>k;
	for(i=1;i<=k;i++)
		cout<<i<<"!="<<fun(i)<<endl;
	
	return 0;

}
int fun(int n)
{
    static int f=1;
    f=f*n;
    return f;
}


例24

#include<iostream>
using namespace std;

int fun(int);

int main()
{
    int i,k;
	cout<<"请输入一个非负整数:";
	cin>>k;
	for(i=1;i<=k;i++)
		cout<<i<<"!= "<<fun(i)<<endl;
	
	return 0;
}
int fun(int n)
{
    register int i,f=1;
	for(i=1;i<=n;i++)
		f=f*i;
	return f;
}

例25

#include<iostream>
using namespace std;

int a;
int power(int n);

int main()
{
    int b=3,c,d,m;
	cout<<"enter a and m:"<<endl;
	cin>>a>>m;
	c=a*b;
	cout<<"a= "<<a<<"\tb= "<<b<<"\tc= "<<c<<endl;
	d=power(m);
	cout<<"a= "<<a<<"\tm= "<<b<<"\td= "<<d<<endl;

	return 0;
}
extern int a;

int power(int n)
{
    int i,y=1;
	for(i=1;i<=n;i++)
		y*=a;
    return y;
}

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

习题2

#include<iostream>
using namespace std;
long intPower(int base,int exponent)
{
    long a=1;
	int i;
	for(i=1;i<=exponent;i++)
		a*=base;
	
	return a;

}
int main()
{
    int base,exponent;
	cout<<"请输入一个数:"<<endl;
	cin>>base;
	cout<<"请输入它的指数:"<<endl;
	cin>>exponent;
	cout<<endl;
	cout<<"这个数的幂为:"<<intPower(base,exponent)<<endl;;

	return 0;

}

习题3

#include<iostream>
using namespace std;
int sushu(int x)
{
int i,j=1;
for(i=2;i<x/2&&j==1;i++)
if(x%i==0)
j=0;
return j;
}


int main()
{
int n;
cout<<"请输入一个正整数:";
cin>>n;
if(n<=0)
cout<<"输入的不是正整数"<<endl;
else if(n==1||n==4)
cout<<"这个数不是素数"<<endl;
else if(n==2||sushu(n))
cout<<"这个数是素数"<<endl;
else
cout<<"这个数不是素数"<<endl;
return 0;
}

习题4


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值