본문 바로가기

Code/C++

[C++] (example) private& public/ 계좌만들기/ 분자& 분모

#include <iostream>

#include <string>

#include <vector>

using namespace std;

 

 

class Point {

public:

        double x;

        double y;

};

int main() {

        Point pt1, pt2;

        pt1.x = 8.5;

        pt1.y = 0.0;

        pt2.x = -4;

        pt2.y = 2.5;

        cout << "pt1=(" << pt1.x << ", " << pt1.y << ")\n";

        cout << "pt2=(" << pt2.x << ", " << pt2.y << ")\n";

        pt1 = pt2;

        cout << "pt1=(" << pt1.x << ", " << pt1.y << ")\n";

        cout << "pt2=(" << pt2.x << ", " << pt2.y << ")\n";

        pt1.x = 0;

        cout << "pt1=(" << pt1.x << ", " << pt1.y << ")\n";

        cout << "pt2=(" << pt2.x << ", " << pt2.y << ")\n";

}

 

 

 


계좌만들기 벡터포함

 

 

class Account {

public:

        string name;

        int id;

        double balance;

};

void add_account(vector<Account>& accts) {

        string name;

        int number;

        double amount;

        cout << "Enter name,account number, and account balance: ";

        cin >> name >> number >> amount;

        Account acct;

        acct.name = name;

        acct.id = number;

        acct.balance = amount;

        accts.push_back(acct);

}

void print_accounts(const vector<Account>& accts) {

        int n = accts.size();

        for (int i = 0; i < n; i++)

               cout << accts[i].name << ", " << accts[i].id

               << ", " << accts[i].balance << '\n';

}

void swap(Account& er1, Account& er2) {

        Account temp = er1;

        er1 = er2;

        er2 = temp;

}

bool less_than_by_name(const Account& e1, const Account& e2) {

        return e1.name < e2.name;

}

bool less_than_by_id(const Account& e1, const Account& e2) {

        return e1.id < e2.id;

}

bool less_than_by_balance(const Account& e1, const Account& e2) {

        return e1.balance < e2.balance;

}

void sort(vector<Account>& db,

        bool(*comp)(const Account&, const Account&)) {

        int size = db.size();

        for (int i = 0; i < size - 1; i++) {

               int smallest = i;

               for (int j = i + 1; j < size; j++)

                       if (comp(db[j], db[smallest]))

                              smallest = j;

               if (smallest != i)

                       swap(db[i], db[smallest]);

        }

}

int main() {

        vector<Account> customers;

        char cmd;

        bool done = false;

        do {

               cout << "[A]dd [N]ame [I]D [B]alance [Q]uit == > ";

               cin >> cmd;

               switch (cmd) {

               case 'A':

               case 'a':

                       add_account(customers);

                       break;

               case 'P':

               case 'p':

                       print_accounts(customers);

                       break;

               case 'N':

               case 'n':

                       sort(customers, less_than_by_name);

                       print_accounts(customers);

                       break;

               case'I':

               case'i':

                       sort(customers, less_than_by_id);

                       print_accounts(customers);

                       break;

               case 'B':

               case 'b':

                       sort(customers, less_than_by_balance);

                       print_accounts(customers);

                       break;

               case'Q':

               case'q':

                       done = true;

                       break;

               }

        } while (!done);

}

 

 

 

 


계좌만들기

 

 

class Account {

        string name;

        int id;

        double balance;

public:

        Account(const string& customer_name, int account_number,

               double amount) :

               name(customer_name), id(account_number), balance(amount) {

               if (amount < 0) {

                       cout << "Warning: negative account balance\n";

                       balance = 0.0;

               }

        }

        void deposit(double amt) {

               balance += amt;

        }

        bool withdraw(double amt) {

               bool result = false;

               if (balance - amt >= 0) {

                       balance -= amt;

                       result = true;

               }

               return result;

        }

        

        void display() {

               cout << "Name: " << name << ",ID: " << id

                       << ", Balance: " << balance << '\n';

        }

};

int main() {

        Account acct1("Joe", 2312, 1000.00);

        Account acct2("Moe", 2400, 500.29);

        acct1.display();

        acct2.display();

        cout << "---------------------" << "\n";

        acct1.withdraw(800.00);

        acct2.deposit(22.00);

        acct1.display();

        acct2.display();

}

 

 

 

Name: Joe,ID: 2312, Balance: 1000

Name: Moe,ID: 2400, Balance: 500.29

---------------------

Name: Joe,ID: 2312, Balance: 200

Name: Moe,ID: 2400, Balance: 522.29

 

 


 

numerator & denominator

 

 

#include <iostream>

#include <string>

#include <vector>

using namespace std;

 

 

class SimpleRational {

        int numerator;

        int denominator;

public:

        SimpleRational(int n, int d) : numerator(n), denominator(d) {

               if (d == 0) {

                       cout << "Zero denominator error \n";

                       exit(1);

               }

        }

        //default constructor makes a zero rational number 0/1

        SimpleRational():numerator(0),denominator(1){}

        void set_numerator(int n) {

               numerator = n;

        }

        void set_dominator(int d) {

               if (d != 0)

                       denominator = d;

               else {

                       cout << "Zero denominator error \n";

                       exit(1);

               }

        }

        int get_numerator() {

               return numerator;

        }

        

        int get_denominator() {

               return denominator;

        }

};

SimpleRational multiply(SimpleRational f1, SimpleRational f2) {

        return { f1.get_numerator() * f2.get_numerator(),

        f1.get_denominator() * f2.get_denominator() };

}

void print_fraction(SimpleRational f) {

        cout << f.get_numerator() << "/" << f.get_denominator();

}

int main() {

        SimpleRational fract(1, 2);

        cout << "The fraction is ";

        print_fraction(fract);

        cout << "\n";

        fract.set_numerator(19);

        fract.set_dominator(4);

        cout << "The fraction now is ";

        print_fraction(fract);;

        cout << '\n';

        SimpleRational fract1(1, 2), fract2(2, 3);

        auto prod = multiply(fract1, fract2);

        cout << "The product of ";

        print_fraction(fract1);

        cout << " and ";

        print_fraction(fract2);

        cout << " is ";

        print_fraction(prod);

        cout << "\n";

}

 

 

 

>>>

The fraction is 1/2

The fraction now is 19/4

The product of 1/2 and 2/3 is 2/6

 

 

 

 

 


 

해당 자료는 경희대학교 소프트웨어융합학과 배성호교수님 수업내용을 참조하였습니다.