MaskerQwQ

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

task4:

Complex.hpp:

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<cmath>
 5 
 6 using std::cout;
 7 using std::endl;
 8 
 9 class Complex{
10     public:
11         Complex(double r=0.0,double i=0.0):real{r},imag{i}{}
12         Complex(const Complex &p):real{p.real},imag{p.imag}{}
13         
14         double get_real() const {return real;}
15         double get_imag() const {return imag;}
16 
17         void show() const{
18             if(imag==0){
19                 cout<<real;
20             }else{ 
21                 if(imag>0){
22                     cout<<real<<"+"<<fabs(imag)<<"i";
23                 }else{
24                     cout<<real<<"-"<<fabs(imag)<<"i";
25                 }
26             }
27         }
28         void add(const Complex &p){
29             real+=p.real;
30             imag+=p.imag;
31         }
32         friend Complex add(const Complex &p1,const Complex &p2){
33             return Complex(p1.real+p2.real,p1.imag+p2.imag);
34         }
35         friend bool is_equal(const Complex &p1,const Complex &p2){
36             if(p1.real==p2.real && p1.imag==p2.imag){
37                 return true;
38             }else{
39                 return false;
40             }
41         }
42         friend double abs(const Complex &p){
43             return sqrt(pow(p.real,2)+pow(p.imag,2));
44         }
45     private:
46         double real,imag;
47 };

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>

using namespace std;

class User{
    public:
        
        User(string n,string p="111111",string e=""):name{n},passwd{p},email{e}{ n+=1;}
        
        void set_email(){
            cout<<"Enter email address:";
            cin>>email;
            cout<<"email is set successfully..."<<endl;
        };
        
        void change_passwd(){
            
            string testpasswd;
            cout<<"Enter old password:";
            cin>>testpasswd;
            
            for(int times=1;times<3&&testpasswd!=passwd;times++){
                cout<<"password input error.Please re-enter again:";
                cin>>testpasswd;
            }
            if(testpasswd!=passwd){
                cout<<"password input error.Please try after a while";
            }else{
                cout<<"Enter new passwd:";
                cin>>passwd;
                cout<<"new password is set successfully..";
            }
        };
        void print_info(){
            cout<<"name:      "<<name<<endl;
            cout<<"password:";
            for(int i=1;passwd[i];i++){
                cout<<"*";
            }
            cout<<endl;
            cout<<"email:   "<<email<<endl;
        };
        static void print_n(){
            cout<<"there are"<<n<<"users"<<endl;
        }
        
    private:
        string name,passwd,email;
        static int n;
    
};
    int User::n=0;

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();
}

User.hpp:

#pragma once

#include<iostream>
#include<string>

using namespace std;

class User{
    public:
        
        User(string n,string p="111111",string e=""):name{n},passwd{p},email{e}{num++;}
        
        void set_email(){
            cout<<"Enter email address:";
            cin>>email;
            cout<<"email is set successfully..."<<endl;
        };
        
        void change_passwd(){
            
            string testpasswd;
            cout<<"Enter old password:";
            cin>>testpasswd;
            
            for(int times=1;times<3&&testpasswd!=passwd;times++){
                cout<<"password input error.Please re-enter again:";
                cin>>testpasswd;
            }
if(testpasswd!=passwd){ cout<<"password input error.Please try after a while"<<endl; }else{ cout<<"Enter new passwd:"; cin>>passwd; cout<<"new password is set successfully.."; } };
void print_info(){ cout<<"name: "<<name<<endl; cout<<"password:"; for(int i=1;passwd[i];i++){ cout<<"*"; } cout<<endl; cout<<"email: "<<email<<endl; };
static void print_n(){ cout<<"there are "<<num<<" users."<<endl; } private:
string name,passwd,email; static int num; }; int User::num=0;

测试1截图:

 

 测试2截图:

 

posted on 2022-10-17 22:58  MaskerQwQ  阅读(21)  评论(0编辑  收藏  举报