面向对象程序设计第三章

简介: 面向对象程序设计第三章

1.点、线、三角形

【问题描述】根据点的类构造线三角形类,并测试

#include <iostream>
//根据点的定义,写出线段和三角形的定义,并通过主程序进行验证
//mypoint.h
class myPoint
{
public:
  myPoint();
  myPoint(double x, double y);
  double getX();
  double getY();
private:
  double mX,mY;
};
#include<cmath>
#include<iostream>
using namespace std;
class Line
{
public:
  Line(const myPoint& p1, const myPoint& p2)
  {
    this->startPoint = p1;
    this->endPoint = p2;
  }
  double GetDistance()
  {
    double ans,dx, dy;
    dx = (this->startPoint.getX()) - (this->endPoint.getX());
    dy = (this->startPoint.getY()) - (this->endPoint.getY());
    ans = sqrt(dx * dx + dy * dy);
    return ans;
  }
private:
  myPoint startPoint, endPoint;
};
class Triangle
{
public:
  Triangle(const myPoint& _p1, const myPoint& _p2, const myPoint& _p3)
  {
    p1 = _p1;
    p2 = _p2;
    p3 = _p3;
  }
  double getGirth()
  {
    Line L1(p1, p2), L2(p2, p3), L3(p1, p3);
    return L1.GetDistance() + L2.GetDistance() + L3.GetDistance();
  }
  double getArea()
  {
    Line L1(p1, p2), L2(p2, p3), L3(p1, p3);
    double p, a, b, c, s;
    a = L1.GetDistance();
    b = L2.GetDistance();
    c = L3.GetDistance();
    p = (a + b + c) / 2;
    s = sqrt(p * (p - a) * (p - b) * (p - c));
    return s;
  }
private:
  myPoint p1, p2, p3;
};
myPoint::myPoint()
{
  mX = 0; mY = 0;
}
myPoint::myPoint(double x, double y)
{
  mX = x;
  mY = y;
}
double myPoint::getX()
{
  return mX;
}
double myPoint::getY()
{
  return mY;
}
int main()
{
  double x1, x2, x3, y1, y2, y3;
  cout << "请输入点1的x的值:";
  cin >> x1;
  cout << "请输入点1的y的值:";
  cin >> y1;
  cout << "请输入点2的x的值:";
  cin >> x2;
  cout << "请输入点2的y的值:";
  cin >> y2;
  cout << "请输入点3的x的值:";
  cin >> x3;
  cout << "请输入点3的y的值:";
  cin >> y3;
  cout << "点1的坐标为:(" << x1 << "," << y1 << ")" << endl;
  cout << "点2的坐标为:(" << x2 << "," << y2 << ")" << endl;
  cout << "点3的坐标为:(" << x3 << "," << y3 << ")" << endl;
  myPoint p1(x1, y1), p2(x2, y2), p3(x3, y3);
  Line  line1(p1,p2);
  cout<<"线长度:"<<line1.GetDistance()<<endl;
  Triangle t(p1, p2, p3);
  cout << "该三角形的周长为:" << t.getGirth() << endl;
  cout << "该三角形的面积为:" << t.getArea() << endl;
  return 0;
}

2. 编写程序,实现动态顺序表,增加拷贝构造函数

#include <iostream>
using namespace std;
class myArrayList
{
public:
  myArrayList(int size=0);
  myArrayList(const myArrayList &t);
  ~myArrayList(void);
  void Input(int n);
  void Output();
  void SortBySelect();//选择排序
  void Append(int num);//在最后插入一个数num
private:
  int *m_arr;
  int m_size;
  int m_len;
};
#include<iostream>
using namespace std;
myArrayList::myArrayList(int size)
{
  if (size > 0)
  {
    this->m_arr = new int[size];
    this->m_size = size;
    this->m_len = 0;
    for (int i = 0; i < this->m_size; i++)
    {
      m_arr[i] = 0;
    }
  }
  else
  {
    m_arr = nullptr;
    m_size = 0;
    m_len = 0;
  }
}
myArrayList::myArrayList(const  myArrayList& t) 
{
  m_size = t.m_size;
  m_arr = new int[m_size];
  m_len = t.m_len;
  for (int i = 0; i < m_len; i++)
    m_arr[i] = t.m_arr[i];
}
void myArrayList::Append(int t)
{
  if (m_len < m_size)
  {
    m_arr[m_len] = t;
    m_len++;
  }
}
void myArrayList::SortBySelect()
{
  int i, j, t;
  for (i = 0; i < m_len; i++)
  {
    for (j = 0; j < m_len - i - 1; j++)
    {
      if (m_arr[j] > m_arr[j + 1])
      {
        t = m_arr[j];
        m_arr[j] = m_arr[j + 1];
        m_arr[j + 1] = t;
      }
    }
  }
}
myArrayList::~myArrayList(void)
{
  delete[]m_arr;
}
void myArrayList::Input(int n)
{
  this->m_len=n;
  for(int i=0;i<this->m_len;i++)
    cin>>this->m_arr[i];
}
void myArrayList::Output()
{
  cout<<endl;
  for(int i=0;i<this->m_len;i++)
    cout<<this->m_arr[i]<<"  ";
    cout<<endl;
}
int main()
{
  myArrayList list1(20);
  list1.Input(5);
  list1.SortBySelect();
  list1.Output();
  list1.Append(8);
  list1.Output();
  myArrayList list2(list1);
  list2.Output();
}


目录
打赏
0
0
0
0
54
分享
相关文章
|
3月前
|
面向对象程序设计
面向对象程序设计
69 26
20 C++ - 面向对象程序设计案例
20 C++ - 面向对象程序设计案例
134 0
什么是程序设计
一、什么是程序设计 程序设计是指通过编写、测试和维护计算机程序来解决问题或实现特定功能的过程。它涉及到确定问题的需求、设计算法、选择合适的编程语言、编写代码、调试和测试程序等步骤。程序设计的目标是创建高效、可靠、易于理解和维护的软件。 二、程序设计具有以下特点 1. 抽象性:程序设计是一种高度抽象的活动,它涉及到将实际问题转化为计算机可以理解和执行的指令。 2. 逻辑性:程序设计需要遵循严格的逻辑结构和规则,以确保程序的正确性和可靠性。逻辑思维和分析能力是程序设计的重要组成部分。 3. 创造性:程序设计是一种创造性的活动,程序员需要在解决问题的过程中提出新的思路和方法,以实现更好的效果。
706 0
面向对象程序设计C++
面向对象程序设计C++
|
10月前
|
C++
25面向对象的程序设计
25面向对象的程序设计
63 1
|
10月前
|
程序设计 (3)
程序设计 (3)
64 0
|
10月前
|
程序设计 (4)
程序设计 (4)
50 0
|
10月前
|
程序设计 (2)
程序设计 (2)
43 0
面向对象程序设计第四章
面向对象程序设计第四章
168 1
面向对象程序设计第四章
面向对象程序设计-第二章
面向对象程序设计-第二章
85 0
面向对象程序设计-第二章
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等