任务三:
complex.hpp代码:
# include<iostream>
# include<cmath>
class Complex{
public:
Complex(double r = 0, double i = 0){
real = r;
imag = i;
};
Complex(const Complex &c){
real = c.real;
imag = c.imag;
};
~Complex(){};
double get_real() const {return real;};
double get_imag() const {return imag;};
void add(const Complex &c);
void show() const;
friend Complex add(const Complex &c1 , const Complex &c2);
friend bool is_equal(const Complex &c1 , const Complex &c2);
friend double abs(const Complex &c1);
private:
double real;
double imag;
};
void Complex::add(const Complex &c){
real += c.real;
imag += c.imag;
}
void Complex::show() const {
if (real == 0 && imag == 0) { std::cout << 0; }
else if (real == 0) { std::cout << imag << "i"; }
if(imag>0)
{std::cout << real << " + " << imag << "i"; }
else if(imag<0)
{std::cout << real << "-"<< -imag << "i"; }
else if(imag ==0)
{std::cout << real ; }
}
Complex add(const Complex &c1 , const Complex &c2){
double resultreal = c1.real + c2.real;
double resultimag = c1.imag + c2.imag;
return Complex (resultreal,resultimag);
}
bool is_equal(const Complex &c1 , const Complex &c2){
return (c1.real == c2.real)&&(c1.imag == c2.imag);
}
double abs(const Complex &c){
double x = sqrt(c.real*c.real + c.imag*c.imag);
return x;
}
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 = ";
c2.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;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}
int main(){
test();
}
实验结果:
任务4:
User.hpp代码
#include <iostream>
#include <string>
#include <limits>
class User {
private:
static int n;
public:
static void print_n();
private:
std::string name, password, email;
public:
User(std::string nm,
std::string p = "111111",
std::string e = ""): name{nm}, password{p}, email{e}
{
n += 1;
};
void set_email();
void change_passwd();
void print_info();
};
int User::n = 0;
void User::print_n() {
std::cout << "there are " << n << " users.\n";
}
void User::set_email() {
using namespace std;
cout << "Enter email address: ";
string e;
cin >> e;
cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
cout << "email is set successfully...\n";
email = e;
}
void User::change_passwd() {
using namespace std;
string o, n;
for (int i = 0; i <= 3; i++) {
if (i == 3) {
cout << "Please try after a while. \n";
break;
}
if (i == 0)
cout << "Enter old password: ";
else
cout << "Please re-enter again: ";
cin >> o;
cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
if (o != password) {
cout << "password input error. ";
continue;
}
else {
cout << "Enter new passwd: ";
cin >> n;
cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
password = n;
cout << "new passwd is set successfully...\n";
break;
}
}
}
void User::print_info() {
using namespace std;
cout << "name: \t" << name << endl;
string s(password.size(),'*'); cout << "password: \t" << s << endl;
cout << "email: \t" << email << endl;
}
void User::print_n(){
cout << "there are " << n << "users" << endl;
}
main.cpp代码
#include "User.hpp"
#include <iostream>
void test() {
using std::cout;
using std::endl;
cout << "testing 1......\n";
User user1("Jonny", "92197", "xyz@hotmail.com");
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();
}
实验结果:
任务5:
account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount {
private:
int id;
double balance;
double rate;
int lastDate;
double accumulation;
static double total;
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;
};
#endif
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 > 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;
}
test.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;
}
实验结果:
实验总结:通过这次实验我了解了如何运用多文件组织代码并使其正确的运行实现所要的作用。
标签:const,cout,int,double,void,Complex,实验 From: https://www.cnblogs.com/lwjddd/p/17781211.html