首页 > 其他分享 >实验2 类和对象

实验2 类和对象

时间:2023-10-21 21:14:29浏览次数:35  
标签:real const cout 对象 double void int 实验

实验任务3

Complex.hpp
#pragma once

#include <iostream>
#include <cmath>

class Complex {
public:
    Complex(double r = 0, double i = 0) {
        real = r;
        imag = i; 
    }
    
    Complex(const Complex &x) {
        real = x.real;
        imag = x.imag;
    }
    
    double get_real() const {return real;}
    double get_imag() const {return imag;}
    
    void add(const Complex &x) {
        real += x.real;
        imag += x.imag;
    }
    
    void show() const {
        using namespace std; 
        if(real == 0 && imag == 0) {cout << 0; }
        else if(real == 0) {cout << imag << "i";}
        else if(imag == 0) {cout << real;}
        else {
            if(imag > 0){
                cout << real << " + " << imag << "i";
            }
            else if(imag < 0) {
                cout << real << " - " << -imag << "i";
            }
        }
    }
    
    friend Complex add(const Complex &c1, const Complex &c2);
    friend bool is_equal(const Complex &c1, const Complex &c2);
    friend double abs(const Complex &c1);
    
private:
    double real;
    double imag;
}; 

Complex add(const Complex &c1, const Complex &c2) {
    double real = c1.real + c2.real;
    double imag = c1.imag + c2.imag;
    return Complex(real, 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 &c1) {
    return sqrt(c1.real * c1.real + c1.imag * c1.imag);
}


main1.cpp
#include "Complex.hpp"

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

 

实验任务4

User.hpp
#pragma once

#include<iostream>
#include<string>

using namespace std;


class User {
public:
    User(string name,string passwd = "111111",string email = " "): n{name}, p{passwd}, e{email} {count++;}
    ~User() = default;    
 
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
 
private:
    string n,p,e;
    static int count;
    
};
 
int User::count = 0;

void User::set_email()
{
    cout << "Enter email address: " ;
    cin >> e ;
    cout << "email is set successfully..." << endl;
}
 
void User::change_passwd()
{
    cout << "Enter old password: ";
    string temp;
    int i = 3;
    while(i)
    {
        cin >> temp;
        if(temp == p)
        {   
            cout << "Enter new passwd: ";
            cin >> p;  
            cout << "new passwd is set successfully..." << endl;
            break;
        }
        else 
        {
            i--;
            if(i != 0)
                cout << "password input error. Please re-enter again: " ;
        }
        if(i == 0)
        {
            cout << "password input error. Please try after a while." << endl;
        }
    }
 
}
 
void User::print_info()
{   
    string pp(p.size(), '*');
    cout << "name:   " << n << endl;
    cout << "passwd: " << pp << endl;
    cout << "email:  " << e << endl;
}
 
void User::print_n()
{
    cout << "there are " << count << " users." << endl;
}

task4.cpp
#include "User.hpp"

void test() {
   using std::cout;
   using std::endl;
   
   cout << "testing 1......\n";
   User user1("Jonny", "92197", "[email protected]");
   user1.print_info();
   
   cout << endl;
   cout << "testing 2......\n";
   
   User user2("Leonard");
   user2.print_info();
   user2.change_passwd();
   user2.set_email();
   user2.print_info();
   
   cout << endl;
   User::print_n();
}

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

 

实验任务5

//account.h
class SavingsAccount {
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;
    static double total;
    
    void record(int date, double amount);
    double accumulate(int date) const {
        return accumulation+balance*(date-lastDate);
    }
public:
    SavingsAccount(int date, 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 data, double amount);
    void withdraw(int data, double amount);
    void settle(int data);
    void show() const; 
};

//account.cpp
#include "account.h"
#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 date) {
    double interest = accumulate(date) * rate/365;
    if(interest != 0)
      record(date, interest);
    accumulation = 0;
}
void SavingsAccount::show() const {
    cout << "#" << id << "\tBalance: " << balance;
}

//5_11.cpp
#include "account.h"
#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;
}
View Code

 

标签:real,const,cout,对象,double,void,int,实验
From: https://www.cnblogs.com/Lucky-19/p/17767882.html

相关文章

  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;pr......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516......
  • 实验二
    实验任务3:1.complex.hpp1#pragmaonce2#include<iostream>3#include<cmath>4classComplex{public:5Complex(doubler=0,doublei=0){6real=r;imag=i;}7Complex(constComplex&x){8real=x.real;......
  • 实验二
    Task1: 1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));1415for(i=0;i<N;......
  • 试验2 类和对象——基础编程2
    任务31.代码complex.hpp:1#ifndefcomplex_H2#definecomplex_H34#include<iostream>5#include<cmath>6usingnamespacestd;78classComplex9{10public:11Complex(doublex=0,doubley=0):real{x},imag{y}{}12......
  • 实验二
    1.test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%......
  • make clean命令清理在不同目录中编译的对象
    gnu-makemakefile UsingMakefiletocleansubdirectories是否可以从父目录执行makeclean,而该父目录又递归清除所有子目录,而不必在每个子目录中都包含makefile?例如,当前在我的Makefile中,我有类似以下内容:123456789SUBDIRS=src,src1.PHONY:cleansubdirs$(S......
  • map遍历数组返回包含所需字段的对象
    假如dataList为后台假数据,我想分别得到number和chargeTime、number和freeTime,来分别画图,就可以这么写,当然直接for循环更可以。1constdataList={2list:[3{4number:"0",5chargeTime:2,6freeTime:57......
  • 实验2 C语言分支与循环基础应用编程
    1.实验任务1task1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));//以当前......