作业18
#include <iostream>
#define N 5
using namespace std;
void main()
{
double *p;
double max,min,temp;
p=new double[N];
for (int i=0;i<N;i++)
cin>>*(p+i);
max=*p;
min=*p;
for (i=1;i<N;i++)
{
if (*(p+i)>max)
max=*(p+i);
if (*(p+i)<min)
min=*(p+i);
}
cout<<"最大值是:"<<max<<endl;
cout<<"最小值是:"<<min<<endl;
//逆序输出数组元素
for (i=0;i<int(N/2);i++)
{
temp=*(p+i);
*(p+i)=*(p+N-i-1);
*(p+N-i-1)=temp;
}
for (i=0;i<N;i++)
{
cout<<*(p+i)<<" ";
}
delete p;
}
作业19
#include <iostream>
using namespace std;
const int n=10;
typedef int array[n];
int main()
{
int start,end;
array a={60,34,55,78,90,99,76,85,96,43};
int m[10];
//int c[6];
array &b=a;
//a[2]=101;
//奇数靠左,偶数靠右
start=0;
end=n-1;
for (int i=0;i<n;i++)
{
if (b[i]%2==1)
m[start++]=a[i];
else
m[end--]=b[i];
}
//奇数由小到大排序,偶数由大到小排序
for (i=0;i<start;i++)
{
for (int j=0;j<start-i-1;j++)
{
if (m[j]>=m[j+1])
{
int temp=m[j];
m[j]=m[j+1];
m[j+1]=temp;
}
}
}
for (i=start;i<=n;i++)
{
for (int j=i-1;j<start+n-i-1;j++)
{
if (m[j]<m[j+1])
{
int temp=m[j];
m[j]=m[j+1];
m[j+1]=temp;
}
}
}
for (i=0;i<n;i++)
cout<<m[i]<<" ";
return 0;
}
作业20
#include <iostream>
#include <algorithm>
#define N 5
using namespace std;
void main()
{
float *p,sum=0;
int i;
p=new float [N];
cout<<"输入5个float类型的值:"<<endl;
for (i=0;i<N;i++)
{
cin>>*(p+i);
sum=sum+*(p+i);
}
for (i=0;i<N;i++)
{
cout<<*(p+i)<<",";
}
cout<<endl;
cout<<"和为:"<<sum<<endl;
sort(p,p+N);//由小到大排序
cout<<"\n最小值是:"<<*p<<endl;
delete p;
}
作业21
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
int a[]={1,2,3,4,5,6,7,8},b[8];
int i;
cout<<"数组a中'4'的位置是:"<<*find(a,a+8,4)<<endl;
int *x;
x=find(a,a+8,4);
cout<<*x<<endl;
copy(a,a+8,b);//将数组a复制给数组b
reverse_copy(b,b+8,a);//把数组b,逆向复制给数组a,完成a的逆转
cout<<"数组a反转后,'4'的位置是:"<<find(a,a+8,4)<<endl;
cout<<"数组a:"<<endl;
for (i=0;i<8;i++)
cout<<a[i]<<",";
cout<<"\n数组b:"<<endl;
for (i=0;i<8;i++)
cout<<b[i]<<",";
}
作业22
#include <iostream>
using namespace std;
template <class T>//定义模板
void sort(T a,T b,T c)
{
T array[3],temp;
int i,j;
array[0]=a;
array[1]=b;
array[2]=c;
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
{
if (array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<array[0]<<" "<<array[1]<<" "<<array[2]<<endl;
}
void main()
{
sort(5,1,9);
sort(5.5,1.1,9.9);
sort('m','b','p');
}
作业23
设计一个点类point,再设计一个矩形类,矩形类使用point的两个坐标点作为
//矩形的对角顶点,并可以输出4个坐标值和面积,使用测试程序验证
#include <iostream>
using namespace std;
class Point
{
int x,y;
public:
Point()
{
x=0;
y=0;
}
Point(int a,int b)
{
x=a;
y=b;
}
void setXY(int a,int b)
{
x=a;
y=b;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
class Rectangle //矩形类
{
Point point1,point2,point3,point4;
public:
Rectangle(){};
Rectangle(Point one,Point two) //用点对象做初始化的构造函数
{
point1=one;
point4=two;
init();
}
Rectangle(int x1,int y1,int x2,int y2)//两对坐标,1和4的对角线顶点
{
point1.setXY(x1,y1);
point4.setXY(x2,y2);
init();
}
void init()
{
point2.setXY(point4.getX(),point1.getY());
point3.setXY(point1.getX(),point4.getY());
}
void printPoint() //输出4个点的函数
{
cout<<"A:("<<point1.getX()<<","<<point1.getY()<<")"<<endl;
cout<<"B:("<<point2.getX()<<","<<point2.getY()<<")"<<endl;
cout<<"C:("<<point3.getX()<<","<<point3.getY()<<")"<<endl;
cout<<"D:("<<point4.getX()<<","<<point4.getY()<<")"<<endl;
}
int getArea()
{
int height,width,area;
height=point1.getY()-point3.getY();
width=point1.getX()-point2.getX();
area=height*width;
if (area>0)
return area;
else
return -area;
}
};
int main()
{
Point p1(-15,56),p2(89,-10);//定义两个点
Rectangle r1(p1,p2);//用两个点作参数,声明一个矩形对象r1
Rectangle r2(1,5,5,1);//用两对边,声明一个矩形r2
cout<<"矩形r1的4个顶点坐标:"<<endl;
r1.printPoint();
cout<<"矩形r1的面积:"<<r1.getArea()<<endl;
cout<<"矩形r2的4个顶点坐标:"<<end;
r2.printPoint();
cout<<"矩形r2的面积:"<<r2.getArea()<<endl;
return 0;
}
作业24
#include <iostream>
#include <cmath>
using namespace std;
class Line
{
int x1,y1,x2,y2;
public:
Line(int =0,int =0,int =0,int =0);
void printPoint();
double getLength();
};
inline Line::Line(int a,int b,int c,int d)
{
x1=a;
y1=b;
x2=c;
y2=d;
}
inline void Line::printPoint()
{
cout<<"A:"<<x1<<","<<y1<<endl;
cout<<"B:"<<x2<<","<<y2<<endl;
}
inline double Line::getLength()
{
double length;
length=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return length;
}
void main()
{
Line line(10.80,-10.12);
line.printPoint();
cout<<line.getLength()<<endl;
}
作业25
#include <iostream>
using namespace std;
class Complex
{
double real,image;
public:
Complex(){};
Complex(double a,double b)
{
real=a;
image=b;
}
void setRI(double a,double b)
{
real=a;
image=b;
}
double getReal()
{
return real;
}
double getImage()
{
return image;
}
void print()
{
if (image>0)
cout<<"复数:"<<real<<"+"<<image<<"i"<<endl;
if (image<0)
cout<<"复数:"<<real<<"-"<<image<<"i"<<endl;
}
friend Complex add(Complex,Complex);
};
Complex add(Complex c1,Complex c2)
{
Complex c3;
c3.real=c1.real+c2.real;
c3.image=c1.image+c2.image;
return c3;
}
void main()
{
Complex c1(19,0.864),c2,c3;
c2.setRI(90,125.012);
c3=add(c1,c2);
cout<<"复数一:";
c1.print();
cout<<"复数二:";
c2.print();
cout<<"相加后:"
c3.print();
}