程序设计与算法(三)第4周测验(2019夏季)

本文深入探讨了C++中自定义字符串类MyString的实现,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载、以及输入输出流操作符的重载。同时,通过实例展示了运算符重载在类中的应用,如减法运算符和类型转换运算符的使用,以及大型整数类CHugeInt的设计与实现,涵盖了加法、前缀和后缀递增运算符的重载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

001:MyString

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class MyString {
	char* p;
public:
	MyString(const char* s) {
		if (s) {
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
			p = NULL;

	}
	~MyString() { if (p) delete[] p; }
	// Your Code Here
	MyString(const MyString& s) {
		if (s.p) {
			p = new char[strlen(s.p) + 1];
			strcpy(p, s.p);
		}
		else
			p = NULL;
	}
	void Copy(char* s) {
		if (p)
			delete[]p;
		if (s) {
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
			p = NULL;
	}
	MyString& operator =(const MyString& s) {
		if (s.p == p)
			return *this;
		if (p)
			delete[]p;
		if (s.p) {
			p = new char[strlen(s.p) + 1];
			strcpy(p, s.p);
		}
		else
			p = NULL;
	}
	MyString& operator=(const char* s) {
		if (p)
			delete[]p;
		if (s) {
			p = new char[strlen(s) + 1];
			strcpy(p, s);
		}
		else
			p = NULL;
	}
	friend ostream& operator<<(ostream& o, const MyString& s) {
		if (s.p) {
			cout << s.p;
		}
		return o;
	}
};
int main()
{
	char w1[200], w2[100];
	while (cin >> w1 >> w2) {
		MyString s1(w1), s2 = s1;
		MyString s3(NULL);
		s3.Copy(w1);
		cout << s1 << "," << s2 << "," << s3 << endl;

		s2 = w2;
		s3 = s2;
		s1 = s3;
		cout << s1 << "," << s2 << "," << s3 << endl;

	}
}

002:Operator overload

#include <iostream> 
using namespace std;
class MyInt
{
	int nVal;
public:
	MyInt(int n) { nVal = n; }
	// Your Code Here
	MyInt& operator-(int n) {
		nVal -= n;
		return *this;
	}
	operator int() {
		return nVal;
	}
};
int Inc(int n) {
	return n + 1;
}
int main() {
	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;
}

003:Amazing input and ouput of point

#include <iostream> 
using namespace std;
class Point { 
	private: 
		int x; 
		int y; 
	public: 
		Point() { };
	friend istream& operator>>(istream& is, Point &p) {
		is >> p.x >> p.y;
		return is;
	}
	friend ostream& operator<<(ostream& os, const Point &p) {
		os << p.x << "," << p.y;
		return os;
	}
}; 
int main() 
{ 
 	Point p;
 	while(cin >> p) {
 		cout << p << endl;
	 }
	return 0;
}

004:Program completion 3

#include <iostream>
#include <cstring>
using namespace std;

class Array2 {
	int rows;
	int cols;
	int* ptr;
public:
	Array2(int r=0, int c=0) :rows(r), cols(c) {
		ptr = new int[cols * rows];
	}
	~Array2() {
		delete []ptr;
	}
	int& operator()(int i, int j) const{
		return *(ptr + i * cols + j);
	}
	int* operator[](int i)const {
		return (ptr + i * cols);
	}
	Array2& operator =(const Array2& b) {
		if (ptr)
			delete[]ptr;
		if (b.ptr) {
			cols = b.cols;
			rows = b.rows;
			ptr = new int[cols * rows];
			memcpy(ptr, b.ptr, sizeof(int) * rows * cols);
		}
		else {
			cols = rows = 0;
			ptr = NULL;
		}
		return *this;
	}
};

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

005:Large integer

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
#pragma warning(disable:4996)
using namespace std;
const int MAX = 110;
class CHugeInt {
	// Your Code Here
	char c[210];
	int len;
public:
	void reverse() {
		int i = 0, j = len - 1;
		while (i < j)
			swap(c[i++], c[j--]);
	}
	CHugeInt(char s[]) {
		memset(c, 0, 210);
		len = strlen(s);
		memcpy(c, s, len);
		reverse();
	}
	CHugeInt(int n) {
		memset(c, 0, 210);
		sprintf(c, "%d", n);
		len = strlen(c);
		reverse();
	}
	CHugeInt operator+(const CHugeInt& b) const{
		char temp[210] = { 0 };
		int result = 0;
		int max_len = len > b.len ? len : b.len;
		int i;
		for (i = 0;i < max_len;i++) {
			char c1 = c[i], c2 = b.c[i];
			if (c1 == 0)
				c1 = '0';
			if (c2 == 0)
				c2 = '0';
			result += c1 + c2 - '0' - '0';
			temp[i] = result % 10 + '0';
			result /= 10;
		}
		if (result)
			temp[i] = '1';
		CHugeInt temp_c(temp);
		temp_c.reverse();
		return temp_c;
	}
	friend CHugeInt operator +(const int n, const CHugeInt& a) {
		return a + n;
	}
	CHugeInt operator++() {
		*this = *this + 1;
		return *this;
	}
	CHugeInt operator++(int k) {
		CHugeInt temp(*this);
		++(*this);
		return temp;
	}
	CHugeInt operator+=(const int n) {
		*this = *this + n;
		return *this;
	}
	friend ostream& operator << (ostream& os,const CHugeInt& c) {
		for (int i = c.len - 1;i > -1;--i)
			os << c.c[i];
		return os;
	}
};
int  main()
{
	freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
	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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值