环境:Visual Studio 2022
bign类
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#define MAX_L 2005
using namespace std;
class bign {
public:
int len;
int s[MAX_L];
bign();
bign(const char*);
bign(int);
bool sign;//1 is positive
string to_str()const;
friend istream& operator>>(istream&, bign&);
friend ostream& operator<<(ostream&, bign&);
//重载复制
bign operator=(const char*);
bign operator=(int);
bign operator=(const string);
//重载比较
bool operator>(const bign&)const;
bool operator>=(const bign&)const;
bool operator<(const bign&)const;
bool operator<=(const bign&)const;
bool operator==(const bign&)const;
bool operator!=(const bign&)const;
//四则运算重载
bign operator+(const bign&)const;
bign operator++();
bign operator++(int);
bign operator+=(const bign&);
bign operator-(const bign&)const;
bign operator--();
bign operator--(int);
bign operator-=(const bign&);
bign operator*(const bign&)const;
bign operator*(const int num)const;
bign operator*=(const bign&);
bign operator/(const bign&)const;
bign operator/=(const bign&);
//四则扩展运算符重载
bign operator%(const bign&)const;
//剩下的函数
void dis() {
if (sign == 0)cout << "-";
for (int i = 0; i < len; i++)std::cout << s[len - i - 1];
std::cout << std::endl;
}
};
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
bign::bign() {
memset(s, 0, sizeof(s)); len = 1; sign = 1;
}
bign::bign(const char* num) {
*this = num;
}
bign::bign(int num) {
*this = num;
}
string bign::to_str()const {
string res;
res = "";//初始化
int i;
for (i = 0; i < len; i++) {
res = (char)(s[i] + '0') + res;
}
if (res == "") return "0";
if (sign != 1 && res != "") res = '-' + res;
return res;
}
istream& operator>>(istream& in, bign& num) {
string res;
in >> res;
num = res;
return in;
}
ostream& operator<<(ostream& out, bign& num) {
out << num.to_str();
return out;
}
bign bign::operator=(const char* num) {
memset(s, 0, sizeof(s));//s初始化
char tmp[MAX_L] = "";
if (num[0] != '-')strcpy(tmp, num);
else { for (int i = 1; i < strlen(num); i++)tmp[i - 1] = num[i]; }
len = strlen(tmp); sign = !(num[0] == '-');
for (int i = 0; i < len; i++)
s[i] = tmp[len - i - 1] - '0';
int i = len - 1;
while (s[i] == 0 && i > 0) { i--; len--; }
return *this;
}
bign bign::operator=(int num) {
//也可以采用sprintf
int i = 0;
if (num < 0) { sign = 0; num = -1 * num; }
else sign = 1;
int a = num; int tmp;
while (a > 0) { tmp = a; a = a / 10; s[i++] = tmp % 10; }
len = i;
i = len - 1;
while (s[i] == 0 && i > 0) { i--; len--; }
return *this;
}
bign bign::operator=(const string num) {
const char* tmp;
tmp = num.c_str();
*this = tmp;
return *this;
}
bool bign::operator>(const bign& obj)const {
if (sign ^ obj.sign)return (*this).sign;//不同符号
if (len != obj.len)return sign ? (len > obj.len) : (!(len > obj.len));//相同符号不同长度
for (int i = len - 1; i >= 0; i--)
if (s[i] != obj.s[i])
return sign ? (s[i] > obj.s[i]) : (!(s[i] > obj.s[i]));
return 0;
}
bool bign::operator<(const bign& obj)const {
return (obj > *this);
}
bool bign::operator>=(const bign& obj)const {
return !(*this < obj);
}
bool bign::operator<=(const bign& obj)const {
return ~(*this > obj);
}
bool bign::operator!=(const bign& obj)const {
return (*this > obj) || (*this < obj);
}
bool bign::operator==(const bign& obj)const {
return !(*this != obj);
}
bign bign::operator+(const bign& num)const {
bign res;
res.len = 0;
int i;
if (sign ^ num.sign) {
bign tmp = sign ? num : *this;//找出减数
tmp.sign = 1;//加法变成减法
res = sign ? *this - tmp : num - tmp;
}//两者不同符号
else {
int add = 0;
for (i = 0; add || i < (max(len, num.len)); i++)
{
int t = s[i] + num.s[i] + add;
res.s[res.len++] = t % 10; add = t / 10;
}
res.sign = sign;
int i = res.len - 1;
while (res.s[i] == 0 && i > 0) { i--; res.len--; }
}//两者同号
return res;
}
bign bign::operator++() {
*this = *this + 1;
return *this;
}
bign bign::operator++(int) {
bign tmp = *this;
++(*this);
return tmp;
}
bign bign::operator+=(const bign& num) {
*this = *this + num;
return *this;
}
bign bign::operator-(const bign& num)const {
bign a = *this;//被减数
bign b = num;//减数
bign res; res.len = 0;
if (!sign && !num.sign) {
a.sign = 1; b.sign = 1;
res = b - a;
}//两数都是负数
else if (!num.sign) { b.sign = 1; res = a + b; }//减数是负数
else if (!sign) { a.sign = 1; res = bign(0) - (a + b); }//被减数是负数
else {
if (a < b) { res = b - a; res.sign = 0; }//被减数小
else {
int add = 0;
for (int i = 0; i < a.len; i++)
{
int t = a.s[i] - add;
if (i < b.len)t = t - b.s[i];
if (t >= 0)add = 0;
else { add = 1; t = t + 10; }//退位
res.s[res.len++] = t;
}
}//被减数大
}//两者都是正数
int i = res.len - 1;
while (res.s[i] == 0 && i > 0) { i--; res.len--; }
return res;
}
bign bign::operator--() {
*this = *this - 1;
return *this;
}
bign bign::operator--(int) {
bign tmp = *this;
--(*this);
return tmp;
}
bign bign::operator-=(const bign& num) {
*this = *this - num;
return *this;
}
bign bign::operator*(const bign& num)const {
bign res;
res.len = len + num.len - 1;
int i, j;
for (i = 0; i < len; i++)
for (j = 0; j < num.len; j++)
{
res.s[i + j] += s[i] * num.s[j];
}
int add = 0;
for (i = 0; i < res.len; i++)
{
int t = res.s[i] + add; res.s[i] = t % 10; add = t / 10;
}
res.sign = !(sign ^ num.sign);
i = res.len - 1;
while (res.s[i] == 0 && i > 0) { i--; res.len--; }
return res;
}
bign bign::operator*=(const bign& num) {
*this = *this * num;
return *this;
}
bign bign::operator*(const int num)const {
bign x = num;
return *this * x;
}
bign bign::operator/(const bign& num)const {
bign res;
res.len = len - num.len + 1;
if (res.len < 0) { res.len = 1; return res; }//被除数小
bign a = *this;//被除数
bign b = num;//除数
a.sign = b.sign = 1;
int i;
bign tmp = 0;
for (i = len - 1; i >= 0; i--)
{
tmp = tmp * 10;
tmp.s[0] = s[i];//每次循环就是一次移位
while (tmp >= b) { tmp -= b; res.s[i]++; }
}
res.sign = !(sign ^ num.sign);
i = res.len - 1;
while (res.s[i] == 0 && i > 0) { i--; res.len--; }
return res;
}
bign bign::operator/=(const bign& num) {
*this = *this / num;
return *this;
}
bign bign::operator%(const bign& num)const {
bign res = *this / num;
res = *this - res * num;
return res;
}
CheckedPtr类
#include <iostream>
using namespace std;
class CheckedPtr {
public:
CheckedPtr(int *b,int *e):beg(b),end(e),curr(b){}
CheckedPtr& operator++() {
++curr;
return *this;
}
CheckedPtr& operator--() {
--curr;
return *this;
}
CheckedPtr operator++(int c) {
CheckedPtr ret(*this);//记住对象在变化前的状态
++* this;
return ret;
}
CheckedPtr operator--(int c) {
CheckedPtr ret(*this);//记住对象在变化前的状态
--* this;
return ret;
}
int* GetBeg() {
return beg;
}
int* GetEnd() {
return end;
}
int* GetCurr() {
return curr;
}
private:
int* beg;
int* end;
int* curr;
};
int main() {
int n;
cin >> n;
int* array = new int[n];
for (int i = 0; i < n; i++) {
cin >> array[i];
}
CheckedPtr cp(array, array + n);
for(; cp.GetCurr() < cp.GetEnd(); cp++) {
cout << *cp.GetCurr() << " ";
}
cout << endl;
for (--cp; cp.GetCurr() > cp.GetBeg(); cp--) {
cout << *cp.GetCurr() << " ";
}
cout << *cp.GetCurr() << endl;
delete[]array;
return 0;
}
Complex类
#include <iostream>
using namespace std;
class Complex {
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0) {}
Complex& operator+=(const Complex& c) {
x += c.x;
y += c.y;
return *this;
}
Complex& operator-=(const Complex& c) {
x -= c.x;
y -= c.y;
return *this;
}
Complex& operator*=(const Complex& c) {
int a= x * c.x - y * c.y;
int b = x * c.y + y * c.x;
x = a;
y = b;
return *this;
}
Complex& operator/=(const Complex& c) {
int a = (x * c.x + y * c.y) / (c.x * c.x + c.y * c.y);
int b = (y * c.x - x * c.y) / (c.x * c.x + c.y * c.y);
x = a;
y = b;
return *this;
}
friend Complex operator+(const Complex& c1, const Complex& c2) {
Complex c;
c.x = c1.x + c2.x;
c.y = c1.y + c2.y;
return c;
}
friend Complex operator-(const Complex& c1, const Complex& c2) {
Complex c;
c.x = c1.x - c2.x;
c.y = c1.y - c2.y;
return c;
}
friend Complex operator*(const Complex& c1, const Complex& c2) {
Complex c;
c.x = c1.x * c2.x - c1.y * c2.y;
c.y = c1.x * c2.y + c1.y * c2.x;
return c;
}
friend Complex operator/(const Complex& c1, const Complex& c2) {
Complex c;
c.x = (c1.x * c2.x + c1.y * c2.y) / (c2.x * c2.x + c2.y * c2.y);
c.y = (c1.y * c2.x - c1.x * c2.y) / (c2.x * c2.x + c2.y * c2.y);
return c;
}
friend bool operator==(const Complex& c1, const Complex c2) {
if (c1.x==c2.x&&c1.y==c2.y) return 1;
else return 0;
}
friend bool operator!=(const Complex& c1, const Complex c2) {
if (c1.x != c2.x || c1.y != c2.y) return 1;
else return 0;
}
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.x << " + " << c.y << "i";
return os;
}
friend istream& operator>>(istream& is, Complex& c) {
is >> c.x >> c.y;
return is;
}
};
int main() {
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
Person_Student
#include <iostream>
using namespace std;
class Person {
public:
virtual void input();
virtual void display();
private:
string name;
};
void Person::input() {
cin >> name;
}
void Person::display() {
cout << this->name << endl;
}
class Student :public Person{
private:
string no;
public:
void input();
virtual void display();
};
void Student::input() {
cin >> no;
Person::input();
}
void Student::display() {
cout << no << " ";
Person::display();
}
int main() {
Person* p;
p = new Person;
p->input();
p->display();
delete p;
p = new Student;
p->input();
p->display();
delete p;
return 0;
}
Sales_data
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
class Sales_data {
//依次输入书号、销量、收入
friend istream& operator>>(istream& is, Sales_data& s) {
is >> s.bookNo >> s.units_sold >> s.revenue;
return is;
}
//依次输入书号、销量、收入、均价
friend ostream& operator<<(ostream& os, const Sales_data& s) {
os << s.bookNo << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(s.revenue, s.units_sold);
return os;
}
friend bool operator==(const Sales_data& s1, const Sales_data& s2) {
if (s1.units_sold == s2.units_sold) return 1;
else return 0;
}
friend bool operator!=(const Sales_data& s1, const Sales_data& s2) {
if (s1.units_sold != s2.units_sold) return 1;
else return 0;
}
//for "+"assume that both objects refer to the same book
friend Sales_data operator+(const Sales_data& s1, const Sales_data& s2) {
Sales_data s;
s.bookNo = s1.bookNo;
s.units_sold = s1.units_sold + s2.units_sold;
s.revenue = s1.revenue + s2.revenue;
return s;
}
private:
double avg_price(double r, unsigned n)const {//均价,等于收入除以销量
return r / n;
}
string bookNo;//书号
unsigned units_sold;//销量
double revenue;//收入
public:
Sales_data() :units_sold(0), revenue(0.0) {}
Sales_data(const string& s, unsigned n, double r) :bookNo(s), units_sold(n), revenue(r) {}
string get_bookNo()const {
return this->bookNo;
}
//for "+=" assume that both objects refer to the same book
Sales_data& operator+=(const Sales_data& s) {
this->units_sold += s.units_sold;
this->revenue += s.revenue;
return *this;
}
};
int main() {
Sales_data item1, item2;
while (cin >> item1 >> item2) {
cout << item1 << "\n" << item2 << "\n";
if (item1 == item2) {
cout << item1.get_bookNo() << " equals " << item2.get_bookNo() << "\n";
}
if (item1 != item2) {
cout << item1.get_bookNo() << " doesn't equal " << item2.get_bookNo() << "\n";
}
cout << (item1 + item2) << "\n";
item1 += item2;
cout << item1 << "\n";
}
return 0;
}
Singer
#include <iostream>
using namespace std;
class Singer {
private:
string name;
string sex;
int age;
double score;
public:
string getName() {
return this->name;
}
friend istream& operator>>(istream&is, Singer&s) {
is >> s.name >> s.sex >> s.age >> s.score;
return is;
}
friend ostream& operator<<(ostream&os, Singer&s) {
os << s.name <<" " << s.sex << " " << s.age << " " << s.score;
return os;
}
friend bool operator>(const Singer&s1, const Singer&s) {
if (s1.score > s.score) return 1;
else return 0;
}
friend bool operator==(const Singer&s1, const Singer&s) {
if (s1.score == s.score) return 1;
else return 0;
}
friend bool operator<(const Singer&s1, const Singer&s) {
if (s1.score < s.score) return 1;
else return 0;
}
};
int main() {
Singer s1, s2;
cin >> s1 >> s2;
cout << s1 << "\n" << s2 << endl;
if (s1 > s2) {
cout << s1.getName() << "'s score is higher than " << s2.getName() << "'s.\n";
}
else if (s1==s2){
cout << s1.getName() << "'s score is equal to " << s2.getName() << "'s.\n";
}
else {
cout << s1.getName() << "'s score is lower than " << s2.getName() << "'s.\n";
}
return 0;
}
String
#include<iostream>
#include<string.h>
using namespace std;
class String
{
private:
char* s;
public:
String();
String(const char*);
String(const String&);
~String();
String& operator=(const char*);
String& operator=(const String&);
String operator+(const char*);
String operator+(const String&);
String& operator+=(const char*);
String& operator+=(const String&);
friend istream& operator>>(istream&, String&);
friend ostream& operator<<(ostream&, const String&);
friend bool operator==(const String&, const char*);
friend bool operator==(const String&, const String&);
friend bool operator!=(const String&, const char*);
friend bool operator!=(const String&, const String&);
};
String::String() {
s = new char[100];
}
String::~String() {
delete[]s;
}
String::String(const char* c) {
s = new char[strlen(c) + 1];
strcpy(s, c);
}
String::String(const String& c) {
s = new char[strlen(c.s) + 1];
strcpy(s, c.s);
}
String& String::operator=(const char* c) {
return operator=(String(c));
}
String& String::operator=(const String& c) {
s = new char[strlen(c.s) + 1];
strcpy(s, c.s);
return *this;
}
String String::operator+(const char* c) {
return String(s) + String(c);
}
String String::operator+(const String& c) {
return String(s) + c;
}
String& String::operator+=(const char* c) {
return operator+=(String(c));
}
String& String::operator+=(const String& c) {
char* r = new char[strlen(s) + strlen(c.s) + 1];
r = this->s;
strcat(r, c.s);
this->s = r;
return *this;
}
istream& operator>>(istream& stream, String& c) {
stream >> c.s;
return stream;
}
ostream& operator<<(ostream& stream, const String& c) {
stream << c.s;
return stream;
}
bool operator==(const String& c1, const char* c2) {
if (strcmp(c1.s, c2) == 0)
return true;
else
return false;
}
bool operator==(const String& c1, const String& c2) {
if (strcmp(c1.s, c2.s) == 0)
return true;
else
return false;
}
bool operator!=(const String& c1, const char* c2) {
return(strcmp(c1.s, c2) != 0);
}
bool operator!=(const String& c1, const String& c2) {
return(strcmp(c1.s, c2.s) != 0);
}
int main()
{
String s;
s += "hello";
cout << s << endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}