首页 > 其他分享 >实验二 类和对象

实验二 类和对象

时间:2023-10-22 23:23:58浏览次数:40  
标签:real cout 对象 double void Complex 实验 include

实验任务三

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 class Complex {
 5 
 6 private:
 7     double real;
 8     double imag;
 9 
10 public:
11     Complex(double r = 0, double i = 0) {
12         real = r;
13         imag = i;
14     }
15 
16     Complex(const Complex& x) {
17         real = x.real;
18         imag = x.imag;
19     }
20 
21     double get_real() const { return real; }
22     double get_imag() const { return imag; }
23 
24     void add(const Complex& x) {
25         real += x.real;
26         imag += x.imag;
27     }
28 
29     void show() const {
30         using namespace std;
31         if (real == 0 && imag == 0) { cout << 0; }
32         else if (real == 0) { cout <<"0+"<< imag << "i"; }
33         else if (imag == 0) { cout << real; }
34         else {
35             if (imag > 0) {
36                 cout << real << " + " << imag << "i";
37             }
38             else if (imag < 0) {
39                 cout << real << " - " << -imag << "i";
40             }
41         }
42     }
43 
44     friend Complex add(const Complex& c1, const Complex& c2);
45     friend bool is_equal(const Complex& c1, const Complex& c2);
46     friend double abs(const Complex& c1);
47 };
48 
49 Complex add(const Complex& c1, const Complex& c2) {
50     double real = c1.real + c2.real;
51     double imag = c1.imag + c2.imag;
52     return Complex(real, imag);
53 }
54 double abs(const Complex& c1) {
55     return sqrt(c1.real * c1.real + c1.imag * c1.imag);
56 }
57 
58 bool is_equal(const Complex& c1, const Complex& c2) {
59     if (c1.real == c2.real && c1.imag == c2.imag)
60         return true;
61     else
62         return false;
63 }
complex.hpp
 1 #include "Complex.hpp"
 2 void test() {
 3     using namespace std;
 4 
 5     Complex c1(3, -4);
 6     const Complex c2(4.5);
 7     Complex c3(c1);
 8 
 9     cout << "c1 = ";
10     c1.show();
11     cout << endl;
12 
13     cout << "c2 = ";
14     c2.show();
15     cout << endl;
16     cout << "c2.imag = " << c2.get_imag() << endl;
17 
18     cout << "c3 = ";
19     c3.show();
20     cout << endl;
21 
22     cout << "abs(c1) = ";
23     cout << abs(c1) << endl;
24 
25     cout << boolalpha;
26     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
27     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
28 
29     Complex c4;
30     c4 = add(c1, c2);
31     cout << "c4 = c1 + c2 = ";
32     c4.show();
33     cout << endl;
34 
35     c1.add(c2);
36     cout << "c1 += c2, " << "c1 = ";
37     c1.show();
38     cout << endl;
39 }
40 
41 int main() {
42     test();
43 }
main.cpp

 实验任务四

 1 #pragma once  
 2 #include<iostream>  
 3 #include<string>  
 4 using namespace std;  
 5   
 6 class User {  
 7 public:  
 8     User(string name, string passwd = "111111", string email = " ") : UserName{ name }, Password{ passwd }, Email{ email } { NumUsers++; }  
 9     ~User() = default;  
10   
11     void set_email();  
12     void change_passwd();  
13     void print_info();  
14     static void print_n();  
15   
16 private:  
17     string UserName, Password, Email;  
18     static int NumUsers;  
19 };  
20   
21 int User::NumUsers = 0;  
22   
23 void User::set_email()  
24 {  
25     cout << "Enter email address: ";  
26     cin >> Email;  
27     cout << "Email is set successfully..." << endl;  
28 }  
29   
30 void User::change_passwd()  
31 {  
32     string OldPassword;  
33     int RemainingTries = 3;  
34     while (RemainingTries)  
35     {  
36         cout << "Enter old password: ";  
37         cin >> OldPassword;  
38         if (OldPassword == Password)  
39         {  
40             cout << "Enter new password: ";  
41             string NewPassword;  
42             cin >> NewPassword;  
43             cout << "New password is set successfully..." << endl;  
44             Password = NewPassword;  
45             break;  
46         }  
47         else  
48         {  
49             RemainingTries--;  
50             if (RemainingTries != 0)  
51                 cout << "Password input error. Please re-enter again: ";  
52         }  
53         if (RemainingTries == 0)  
54         {  
55             cout << "Password input error. Please try after a while." << endl;  
56         }  
57     }  
58 }  
59   
60 void User::print_info()  
61 {  
62     string StarPassword(Password.size(), '*');  
63     cout << "Name:   " << UserName << endl;  
64     cout << "Password: " << StarPassword << endl;  
65     cout << "Email:  " << Email << endl;  
66 }  
67   
68 void User::print_n()  
69 {  
70     cout << "There are " << NumUsers << " users." << endl;  
71 }
user.hpp
 1 #include "User.hpp"
 2 #include <iostream>
 3 // 测试User类
 4 void test() {
 5     using std::cout;
 6     using std::endl;
 7     cout << "testing 1......\n";
 8     User user1("Jonny", "92197", "[email protected]");
 9     user1.print_info();
10     cout << endl
11         << "testing 2......\n\n";
12     User user2("Leonard");
13     user2.change_passwd();
14     user2.set_email();
15     user2.print_info();
16     cout << endl;
17     User::print_n();
18 }
19 int main() {
20     test();
21 }
main.cpp

 任务实验五

 1 class SavingsAccount {  
 2 public:  
 3     // ... 其他函数 ...  
 4   
 5     // 构造函数  
 6     SavingsAccount(int date, int id, double rate) {  
 7         this->id = id;  
 8         this->balance = 0.0;  
 9         this->rate = rate;  
10         this->lastDate = date;  
11         this->accumulation = 0.0;  
12     }  
13   
14     // 记录一笔账  
15     void record(int date, double amount) {  
16         lastDate = date;  
17         balance += amount;  
18         accumulation += amount;  
19         total += amount;  // 更新total here  
20     }  
21   
22     // 存入现金  
23     void deposit(int date, double amount) {  
24         if (amount > 0) {  
25             record(date, amount);  
26             std::cout << "Deposited: " << amount << " on " << date << std::endl;  
27             total += amount;  // 更新total here  
28         }  
29     }  
30   
31     // 取出现金  
32     void withdraw(int date, double amount) {  
33         if (amount > 0 && amount <= balance) {  
34             record(date, -amount);  
35             std::cout << "Withdrew: " << amount << " on " << date << std::endl;  
36             total -= amount;  // 更新total here  
37         } else {  
38             std::cout << "Insufficient funds!" << std::endl;  
39         }  
40     }  
41   
42     // 结算利息  
43     void settle(int date) {  
44         double interest = balance * rate / 365 * (date - lastDate); // assuming simple interest here  
45         balance += interest;  
46         accumulation += interest;  
47         total += interest;  // 更新total here  
48         lastDate = date; // update the lastDate to current date after settlement  
49     }  
50   
51     // 显示账户信息  
52     void show() const {  
53         std::cout << "Account ID: " << id << std::endl;  
54         std::cout << "Balance: " << balance << std::endl;  
55         std::cout << "Rate: " << rate << std::endl;  
56         std::cout << "Last Date: " << lastDate << std::endl;  
57         std::cout << "Accumulation: " << accumulation << std::endl;  
58         std::cout << "Total: " << total << std::endl;  // Add total to the output here  
59     }  
60     
61 private:    
62     double total; // Declare total here    
63     // ... 其他变量 ...    
64 };
account.hpp
 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 SavingsAccount::SavingsAccount(int date, int id, double rate):id(id), balance(0), rate(rate), lastDate(date), accumulation(0){
 8     cout << date << "\t#" << id << "is created" << endl;
 9 }
10 void SavingsAccount::record(int date, double amount){
11     accumulation = accumulate(date);
12     lastDate = date;
13     amount = floor(amount * 100 + 0.5) / 100;  //保留小数点后两位
14     balance += amount;
15     total += amount;
16     cout << date << "\t#" << id << "\t" << amount << "\t" << balance <<endl;
17 }
18 void SavingsAccount::deposit(int date, double amount) {
19     record(date, amount);
20 }
21 void SavingsAccount::withdraw(int date, double amount){
22     if (amount > getBlance())
23         cout << "Error: not enough money" << endl;
24     else
25         record(date, -amount);
26 }
27 void SavingsAccount::settle(int date){
28     double interest = accumulate(date) * rate/365;  //计算年息
29     if (interest != 0)
30         record(date, interest);
31     accumulation = 0;
32 }
33 void SavingsAccount::show() const {
34     cout << "#" << id << "\tBlance:" << balance;
35 }
main.cpp

 

标签:real,cout,对象,double,void,Complex,实验,include
From: https://www.cnblogs.com/8716ydc/p/17781365.html

相关文章

  • 实验二
    任务三: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}......
  • 实验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......