实验2
#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class complex {
public:
complex(double r = 0.0, double i = 0.0) :real(r), imag(i){}
complex(complex& p) :real(p.real), imag(p.imag){}
double get_real() const { return real; }
double get_imag() const { return imag; }
void add(const complex& p) { real += p.real; imag += p.imag; }
void show() const;
private:
double real, imag;
friend complex add(const complex& p1,const complex& p2);
friend bool is_equal (const complex& p1, const complex& p2);
friend double abs(const complex& p);
};
void complex::show()const {
cout << real;
if (imag > 0) cout << "+" << imag << "i";
else if (imag < 0) cout << imag << "i";
}
complex add(const complex& p1, const complex& p2) {
complex p;
p.real =p1.real + p2.real;
p.imag = p1.imag +p2.imag;
return p;
}
bool is_equal (const complex& p1, const complex& p2) {
if (p1.real == p2.real && p1.imag == p2.imag)
return true;
else return false;
}
double abs(const complex& p) {
double m = sqrt(p.real * p.real + p.imag * p.imag);
return m;
}
#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();
}

#pragma once
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class user {
private:
string name;
string passwd;
string email;
static int n;
public:
user(string name0, string passwd0 = "111111", string email0 = "111111");
void set_email() { cout << "enter email address: "; cin >> email; cout << "email is set successfully..." << endl; }
void change_passwd();
void print_info() const;
void static print_n() { cout << "there are "<<n <<" users" << endl; }
};
user::user(string name0, string passwd0, string email0) :name(name0), passwd(passwd0), email(email0) { n++; }
int user::n = 0;
void user::change_passwd () {
string old = passwd;
int count;
cout << "enter old password: ";
cin >> passwd;
count = 1;
while (old!=passwd&&count<3) {
cout << "password input error.please re-enter again: ";
cin >> passwd;
count++;
if (count == 3) {
cout << "password input error.please try after a while." << endl;
}
}
if (count != 3) {
cout << "enter new passwd: ";
cin >> passwd;
cout << "new passwd is set successfully..." << endl;
}
}
void user::print_info() const {
int m;
cout << "name: " << name << endl;
cout << "passwd: ";
m = size(passwd);
while (m--) cout << "*";
cout << endl;
cout << "email: " << email << endl;
}
#include<iostream>
#include"user.hpp"
void test() {
using std::cout;
using std::endl;
cout << "testong 1......\n";
user user1("jonny", "92197", "xyz@hotmail.com");
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();
}


浙公网安备 33010602011771号