其他分享
首页 > 其他分享> > 第六次课——7.11

第六次课——7.11

作者:互联网

作业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;

}

 

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

}

 

设计一个点类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);

    intit();

  }

  void init()

  {

    Point2,setXY(Point4.getX(),Point1.getY());

    Point3.setXY(Point1.getX(),Point4.getY());

  }

  void printPoint()

  {

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

  Rectangle r2(1,5,5,1);

  cout<<"矩形r1的4个顶点坐标:"<<endl;

  r1.printPoint();

  cout<<"矩形r1的面积:"<<r1.getArea()<<endl;

  cout<<"矩形r2的4个顶点坐标:"<<end;

  r2.printPoint();

  cout<<"矩形r2的面积:"<<r2.getArea()<<endl;

  return 0;

}

  

 

标签:return,7.11,temp,Point,int,第六次,array,cout
来源: https://www.cnblogs.com/cnxm/p/14998682.html