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;
}