实验二
task4
Complex.hpp
#pragma once
#include<iostream>
#include<cmath>
using std::cout;
using std::endl;
class Complex
{
public:
Complex(float x=0,float y=0):
real{x},imag{y}{}
Complex(const Complex& obj) :
real{ obj.real }, imag{ obj.imag } {}
double get_real() const {return real;}
double get_imag() const {return imag;}
void show() const ;
void add(const Complex &obj);
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 real,imag;
};
void Complex::show() const
{
if (imag > 0)
cout << real << '+' << imag << 'j';
else if (imag < 0)
cout << real << imag << 'j';
else
cout << real;
}
void Complex::add(const Complex &obj)
{
real+=obj.get_real();
imag+=obj.get_imag();
}
Complex add(const Complex &c1,const Complex &c2)
{
float add_real=c1.real+c2.real;
float add_imag=c1.imag+c2.imag;
Complex c(add_real,add_imag);
return c;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
return true;
return false;
}
double abs(const Complex &c)
{
return sqrt(pow(c.real,2)+pow(c.imag,2));
}
task4.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
User.hpp
#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::setw;
class User
{
public:
User(string x,string y="666666", string z="\0") :
name{ x }, password{ y }, email{ z } {n++;}
~User() { n--;}
void set_email();
void change_passwd();
void print_info() const;
static void print_n() { cout <<"There are " <<n <<" users" << endl; }
private:
string name, password, email;
static int n;
};
int User::n = 0;
void User::set_email()
{
string s;
cout << "Enter email address :";
cin >> s;
email = s;
cout << "email is set successfully" << endl;
}
void User::change_passwd()
{
string old_password;
cout << "Enter old password : ";
int num;
for (num=1;num<=3;++num)
{
cin >> old_password;
if (password == old_password)
{
password = old_password;
cout << "password is changed successfully" << endl;
break;
}
else
cout << "password input error.";
if(num<=2)
cout << "Please re-enter again : ";
}
if (num > 3)
cout << "Please try after a while." << endl;
}
void User::print_info() const
{
cout << std::left<<setw(12) << "name:" << name<<endl;
cout << std::left<< setw(12) << "password:";
for (int i = 1; i <= password.size(); ++i)
cout<<"*";
cout << endl;
cout << std::left<< setw(12) << "email:" << email << endl;
}
task5.cpp
#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
using std::cout;
using std::endl;
cout << "testing 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号