编程打卡: C++ 语言程序设计
复数类
代码实现
#include <iostream>
using namespace std;
class Complex
{
protected:
double real;
double imag;
public:
Complex (double r = 0.0, double i = 0.0): real(r), imag(i) {};
Complex operator + (const Complex& c)
{
return Complex(real + c.real, imag + c.imag);
}
Complex operator - (const Complex& c)
{
return Complex(real - c.real, imag - c.imag);
}
Complex operator * (const Complex& c)
{
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
Complex operator / (const Complex& c)
{
return Complex((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag), (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}
friend ostream& operator << (ostream& os, const Complex& c)
{
os << c.real << " + " << c.imag << "i";
return os;
}
friend istream& operator >> (istream& is, Complex& c)
{
is >> c.real >> c.imag;
return is;
}
};
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << c1 + c2 << endl;
cout << c1 - c2 << endl;
cout << c1 * c2 << endl;
cout << c1 / c2 << endl;
return 0;
}
vector3D
代码实现
#include <iostream>
using namespace std;
class vector3D {
protected:
double x, y, z;
public:
vector3D (double a = 0.0, double b = 0.0, double c = 0.0): x(a), y(b), z(c) {};
vector3D operator+(const vector3D& v) {
return vector3D(x + v.x, y + v.y, z + v.z);
}
vector3D operator-(const vector3D& v) {
return vector3D(x - v.x, y - v.y, z - v.z);
}
vector3D operator*(const vector3D& v) {
return vector3D(x * v.x, y * v.y, z * v.z);
}
friend ostream& operator<<(ostream& os, const vector3D& v) {
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
friend istream& operator>>(istream& is, vector3D& v) {
is >> v.x >> v.y >> v.z;
return is;
}
};
int main() {
vector3D v1, v2;
cout << "v1 坐标值为: ";
cin >> v1;
cout << "v2 坐标值为: ";
cin >> v2;
cout << v1 + v2 << endl;
cout << v1 - v2 << endl;
cout << v1 * v2 << endl;
return 0;
}
vector_N
代码实现
#include <iostream>
using namespace std;
class vector_N
{
private:
int n;
int *p;
public:
vector_N ()
{
n = 0;
p = nullptr;
}
explicit vector_N (int dim)
{
n = dim;
p = new int[n];
}
vector_N (const vector_N& v)
{
n = v.n;
p = new int[v.n];
for (int i = 0; i < n; i++)
{
p[i] = v.p[i];
}
}
vector_N& operator = (const vector_N& v)
{
if (this != &v)
{
n = v.n;
p = new int[v.n];
for (int i = 0; i < n; i++)
{
p[i] = v.p[i];
}
}
return *this;
}
friend istream& operator >> (istream& is,vector_N& v)
{
is >> v.n;
if (v.n <= 0)
{
cout << "Error Length!\n";
exit(0);
}
v.p = new int[v.n];
for (int i = 0; i < v.n; i++)
{
is >> v.p[i];
}
return is;
}
friend ostream& operator << (ostream& os,const vector_N& v)
{
for (int i = 0; i < v.n; i++)
{
os << v.p[i] << " ";
}
return os;
}
vector_N operator + (const vector_N& v)
{
if (n != v.n)
{
cout << "Mismatch Length!\n" ;
exit(0);
}
vector_N res(n);
for (int i = 0; i < n; i++)
{
res.p[i] = p[i] + v.p[i];
}
return res;
}
vector_N operator - (const vector_N& v)
{
if (n != v.n)
{
cout << "Mismatch Length!\n" ;
exit(0);
}
vector_N res(n);
for (int i = 0; i < n; ++i)
{
res.p[i] = p[i] - v.p[i];
}
return res;
}
vector_N operator * (int x)
{
vector_N res(n);
for (int i = 0; i < n; ++i)
{
res.p[i] = p[i] * x;
}
return res;
}
int operator * (const vector_N& v)
{
if (n != v.n)
{
cout << "Mismatch Length!\n" ;
exit(0);
}
int res = 0;
for (int i = 0; i < n; i++)
{
res += p[i] * v.p[i];
}
return res;
}
int operator [] (int x)
{
if (x < 0 || x > n)
{
cout << "Error Index\n";
exit(0);
}
return p[x];
}
~vector_N()
{
delete p;
n = 0;
}
};
int main ()
{
vector_N v1,v2,v3;
cin >> v1 >> v2;
cout << v1 + v2<< endl;
cout << v1 - v2 << endl;
cout << v1 * 3 << endl;
cout << v1[1] << endl;
}
Clock
代码实现
#include <iostream>
#include <iomanip>
using namespace std;
class Clock
{
private:
int ss;
public:
Clock(int s = 0):ss(s){};
void refresh()
{
if (ss >= 86400)
ss %= 86400;
if (ss < 0)
ss += 86400;
}
Clock &operator++()
{
ss++;
refresh();
return *this;
}
Clock &operator--(int)
{
ss--;
refresh();
return *this;
}
friend ostream &operator<<(ostream &os, const Clock &c)
{
int h = c.ss / 3600;
int m = (c.ss - h * 3600) / 60;
int s = c.ss - h * 3600 - m * 60;
os << setfill('0') << setw(2) << h << ":" << setw(2) << m << ":" << setw(2) << s << endl;
return os;
}
friend istream &operator>>(istream &is, Clock &c)
{
int h, m, s;
is >> h >> m >> s;
c.ss = h * 3600 + m * 60 + s;
c.refresh();
return is;
}
};
int main()
{
Clock c;
cin >> c;
cout << c;
++c;
cout << c;
c--;
c--;
cout << c;
return 0;
}
分数
代码实现
#include <iostream>
using namespace std;
class Fraction
{
private:
int num;
int den;
public:
Fraction(int n, int d) : num(n), den(d) {};
void simplify()
{
int gcd = 1;
int j = num < den ? num : den;
for (int i = 1; i <= j; i++)
{
if (num % i == 0 && den % i == 0)
{
gcd = i;
}
}
num /= gcd;
den /= gcd;
}
Fraction operator+(const Fraction& f)
{
int n = num * f.den + den * f.num;
int d = den * f.den;
Fraction result(n, d);
result.simplify();
return result;
}
Fraction operator-(const Fraction& f)
{
int n = num * f.den - den * f.num;
int d = den * f.den;
Fraction result(n, d);
result.simplify();
return result;
}
Fraction operator*(const Fraction& f)
{
int n = num * f.num;
int d = den * f.den;
Fraction result(n, d);
result.simplify();
return result;
}
Fraction operator/(const Fraction& f)
{
int n = num * f.den;
int d = den * f.num;
Fraction result(n, d);
result.simplify();
return result;
}
bool operator==(Fraction f)
{
f.simplify();
simplify();
return (num == f.num && den == f.den);
}
bool operator!=(Fraction f)
{
f.simplify();
simplify();
return (num != f.num || den != f.den);
}
bool operator>(Fraction f)
{
f.simplify();
simplify();
return (num * f.den > den * f.num);
}
bool operator<(Fraction f)
{
f.simplify();
simplify();
return (num * f.den < den * f.num);
}
bool operator>=(Fraction f)
{
f.simplify();
simplify();
return (num * f.den >= den * f.num);
}
bool operator<=(Fraction f)
{
f.simplify();
simplify();
return (num * f.den <= den * f.num);
}
friend istream& operator>>(istream& is, Fraction& f)
{
is >> f.num >> f.den;
return is;
}
friend ostream& operator<<(ostream& os, Fraction& f)
{
os << f.num << "/" << f.den;
return os;
}
};
int main()
{
Fraction f1(1, 2);
Fraction f2(1, 3);
Fraction f3 = f1 + f2;
Fraction f4 = f1 - f2;
Fraction f5 = f1 * f2;
Fraction f6 = f1 / f2;
cout << f3 << endl;
cout << f4 << endl;
cout << f5 << endl;
cout << f6 << endl;
if (f1 == f2)
{
cout << "f1 == f2" << endl;
}
if (f1 != f2)
{
cout << "f1 != f2" << endl;
}
if (f1 > f2)
{
cout << "f1 > f2" << endl;
}
if (f1 < f2)
{
cout << "f1 < f2" << endl;
}
if (f1 >= f2)
{
cout << "f1 >= f2" << endl;
}
if (f1 <= f2)
{
cout << "f1 <= f2" << endl;
}
return 0;
}
学生类
代码实现
#include <iostream>
#include <string>
using namespace std;
class people
{
protected:
int age;
string name;
public:
people() {}
people(int a, string n) : age(a), name(n) {}
virtual void display() = 0;
void setValue(int m, string str)
{
age = m;
name = str;
}
};
class student : public people
{
private:
int studentID;
public:
student() {}
student(int a, string n, int id) : people(a, n), studentID(id) {}
void setID(int m)
{
studentID = m;
}
void display()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "学号:" << studentID << endl;
}
};
class teacher : public people
{
private:
int teacherID;
public:
teacher() {}
teacher(int a, string n, int id) : people(a, n), teacherID(id) {}
void setID(int m)
{
teacherID = m;
}
void display()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工号:" << teacherID << endl;
}
};
int main()
{
people *p1 = new student(13, "dick", 20223969);
people *p2 = new teacher(14, "john", 114514);
p1->display();
p2->display();
return 0;
}
动物类
代码实现
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal()
{
cout << "Animal constructor called." << endl;
}
virtual ~Animal()
{
cout << "Animal destructor called." << endl;
}
virtual void speak()
{
cout << "My name is Animal." << endl;
}
};
class Cat : public Animal
{
public:
Cat()
{
cout << "Cat constructor called." << endl;
}
~Cat()
{
cout << "Cat destructor called." << endl;
}
void speak()
{
cout << "My name is Cat." << endl;
}
};
class Leopard : public Animal
{
public:
Leopard()
{
cout << "Leopard constructor called." << endl;
}
~Leopard()
{
cout << "Leopard destructor called." << endl;
}
void speak()
{
cout << "My name is Leopard." << endl;
}
};
int main()
{
Animal *p;
Animal a;
Cat c;
Leopard l;
p = &a;
p->speak();
p = &c;
p->speak();
p = &l;
p->speak();
return 0;
}
形状类
代码实现
#include <iostream>
#include <cmath>
using namespace std;
class shape
{
public:
virtual void setvalues() = 0;
virtual float area() = 0;
};
class rectangle : public shape
{
private:
float bottom, height;
public:
void setvalues()
{
cin >> bottom >> height;
}
float area()
{
return bottom * height;
}
};
class triangle : public shape
{
private:
float bottom, height;
public:
void setvalues()
{
cin >> bottom >> height;
}
float area()
{
return bottom * height / 2;
}
};
class square : public shape
{
private:
float side;
public:
void setvalues()
{
cin >> side;
}
float area()
{
return side * side;
}
};
class circle : public shape
{
private:
float radius;
public:
void setvalues()
{
cin >> radius;
}
float area()
{
return 3.14 * radius * radius;
}
};
int main()
{
shape *p;
rectangle r;
triangle t;
square s;
circle c;
p = &r;
p->setvalues();
if (p->area() <= 0)
{
cout << "Set Value Error!" << endl;
return 0;
}
cout << p->area() << endl;
p = &t;
p->setvalues();
if (p->area() <= 0)
{
cout << "Set Value Error!" << endl;
return 0;
}
cout << p->area() << endl;
p = &s;
p->setvalues();
if (p->area() <= 0)
{
cout << "Set Value Error!" << endl;
return 0;
}
cout << p->area() << endl;
p = &c;
p->setvalues();
if (p->area() <= 0)
{
cout << "Set Value Error!" << endl;
return 0;
}
cout << p->area() << endl;
return 0;
}
学生类
代码实现
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
string name;
int id;
public:
student(string n,int i):name(n),id(i){}
void setnameid(string n,int i)
{
name=n;
id=i;
}
void displaynameid()
{
cout<<name<<" "<<id<<endl;
}
virtual void setmajor(string)=0;
virtual string getmajor()=0;
virtual void setadvisor(string)=0;
virtual string getadvisor()=0;
};
class understud:public student
{
protected:
string major;
public:
understud(string n,int i,string m):student(n,i),major(m){}
void setmajor(string m)
{
major=m;
}
string getmajor()
{
return major;
}
};
class poststud:public understud
{
private:
string advisor;
public:
poststud(string n="***",int i=-1,string m="###",string a="&&&"):understud(n,i,m),advisor(a){}
void setadvisor(string a)
{
advisor=a;
}
string getadvisor()
{
return advisor;
}
};
int main()
{
int flag;
cin>>flag;
if(flag==0)
{
poststud a;
a.displaynameid();
cout<<a.getmajor()<<endl;
cout<<a.getadvisor()<<endl;
}
else
{
string n,m,a;
int i;
cin>>n>>i>>m>>a;
poststud b(n,i,m,a);
b.displaynameid();
cout<<b.getmajor()<<endl;
cout<<b.getadvisor()<<endl;
}
return 0;
}