实验结论:
实验任务四:
Complex.hpp:
#pragma once #include <iostream> #include<math.h> using namespace std; class Complex { public: Complex(double r = 0, double i = 0) :real{ r }, imag{ i } {}; Complex(const Complex& 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 c1) { real += c1.real; imag += c1.imag; }; friend double abs(Complex c) { return sqrt(c.real * c.real + c.imag * c.imag); } friend bool is_equal(Complex c1, Complex c2) { if (c1.real == c2.real && c1.imag == c2.imag) { return 1; } else { return 0; } }; friend Complex add(Complex c1, Complex c2) { Complex c; c.real = c1.real + c2.real; c.imag = c1.imag + c2.imag; return c; }; private: double real, imag; }; void Complex::show() const { if (imag >= 0) { cout << real << "+" << imag << "i"; } else { cout << real << imag << "i"; }; }
task 4:
#include "Complex.hpp" #include <iostream> void test() { using namespace std; Complex c1(5, 12); 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:
User.hpp
#pragma once #include <iostream> #include<string> using namespace std; class User { public: User(string name, string passwd = "111111", string email = "") :name{ name }, passwd{ passwd }, email{email} { count++; }; void print_info() { cout << name << endl; string s2(passwd.length(), '*'); cout << s2 << endl; cout << email << endl; }; static void print_n() { cout <<"there are" <<" "<< count <<" users" << endl; }; void set_email() { email = ""; cout << "enter email address:"; cin >> email; cout << "email is set successfully...." << endl; ; }; void change_passwd(){ int i; string s1; cout << "Enter old password:"; for (i=0;i<3;i++) { cin >> s1; if (s1 == passwd) { cout << " Enter new password:"; cin >> passwd; cout << "new passwd is set successfully...." << endl; break; } else { cout << "password input error.Please re-enter again:"; } } if (i == 3) { cout << "password input error.Please try after awhile" << endl; }; } private: string name; string passwd; string email; static int count; }; int User::count = 0;
task 5:
#include "User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl<< "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
测试1:
测试2:
标签:real,cout,对象,imag,Complex,实验,c1,include From: https://www.cnblogs.com/lovelexington/p/16784561.html