实验一
作者:互联网
task3:
#ifndef Complex_HPP #define Complex_HPP #include <iostream> #include <iomanip> #include <string> #include<cmath> using namespace std; class Complex { public: Complex(double x = 0, double y = 0) :real{ x }, imag{ y }{} Complex(const Complex& c0) :real{ c0.real }, imag(c0.imag){} double get_real() { return real; } double get_imag()const { return imag; } void show()const; void add(Complex c1); friend Complex add(const Complex& c2,const Complex& c3); friend bool is_equal(Complex c4, Complex c5); friend double abs(Complex c6); private: double real; double imag; }; void Complex::show()const { if (imag < 0) { cout << real << imag << "i" ; } else if (imag == 0) { cout << real; } else cout << real << "+" << imag << "i"; } void Complex::add(Complex c1) { real += c1.real; imag += c1.imag; } Complex add(const Complex& c2,const Complex& c3) { Complex ch; ch.real = c2.real + c3.real; ch.imag = c2.imag + c3.imag; return ch; } bool is_equal(Complex c4, Complex c5) { if (c4.real == c5.real && c4.imag == c5.imag) { return 1; } else return 0; } double abs(Complex c6) { return static_cast<double>(sqrt(c6.real * c6.real + c6.imag * c6.imag)); } #endif
cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(5, -12); const Complex c2(7.3); 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; }
task 4:
#ifndef User_HPP #define User_HPP #include <iostream> #include <iomanip> #include <string> using namespace std; class User { public: User(string name0, string passwd0 = "111111", string email0 = "") :name{ name0 }, passwd{ passwd0 }, email{ email0 }{++n; } void set_email(); void change_passwd(); void print_info()const; static void print_n(); private: string name; string passwd; string email; static int n; }; int User::n = 0; void User::set_email() { string skt; cout << "Enter email address:"; cin >> skt; email = skt; cout << "email is set successfully..." << endl; } void User::print_info()const{ cout << "name:\t" << name << endl; cout << "passwd:\t" << "******" << endl; cout << "email:\t" << email << endl; } void User::print_n() { if (n == 1) cout << "there are " << n << " user\n"; else cout << "there are " << n << " users\n"; } void User::change_passwd() { string rox; cout << "Enter old password: "; cin >> rox; if (rox != passwd) { cout << "password input error.Please re-enter again: "; string rox1; cin >> rox1; if (rox1 != passwd) { cout << "password input error.Please re-enter again: "; string rox2; cin >> rox2; if (rox2 != passwd) { cout << "password input error.Please try after a while." << endl; } else { string rox5; cout << "Enter new password: "; cin >> rox5; passwd = rox5; cout << "new passwd is set successfully..." << endl; } } else { string rox4; cout << "Enter new password: "; cin >> rox4; passwd = rox4; cout << "new passwd is set successfully..." << endl; } } else { string rox3; cout << "Enter new password: "; cin >> rox3; passwd = rox3; cout << "new passwd is set successfully..." << endl; } } #endif
cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "shadoubuhui@.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Liyundi"); user2.change_passwd(); user2.set_email(); user2.print_info(); User user3("leshi", "666666"); user3.change_passwd(); user3.set_email(); user3.print_info(); User::print_n(); }
标签:const,cout,passwd,imag,Complex,实验,include 来源: https://www.cnblogs.com/AJ1013/p/15449670.html