task.3
.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 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 add( const Complex &x) {real+=x.real; imag+=x.imag;} 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); }View Code
.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.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; } int main(){ test(); }View Code
task.4
.hpp
#pragma once #include <iostream> #include <string> using namespace std; class User{ public: User(std::string name, std::string passwd = "111111", std::string email = " "):name{name}, passwd{passwd}, email{email} {n++;} void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name; string passwd; string email; static int n; }; int User::n = 0; void User::set_email(){ cout << "Enter email address: " ; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { string p,a; cout << "Enter old password: " ; cin >> p; int i = 0; while (p != passwd){ i ++; cout << "password input error. Please re-enter again: "; cin >> p; if (i>=2){ cout << "passwword input error. Please try after a while" << endl; break; } } if (p == passwd){ cout << " Enter new passwd: "; cin >> a; cout << "new passwd is set successfully..."; } } void User::print_info(){ string s(passwd.size(), '*'); cout << "name: " << name << endl; cout << "passwd: " << s << endl; cout << "email: " << email << endl; } void User::print_n(){ cout << "there are " << n << "users" << endl; }View Code
.cpp
#include "User.hpp" #include <iostream> //测试IUser类 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.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main(){ test(); }View Code
task.5
.hpp
#ifndef ACCOUNT_H #define ACCOUNT_H class SavingsAccount { //储蓄账户类 private: int id; //账号 double balance; //余额 double rate; //存款的年利率 int lastDate; //上次变更余额的时期 double accumulation; //余额按日累加之和 static double total; //所有账户的总金额 //记录一笔账,date为日期,amount为金额,desc为说明 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 getBlance() 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); //取出现金 //结算利息,每年1月1日调用一次该函数 void settle(int date); //显示账户信息 void show() const; }; #endif //_ _ACCOUNT_H_ _View Code
.cpp
#include "account.h" #include <cmath> #include <iostream> using namespace std; double SavingsAccount::total = 0; //SavingsAccount类相关成员函数的实现 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 > getBlance()) 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 << "\tBlance:" << balance; }View Code
.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); //开户后第90天到了银行的计息日,结算所有账户的年息 sa0.settle(90); sa1.settle(90); //输出各个账户信息 sa0.show(); cout << endl; sa1.show(); cout << endl; cout << "Total: " << SavingsAccount::getTotal() << endl; return 0; }View Code
标签:const,cout,int,double,void,实验,include From: https://www.cnblogs.com/qazmlp10/p/17780986.html