首页 > 其他分享 >实验二

实验二

时间:2023-10-22 19:45:18浏览次数:28  
标签:std cout int double void 实验 include

试验任务三
complex.hpp

点击查看代码
#pragma once
#include<iostream>
#include<cmath>
class Complex{ public :
    Complex(double r=0,double i=0){
        real=r;imag=i ;}
        Complex(const Complex &x){
            real=x.real;
            imag=x.imag;
        }


    double get_real() const{
        return real;
    }
    double  get_imag() const{
        return imag;
    }
    void show() const{
        using namespace std;
        if(real==0&&imag==0){cout<<0;}
        else if(real==0){cout<<imag<<"i";}
        else if(imag==0){cout<<real;}
        else{
            if(imag>0){
                cout<<real<<"+"<<imag<<"i";
            }

        else if(imag<0){
            cout<<real<<"-"<<-imag<<"i";
        }
        }
}


    void add( const Complex &x) {real+=x.real;
    imag+=x.imag;}
     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;


};
Complex add(const Complex &c1,const Complex &c2){
    double real=c1.real+c2.real;
    double imag=c1.imag+c2.imag;
    return Complex(real,imag);
}
bool is_equal(const Complex &c1,const Complex&c2){
    if(c1.real==c2.real&&c1.imag==c2.imag)
    return true;
    else return false;
}
double abs(const Complex &c1){
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

complex.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();
}

试验任务四
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;
}

task4.cpp

点击查看代码
1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 
 5 void test(){
 6       using std::cout;
 7       using std::endl;
 8 
 9       cout << "testing 1......\n";
10       User user1("Jonny", "92197", "[email protected]");
11       user1.print_info();
12 
13       cout << endl
14            << "testing 2......\n\n";
15 
16       User user2("Leonard");
17       user2.change_password();
18       user2.set_email();
19       user2.print_info();
20 
21       cout << endl;
22       User::print_n();
23  }
24 
25  int main(){
26   test();
27  }

试验任务五
account.h

点击查看代码
1 //account.h
 2 #ifndef __ACCOUNT_H__
 3 #define __ACCOUNT_H__
 4 class SavingsAccount {            //储蓄账户类
 5     private:
 6         int id;                    //帐号
 7         double balance;            //余额
 8         double rate;            //存款的年利率
 9         int lastDate;            //上次变更余额的时期
10         double accumulation;    //余额按日累加之和
11         static double total;    //所有帐户的总金额
12         //记录一笔账,date为日期,amount为金额,desc为说明
13         void record(int date, double amount);
14         //获得到指定日期为止的存款金额按日累积值
15         double accumulate(int date) const {
16             return accumulation + balance * (date - lastDate);
17         }
18     public:
19         //构造函数
20         SavingsAccount(int date, int id, double rate);
21         int 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(int date, double amount);        //存入现金
26         void withdraw(int date, double amount);        //取出现金
27         //结算利息,每年1月1日调用一次该函数
28         void settle(int date);
29         //显示账户信息
30         void show() const;
31 };
32 #endif //__ACCOUNT _H__

account.cpp

点击查看代码
1 //account.cpp
 2 #include "account.h"
 3 #include <cmath>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 double SavingsAccount::total = 0;
 8 //SavingsAccount类相关成员函数的实现
 9 SavingsAccount::SavingsAccount(int date, int id, double rate)
10     : id(id), balance(0), rate(rate), lastDate (date), accumulation(0) {
11     cout << date << "\t#" << id << " is created" << endl;
12 }
13 void SavingsAccount::record(int date, double amount) {
14     accumulation = accumulate(date);
15     lastDate = date;
16     amount = floor(amount * 100 + 0.5) / 100;        //保留小数点后两位
17     balance += amount;
18     total += amount;
19     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
20 }
21 void SavingsAccount::deposit(int date, double amount) {
22     record(date, amount);
23 }
24 void SavingsAccount::withdraw(int date, double amount) {
25     if ( amount > getBalance())
26         cout << "Error: not enough money" << endl;
27     else
28         record(date, - amount);
29 }
30 void SavingsAccount::settle(int date) {
31     double interest = accumulate(date) * rate / 365;
32 //计算年息
33     if (interest != 0)
34         record(date, interest);
35     accumulation = 0;
36 }
37 void SavingsAccount::show() const {
38     cout << "#" << id << "\tBalance: " << balance;
39 }

5_11.cpp

点击查看代码
1 //5_11.cpp
 2 #include "account.h"
 3 #include <iostream>
 4 using namespace std;
 5 int main() {
 6     //建立几个帐户
 7     SavingsAccount sa0(1, 21325302, 0.015);
 8     SavingsAccount sa1(1, 58320212, 0.015);
 9     //几笔账目
10     sa0.deposit(5, 5000) ;
11     sa1.deposit(25, 10000);
12     sa0.deposit(45, 5500);
13     sa1.withdraw(60, 4000);
14     //开户后第90天到了银行的计息日,结算所有账户的年息
15     sa0.settle(90);
16     sa1.settle(90);
17     //输出各个帐户信息
18     sa0.show();cout << endl;
19     sa1.show();cout << endl;
20     cout << "Total:" << SavingsAccount::getTotal() << endl;
21     return 0;
22 }

标签:std,cout,int,double,void,实验,include
From: https://www.cnblogs.com/liujiandong66da/p/17780919.html

相关文章

  • 实验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}......
  • 实验2 类与对象
    实验一方式一t.h#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();voidset_m1(intx);intget_m1()const;intge......
  • 实验2 c语言分支与循环基础应用编程
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));//以当前系统时间作为随机种子14for(i=0;i<N;......
  • 实验2_C语言分枝与循环基础应用编程
    试验任务1task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=r......
  • 实验二
    实验1代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));//以当前系统时间作为随机种子1415......
  • 实验二 类和对象
    task1.cpp方式一t.h#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;classT{public:T(intx=0,inty=0);T(constT&t);T(T&&t);~T();voidset_m1(intx);intget_m1()const;......
  • 实验二 类和对象
    实验任务1task1.cpp源码t.h:1#ifndefT_H2#defineT_H34#include<iostream>5#include<string>67usingnamespacestd;89//类T的声明10classT{11public:12T(intx=0,inty=0);//带有默认形值的构造函数13T(constT&t)......
  • 实验2
     ......
  • 实验二
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2......