实验2

任务3

#include "Complex.h"
 #include<iostream>
 #include<cmath>
 Complex::Complex(double real, double imag): real{real}, imag{imag} {}
 Complex::Complex(const Complex &c): real{c.real}, imag{c.imag} {}
 Complex::~Complex() {}
 double Complex::get_real() const {
     return real;
 }
 
 double Complex::get_imag() const {
     return imag;
 }
 void Complex::show() const {
     using namespace std;
     if(imag == 0) {
         cout << real << endl;
     }
    else if(imag > 0) {
         cout << real << "+" << imag << "i" << endl;
     }
     else if(imag < 0) {
         cout << real << imag << "i" << endl;
     }
 }
 void Complex::add(const Complex &c) {
     real += c.real;
     imag += c.imag;
 }
 Complex add(const Complex &c1, const Complex &c2) {
     return Complex(c1.real + c2.real, c1.imag + c2.imag);
 }
 bool is_equal(const Complex &c1, const Complex &c2) {
     if(c1.real == c2.real && c1.imag == c2. imag) {
         return true;
     }
     else {
         return false;
     }
 }
 double abs(const Complex &c) {
     return sqrt(c.real * c.real + c.imag * c.imag);
 }
 #ifndef Complex_H
 #include<iostream>
 #include<cmath>
  class Complex{
     public:
         Complex(double real = 0.0, double imag = 0.0);
         Complex(const Complex &c);
        ~Complex();
         
         double get_real() const;
        double get_imag() const;
        void add(const Complex &c);
         void show() const;
         
     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;
 };
 
 #endif

任务4

 #include <iostream>
 #include <string>
  class User {
  private:
      std::string name;
      std::string passwd;
     std::string email;
      static int n;
public:
     User(const std::string& name, const std::string& passwd, const std::string& email) :
         name(name), passwd(passwd), email(email) {
         ++n;
     }
      User(const std::string& name) : name(name) {
         ++n;
     }
      void set_email() {
         std::string new_email;
        std::cout << "Enter email address: ";
         std::cin >> new_email;
         std::cout << "Email is set successfully..." << std::endl;
         email = new_email;
    }
     void change_passwd() {
         std::string old_passwd;
         std::string new_passwd1;

         int try_count = 0;
         while (try_count < 3) {
             std::cout << "Enter old password: ";
             std::cin >> old_passwd;
             if (old_passwd == passwd) {
                 break;
            } else {
                 std::cout << "Password input error. Please re-enter again: " << std::endl;
                 ++try_count;
             }
         }
         if (try_count == 3) {
             std::cout << "Password input error. Please try after a while." << std::endl;
             return;
        }
         while (true) {
             std::cout << "Enter new password: ";
             std::cin >> new_passwd1;
            std::cout << "new passwd is set successfully..." << std::endl;
             passwd = new_passwd1;
            break;
         }
    }
      void print_info() const {
         std::cout << "name: " << name << std::endl;
         std::cout << "passwd: ";
        for (size_t i = 0; i < passwd.size(); ++i) {
             std::cout << "*";
         }
         std::cout << std::endl;
        std::cout << "email: " << email << std::endl;
     } 
     static void print_n() {
         std::cout << "there are " << n << " users." << std::endl;
     }
};
int User::n = 0;
 #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.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main() {
 test();
}

任务5

#ifndef__ACCOUNT_H__
#define__ACCOUNT_H__

class SavingsAccount
{
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;
    static double total;
    void record(int data,double amount);
    double accumulate(int date)const
    {
        return accumulation+balance*(date-lastDate);
    }
public:
    SavingsAccount(int data,int id,double rate);
    int getId()const{return id;}
    double getBalance()const{return balance;}
    double getRate()const{return rate;}
    static double getTotal(){return total;}
    void deposit(int date,double amount);
    void withdraw(int data,double amount);
    void settle(int date);
    void show() const;
};
#include"accout.hpp"
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total=0;
SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0)
{
    cout<<date<<"\t#"<<id<<"is created"<<endl;
}
void SavingsAccount::record(int date,double amount)
{
    accumulation=accumulate(date);
    lastDate=date;
    amount=floor(amount*100+0.5)/100;
    balance+=amount;
    total+=amount;
    cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void SavingsAccount::deposit(int date,double amount)
{
    record(date,amount);
}
void SavingsAccount::withdraw(int date,double amount)
{
    if(amount>getBalance())
        cout<<"Error:not enough money"<<endl;
    else
        record(date,-amount);
}
void SavingsAccount::settle(int data)
{
    double interest=accumulate(data)*rate/365;
    if(interest!=0)
        record(data,interest);
    accumulation=0;
}
void SavingsAccount::show() const
{
    cout<<"#"<<id<<"\tBalance:"<<balance;
}
#include "accout.hpp"
#include <iostream>
using namespace std;
int main() {

    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sa1(1, 58320212, 0.015);
 
    sa0.deposit(5, 5000) ;
    sa1.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);

    sa0.settle(90);
    sa1.settle(90);
 
    sa0.show();cout << endl;
    sa1.show();cout << endl;
    cout << "Total:" << SavingsAccount::getTotal() << endl;
    return 0;
}

 

posted @ 2023-10-23 01:49  Tantivy  阅读(19)  评论(0)    收藏  举报