1.实验任务3
程序代码组织如下:
•Complex.h 类Complex的声明
#pragma once # include<iostream> # include<cmath> class Complex{ public: Complex(double r = 0, double i = 0); Complex(const Complex &c); ~Complex(){}; double get_real() const; double get_imag() const; void add(const Complex &c); void show() const; friend Complex addl(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.cpp 类Complex的实现
# include"Complex.h" Complex::Complex(double r,double i):real{r},imag{i}{} Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{} double Complex::get_real() const {return real;} double Complex::get_imag() const {return imag;} void Complex::add(const Complex &c){ real += c.real; imag += c.imag; } void Complex::show() const { if(imag>0) {std::cout << real << " + " << imag << "i"; } else if(imag<0) {std::cout << real << imag << "i"; } else if(imag ==0) {std::cout << real ; } } Complex addl(const Complex &c1 , const Complex &c2){ double resultreal = c1.real + c2.real; double resultimag = c1.imag + c2.imag; return Complex (resultreal,resultimag); } bool is_equal(const Complex &c1 , const Complex &c2){ return (c1.real == c2.real)&&(c1.imag == c2.imag); } double abs(const Complex &c){ double x = sqrt(c.real*c.real + c.imag*c.imag); return x; }
•main.cpp 测试代码、主函数
# include"Complex.h" 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 = addl(c1,c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main(){ test(); }
运行测试结果截图:
2.实验任务4
程序组织代码如下:
•User.hpp 文件源码
# pragma once # include<iostream> # include<string> # include<iomanip> using namespace std; class User{ public: User(); User(string name0, string passwd0 = "111111", string email0 = " "); void set_email(); void change_passwd(); string get_name() const; string get_passwd() const; string get_email() const; void print_info(); public: static void print_n(); private: string name, passwd, email; private: static int n; }; int User::n = 0; User::User(){ ++n;} User::User(string name0, string passwd0, string email0):name{name0}, passwd{passwd0}, email{email0}{ ++n;} string User::get_name() const { return name; }; string User::get_email() const { return email; }; string User::get_passwd() const { return passwd; }; void User::set_email() { cout << "Enter email address: " ; cin >> email; cout <<"email is set successfully......" << endl; } void User::change_passwd(){ string passwd1; int i = 0; cout << "Enter old password: "; cin >> passwd1; while(passwd1 != "111111" && i < 2 ) { i++; cout << "password input error. Please re-enter again: "; cin >> passwd1; } if(passwd1 == "111111"){ cout << "Enter new passwd: "; string reset_passwd; cin >> reset_passwd; passwd = reset_passwd; cout << "new passwd is set successfully..." << endl ; } else if( i = 2 ) cout << "password input error. Please try after a while." << endl; } void User::print_info(){ string s2(passwd.size(),'*'); cout << "name: " << name << endl; cout << "password: " << s2 << endl; cout << "email: " << email <<endl; } void User::print_n(){ cout << "there are " << n << " users." << endl; }
•task4.cpp 源码
# 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 << "testing2......\n"; User user2("Leonard"); user2.print_info(); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main(){ test(); }
运行结果测试截图1:
运行结果测试截图2:
3.实验任务5
程序组织代码如下:
•account.h 类account的声明
# 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 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 date, double amount); void withdraw(int date, double amount); void settle(int date); void show() const; }; # endif
•account.cpp 类account的实现
# 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:nit 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; }
运行结果测试截图:
实验备注:为了使输出界面使给出的一致,这里在编写代码的时候对测试函数进行了一些类似于格式控制的修改。
标签:const,对象,double,void,编程,int,Complex,实验,include From: https://www.cnblogs.com/Rainbow-forest/p/17780812.html