C++编程题

1.用函数模板实现3个数值中按最小值到最大值排序的程序。

#include < iostream >

using namespace std;

template 

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

}

2.利用函数模板设计一个求数组元素中和的函数,并检验之。

#include < iostream >

using namespace std;

template 

T sum (T a[],int n)

{

          int i;

          T s=0;

          for(i=0;i< n;i++)

                    s = s + a[i];

          return s;

}

void main ()

{

          int a[5]={1,2,3,4,5};

          int s = sum(a,5);

          cout<< s<< endl;

}

3.重载上题中的函数模板,使他能够进行两个数组的求和。
#include < iostream >
using namespace std;
template 
T sum (T a[], int n)
{
          int i;
          T s=0;
          for(i=0;i< n;i++)
                    s = s + a[i];
          return s;
}
template //重载上面的模板
T sum (T a[], int n, T b[], int m)
{
          return sum(a,n)+sum(b,m);
}
void main ()
{
          int a[5]={1,2,3,4,5};
          int b[10]={1,2,3,4,5,6,7,8,9,10};
          int s1 = sum(a, 5);
          int s2 = sum(b, 10);
          int s3= sum(a, 5, b, 10);
          cout<< s1<< endl;
          cout<< s2<< endl;
          cout<< s3<< endl;
}
4.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。并可以输出4个坐标值和面积。使用测试程序验证程序。

#include 

using namespace std;

class Point         //点类

{

          private:

                    int x, y;//私有成员变量,坐标

          public :

                    Point()//无参数的构造方法,对xy初始化

                               {

                                         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()//得到x的方法

                               {

                                         return x;

                               }

                               int getY()//得到有的函数

                               {

                                         return y;

                               }

};

class Rectangle     //矩形类

{

private:

          Point point1, point2, point3, point4;

  public :

                               Rectangle();//类Point的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了

Rectangle(Point one, Point two)

                               {

                                         point1 = one;

                                         point4 = two;

                                         init();

                               }

          Rectangle(int x1, int y1, int x2, int y2)

                               {

                               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()//打印四个点的函数

                               {

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;

                                         

                               }

};

void 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<<"\n矩形r2的4个定点坐标:"<< endl;

r2.printPoint();

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

}

5.使用内联函数设计一个类,用来表示直角坐标系中的任意一条直线并输出它的属性。

#include < iostream.h >

#include < math.h >

class Line

{

          private:

                    int x1, y1, x2, y2;

          public :

                    Line();

                    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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值