实验一 类与对象
作者:互联网
任务3 源代码:
Complex.hpp
#pragma onnce #include <iostream> #include <cmath> class Complex { friend Complex add(const Complex &, const Complex &); friend bool is_equal(const Complex &, const Complex &); friend double abs(const Complex &); public: Complex(const double &r = 0, const double &i = 0) : real{r}, imag{i} { ; } Complex(const Complex &c) : real{c.real}, imag{c.imag} { ; } ~Complex() = default; double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); private: double real; double imag; }; double Complex::get_real() const { return this->real; } double Complex::get_imag() const { return this->imag; } void Complex::show() const { using std::cout; cout << this->real; if(this->imag == 0) return; else { if(this->imag < 0) cout << " - "; else cout << " + "; cout << fabs(this->imag) << "i"; } } void Complex::add(const Complex &c) { this->real += c.real; this->imag += c.imag; } inline Complex add(const Complex &c1, const Complex &c2) { Complex c3(c1.real + c2.real, c1.imag + c2.imag); return c3; } inline bool is_equal(const Complex &c1, const Complex &c2) { return c1.real == c2.real && c1.imag == c2.imag; } inline double abs(const Complex &c) { return sqrt(c.real * c.real + c.imag * c.imag); }
task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(3.6666, -10.0001); const Complex c2(13.5555); 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
#pragma once #include <iostream> #include <iomanip> bool CheckStr(const std::string &str) { const char *cstr = str.c_str(); char *ptr = const_cast<char *>(cstr); while(*ptr != '\0') { if(*ptr > '0' && *ptr < '9') ptr++; else return false; } return true; } class User { public: User(const std::string &name, const std::string &passwd = "111111", const std::string &email = "") : name{name}, passwd{passwd}, email{email} { this->total_user++; } ~User() = default; static void print_n() { std::cout << "there arr " << User::total_user << " users."; } void set_email(); void change_passwd(); void print_info() const; private: std::string name; std::string passwd; std::string email; static int total_user; }; int User::total_user = 0; void User::set_email() { std::string tmp_email; while(true) { std::cout << "Enter email address: "; std::cin >> tmp_email; if(tmp_email.find('@') < tmp_email.size() && tmp_email.find(".com") < tmp_email.size()) { std::cout << "email is set successfully..." << std::endl; this->email = tmp_email; return; } else std::cout << "your email address is illegal..." << std::endl; } } void User::change_passwd() { std::string input_passwd; int attempts = 3; std::cout << "Enter old password: "; std::cin >> input_passwd; while(--attempts) { if(input_passwd.compare(this->passwd)) { std::cout << "password input error. Please re-enter again: "; std::cin >> input_passwd; } else { while(true) { std::string new_passwd; std::cout << "Enter new passwd: "; std::cin >> new_passwd; if(new_passwd.size() == 6 && CheckStr(new_passwd)) { this->passwd = new_passwd; return; } else std::cout << "the input contains invalid characters or the length is less than or more than six characters..." << std::endl; } } } std::cout << "Please try after a while. " << std::endl; } void User::print_info() const { using namespace std; cout.width(8); cout << setiosflags(ios::left) << "name: "; cout<< this->name << endl; cout.width(8); cout << setiosflags(ios::left) << "passwd: "; cout << "******" << endl; cout.width(8); cout << setiosflags(ios::left) << "email: "; cout << this->email << endl; }
task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl << "testing 3......" << endl << endl; User user3("Ricky", "123456"); user3.set_email(); user3.change_passwd(); user3.print_info(); User::print_n(); }
标签:real,std,const,对象,imag,passwd,Complex,实验 来源: https://www.cnblogs.com/rickychan/p/15451809.html