实验任务3:
"Complex.hpp"
#include<iostream> #include<math.h> using namespace std; class Complex { public: Complex(double x = 0, double y = 0) :real(x), imag(y) {}; Complex(Complex const& c) :real(c.real), imag(c.imag) {}; double get_real() const { return real; }; double get_imag() const { return imag; }; void show()const; void add(Complex const &c1) { Complex c; c.real = real + c1.real; c.imag = imag + c1.imag; real = c.real; imag = c.imag; }; friend Complex add(Complex c1, Complex c2); friend bool is_equal(Complex const c1, Complex const c2); friend double abs(Complex c); private: double real; double imag; }; void Complex::show()const { { if (imag == 0) cout << real; else if (imag >= 0) cout << real << "+" << imag << 'i'; else cout << real << imag << "i"; }; } Complex add(Complex c1, Complex c2) { Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; }; bool is_equal(Complex const c1, Complex const c2) { if (c1.real == c2.real && c1.imag == c2.imag) return true; else return false; }; double abs(Complex c) { return sqrt(c.real * c.real + c.imag * c.imag); };
task3.hpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(2, 2); const Complex c2(3.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; }
运行结果:
实验4:
“User.hpp”
#include<iostream> #include<string> using namespace std; class User { public: User(string n, string p = "111111", string e = "") :name(n), passwd(p), email(e) { ++count; }; void set_email() { cout << "Enter e-mail address:"; cin >> email; cout << endl; cout<< "email is set successfully..." << endl; }; void change_passwd(); void print_info() { cout << "name:" << name<<endl; cout << "passwd:******" << endl; cout << "email:" << email << endl; }; static void print_n(); private: string name; string passwd; string email; static int count; }; int User::count = 0; void User::change_passwd() { string p; int i; cout << "Enter old passwd:"; cin >> p; cout << endl; if(p != passwd) { for (i = 0; i <= 1; i++) { cout << "passwd input error. Please re-enter again: "; cin >> p; cout << endl; } cout << "passwd input error. Please try again while" << endl; } else { cout << "Enter new passwd:"; cin >> p; cout << endl; cout<< "new passwd is set successfully..." << endl; passwd = p; } } void User::print_n() { cout << "there are " << count << "users ." << endl; }
task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Taylor", "19891213", "cat@gmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Adele"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
运行结果: