作业管理系统上面的题目。
Question 1:课程类
#includeQuestion 2:运算符重载和友元#include #include #include using namespace std; class Course { private: string name; string place; string teacher; string time; double total_time; double grade; public: Course(); Course(string, string, string, string, double, double); Course(const Course&); friend istream& operator>>(istream&, Course&); friend ostream& operator<<(ostream&, const Course&); }; Course::Course() :name("no"), place("no"), teacher("no"), time("no"), total_time(0), grade(0) {}; Course::Course(string name, string place, string teacher, string time, double total_time, double grade) { this->name = name; this->place = place; this->teacher = teacher; this->time = time; this->total_time = total_time; this->grade = grade; } Course::Course(const Course& o) :name(o.name), place(o.place), teacher(o.teacher), time(o.time) , total_time(o.total_time), grade(o.grade) {}; ostream& operator<<(ostream& a, const Course& b) { a << b.name <<" "<< b.place << " "<< b.teacher << " " << b.time << " " << b.total_time << " " << b.grade; return a; } istream& operator>>(istream& a, Course& b) { a >> b.name >> b.place >> b.teacher >> b.time >> b.total_time >> b.grade; return a; } int main() { Course c; cin >> c; //键 盘 输 入 到 对 象 c cout << c << endl; //对 象 c 输 出 屏 幕 ofstream ofs("course.txt", ios::out); //创 建 文 件 ofs << c << endl; //对 象 c 输 出 到 文 件 return 0; }
#includeQuestion 3:有理数类#include #include class Vector { private: double a, b, c; public: Vector(); Vector(double, double, double); Vector(const Vector&); void print(); double model(); friend std::istream& operator>>(std::istream&, Vector&); friend std::ostream& operator<<(std::ostream&, const Vector&); friend Vector operator+(const Vector&, const Vector&); friend Vector operator*(const Vector&, const Vector&); friend Vector operator*(double, const Vector&); friend bool operator==(const Vector&, const Vector&); Vector& operator-=(const Vector&); }; Vector::Vector() {}; Vector::Vector(double aa, double bb, double cc) :a(aa), b(bb), c(cc) {}; Vector::Vector(const Vector& o) :a(o.a), b(o.b), c(o.c) {}; void Vector::print() { std::cout << "(" << a << "," << b << "," << c << ")" << std::endl; } double Vector::model() { double sum = a * a + b * b + c * c; sum = sqrt(sum * 1.0); return sum; } std::istream& operator>>(std::istream& a, Vector& b) { a >> b.a >> b.b >> b.c; return a; } std::ostream& operator<<(std::ostream& a, const Vector& b) { a << b.a << " " << b.b << " " << b.c; return a; } Vector operator+(const Vector& x, const Vector& y) { Vector temp; temp.a = x.a + y.a; temp.b = x.b + y.b; temp.c = x.c + y.c; return temp; } Vector operator*(const Vector& x, const Vector& y) { Vector temp; temp.a = x.b * y.c - x.c * y.b; temp.b = x.c * y.a - x.a * y.c; temp.c = x.a * y.b - x.b * y.a; return temp; } Vector& Vector::operator-=(const Vector& o) { a = a - o.a; b = b - o.b; c = c - o.c; return *this; } Vector operator*(double k, const Vector& o) { Vector temp; temp.a = k * o.a; temp.b = k * o.b; temp.c = k * o.c; return temp; } bool operator==(const Vector& x, const Vector& y) { if (x.a == y.a && x.b == y.b && x.c == y.c)return true; else return false; } int main(int argc, char const* argv[]) { double a, b, c; std::cout << "输入三个量:" << "n"; std::cin >> a >> b >> c; Vector p1(a, b, c), p2, p3(p1), p4, x; p2 = p3; //拷贝 std::cout << "再输入三个量:" << "n"; std::cin >> p4; std::cout << "显示向量:" << "n"; p4.print(); //显示向量 std::cout << "取模:" << "n"; std::cout << p4.model() << std::endl; //取模 x = p1 + p4; std::cout << "重载+:" << "n"; std::cout << x << std::endl; p2 -= p4; std::cout << "重载-=:" << "n"; std::cout << p2 << std::endl; x = p2 * p3; std::cout << "向量积:" << "n"; std::cout << x << std::endl; //向量积 int flag = p1 == p2; std::cout << "是否相等:" << "n"; std::cout << flag << std::endl; //是否相等 return 0; }
#includeQuestion 4:矩阵转置class Rational { private: int up; int down; public: Rational(); Rational(int, int); friend std::istream& get(); friend std::ostream& operator<<(std::ostream&, const Rational&); friend Rational operator+(const Rational&, const Rational&); friend Rational operator-(const Rational&, const Rational&); friend Rational operator*(const Rational&, const Rational&); friend Rational operator/(const Rational&, const Rational&); }; Rational::Rational() {}; Rational::Rational(int a, int b) :up(a), down(b) {}; std::ostream& operator<<(std::ostream& a, const Rational& b) { a << b.up << "/" << b.down; return a; } Rational operator+(const Rational& a, const Rational& b) { Rational temp; temp.down = a.down * b.down; temp.up = a.up * b.down + b.up * a.down; return temp; } Rational operator-(const Rational& a, const Rational& b) { Rational temp; temp.down = a.down * b.down; temp.up = a.up * b.down - b.up * a.down; return temp; } Rational operator*(const Rational& a, const Rational& b) { Rational temp; temp.down = a.down * b.down; temp.up = a.up * b.up; return temp; } Rational operator/(const Rational& a, const Rational& b) { Rational temp; temp.down = a.down * b.up*1.0; temp.up = a.up * b.down*1.0; return temp; } std::istream& get() { return std::cin; } int main() { Rational c(0, 12); std::cout << c << std::endl; Rational c1(3, 12), c2(5, 15), c3; c3 = c1 + c2; std::cout << c3 << std::endl; c3 = c1 - c2; std::cout << c3 << std::endl; c3 = c1 * c2; std::cout << c3 << std::endl; c3 = c1 / c2; std::cout << c3 << std::endl; std::cin.get(); return 0; }
#include#include void pc(int n, int a[][100]) { for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) { std::cout << a[i][k]<<" "; } std::cout << "n"; } std::cout << "-------------------------" << "n"; } void Trans(int m, int a[][100]) { int tempp[100][100]; for (int i = m - 1; i >= 0; i--) { for (int k = 0; k < m; k++) { tempp[i][k] = a[k][m - 1 - i]; } } pc(m, tempp); } int main() { int n; std::cin >> n; int temp[100][100]; int ret = 1; for (int i = 0; i < n; i++) for (int k = 0; k < n; k++) temp[i][k] = ret++; pc(n, temp); Trans(n, temp); return 0; }



