实验任务3
t.hpp
#pragma once #include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex(); Complex(double real0); Complex(double real0,double imag0); Complex(const Complex &x); double get_real()const {return real;} double get_imag()const {return imag;} void show()const; void add(const Complex &x); friend Complex add(Complex x1, Complex x2); friend bool is_equal(const Complex &x1,const Complex &x2
friend double abs(const Complex &x); private: double real,imag; }; Complex::Complex(){ real = 0; imag = 0; } Complex::Complex(double real0){ real = real0; imag = 0; } Complex::Complex(double real0,double imag0) { real = real0; imag = imag0; } Complex::Complex(const Complex &x){ real = x.real; imag = x.imag; } void Complex::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" ; } } } void Complex::add(const Complex &x){ real = real+x.real; imag = imag+x.imag; } Complex add(Complex x1, Complex x2) { double real,imag; x1.real = x1.real + x2.real; x2.imag = x1.imag + x2.imag; return x1; } bool is_equal(const Complex &x1, const Complex &x2) { bool real,imag; real = x1.real == x2.real; imag = x1.imag == x2.imag; return real && imag; } double abs(const Complex &x) { return sqrt(pow(x.real, 2) + pow(x.imag, 2)); }
main.cpp
#include "t.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(); }
实验任务4
User.hpp
#pragma once #include <iostream> #include <string> class User{ public: User(const std::string& name, const std::string& passwd = "111111", const std::string& email = ""):name_(name),passwd_(passwd),email_(email) {} std::string get_name() const { return name_; } std::string get_passwd() const { return passwd_; } std::string get_email() const { return email_; } static void print_n(); void set_email() const{ using namespace std; string email_; std::cout << "Enter email address:"; cin >> email_; cout << "email is set successfully..." << endl; } void change_passwd()const { using namespace std; string mm; std::cout << "Enter old password:"; cin >> mm; if(mm==passwd_) { cout << "Enter new passwd: oop_cplus_2023" << endl; cout << "new passwd is set successfully..." << endl; } else{ int i; for(i = 0; i<3;i++){ if(i <2 ) { std::cout << "password input error.Please re-enter again:" << std::endl; cin >> mm; continue; } else std::cout << "password input error.Please try after a while." << std::endl; return; } } } void print_info()const { std::cout << "name:" << name_ << std::endl; std::cout << "passwd:"; for (int i = 0; i < passwd_.size(); ++i){ std::cout << "*"; } std::cout << std::endl; std::cout << "email:" << email_ << std::endl; } private: std::string name_; std::string passwd_; std::string email_; }; using namespace std; void User::print_n() { cout << "there are " << 2 << " users." << endl; }
task.cpp
#include "User.hpp" #include <iostream> void test(){ using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny","92197","[email protected]"); 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(); }
实验任务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; }
标签:std,const,cout,对象,double,int,Complex,实验 From: https://www.cnblogs.com/yzsya/p/17776920.html