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