实验3
实验任务1:
问题1:一共2个类,使用了标准库的string和vector
问题2:剩余成员函数基本不改变对象的值,没有必要加const,可以设置inline
问题3:初始化一个string类型的对象用于分割输出结果
实验任务2:
问题1:第一行定义并初始化一个vector类型存储int类型数据的对象,第二行定义一个vector类型的对象并复制v1的内容,第三行将v1中第一个数据值设置为-999
问题2:第一行定义并初始化一个vector类型存储vector类型数据的对象,第二行定义一个const vector类型的对象并复制v1的内容,第三行将v1中第一个vector类型对象添加一个数据-999
问题3:第一行定义一个vector类型存储int类型数据的对象t1并复制v1的第一个元素,第二行输出t1最后一个元素,第三行定义一个const vector类型存储int类型数据的对象t2并复制v2的第一个元素,第四行输出t2最后一个元素
问题4:深复制,需要
实验任务3
问题1:深复制
问题2:不能,有安全隐患,引用类型的值可能被改变
问题3:不能,assign函数需要返回引用值
实验任务4
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 类Matrix的声明 10 class Matrix { 11 public: 12 Matrix(int n, int m):lines{n},cols{m},ptr{new double[lines*cols]}{ 13 14 15 } 16 17 // 构造函数,构造一个n*m的矩阵, 初始值为value 18 Matrix(int n){ 19 lines=n;cols=n; 20 ptr=new double[lines*cols]; 21 } // 构造函数,构造一个n*n的矩阵, 初始值为value 22 Matrix(const Matrix &x){ 23 lines=x.lines; 24 cols=x.cols; 25 ptr=new double[lines*cols]; 26 for(int i=0;i<lines*cols;i++){ 27 ptr[i]=x.ptr[i]; 28 } 29 30 } 31 // 复制构造函数, 使用已有的矩阵X构造 32 ~Matrix(){ 33 delete[] ptr; 34 } 35 36 void set(const double *pvalue){ 37 int i; 38 for(i=0;i<lines*cols;i++){ 39 ptr[i]=pvalue[i]; 40 } 41 42 } // 用pvalue指向的连续内存块数据按行为矩阵赋值 43 void clear(){ 44 int i; 45 for(i=0;i<lines*cols;i++){ 46 ptr[i]=0; 47 } 48 } 49 // 把矩阵对象的值置0 50 51 const double& at(int i, int j) const{ 52 assert(i>=0&&i<lines&&j>=0&&j<cols); 53 return ptr[i*cols+j]; 54 } // 返回矩阵对象索引(i,j)的元素const引用 55 double& at(int i, int j){ 56 assert(i>=0&&i<lines&&j>=0&&j<cols); 57 return ptr[i*cols+j]; 58 } // 返回矩阵对象索引(i,j)的元素引用 59 60 int get_lines() const{ 61 return lines; 62 } // 返回矩阵对象行数 63 int get_cols() const{ 64 return cols; 65 } // 返回矩阵对象列数 66 67 void display() const{ 68 int i,j; 69 70 for(i=0;i<lines;i++){ 71 cout<<ptr[i+cols]; 72 for(j=1;j<cols;j++){ 73 cout<<","<<ptr[i*cols+j]; 74 } 75 cout<<endl; 76 } 77 cout<<endl; 78 } // 按行显示矩阵对象元素值 79 80 private: 81 int lines; // 矩阵对象内元素行数 82 int cols; // 矩阵对象内元素列数 83 double *ptr; 84 };
实验任务5
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 class User{ 6 private: 7 string name; 8 string password; 9 string email; 10 11 public: 12 User(string a,string b="123456",string c=""){ 13 name=a; 14 password=b; 15 email=c; 16 } 17 void set_email(){ 18 string x; 19 string::size_type idx; 20 string y="@"; 21 cout<<"Enter email address:"; 22 while(1){ 23 cin>>x; 24 idx=x.find(y); 25 if(idx==string::npos) 26 cout<<"illegal email.Please re-enter email:"; 27 else{ 28 cout<<"email is set successfully..."<<endl; 29 email=x; 30 break; 31 } 32 33 } 34 } 35 void change_password(){ 36 string x; 37 string y; 38 int i=0; 39 cout<<"Enter old password:"; 40 while(i<3){ 41 cin>>x; 42 if(x==password){ 43 cout<<"Enter new password:"; 44 cin>>y; 45 password=y; 46 cout<<"new password is set successfully..."<<endl; 47 break; 48 } 49 else{ 50 i++; 51 if(i==3){ 52 cout<<"password input error.Please try after a while."; 53 break; 54 } 55 cout<<"password input error.Please re-enter again:"; 56 57 } 58 } 59 60 61 } 62 void display(){ 63 int i; 64 cout<<"name: "<<name<<endl<<"pass: "; 65 for(i=0;i<password.size();i++){ 66 cout<<"*"; 67 } 68 cout<<endl<<"email: "<<email<<endl; 69 } 70 71 };
实验任务6
25.cpp:
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 Date date(2008, 11, 1); 6 SavingsAccount accounts[] = { 7 SavingsAccount(date,"03755217",0.015), 8 SavingsAccount(date,"02342342",0.015) 9 10 }; 11 const int n = sizeof(accounts) / sizeof(SavingsAccount); 12 accounts[0].deposit(Date(2008, 11, 5), 5000, "salary"); 13 accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 14 accounts[0].deposit(Date(2008, 12, 5), 5500, "salary"); 15 accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop"); 16 cout << endl; 17 for (int i = 0; i < n; i++) { 18 accounts[i].settle(Date(2009, 1, 1)); 19 accounts[i].show(); 20 cout << endl; 21 } 22 cout << "Total:" << SavingsAccount::getTotal() << endl; 23 return 0; 24 }
account.cpp:
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double SavingsAccount::total = 0; 6 SavingsAccount::SavingsAccount(const Date &date, const string& id, double rate) 7 :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 8 date.show(); 9 cout << "\t#" << id << "created" << endl; 10 11 } 12 void SavingsAccount::record(const Date& date, double amount, const string& desc) { 13 accumulation = accumulate(date); 14 lastDate = date; 15 amount = floor(amount * 100 + 0.5) / 100; 16 balance -= amount; 17 total += amount; 18 date.show(); 19 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 20 21 } 22 void SavingsAccount::error(const string& msg)const { 23 cout << "Error(#" << id << "):" << msg << endl; 24 } 25 void SavingsAccount::deposit(const Date &date, double amount, const string& desc){ 26 record(date,amount,desc); 27 } 28 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc){ 29 if (amount > getBalance()) 30 error("not enough money"); 31 else 32 record(date,-amount,desc); 33 34 } 35 void SavingsAccount::settle(const Date& date) { 36 double interest = accumulate(date) * rate 37 / date.distance(Date(date.getYear() - 1, 1, 1)); 38 if (interest != 0) 39 record(date, interest, "interest"); 40 accumulation=0; 41 42 } 43 void SavingsAccount::show()const { 44 cout << id << "\tBalance:" << balance; 45 }
accounnt.h:
1 #ifndef _ _ACCOUNT_H_ _ 2 #define _ _ACCOUNT_H_ _ 3 #include"date.h" 4 #include<string> 5 class SavingsAccount { 6 private: 7 std::string id; 8 double balance; 9 double rate; 10 Date lastDate; 11 double accumulation; 12 static double total; 13 void record(const Date& date, double amount, const std::string& desc); 14 void error(const std::string& msg) const; 15 double accumulate(const Date& date)const { 16 return accumulation + balance * date.distance(lastDate); 17 18 } 19 public: 20 SavingsAccount(const Date& date, const std::string& id, double rate); 21 const std::string& 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(const Date& date, double amount, const std::string& desc); 26 void withdraw(const Date& date, double amount, const std::string& desc); 27 void settle(const Date& date); 28 void show()const; 29 }; 30 #endif //_ _ACCOUNT_H_ _
date.cpp:
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; 7 } 8 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 9 if (day <= 0 || day > getMaxDay()) { 10 cout << "Invalid date: "; 11 show(); 12 cout << endl; 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2)totalDays++; 18 } 19 int Date::getMaxDay()const { 20 if (isLeapYear() && month == 2) 21 return 29; 22 else 23 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 24 } 25 void Date::show()const { 26 cout << getYear() << "-" << getMonth() << "-" << getDay(); 27 }
date.h:
1 class Date { 2 private: 3 int year; 4 int month; 5 int day; 6 int totalDays; 7 public: 8 Date(int year, int month, int day); 9 int getYear() const { return year; } 10 int getMonth()const { return month; } 11 int getDay()const { return day;} 12 int getMaxDay()const; 13 bool isLeapYear()const { 14 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;} 15 void show() const; 16 int distance(const Date & date )const{ 17 return totalDays - date.totalDays; 18 } 19 };