实验2 类和对象

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

// 复数类Complex: 测试
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();
}
View Code

 

#pragma once
#include <iostream>
#include <string>

class User {
private:
    std::string name;
    std::string passwd;
    std::string email;
    static int n;

public:
    User(std::string n, std::string p = "111111", std::string e = "") : name(n), passwd(p), email(e) {
        if (e.empty()) {
            email = "";
        }
        n++;
    }

    void set_email() {
        std::cout << "Enter email: ";
        std::cin >> email;
    }

    void change_passwd() {
        std::string oldPasswd;
        int attempts = 0;
        while (attempts < 3) {
            std::cout << "Enter old password: ";
            std::cin >> oldPasswd;
            if (oldPasswd == passwd) {
                std::cout << "Enter new password: ";
                std::cin >> passwd;
                std::cout << "Password changed successfully.\n";
                return;
            } else {
                std::cout << "Incorrect password. Try again.\n";
                attempts++;
            }
        }
        std::cout << "Too many incorrect attempts. Try again later.\n";
    }

    void print_info() const {
        std::string maskedPasswd(passwd.length(), '*');
        std::cout << "Name: " << name << std::endl;
        std::cout << "Password: " << maskedPasswd << std::endl;
        std::cout << "Email: " << email << std::endl;
    }

    static void print_n() {
        std::cout << "Total number of users: " << n << std::endl;
    }
};

int User::n = 0;
View Code
#include "User.hpp"
#include <iostream>

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.print_info();
    user2.change_passwd();
    user2.set_email();
    cout << endl;
    user2.print_info();
    User::print_n();
}

int main(){
    test();
}
View Code

 

 

 1 class SavingsAccount {
 2 private:
 3     int id;
 4     double balance;
 5     double rate;
 6     int lastDate;
 7     double accumulation;
 8     static double total;
 9     
10     void record(int date, double amount);
11     double accumulate(int date) const {
12         return accumulation+balance*(date-lastDate);
13     }
14 public:
15     SavingsAccount(int date, int id, double rate);
16     int getId() const {return id;}
17     double getBalance() const {return balance;}
18     double getRate() const {return rate;}
19     static double getTotal() {return total;}
20     void deposit(int data, double amount);
21     void withdraw(int data, double amount);
22     void settle(int data);
23     void show() const; 
24 };
View Code
 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total=0;
 7 SavingsAccount::SavingsAccount(int date, int id, double rate): id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
 8     cout << date << "\t#" << id << "is created" << endl;
 9 }
10 void SavingsAccount::record(int date, double amount) {
11     accumulation = accumulate(date);
12     lastDate = date;
13     amount = floor(amount*100+0.5)/100;
14     balance += amount;
15     total += amount;
16     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 
17 }
18 void SavingsAccount::deposit(int date, double amount) {
19     record(date, amount);
20 }
21 void SavingsAccount::withdraw(int date, double amount) {
22     if(amount>getBalance())
23       cout << "Error: not enough money" << endl;
24     else
25       record(date, -amount);
26 }
27 void SavingsAccount::settle(int date) {
28     double interest = accumulate(date) * rate/365;
29     if(interest != 0)
30       record(date, interest);
31     accumulation = 0;
32 }
33 void SavingsAccount::show() const {
34     cout << "#" << id << "\tBalance: " << balance;
35 }
View Code
 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main() {
 6     SavingsAccount sa0(1, 21325302, 0.015);
 7     SavingsAccount sa1(1, 58320212, 0.015);
 8     
 9     sa0.deposit(5,5000);
10     sa1.deposit(25,10000);
11     sa0.deposit(45,5500);
12     sa1.withdraw(60,4000);
13     
14     sa0.settle(90);
15     sa1.settle(90);
16     
17     sa0.show(); cout << endl;
18     sa1.show(); cout << endl;
19     cout << "Total: " << SavingsAccount::getTotal() << endl;
20     return 0;
21 }
View Code

 

posted @ 2023-10-23 09:45  张帅yyds  阅读(6)  评论(0编辑  收藏  举报