实验任务3
complex.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, new_passwd; 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 << "password input error. Please try after a while" << endl; break; } } if (p == passwd){ cout << "Enter new password: "; cin >> new_passwd; cout << "new password is set successfully..." << endl; passwd = new_passwd; } else { cout << "The entered old password is incorrect." << endl; } } void User::print_info(){ cout << "name: " << name << endl; cout << "passwd: " << passwd << endl; // Print password without masking. For example, if the password is 12345, it will be displayed as 12345. cout << "email: " << email << endl; } void User::print_n(){ cout << "there are " << n << "users" << endl; }View Code
main.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
实验任务4
uesr.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 << "password input error. Please try after a while" << endl; break; } } if (p == passwd){ cout << "Enter new passwd: "; cin >> a; passwd = a; // Add this line to update the password. cout << "new passwd is set successfully..." << endl; } else { cout << "The entered old password is incorrect." << endl; } } void User::print_info(){ string s(passwd.size(), '*'); // This line is not necessary. You can remove it. // It seems like you are trying to mask the password with '*'. However, the variable 's' is not used anywhere. // You should directly use 'passwd' instead of 's'. cout << "name: " << name << endl; cout << "passwd: " << passwd << endl; // Use 'passwd' instead of 's'. cout << "email: " << email << endl; } void User::print_n(){ cout << "there are " << n << "users" << endl; }View Code
task4.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
实验任务5
account.hpp
class SavingsAccount { public: // ... 其他函数 ... // 构造函数 SavingsAccount(int date, int id, double rate) { this->id = id; this->balance = 0.0; this->rate = rate; this->lastDate = date; this->accumulation = 0.0; } // 记录一笔账 void record(int date, double amount) { lastDate = date; balance += amount; accumulation += amount; total += amount; // 更新total here } // 存入现金 void deposit(int date, double amount) { if (amount > 0) { record(date, amount); std::cout << "Deposited: " << amount << " on " << date << std::endl; total += amount; // 更新total here } } // 取出现金 void withdraw(int date, double amount) { if (amount > 0 && amount <= balance) { record(date, -amount); std::cout << "Withdrew: " << amount << " on " << date << std::endl; total -= amount; // 更新total here } else { std::cout << "Insufficient funds!" << std::endl; } } // 结算利息 void settle(int date) { double interest = balance * rate / 365 * (date - lastDate); // assuming simple interest here balance += interest; accumulation += interest; total += interest; // 更新total here lastDate = date; // update the lastDate to current date after settlement } // 显示账户信息 void show() const { std::cout << "Account ID: " << id << std::endl; std::cout << "Balance: " << balance << std::endl; std::cout << "Rate: " << rate << std::endl; std::cout << "Last Date: " << lastDate << std::endl; std::cout << "Accumulation: " << accumulation << std::endl; std::cout << "Total: " << total << std::endl; // Add total to the output here } private: double total; // Declare total here // ... 其他变量 ... };View Code
account.cpp
#include "account.h" #include <cmath> #include <iostream> using namespace std; double SavingsAccount::total = 0; 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
标签:std,cout,对象,void,passwd,int,实验,email From: https://www.cnblogs.com/grcvafg/p/17781128.html