c++ code:(10)operator overload

//输入
//多组数据,每组一行,整数 n
//输出
//对每组数据,输出一行,包括两个整数, n - 5 和 n - 8
//输入样例
//20
//30
//输出样例
//15, 12
//25, 22

#include <iostream>
using namespace std;

class MyInt
{
private:
	int nVal;

public:
	MyInt(int n)
	{
		nVal = n;
	}

	//overload 类型转换符
	operator int()
	{
		return nVal;
	}
	//overload - 单目运算符
	MyInt & operator -(int n)
	{
		nVal -= n;
		return *this;
	}

};

int Inc(int n)
{
	return n + 1;
}

int main()
{
	freopen("f:\\freopen.txt", "r", stdin);
	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;
}
/*
输入
多组数据,每组两个整数
输出
对每组数据,输出一行,就是输入的两个整数
输入样例
2 3
4 5
输出样例
2,3
4,5
*/
#include <iostream>
using namespace std;

class Point
{
private:
	int x;
	int y;

public:
	Point() {};
	friend ostream& operator << (ostream & o, const Point &p)
	{
		cout << p.x<<',' << p.y;
		return o;
	}
	friend istream& operator >> (istream & i, Point &p)
	{
		//cin >> p.x >> p.y;
		i >> p.x >> p.y;
		return i;
	}
};

int main()
{
	freopen("f:\\freopen.txt", "r", stdin);
	Point p;
	while (cin>>p)
	{
		cout << p << endl;
	}
	return 0;
}
/*
用一维数组来存放二维数组
a[i][j]的计算过程从左到右,
a[i] 的返回值是个指针,
指向第 i 行的首地址。
a[i][j] 就会是第 i 行第 j 个元素了。
写一个二维数组类 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>
using namespace std;

class Array2
{
private:
	int *p;
	int r, c;
public:
	Array2() { p = NULL; }
	Array2(int r_, int c_) :r(r_), c(c_) {
		p = new int[r*c];
	}

	//复制constructor
	Array2(Array2 & a) :r(a.r), c(a.c)
	{
		p = new int[r*c];
		memcpy(p, a.p, sizeof(int)*r*c);
	}

	//overload =
	Array2& operator=(const Array2 & a) {
		if (p)
			delete[]p;
		r = a.r;
		c = a.c;
		p = new int[r*c];
		memcpy(p, a.p, sizeof(int)*r*c);
		return * this;
	}

	~Array2()
	{
		if (p)
			delete[]p;
	}

	int * operator[](int i)
	{
		return p + i*c;
	}
	int & operator()(int i, int j)
	{
		return p[i*c + j];
	}
};

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;
}
/*
输入
多组数据,每组数据是两个非负整数 s 和 n。 s 最多可能 200 位, n 用 int 能表
示
输出对每组数据,输出 6 行,内容分别是:
s + n
s + n
s + n
2n + 1
2n + 1
2n + 2
样例输入
99999999999999999999999999888888888888888812345678901234567789 12
6 6
样例输出:
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
*/

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 100;

class CHugeInt {

private:
	char buf[200];

public:
	void reverse(char * p) {
		int len = strlen(p);
		int i = 0, j = len - 1;
		while (i<=j)
		{
			swap(p[i], p[j]);
			++i;
			--j;
		}
	}

	CHugeInt(char * p) {
		memset(buf, 0, sizeof(buf));
		strcpy(buf, p);
		reverse(buf);
	}

	CHugeInt(int n) {
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "%d", n);  //int to string in buf
		reverse(buf);
	}

	CHugeInt operator+(int n) {
		return * this + CHugeInt(n);
	}

	CHugeInt operator +(const CHugeInt & n)const {
		CHugeInt tmp(0);
		int carry = 0;
		for (int i = 0; i < 210; ++i)
		{
			char c1 = buf[i];
			char c2 = n.buf[i];
			if (c1 == 0 && c2 == 0 && carry == 0)
				break;
			if (c1 == 0)
				c1 = '0';
			if (c2 == 0)
				c2 = '0';

			int k = c1 - '0' + c2 - '0' + carry;
			if (k >= 10) {
				carry = 1;
				tmp.buf[i] = k - 10 + '0';
			}
			else {
				carry = 0;
				tmp.buf[i] = k + '0';
			}
		}
		return tmp;
	}

	friend CHugeInt operator+(int n, const CHugeInt& h)
	{
		return h + n;
	}

	friend ostream& operator <<(ostream& o, const CHugeInt & h) {
		int len = strlen(h.buf);
		for (int i = len - 1; i >= 0; --i)
			cout << h.buf[i];
		return o;
	}

	CHugeInt & operator +=(int n)
	{
		*this = *this + n;
		return *this;
	}
	CHugeInt & operator ++()
	{
		*this = *this + 1;
		return *this;
	}
	CHugeInt operator ++(int)
	{
		CHugeInt tmp(*this);
		*this = tmp + 1;
		return tmp;
	}
};

int main()
{
	freopen("f:\\freopen.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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值