首页 > 其他分享 >实验2

实验2

时间:2023-10-22 23:24:46浏览次数:37  
标签:real const int double Complex 实验 include

3.
c.hpp
#include <iostream> #include <cmath> class Complex { public: Complex(double r = 0, double i = 0); Complex(const Complex& c); double get_real() const; double get_imag() const; void show() const; void add(const Complex& c1); private: double real, imag; public: friend Complex add(const Complex& c1, const Complex& c2) { return (c1.get_real() + c2.get_real(), c1.get_real() + c2.get_real()); } friend bool is_equal(const Complex& c1, const Complex& c2) { if (c1.real == c2.real && c1.imag == c2.imag) return 1; else return 0; } friend double abs(const Complex& c1) { return sqrt(c1.real * c1.real + c1.imag * c1.imag); } }; Complex::Complex(double r , double i ) { real = r; imag = i; } 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 << "+" << (imag) << "i"; } void Complex::add(const Complex& c1) { real = real + (c1.real); imag = imag + (c1.imag); }
#include"c.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();
    system("pause");

}

4.User.hpp

#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class User
{public:
    User(string name = "Mary", string passwd = "111111", string email = "NULL");
    string set_email();
    void  change_passwd();
    void print_info();
    void  static print_n();
    
private:
    string name, passwd, email;
private:
    static int count;

};
int User::count = 0;
User::User(string n , string p , string e ) {
    name = n;
    passwd = p;
    email = e;
    count++;
}
string User::set_email(){
    return email;
        
}
void User::change_passwd() {
    cout << "Enter Old password" << endl;
    int i = 0;string s3;
    std::cin >> s3;
    for (i = 0;i <=2;i++)
    {
        if ( s3!= passwd)
        {
            cout << "password input error.Please re-enter again" << endl;
        }
        else if (i == 2)
        {
            cout << "password input error.Please try after a while" << endl;
        }
            i++;
    }
    
}
void User::print_info() {
    string s1 = User::passwd;
    string s2(s1.size(), '*');
    cout << name << "`" << s2 << "`" << email << endl;
}
void User::print_n() {
    cout << "there are"<<count <<"users" << endl;
}

User.cpp

#include "User.hpp"
#include <iostream>
// 测试User类
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();}

5.account.h

 #ifndef  ACCOUNT_H
 #include "account.h"
  #include <cmath>
  #include <iostream>
 using namespace std;
  double SavingsAccount::total = 0;
  //SavingsAccount类相关成员函数的实现
 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;
 }

 

#define  ACCOUNT_H
 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 getBlance() 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);  //取出现金
     //结算利息,每年1月1日调用一次该函数
     void settle(int date);
     //显示账户信息
     void show() const; 
 };
 #endif //_ _ACCOUNT_H_ _

account.cpp

 #include "account.h"
  #include <cmath>
  #include <iostream>
  using namespace std;
   double SavingsAccount::total = 0;
  //SavingsAccount类相关成员函数的实现
  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;
 }

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);
     //开户后第90天到了银行的计息日,结算所有账户的年息
     sa0.settle(90);
     sa1.settle(90);
     //输出各个账户信息
     sa0.show(); cout << endl;
     sa1.show(); cout << endl; 
     cout << "Total: " << SavingsAccount::getTotal() << endl;
     return 0; }

 

 

 

 









标签:real,const,int,double,Complex,实验,include
From: https://www.cnblogs.com/32re/p/17781361.html

相关文章

  • 实验二 类和对象
    实验任务三1#include<iostream>2#include<cmath>34classComplex{56private:7doublereal;8doubleimag;910public:11Complex(doubler=0,doublei=0){12real=r;13imag=i;14}15......
  • 实验二
    任务三:complex.hpp代码:#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;};Complex(constComplex&c){real=c.real;imag=c.imag;};~Complex(){};doubleget_real()const{returnreal......
  • 实验2 类和对象
    实验任务3t.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex();Complex(doublereal0);Complex(doublereal0,doubleimag0);Complex(constComplex&x);doub......
  • 实验二 类与对象
    实验任务3complex.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classUser{public:User(std::stringname,std::stringpasswd="111111",std::stringemail=""):name{name},p......
  • 实验2 类和对象_基础编程2
    //第一个任务简单Complex类#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doublex0=0,doubley0=0);//构造函数Complex(constComplex&c);//拷贝构造函数~Complex(){};//析构函数doubleget_real()con......
  • 北京电子科技学院(BESTI)实验报告
    实验报告课程:计算机基础与程序设计班级:2314姓名:高伟光学号:20231404成绩:指导教师:娄佳鹏实验日期:2022.9.26实验密级:预习程度:部分预习实验时间:34节仪器组次:无必修/选修:必修实验序号:实验一实验名称:Li......
  • 实验二
    task.3.hpp#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real;imag=x.imag;......
  • 实验二
    试验任务三complex.hpp点击查看代码#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real;imag=x.i......
  • 实验2 类和对象_基础编程2
    1.实验任务3程序代码组织如下:•Complex.h类Complex的声明#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0);Complex(constComplex&c);~Complex(){};doubleget_real()cons......
  • 实验2 类与对象
     3.实验任务31#pragmaonce23#include<iostream>4#include<cmath>56classComplex{7public:8Complex(doubler=0,doublei=0):real(r),imag(i){}910doubleget_real()const{11returnreal;12}......