任务3
1.代码
complex.hpp:
1 #ifndef complex_H 2 #define complex_H 3 4 #include <iostream> 5 #include <cmath> 6 using namespace std; 7 8 class Complex 9 { 10 public: 11 Complex(double x=0,double y=0):real{x},imag{y}{} 12 Complex(const Complex &t):real{t.real},imag{t.imag}{} 13 double get_real()const 14 { 15 return real; 16 } 17 double get_imag()const 18 { 19 return imag; 20 } 21 void show()const 22 { 23 if(imag<0) 24 std::cout<<real<<imag<<"i"<<std::endl; 25 if(imag>0) 26 std::cout<<real<<"+"<<imag<<"i"<<std::endl; 27 if(imag==0) 28 std::cout<<real<<std::endl; 29 } 30 double add(const Complex &a) 31 { 32 real+=a.real; 33 imag+=a.imag; 34 } 35 36 friend Complex add(const Complex &p1, const Complex &p2); 37 friend bool is_equal(const Complex &p1, const Complex &p2); 38 friend double abs(const Complex &p1); 39 private: 40 double real,imag; 41 }; 42 Complex add(const Complex &p1, const Complex &p2) 43 { 44 Complex x; 45 x.real=p1.real + p2.real; 46 x.imag=p1.imag + p2.imag; 47 return (x); 48 } 49 bool is_equal(const Complex &p1, const Complex &p2) 50 { 51 if((p1.real==p2.real)&&(p1.imag == p2.imag)) 52 { 53 return true; 54 } 55 else 56 return false; 57 } 58 double abs(const Complex &p1) 59 { 60 double mo=0; 61 mo=sqrt(p1.real*p1.real+p1.imag*p1.imag); 62 return mo; 63 } 64 void test() { 65 using namespace std; 66 Complex c1(3, -4); 67 const Complex c2(4.5); 68 Complex c3(c1); 69 cout << "c1 = "; 70 c1.show(); 71 cout << endl; 72 cout << "c2 = "; 73 c2.show(); 74 cout << endl; 75 cout << "c2.imag = " << c2.get_imag() << endl; 76 cout << "c3 = "; 77 c3.show(); 78 cout << endl; 79 cout << "abs(c1) = "; 80 cout << abs(c1) << endl; 81 cout << boolalpha; 82 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 83 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 84 Complex c4; 85 c4 = add(c1, c2); 86 cout << "c4 = c1 + c2 = "; 87 c4.show(); 88 cout << endl; 89 c1.add(c2); 90 cout << "c1 += c2, " << "c1 = "; 91 c1.show(); 92 cout << endl; 93 } 94 95 #endifView Code
main.cpp:
1 #include"complex.hpp" 2 int main() { 3 test(); 4 }View Code
2.图片:
任务4:
1.代码:
user.hpp:
1 #ifndef user_H 2 #define user_H 3 4 #include<iostream> 5 #include<string> 6 #include <vector> 7 8 using namespace std; 9 class User 10 { 11 private: 12 string name; 13 string password; 14 string email; 15 private: 16 static int n; 17 public: 18 static void print_n(); 19 public: 20 User(string x,string y="111111",string z=" "):name{x},password{y},email{z} 21 { 22 ++n; 23 } 24 ~User(){--n; 25 } 26 void set_email() 27 { 28 string a; 29 cout<<"Enter email address:"; 30 cin>>a; 31 email=a; 32 cout<<"email is set successfully..."<<endl; 33 } 34 void change_passwd() 35 { 36 int count=0; 37 string b,c; 38 cout<<"Enter old passowrd:"; 39 cin>>b; 40 while(b!=password) 41 { 42 count++; 43 cout<<"passowrd input error. please re-enter again:"; 44 cin>>b; 45 if(count>=2) 46 { 47 cout<<"passowrd input error. please try after a while:"<<endl; 48 break; 49 } 50 } 51 if(b==password) 52 { 53 cout<<"Enter new passowrd:"; 54 cin>>c; 55 password=c; 56 cout<<"new password is set successfully..."<<endl; 57 58 } 59 } 60 void print_info() 61 { 62 string s2(password.size(), '*'); 63 cout<<"name: "<<name<<endl; 64 cout<<"password: "<<s2<<endl; 65 cout<<"email: "<<email<<endl; 66 } 67 68 }; 69 70 int User::n=0; 71 void User::print_n() 72 { 73 std::cout<<"there are"<< n <<"users"<<std::endl; 74 }; 75 76 77 #endifView Code
main.cpp:
1 #include "User.hpp" 2 #include <iostream> 3 // 测试User类 4 void test() { 5 using std::cout; 6 using std::endl; 7 cout << "testing 1......\n"; 8 User user1("Jonny", "92197", "[email protected]"); 9 user1.print_info(); 10 cout << endl<< "testing 2......\n\n"; 11 User user2("Leonard");user2.print_info(); 12 user2.change_passwd(); 13 user2.set_email(); 14 user2.print_info(); 15 cout<<endl; 16 User::print_n(); 17 } 18 int main() { 19 test(); 20 }View Code
2.图片:
任务5:
1.代码:
1.account.h
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 4 class SavingsAccount { 5 private: 6 int id; 7 double balance; 8 double rate; 9 int lastDate; 10 double accumulation; 11 static double total; 12 void record(int date, double amount); 13 double accumulate(int date) const { 14 return accumulation+balance*(date-lastDate); 15 } 16 public: 17 SavingsAccount(int date, int id, double rate); 18 int getId() const {return id;} 19 double getBalance() const {return balance;} 20 double getRate() const {return rate;} 21 static double getTotal() {return total;} 22 void deposit(int date, double amount); 23 void withdraw(int date, double amount); 24 void settle(int date); 25 void show() const; 26 }; 27 28 #endifView Code
2.account.cpp
1 #include<cmath> 2 #include<iostream> 3 using namespace std; 4 5 double SavingsAccount::total = 0; 6 SavingsAccount::SavingsAccount(int date, int id, double rate) 7 :id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 8 cout << date << "\t#" << id << " is created" << endl; 9 } 10 void SavingsAccount::record(int date, double amount) { 11 accumulation=accumulate(date); 12 lastDate = date; 13 amount = floor(amount * 100 + 0.5) / 100; 14 balance += amount; 15 total += amount; 16 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 17 } 18 void SavingsAccount::deposit(int date, double amount) { 19 record(date, amount); 20 } 21 void SavingsAccount::withdraw(int date, double amount) { 22 if ( amount > getBalance()) 23 cout << "Error: not enough money" << endl; 24 else 25 record(date, - amount); 26 } 27 void SavingsAccount::settle(int date) { 28 double interest = accumulate(date) * rate / 365; 29 if (interest != 0) 30 record(date, interest); 31 accumulation = 0; 32 } 33 void SavingsAccount::show() const { 34 cout << "#" << id << "\tBalance: " << balance; 35 }View Code
3.main.cpp
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main() { 5 SavingsAccount sa0(1, 21325302, 0.015); 6 SavingsAccount sa1(1, 58320212, 0.015); 7 sa0.deposit(5, 5000) ; 8 sa1.deposit(25, 10000); 9 sa0.deposit(45, 5500); 10 sa1.withdraw(60, 4000); 11 sa0.settle(90); 12 sa1.settle(90); 13 sa0.show();cout << endl; 14 sa1.show();cout << endl; 15 cout << "Total:" << SavingsAccount::getTotal() << endl; 16 return 0; 17 }View Code
2.图片:
标签:const,cout,对象,double,void,编程,试验,int,include From: https://www.cnblogs.com/yili123/p/17770803.html