//第一个任务简单Complex类
#pragma once #include <iostream> #include <cmath> class Complex { public: Complex(double x0 = 0, double y0 = 0); // 构造函数 Complex(const Complex& c); // 拷贝构造函数 ~Complex(){}; // 析构函数 double get_real() const; // 获取实部 double get_imag() const; // 获取虚部 void show() const; // 显示复数 void add(const Complex &c); // 复数相加 friend Complex add(const Complex &c1, const Complex &c2); // 友元函数,复数相加 friend bool is_equal(const Complex &c1, const Complex &c2); // 友元函数,判断两个复数是否相等 friend void abs(const Complex &c); // 友元函数,计算复数的模 private: double real, imag; // 实部和虚部 }; Complex::Complex(double x0, double y0) : real{x0}, imag{y0} {} // 构造函数的实现 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::show() const { std::cout << real; if (imag > 0) std::cout << "+" << imag << "i"; if (imag < 0) std::cout << imag << "i"; } void Complex::add(const Complex& c) { real += c.real; imag += c.imag; } Complex add(const Complex& c1, const Complex& c2) { Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; } void abs(const Complex& c) { double mo = sqrt(c.real * c.real + c.imag * c.imag); std::cout << mo; } bool is_equal(const Complex& c1, const Complex& c2) { return c1.real == c2.real && c1.imag == c2.imag; } #pragma once 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)="; abs(c1); cout << 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; c1.add(c2); cout << "c1+=c2, c1="; c1.show(); cout << endl; }
#include"Complex.hpp" int main() { test(); return 0; }
//第二个User类简单版本
#pragma once #include<iostream> #include <string> #include <vector> using namespace std; class User { public: // 构造函数,包含密码和电子邮件的默认值 User(const string& name0, const string& passwd0 = "111111", const string& email0 = ""); // 析构函数 ~User(); // 设置用户的电子邮件地址 void set_email(); // 更改用户的密码 void change_passwd(); // 打印用户信息 void print_info() const; // 静态方法,用于打印用户对象的数量 static void print_n(); private: string name, email, passwd; // 用户数据 int c = 3; // 最大密码更改尝试次数设置成私有,防止篡改无限多次 // 验证电子邮件地址有没有@ void isValidEmail(const stri
//Usermain.cpp #include "User1.hpp" #include<iostream> using namespace std; void test() { cout << "测试 1......\n"; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "测试 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
ng& oldEmail0, const string& newEmail0); private: static int n; // 用户对象数量的静态成员变量 }; int User::n = 0; User::User(const string& name0, const string& passwd0, const string& email0) : name(name0), passwd(passwd0), email(email0) { ++n; } User::~User() { --n; } //错误1://默认形参只能声明时候写 void User::isValidEmail(const string& oldEmail0, const string& newEmail0) { string newEmail = newEmail0; string oldEmail = oldEmail0; vector<string> v{oldEmail, newEmail}; for (auto& s : v) { //s.npos 是std::string类中的一个很大的静态成员变量 auto pos = s.find("@"); if (pos == string::npos)// { cout << "Email format is missing @" << endl; } else { email = newEmail; cout << "Email is set successfully" << endl; } } } void User::set_email() { cout << "Enter email address: "; string newEmail; cin >> newEmail; cout << newEmail; isValidEmail(email, newEmail); } void User::change_passwd() { cout << "Name: " << name << endl; string hidden_passwd(passwd.size(), '*'); cout << "Password: " << hidden_passwd << endl; cout << "Email: " << email; cout << "Enter old password: "; string oldPasswd, newPasswd; int i = c; while (i) { cin >> oldPasswd; if (oldPasswd == passwd) { cout << "Enter new password: "; cin >> newPasswd; passwd = newPasswd; cout << "New password is set successfully..." << endl; break; } else { i--; if (i != 0) cout << "Password input error. Please re-enter: "; } if (i == 0) { cout << "Password input error. Please try again later."; exit(1);//强行退出函数 } } } void User::print_info() const { cout << "Name: " << name << endl; string hidden_passwd(passwd.size(), '*'); cout << "Password: " << hidden_passwd << endl; cout << "Email: " << email << endl; } void User::print_n() { cout << "There are " << n << " users." << endl; }
//Usermain.cpp #include "User1.hpp" #include<iostream> using namespace std; void test() { cout << "测试 1......\n"; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "测试 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
//第三个任务课本银行类
//account.h #pragma once 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 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; };
//account.cpp #include"account.h" #include<cmath> #include<iostream> using namespace std; double SavingsAccount::total=0; //SavingAccount 类相关成员函数的实现 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: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<<"\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; }
实验一错误
在编程的时候忘记初始化析构函数,多文件编程的时候造成编译器报错但不知道如何找原因,放在单文件的时候报错信息才发现问题。
实验二收获
一:学习了运用sring类的find函数,查找字符 二:学习了如何隐藏密码 三:运用了exit()函数返回1为异常,返回0为正常
四:运用容器for循环 编程错误:默认形参写了两遍
标签:const,对象,double,void,编程,int,Complex,实验,include From: https://www.cnblogs.com/xiaochengxiatian/p/17781113.html