本来想写单继承的,结果太简单了,就挑了两道,一道是没来得及想简便方法,另一道是麻烦……
题目一:判断矩形是否重叠(复合类+友元)
题目描述:
用CPoint表示点,用两个CPoint对象表示矩形类CRect的对角线两点。分别实现CPoint类和CRect类,并在主函数用输入的坐标定义4个CPoint类对象,每2个CPoint对象再构造1个CRect对象,然后写个友元函数,判断2个矩形是否重叠。
输入要求:
判断次数
矩形1的对角线顶点坐标x1, y1, x2, y2
矩形2的对角线顶点坐标x1, y1, x2, y2
......
输出要求:
是否重叠
输入样例:
3
1 5 2 9
1 3 2 4
5 6 7 8
5 7 7 7
2 5 1 0
9 4 2 9
输出样例:
not overlapped
overlapped
overlapped
代码示例:
#include
#include
#include
#include
#include
#include
using namespace std;
class Point
{
public:
Point() {}
Point(int x0, int y0) :x(x0), y(y0) {}
int getX(){ return x; }
int getY(){ return y; }
private:
int x, y;
};
class Rect
{
public:
Rect() {}
Rect(Point po1, Point po2) :p1(po1), p2(po2) {}
friend void islap(Rect& re1, Rect& re2);
private:
Point p1, p2;
};
void islap(Rect& re1, Rect& re2)
{
int rmaxx1, rminx1, rmaxy1, rminy1;
int rmaxx2, rminx2, rmaxy2, rminy2;
if (re1.p1.getX() > re1.p2.getX())
{
rmaxx1 = re1.p1.getX();
rminx1 = re1.p2.getX();
}
else
{
rminx1 = re1.p1.getX();
rmaxx1 = re1.p2.getX();
}
if (re1.p1.getY() > re1.p2.getY())
{
rmaxy1 = re1.p1.getY();
rminy1 = re1.p2.getY();
}
else
{
rminy1 = re1.p1.getY();
rmaxy1 = re1.p2.getY();
}
if (re2.p1.getX() > re2.p2.getX())
{
rmaxx2 = re2.p1.getX();
rminx2 = re2.p2.getX();
}
else
{
rminx2 = re2.p1.getX();
rmaxx2 = re2.p2.getX();
}
if (re2.p1.getY() > re2.p2.getY())
{
rmaxy2 = re2.p1.getY();
rminy2 = re2.p2.getY();
}
else
{
rminy2 = re2.p1.getY();
rmaxy2 = re2.p2.getY();
}
if (rmaxx1 < rminx2 || rmaxx2 < rminx1 || rminy1 > rmaxy2 || rminy2 > rmaxy1)
{
cout << "not overlapped" << endl;
}
else
{
cout << "overlapped" << endl;
}
}
int main()
{
int t, x11, x12, y11, y12, x21, x22, y21, y22;
cin >> t;
while (t--)
{
cin >> x11 >> y11 >> x12 >> y12;
Point pt1(x11, y11);
Point pt2(x12, y12);
cin >> x21 >> y21 >> x22 >> y22;
Point pt3(x21, y21);
Point pt4(x22, y22);
Rect r1(pt1, pt2);
Rect r2(pt3, pt4);
islap(r1, r2);
}
return 0;
}
这个判断是否重叠太麻烦了,考虑考虑简单的方法,别无脑Copy!
题目二:OOP一元多项式类运算(类+对象)
题目描述:
一元多项式按照升幂表示为:
Pn(x) = p0+ p1x + p2x2+ … +pnxn。(n>=0)
构建一元多项式类,数据成员用于保存多项式中每项的系数和指数,数据操作实现两个一元多项式的加、减、乘。
输入要求:
测试数据数
对于每组测试数据
第一行,第一个多项式:多项式项数n 系数1 指数1 系数2 指数2 ...... 系数n 指数n
第二行,第二个多项式:多项式项数n 系数1 指数1 系数2 指数2 ......系数n 指数n
数据保证输入数据中多项式均为升幂且所有数据都为整数
输出要求:
每组测试数据输出三行,分别是两个多项式的加、减、乘。
具体输出格式见样例(即人们习惯的多项式表示,需要额外注意系数或指数为1,-1的情况。)
输入样例:
2
3 -9 0 4 1 -3 5
1 -7 0
2 1 0 -1 3
3 1 0 1 1 -1 2
输出样例:
-16+4x-3x^5
-2+4x-3x^5
63-28x+21x^5
2+x-x^2-x^3
-x+x^2-x^3
1+x-x^2-x^3-x^4+x^5
代码示例:
#include
#include
#include
#include
#include
#include
using namespace std;
class X
{
public:
X() :C(0) {}
void setX(int c) { C = c; }
void setMul(int c) { C += c; }
int getC() { return C; }
private:
int C;
};
class Pnx
{
public:
Pnx() {}
Pnx(int nn)
{
px = new X[100];
maxc = 0;
int c, n;
for (int i = 0; i < nn; i++)
{
cin >> c >> n;
px[n].setX(c);
if (n > maxc)
maxc = n;
}
}
~Pnx() { delete[]px; }
X getPx(int nn) { return px[nn]; }
X* getPx0(int nn) { return &px[nn]; }
int getMaxc() { return maxc; }
private:
X* px;
int maxc;
};
void add(Pnx& a, Pnx& b)
{
bool mark = false;
int maxi = max(a.getMaxc(), b.getMaxc());
for (int i = 0; i <= maxi; i++)
{
if (i == 0)
{
if (a.getPx(i).getC() + b.getPx(i).getC() != 0)
{
mark = true;
cout << a.getPx(i).getC() + b.getPx(i).getC();
}
}
else if (i == 1)
{
if (mark)
{
if (a.getPx(i).getC() + b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() + b.getPx(i).getC() > 1)
{
cout << "+";
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x";
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == 1)
{
cout << "+";
cout << "x";
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == -1)
{
cout << "-";
cout << "x";
}
else
{
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x";
}
}
}
else
{
if (a.getPx(i).getC() + b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() + b.getPx(i).getC() > 1)
{
mark = true;
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x";
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == 1)
{
mark = true;
cout << "x";
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == -1)
{
mark = true;
cout << "-";
cout << "x";
}
else
{
mark = true;
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x";
}
}
}
}
else
{
if (mark)
{
if (a.getPx(i).getC() + b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() + b.getPx(i).getC() > 1)
{
cout << "+";
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x^" << i;
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == 1)
{
cout << "+";
cout << "x^" << i;
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == -1)
{
cout << "-";
cout << "x^" << i;
}
else
{
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x^" << i;
}
}
}
else
{
if (a.getPx(i).getC() + b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() + b.getPx(i).getC() > 1)
{
mark = true;
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x^" << i;
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == 1)
{
mark = true;
cout << "x^" << i;
}
else if (a.getPx(i).getC() + b.getPx(i).getC() == -1)
{
mark = true;
cout << "-";
cout << "x^" << i;
}
else
{
mark = true;
cout << a.getPx(i).getC() + b.getPx(i).getC() << "x^" << i;
}
}
}
}
}
if (!mark)
{
cout << '0';
}
cout << endl;
}
void sub(Pnx& a, Pnx& b)
{
bool mark = false;
int maxi = max(a.getMaxc(), b.getMaxc());
for (int i = 0; i <= maxi; i++)
{
if (i == 0)
{
if (a.getPx(i).getC() - b.getPx(i).getC() != 0)
{
mark = true;
cout << a.getPx(i).getC() - b.getPx(i).getC();
}
}
else if (i == 1)
{
if (mark)
{
if (a.getPx(i).getC() - b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() - b.getPx(i).getC() > 1)
{
cout << "+";
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x";
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == 1)
{
cout << "+";
cout << "x";
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == -1)
{
cout << "-";
cout << "x";
}
else
{
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x";
}
}
}
else
{
if (a.getPx(i).getC() - b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() - b.getPx(i).getC() > 1)
{
mark = true;
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x";
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == 1)
{
mark = true;
cout << "x";
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == -1)
{
mark = true;
cout << "-";
cout << "x";
}
else
{
mark = true;
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x";
}
}
}
}
else
{
if (mark)
{
if (a.getPx(i).getC() - b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() - b.getPx(i).getC() > 1)
{
cout << "+";
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x^" << i;
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == 1)
{
cout << "+";
cout << "x^" << i;
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == -1)
{
cout << "-";
cout << "x^" << i;
}
else
{
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x^" << i;
}
}
}
else
{
if (a.getPx(i).getC() - b.getPx(i).getC() != 0)
{
if (a.getPx(i).getC() - b.getPx(i).getC() > 1)
{
mark = true;
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x^" << i;
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == 1)
{
mark = true;
cout << "x^" << i;
}
else if (a.getPx(i).getC() - b.getPx(i).getC() == -1)
{
mark = true;
cout << "-";
cout << "x^" << i;
}
else
{
mark = true;
cout << a.getPx(i).getC() - b.getPx(i).getC() << "x^" << i;
}
}
}
}
}
if (!mark)
{
cout << '0';
}
cout << endl;
}
void mul(Pnx& a, Pnx& b)
{
int arr[100];
bool mark = false;
for (int i = 0; i < 100; i++)
{
arr[i] = 0;
}
for (int i = 0; i <= a.getMaxc(); i++)
{
for (int j = 0; j <= b.getMaxc(); j++)
{
arr[i + j] += a.getPx(i).getC() * b.getPx(j).getC();
}
}
for (int i = 0; i <= a.getMaxc() + b.getMaxc(); i++)
{
if (i == 0)
{
if (arr[i] != 0)
{
mark = true;
cout << arr[i];
}
}
else if (i == 1)
{
if (mark)
{
if (arr[i] != 0)
{
if (arr[i] > 1)
{
cout << "+";
cout << arr[i] << "x";
}
else if (arr[i] == 1)
{
cout << "+";
cout << "x";
}
else if (arr[i] == -1)
{
cout << "-";
cout << "x";
}
else
{
cout << arr[i] << "x";
}
}
}
else
{
if (arr[i] != 0)
{
if (arr[i] > 1)
{
mark = true;
cout << arr[i] << "x";
}
else if (arr[i] == 1)
{
mark = true;
cout << "x";
}
else if (arr[i] == -1)
{
mark = true;
cout << "-";
cout << "x";
}
else
{
mark = true;
cout << arr[i] << "x";
}
}
}
}
else
{
if (mark)
{
if (arr[i] != 0)
{
if (arr[i] > 1)
{
cout << "+";
cout << arr[i] << "x^" << i;
}
else if (arr[i] == 1)
{
cout << "+";
cout << "x^" << i;
}
else if (arr[i] == -1)
{
cout << "-";
cout << "x^" << i;
}
else
{
cout << arr[i] << "x^" << i;
}
}
}
else
{
if (arr[i] != 0)
{
if (arr[i] > 1)
{
mark = true;
cout << arr[i] << "x^" << i;
}
else if (arr[i] == 1)
{
mark = true;
cout << "x^" << i;
}
else if (arr[i] == -1)
{
mark = true;
cout << "-";
cout << "x^" << i;
}
else
{
mark = true;
cout << arr[i] << "x^" << i;
}
}
}
}
}
if (!mark)
{
cout << '0';
}
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
Pnx p1(n);
cin >> n;
Pnx p2(n);
Pnx p3(0);
add(p1, p2);
sub(p1, p2);
mul(p1, p2);
}
return 0;
}
也不想写什么注释了,反正就是不是最简单的方法,建议另谋出路!