1、C01:看上去好坑的运算符重载
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空
#include <iostream>
using namespace std;
class MyInt
{
int nVal;
public:
MyInt( int n) { nVal = n ;}
// 在此处补充你的代码
MyInt& operator-(int n)
{
nVal -= n;
return *this;
}
operator int()
{
return nVal;
}
// end of my code
};
int Inc(int n) {
return n + 1;
}
int main () {
//分析main函数的执行过程:
//1.int型为参数的有参构造函数,已实现
//2.重载以int为参数的"-"运算符,由于修改了nVal,所以返回值是自身,返回值类型为类引用
//3.Inc的参数为int,而objInt为类对象,需要重载类型转换运算符
int n;
while(cin >>n) {
MyInt objInt(n);
objInt-2-1-3;
cout << Inc(objInt);
cout <<",";
objInt-2-1;
cout << Inc(objInt) << endl;
}
return 0;
}
输入
多组数据,每组一行,整数n
输出
对每组数据,输出一行,包括两个整数, n-5和n - 8
样例输入
20
30
样例输出
15,12
25,22
来源
Guo Wei
2、C02:惊呆!Point竟然能这样输入输出
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() { };
// 在此处补充你的代码
friend istream & operator>>(istream & is, Point &rhs)
{
is >> rhs.x >> rhs.y;
return is;
}
friend ostream & operator<<(ostream & os, const Point &rhs)
{
os << rhs.x << "," << rhs.y;
return os;
}
// end of my code
};
int main()
{
//分析main函数的执行过程:
//1.无参构造函数,已实现
//2.重载参数为类对象的>>运算符
//3.重载参数为类对象的<<运算符
//Note:istream和ostream类的对象,构造函数均为private,不能自定义流对象
//运算符<<和运算符>>的重载一律声明为友元函数,参数类型和返回值的类型均为流对象的引用
Point p;
while(cin >> p) {
cout << p << endl;
}
return 0;
}
输入
多组数据,每组两个整数
输出
对每组数据,输出一行,就是输入的两个整数
样例输入
2 3
4 5
样例输出
2,3
4,5
来源
Guo Wei
3、C03:第四周程序填空题3
总时间限制: 1000ms 内存限制: 65536kB
描述
写一个二维数组类 Array2,使得下面程序的输出结果是:
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
程序:
#include <iostream>
#include <cstring>
using namespace std;
class Array2 {
// 在此处补充你的代码
private:
int row;
int col;
int size_;
int *pInt;
public:
Array2(int r = 0, int c = 0) :row(r), col(c), size_(r*c)
{
if (size_ == 0)
pInt = nullptr;
else
pInt = new int[size_];
}
Array2 & operator=(const Array2 & rhs)
{
if (this == &rhs)
return *this;
row = rhs.row;
col = rhs.col;
size_ = rhs.size_;
if (size_ == 0)
pInt = nullptr;
else
{
pInt = new int[size_];
memcpy(pInt, rhs.pInt, size_ * sizeof(int));
}
return *this;
}
int * operator[](int n)
{
return pInt + n * col;
}
int operator()(int i, int j)
{
return *(pInt + i * col + j);
}
// end of my code
};
int main() {
//分析main函数的执行过程:
//1.参数为两个int型的有参构造函数,指明了row和col
//开辟了一块内存空间,有成员int* ptr,用size记录需要申请空间的大小
//2.a[i][j]可以作为左值被赋值,类重载了运算符[],返回int*,
//第二个[]是内部类型的解析,不用理会
//3.a(i,j)得到了a[i][j],类重载了参数为两个int型的运算符(),返回值为int
//4.Array2 b,需要无参构造函数,需要初始化各个成员变量,可以并到有参构造函数
//5.b = a,需要重载赋值运算符函数
Array2 a(3,4);
int i,j;
for( i = 0;i < 3; ++i )
for( j = 0; j < 4; j ++ )
a[i][j] = i * 4 + j;
for( i = 0;i < 3; ++i ) {
for( j = 0; j < 4; j ++ ) {
cout << a(i,j) << ",";
}
cout << endl;
}
cout << "next" << endl;
Array2 b; b = a;
for( i = 0;i < 3; ++i ) {
for( j = 0; j < 4; j ++ ) {
cout << b[i][j] << ",";
}
cout << endl;
}
return 0;
}
4、C04:别叫,这个大整数已经很简化了!
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
// 在此处补充你的代码
//核心功能为+运算,需要低位对齐,带进位地向高位计算,int与char * 均按每一位的值逆序存储
//保存操作数的位数,在输出的时候需要用到
//s最多为200位,如果按实际位数分配,那么在实际的+操作时,可能需要比较操作数的位数...等额外的判别
//所以一律固定申请的位数为200+,用length代表实际位数,用空间换取一些逻辑上的简化
//Note:空间大小固定,逆序存储,保存位数
private:
int *ptr;
int length;
public:
CHugeInt(const char *pstr)
{
length = strlen(pstr);
ptr = new int[210];
memset(ptr, 0, 210 * sizeof(int));
for (int i = 0; i < length; ++i)
*(ptr + i) = *(pstr + length - 1 - i) - '0';
}
CHugeInt(int n)
{
ptr = new int[210];
memset(ptr, 0, 210 * sizeof(int));
int i = 0;
while (n)
{
*(ptr + i) = n % 10;
n /= 10;
++i;
}
length = i;
}
CHugeInt(const CHugeInt & rhs) //copy constructor
{
length = rhs.length;
ptr = new int[210];
memset(ptr, 0, 210 * sizeof(int));
memcpy(ptr, rhs.ptr,210*sizeof(int));
}
//为了支持+=,++这种改变了自身状态的操作,需要重载赋值运算符
//由于涉及pointer,浅复制会double free,需要自定义深复制的操作
CHugeInt & operator=(const CHugeInt & rhs)
{
if (this == &rhs)
return *this;
length = rhs.length;
memcpy(ptr, rhs.ptr, 210 * sizeof(int));
return *this;
}
CHugeInt & operator +=(const int &n)
{
//思路是委托函数CHugeInt operator+(const CHugeInt & x, const int & y)执行
//再委托operator+(const CHugeInt & x, const CHugeInt & y)执行
return *this = *this + n;
}
//核心功能bolck,由于3个加法函数又是重载关系,又是委托关系,都设定为友元函数
friend CHugeInt operator+(const CHugeInt & x, const CHugeInt & y)
{
CHugeInt temp(0);//operarator + 返回一个CHugeInt类的对象
//temp作为存储和的对象,两个操作数非负,其长度至少为两个加数中较大的那个
//加法执行完毕之后可能还需依进位增加其长度
temp.length = x.length > y.length ? x.length : y.length;
//这里体现出申请的是int型空间的优越性,一轮完成加法,暂不处理进位
//若是申请的char型的空间,想要表示10到18这9个溢出的加法标志就有些折腾了
//用空间换逻辑性
for (int i = 0; i < temp.length; ++i)
*(temp.ptr + i) = *(x.ptr + i) + *(y.ptr + i);
for (int i = 0; i < temp.length; ++i)
{
if (*(temp.ptr + i)>=10)
{
*(temp.ptr + i) -= 10;
*(temp.ptr + i + 1) += 1;
}
}
//最高位有进位,更新加法结果的位数
if (*(temp.ptr + temp.length) == 1)
++temp.length;
//return call copy constructor
return temp;
}
//下面的两个形式,核心思路都是用参数构造临时对象,委托核心模块完成功能
friend CHugeInt operator+(const int & x, const CHugeInt & y)
{
CHugeInt temp(x);
return temp + y;
}
friend CHugeInt operator+(const CHugeInt & x, const int & y)
{
CHugeInt temp(y);
return x + temp;
}
//下面的两个形式,核心思路都是通过构造临时对象,委托核心模块完成功能
//不同点在于:后置++,返回+完成之后的对象
//前置++,以示区别,重载形式多了个int型的参数,返回+完成之前的对象
CHugeInt operator++()
{
return *this = *this + CHugeInt(1);
}
CHugeInt operator++(int)
{
CHugeInt temp(*this);
*this = temp + CHugeInt(1);
return temp;
}
//重载<<运算符,因为按位逆序存储,输出时从有效存储空间的尾部一直输出到起始位置
friend ostream & operator<<(ostream & os, const CHugeInt & rhs)
{
for (int i = rhs.length - 1; i >= 0; --i)
cout << *(rhs.ptr + i);
return os;
}
~CHugeInt()
{
delete []ptr;
}
// end of my code
};
int main()
{
//分析main函数的执行过程:
// 1.CHugeInt a(s),CHugeInt b(n)说明需要参数类型为int和char*的构造函数
// 2.根据3个加法表达式,需要重载+运算符,有3种不同形式的重载形式,为了委托都设定为友元函数
// 2.1 a + b 需要重载参数为分别为CHugeInt型,CHugeInt型
// 2.2 n + a 需要重载参数为分别为int型,CHugeInt型,委托2.1中的函数执行
// 2.3 a + n 需要重载参数为分别为CHugeInt型,int型的运算符+,委托2.1中的函数执行
// 3. 需要重载+=运算符,同样可以复用+运算符
// 4. 需要重载两种形式的++运算符
// 5. 需要重载参数为CHugeInt型的<<运算符
// EX:根据运算类型,需要复制构造函数,重载赋值运算符
char s[210];
int n;
while (cin >> s >> n) {
CHugeInt a(s);
CHugeInt b(n);
cout << a + b << endl;
cout << n + a << endl;
cout << a + n << endl;
b += n;
cout << ++ b << endl;
cout << b++ << endl;
cout << b << endl;
}
return 0;
}
输入
多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示
输出
对每组数据,输出6行,内容分别是:
样例输入
99999999999999999999999999888888888888888812345678901234567789 12
6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
来源
Guo Wei