实验任务3:
1.complex.hpp
1 #pragma once 2 #include<iostream> 3 #include<cmath> 4 class Complex{ public : 5 Complex(double r=0,double i=0){ 6 real=r;imag=i ;} 7 Complex(const Complex &x){ 8 real=x.real; 9 imag=x.imag; 10 } 11 12 13 double get_real() const{ 14 return real; 15 } 16 double get_imag() const{ 17 return imag; 18 } 19 void show() const{ 20 using namespace std; 21 if(real==0&&imag==0){cout<<0;} 22 else if(real==0){cout<<imag<<"i";} 23 else if(imag==0){cout<<real;} 24 else{ 25 if(imag>0){ 26 cout<<real<<"+"<<imag<<"i"; 27 } 28 29 else if(imag<0){ 30 cout<<real<<"-"<<-imag<<"i"; 31 } 32 } 33 } 34 35 36 void add( const Complex &x) {real+=x.real; 37 imag+=x.imag;} 38 friend Complex add(const Complex &c1,const Complex &c2); 39 friend bool is_equal(const Complex &c1,const Complex &c2); 40 friend double abs(const Complex &c1); 41 private: 42 double real; 43 double imag; 44 45 46 }; 47 Complex add(const Complex &c1,const Complex &c2){ 48 double real=c1.real+c2.real; 49 double imag=c1.imag+c2.imag; 50 return Complex(real,imag); 51 } 52 bool is_equal(const Complex &c1,const Complex&c2){ 53 if(c1.real==c2.real&&c1.imag==c2.imag) 54 return true; 55 else return false; 56 } 57 double abs(const Complex &c1){ 58 return sqrt(c1.real*c1.real+c1.imag*c1.imag); 59 }View Code
2.complex.cpp
1 #include"complex.hpp" 2 3 4 void test(){ 5 using namespace std; 6 Complex c1(3,-4); 7 const Complex c2(4.5); 8 Complex c3(c1); 9 cout<<"c1="; 10 c1.show(); 11 cout<<endl; 12 cout<<"c2.imag="<<c2.get_imag()<<endl; 13 cout<<"c3="; 14 c3.show(); 15 cout<<endl; 16 cout<<"abs(c1)="; 17 cout<<abs(c1)<<endl; 18 cout<<boolalpha; 19 cout<<"c1==c3:"<<is_equal(c1,c3)<<endl; 20 cout<<"c1==c2:"<<is_equal(c1,c2)<<endl; 21 Complex c4; 22 c4=add(c1,c2); 23 cout<<"c4=c1+c2="; 24 c4.show(); 25 cout<<endl; 26 } 27 int main(){ 28 test(); 29 }View Code
实验任务4:
1.User.hpp
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 class User { 5 private: 6 static int n; 7 public: 8 static void print_n(); 9 private: 10 std::string name, password, email; 11 public: 12 User(std::string nm, 13 std::string p = "111111", 14 std::string e = ""): name{nm}, password{p}, email{e} 15 { 16 n += 1; 17 }; 18 void set_email(); 19 void change_passwd(); 20 void print_info(); 21 }; 22 int User::n = 0; 23 24 25 26 void User::print_n() { 27 std::cout << "there are " << n << " users.\n"; 28 } 29 30 void User::set_email() { 31 using namespace std; 32 cout << "Enter email address: "; 33 string e; 34 cin >> e; 35 cin.ignore((std::numeric_limits<streamsize>::max)(),'\n'); 36 cout << "email is set successfully...\n"; 37 email = e; 38 } 39 40 void User::change_passwd() { 41 using namespace std; 42 string o, n; 43 for (int i = 0; i <= 3; i++) { 44 if (i == 3) { 45 cout << "Please try after a while. \n"; 46 break; 47 } 48 if (i == 0) 49 cout << "Enter old password: "; 50 else 51 cout << "Please re-enter again: "; 52 cin >> o; 53 cin.ignore((std::numeric_limits<streamsize>::max)(),'\n'); 54 if (o != password) { 55 cout << "password input error. "; 56 continue; 57 } 58 else { 59 cout << "Enter new passwd: "; 60 cin >> n; 61 cin.ignore((std::numeric_limits<streamsize>::max)(),'\n'); 62 password = n; 63 cout << "new passwd is set successfully...\n"; 64 break; 65 } 66 } 67 } 68 69 void User::print_info() { 70 using namespace std; 71 cout << "name: \t" << name << endl; 72 string s(password.size(),'*'); cout << "password: \t" << s << endl; 73 cout << "email: \t" << email << endl; 74 }View Code
2.task4.cpp
1 #include "User.hpp" 2 #include <iostream> 3 4 5 void test(){ 6 using std::cout; 7 using std::endl; 8 9 cout << "testing 1......\n"; 10 User user1("Jonny", "92197", "[email protected]"); 11 user1.print_info(); 12 13 cout << endl 14 << "testing 2......\n\n"; 15 16 User user2("刘亦菲"); 17 user2.change_passwd(); 18 user2.set_email(); 19 user2.print_info(); 20 21 cout << endl; 22 User::print_n(); 23 } 24 25 int main(){ 26 test(); 27 }View Code
实验任务5:
1.account.h
1 //account.h 2 #ifndef __ACCOUNT_H__ 3 #define __ACCOUNT_H__ 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 //记录一笔账,date为日期,amount为金额,desc为说明 13 void record(int date, double amount); 14 //获得到指定日期为止的存款金额按日累积值 15 double accumulate(int date) const { 16 return accumulation + balance * (date - lastDate); 17 } 18 public: 19 //构造函数 20 SavingsAccount(int date, int id, double rate); 21 int getId() const {return id;} 22 double getBalance() const {return balance;} 23 double getRate() const {return rate;} 24 static double getTotal() {return total;} 25 void deposit(int date, double amount); //存入现金 26 void withdraw(int date, double amount); //取出现金 27 //结算利息,每年1月1日调用一次该函数 28 void settle(int date); 29 //显示账户信息 30 void show() const; 31 }; 32 #endif //__ACCOUNT _H__View Code
2.account.cpp
1 //account.cpp 2 #include "account.h" 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6 7 double SavingsAccount::total = 0; 8 //SavingsAccount类相关成员函数的实现 9 SavingsAccount::SavingsAccount(int date, int id, double rate) 10 : id(id), balance(0), rate(rate), lastDate (date), accumulation(0) { 11 cout << date << "\t#" << id << " is created" << endl; 12 } 13 void SavingsAccount::record(int date, double amount) { 14 accumulation = accumulate(date); 15 lastDate = date; 16 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 17 balance += amount; 18 total += amount; 19 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 20 } 21 void SavingsAccount::deposit(int date, double amount) { 22 record(date, amount); 23 } 24 void SavingsAccount::withdraw(int date, double amount) { 25 if ( amount > getBalance()) 26 cout << "Error: not enough money" << endl; 27 else 28 record(date, - amount); 29 } 30 void SavingsAccount::settle(int date) { 31 double interest = accumulate(date) * rate / 365; 32 //计算年息 33 if (interest != 0) 34 record(date, interest); 35 accumulation = 0; 36 } 37 void SavingsAccount::show() const { 38 cout << "#" << id << "\tBalance: " << balance; 39 }View Code
3.5_11.cpp
1 //5_11.cpp 2 #include "account.h" 3 #include <iostream> 4 using namespace std; 5 int main() { 6 //建立几个帐户 7 SavingsAccount sa0(1, 21325302, 0.015); 8 SavingsAccount sa1(1, 58320212, 0.015); 9 //几笔账目 10 sa0.deposit(5, 5000) ; 11 sa1.deposit(25, 10000); 12 sa0.deposit(45, 5500); 13 sa1.withdraw(60, 4000); 14 //开户后第90天到了银行的计息日,结算所有账户的年息 15 sa0.settle(90); 16 sa1.settle(90); 17 //输出各个帐户信息 18 sa0.show();cout << endl; 19 sa1.show();cout << endl; 20 cout << "Total:" << SavingsAccount::getTotal() << endl; 21 return 0; 22 }View Code
标签:std,cout,int,double,void,实验,include From: https://www.cnblogs.com/shaoshuai-nuist/p/17779346.html