实验二
task4:
complex.hpp:
#pragma once #include<iostream> #include<cmath> using std::cout; using std::endl; class complex{ public: complex(double r=0.0,double i=0.0):real(r),imag(i){} complex(complex& c) :real(c.real), imag(c.imag){}void add(const complex &c){ real+=c.real; imag+=c.imag;} void show()const; double get_real() const { return real; } double get_imag() const { return imag; } friend double abs(const complex &c); friend complex add(const complex &c1,const complex &c2); friend bool is_equal(const complex &c1,const complex &c2); private: double real,imag; }; void complex::show()const{ if(imag!=0) cout << real << "+" << imag << "i" << endl; else cout << real << imag << "i" << endl; } double abs(const complex &c){ double m; m=sqrt(c.real*c.real+c.imag*c.imag); return m; } complex add(const complex &c1,const complex &c2){ complex c3; c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return c3; } bool is_equal(const complex &c1,const complex &c2){ if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; }
task.cpp:
#include "complex.hpp" #include <iostream> void test() { using namespace std; complex c1(3, -4); 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(); }


task5:
users:
#pragma once #include<iostream> #include<cstring> #include<string> using namespace std; class user { public: user(string name0,string passwd0="111111",string email0=""); void set_email(); void change_passwd(); void print_info() const; void static print_n() {cout << "there are " << n << " users" << endl;} private: string name; string passwd; string email; static int n; }; user::user(string name0,string passwd0,string email0):name(name0),passwd(passwd0),email(email0){n++;} int user::n=0; void user::set_email(){ cout << "Enter email address:"; cin >> email; cout << "email is set successfully..." << endl; } void user::change_passwd(){ string oldpasswd = passwd; int count; cout << "Enter old password:"; cin >> passwd; count = 1; while(oldpasswd!=passwd&&count<3){ cout << "password input error. please re-enter again:"; cin >> passwd; count++; if(oldpasswd!=passwd&&count == 3){ cout << "password input error. please try after a while." << endl; } } if(oldpasswd == passwd){ cout << "Enter new passwd:"; cin >> passwd; cout << "new passwd is set successfully..." << endl; } if(oldpasswd!=passwd&&count == 3) passwd = oldpasswd; } void user::print_info() const{ int k; cout << "name: " << name << endl; cout << "passwd: "; k = passwd.size(); while(k--) cout << "*"; cout << endl; cout << "email: " << email << endl; }
task5.cpp:
#include "user.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; user user1("Hansem", "1269854", "qwer@hhhmail.com"); user1.print_info(); cout << endl << "testing 2......\n\n"; user user2("David"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; user::print_n(); } int main() { test(); }



浙公网安备 33010602011771号