4.实验任务4
Complex.hpp
#pragma once #include <iostream> #include <math.h> class Complex { private: double real,imag; public: Complex(); ~Complex(); Complex(double x); Complex(double x, double y); Complex(const Complex& obj); double get_real() const; double get_imag() const; void show() const; void add(const Complex& obj1); friend Complex add(const Complex& obj1, const Complex& obj2); friend bool is_equal(const Complex& a, const Complex& b); friend double abs(const Complex& obj); }; Complex::Complex() : real(0), imag(0) {} Complex::Complex(double x) : real(x), imag(0) {} Complex::Complex(double x, double y) : real(x), imag(y) {} Complex::Complex(const Complex& obj) : real(obj.real), imag(obj.imag) {} double Complex::get_real() const { return real; } double Complex::get_imag() const { return imag; } void Complex::show() const { if(imag > 0) { std::cout << real << " + " << imag << "i"; }else if(imag < 0) { std::cout << real << " - " << abs(imag) << "i"; }else { std::cout << real; } } void Complex::add(const Complex& obj) { real += obj.real; imag += obj.imag; } Complex add(const Complex& obj1, const Complex& obj2) { Complex c(obj1.real + obj2.real, obj1.imag + obj2.imag); return c; } bool is_equal(const Complex& a, const Complex& b) { return(a.real == b.real && a.imag == b.imag); } double abs(const Complex& obj) { double c = sqrt(obj.real * obj.real + obj.imag * obj.imag); } Complex::~Complex() {}
task4.cpp
#include "Complex.hpp" #include <iostream> void test() { using namespace std; Complex c1(3, -4); const Complex c2(4.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main() { test(); }
测试:
5.实验任务5
User.hpp
#pragma once #include <iostream> #include <string> #include <iomanip> using std::string; class User { private: string name; string passwd; string email; static int n; public: User(string N, string P = "111111", string E = ""); ~User(); void set_email(); void change_password(); void print_info(); void static print_n(); }; int User::n = 0; User::User(string N, string P, string E) : name(N), passwd(P), email(E) { n++; } void User::set_email() { std::cout << "Enter email address: "; std::cin >> email; std::cout << "email is set successfully..."; } void User::change_password() { std::cout << "Enter old password: "; string P1,P2; int k = 0; while (k < 3) { std::cin >> P1; if (passwd == P1) { std::cout << "Enter new password: "; std::cin >> P2; std::cout << "new passwd is set successfully..." << std::endl; break; }else { if(k < 2) { std::cout << "password input error. Please re-enter again: "; }else if (k == 2) { std::cout << "password input error. Please try after a while." << std::endl; } k++; } } } void User::print_info() { string P(passwd.length(), '*'); std::cout << std::setw(8) << std::left << "name: " << name << std::endl; std::cout << std::setw(8) << std::left << "passwd: " << P << std::endl; std::cout << std::setw(8) << std::left << "email: " << email << std::endl; } void User::print_n() { std::cout << "there are " << n << " users." << std::endl; } User::~User() {}
task5.cpp
#include "User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_password(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
测试1:
测试2: