实验任务4
.hpp
#pragma once #include <iostream> #include <string> #include <iomanip> #include <cmath> using std::cout; using std::endl; class Complex { public: Complex(); Complex(double a0,double b0); Complex(double a0); Complex(Complex& other); double get_real() const {return a;}; double get_imag() const; void show() const; Complex add(const Complex &c2); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(const Complex &c); private: double a,b; }; Complex::Complex(double a0,double b0) { a = a0;b = b0; } Complex::Complex() { a = 0;b = 0; } Complex::Complex(double a0) { a = a0;b = 0; } double Complex::get_imag()const { return b; } void Complex::show() const { if(b) cout << a << (b > 0 ? "+" : "-") << (b > 0 ? b :(-b) ) << "i" << endl; else cout << a << endl; } Complex::Complex(Complex &other) { a = other.a; b = other.b; } Complex Complex::add(const Complex &c2) { Complex c; c.a += a + c2.a; c.b += b + c2.b; return c; } Complex add(const Complex &c1,const Complex &c2) { Complex c3; c3.a = c1.a + c2.a; c3.b = c1.b + c2.b; return c3; } bool is_equal(const Complex &c1,const Complex &c2) { return (c1.a == c2.a) && (c1.b == c2.b); } double abs(const Complex &c) { return sqrt(c.a * c.a + c.b * c.b); }
.cpp
#include "Complex.hpp" #include <iostream> void test() { using namespace std; Complex c1(4, -7); const Complex c2(4.9); 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
.hpp
#pragma once #include<iostream> #include<string> using namespace std; class User { public: User(string name0); User(string name0,string password0,string email0); void set_email(); void change_passwd(); void printf_info(); static int print_n(); private: string name,password,email; }; User::User(string name0) { name = name0; password = "111111"; email = ' '; } User::User(string name0,string password0,string email0) { name = name0; password = password0; email = email0; } void User::set_email() { cout << "Enter email address:" ; cin >> email; cout << "email is set sucessfully..." << endl; } void User::change_passwd() { cout << "Enter old passord:"; int cnt = 3; while(cnt --) { string s0; cin >> s0; if(s0 != password) { if (cnt != 0) cout << "password input error.Please re-enter again:" ; if (cnt ==0) cout << "password input error.Please try after a while." << endl ; } else { cout << "Enter new password:"; cin >> password; cout << "new password is set successfully..." << endl; break; } } } void User::printf_info() { string s1(password.length(),'*'); cout << "name:" << name << endl; cout <<"password:" << s1 << endl; cout << "Email:" << email << endl; } int User::print_n() { static int x = 1; x ++; cout << "there are " << x << " users." << endl; return x; }
.cpp
#include "User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Hermione", "654123", "[email protected]"); user1.printf_info(); cout << endl << "testing 2......\n\n"; User user2("Granger"); user2.change_passwd(); user2.set_email(); user2.printf_info(); cout << endl; User::print_n(); } int main() { test(); }
标签:const,cout,double,Complex,实验,User,include From: https://www.cnblogs.com/Gjr202183290137/p/16803087.html