文章摘要
文章介绍了两个编程问题,首先是一个有理数类Rational的设计,包括成员变量的定义、构造函数、运算符重载以及一个化简分数的方法。Rational类支持四则运算和比较运算符的重载,并能够输出分数形式。其次,定义了一个集合类Set,包括成员变量、构造函数、添加、删除、查找、合并、求交集以及赋值和差集操作的重载运算符。Set类最多可以存放100个不重复的整数。最后,根据房间Room类设计了教室ClassRoom类,该类继承自Room类并添加了座位数属性和计算平均座位面积的方法。文章还包含了测试代码和运行结果。
T1
描述有理数的
Rational类如下,请补充类的其他成员使其能够执行各种运算。
CPPclass Rational { long numerator; // 分子 long denominator; // 分母 // ........ };要求:
重载算术运算符
+、-、*、/,使之能够适用于有理数的四则运算。重载比较运算符
>、<=和==,使之能够比较两个有理数。重载运算符
<<,使其能以规范的方式输出分数,如1/2,-1/3,分母不能为 0。
简单模拟即可,注意化简部分使用辗转相除取到最大公约数:
CPP
#include <iostream>
#include <cstdlib>
class Rational
{
private:
long numerator; // 分子
long denominator; // 分母
void reduce()
{
if (denominator == 0)
return;
if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
long a = std::abs(numerator);
long b = denominator;
while (b != 0)
{
long temp = a % b;
a = b;
b = temp;
}
long gcd = a;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
}
}
public:
Rational(long num = 0, long den = 1)
: numerator(num), denominator(den)
{
if (denominator == 0)
{
denominator = 1;
}
reduce();
}
long getNumerator() const { return numerator; }
long getDenominator() const { return denominator; }
Rational operator+(const Rational& other) const
{
return Rational(
numerator * other.denominator + other.numerator * denominator,
denominator * other.denominator
);
}
Rational operator-(const Rational& other) const
{
return Rational(
numerator * other.denominator - other.numerator * denominator,
denominator * other.denominator
);
}
Rational operator*(const Rational& other) const
{
return Rational(
numerator * other.numerator,
denominator * other.denominator
);
}
Rational operator/(const Rational& other) const
{
return Rational(
numerator * other.denominator,
denominator * other.numerator
);
}
bool operator>(const Rational& other) const
{
return numerator * other.denominator > other.numerator * denominator;
}
bool operator<=(const Rational& other) const
{
return !(*this > other);
}
bool operator==(const Rational& other) const
{
return numerator == other.numerator && denominator == other.denominator;
}
friend std::ostream& operator<<(std::ostream& os, const Rational& r)
{
if (r.denominator == 1)
os << r.numerator;
else
os << r.numerator << "/" << r.denominator;
return os;
}
};
此题没有写测试方面的代码,故没有运行结果。
T2
定义一个集合类
Set,最多存放 100 个不重复的整数,实现集合的如下操作:
增加某个整型元素时,保证集合中没有重复元素;删除指定的元素,查找该元素在集合中则从集合中删除该元素;
重载运算符
+,实现两个集合对象的合并操作;重载运算符*,求两个集合对象的交集;例如:
CPPSet s, s1, s2; s = s1 + s2; s = s1 * s2;重载赋值运算符
=和复合赋值运算符-=,实现两个集合对象的赋值操作与差集操作。例如:
CPPSet s1, s2; s1 = s2; // 赋值 s1 -= s2; // 集合S1中去掉S2中存在的元素要求:编写完整的类定义并附带测试代码。
也是模拟即可,注意100上限的处理:
CPP
#include <iostream>
using namespace std;
class Set
{
private:
int elements[100]; // 存储集合元素
int size; // 当前元素个数
int findPos(int value) const
{
for (int i = 0; i < size; i++)
{
if (elements[i] == value)
return i;
}
return -1;
}
public:
Set() : size(0) {}
bool add(int value)
{
if (size >= 100)
return false;
if (findPos(value) != -1)
return false;
elements[size++] = value;
return true;
}
bool remove(int value)
{
int pos = findPos(value);
if (pos == -1)
return false;
elements[pos] = elements[size - 1];
size--;
return true;
}
bool contains(int value) const
{
return findPos(value) != -1;
}
int getSize() const { return size; }
int getElement(int index) const
{
if (index >= 0 && index < size)
return elements[index];
return 0;
}
Set operator+(const Set& other) const
{
Set result = *this;
for (int i = 0; i < other.size; i++)
{
result.add(other.elements[i]);
}
return result;
}
Set operator*(const Set& other) const
{
Set result;
for (int i = 0; i < size; i++)
{
if (other.contains(elements[i]))
{
result.add(elements[i]);
}
}
return result;
}
Set& operator=(const Set& other)
{
if (this == &other)
return *this;
size = other.size;
for (int i = 0; i < size; i++)
{
elements[i] = other.elements[i];
}
return *this;
}
Set& operator-=(const Set& other)
{
for (int i = 0; i < other.size; i++)
{
remove(other.elements[i]);
}
return *this;
}
friend ostream& operator<<(ostream& os, const Set& s)
{
os << "{ ";
for (int i = 0; i < s.size; i++)
{
os << s.elements[i];
if (i < s.size - 1)
os << ", ";
}
os << " }";
return os;
}
};
int main()
{
Set s1, s2;
// 向 s1 添加元素
s1.add(1);
s1.add(2);
s1.add(3);
s1.add(4);
s1.add(2); // 重复元素,不会添加
// 向 s2 添加元素
s2.add(3);
s2.add(4);
s2.add(5);
s2.add(6);
cout << "s1 = " << s1 << endl; // { 1, 2, 3, 4 }
cout << "s2 = " << s2 << endl; // { 3, 4, 5, 6 }
// 测试并集
Set s3 = s1 + s2;
cout << "s1 + s2 = " << s3 << endl; // { 1, 2, 3, 4, 5, 6 }
// 测试交集
Set s4 = s1 * s2;
cout << "s1 * s2 = " << s4 << endl; // { 3, 4 }
// 测试赋值
Set s5;
s5 = s1;
cout << "s5 (after s5 = s1) = " << s5 << endl; // { 1, 2, 3, 4 }
// 测试差集
s5 -= s2;
cout << "s5 -= s2 => s5 = " << s5 << endl; // { 1, 2 }
// 测试删除元素
s1.remove(3);
cout << "s1 after removing 3 = " << s1 << endl; // { 1, 2, 4 }
// 测试查找
cout << "s1 contains 2? " << (s1.contains(2) ? "Yes" : "No") << endl;
cout << "s1 contains 5? " << (s1.contains(5) ? "Yes" : "No") << endl;
return 0;
}
其中测试代码由AI完成,运行结果如下:
BASH
root@lsj-nas2:/vol2/1000/c程/信息技术实践# cd "/vol2/1000/c程/信息技术实践/" && g++ 3-2.cpp -o 3-2 && "/vol2/1000/c程/信息技术实践/"3-2
s1 = { 1, 2, 3, 4 }
s2 = { 3, 4, 5, 6 }
s1 + s2 = { 1, 2, 3, 4, 5, 6 }
s1 * s2 = { 3, 4 }
s5 (after s5 = s1) = { 1, 2, 3, 4 }
s5 -= s2 => s5 = { 1, 2 }
s1 after removing 3 = { 1, 2, 4 }
s1 contains 2? Yes
s1 contains 5? No
好的,我们先把这个题目整理成规范的 Markdown 格式,然后完成 ClassRoom 类的设计。
T3
根据房间
Room类设计完成教室ClassRoom类。ClassRoom类是Room类的派生类,具有seat属性,代表教室座位数;还具有函数calAverArea(),可以计算得到每个座位所占的平均面积。房间Room类与主函数定义如下:
CPP#include <iostream> using namespace std; class Room // 房间类 { protected: double length; // 房间的长度 double width; // 房间的宽度 public: Room(double l, double w) : length(l), width(w) {} // 构造函数 }; // ...... int main() { ClassRoom cr(30, 12, 10); cr.show(); }程序运行结果如下:
PLAINTEXT创建房间:12米, 10米, 30个座位 长12米, 宽10米, 30个座位, 平均面积4平米
也是模拟,这个主要是继承的训练:
CPP
#include <iostream>
using namespace std;
class Room // 房间类
{
protected:
double length; // 房间的长度
double width; // 房间的宽度
public:
Room(double l, double w) : length(l), width(w) {} // 构造函数
double getArea() const
{
return length * width;
}
void showRoom() const
{
cout << "长" << length << "米, 宽" << width << "米";
}
};
class ClassRoom : public Room
{
private:
int seat;
public:
ClassRoom(int s, double l, double w) : Room(l, w), seat(s) {}
double calAverArea() const
{
if (seat == 0)
return 0;
return getArea() / seat;
}
void show() const
{
cout << "创建房间:" << length << "米, " << width << "米, " << seat << "个座位" << endl;
cout << "长" << length << "米, 宽" << width << "米, "
<< seat << "个座位, 平均面积" << calAverArea() << "平米" << endl;
}
};
int main()
{
ClassRoom cr(30, 12, 10); // 30个座位, 长12米, 宽10米
cr.show();
return 0;
}
运行结果:
BASH
root@lsj-nas2:/vol2/1000/c程/信息技术实践# cd "/vol2/1000/c程/信息技术实践/" && g++ 3-3.cpp -o 3-3 && "/vol2/1000/c程/信息技术实践/"3-3
创建房间:12米, 10米, 30个座位
长12米, 宽10米, 30个座位, 平均面积4平米
本文是原创文章,采用 CC BY-NC-SA 4.0 协议,完整转载请注明来自 烧鸡
评论
隐私政策
0/500
滚动到此处加载评论...