25.07.29 학습개발일지 / C++ Study4

2025. 7. 30. 22:44·LMS 7/개발일지


과제1. C++ 문제 04-1

1) 원본 코드

<FruitSaleSim1.cpp>

// FruitSaleSim1.cpp
#include <iostream>
using namespace std;

class FruitSeller
{
private:
	int APPLE_PRICE;
	int numOfApples;
	int myMoney;
	
public:
	void InitMembers(int price, int num, int money)
	{
		APPLE_PRICE=price;
		numOfApples=num;
		myMoney=money;
	}
	int SaleApples(int money)  
	{
		int num=money/APPLE_PRICE;
		numOfApples-=num;
		myMoney+=money;
		return num;
	}
	void ShowSalesResult()
	{
		cout<<"남은 사과: "<<numOfApples<<endl;
		cout<<"판매 수익: "<<myMoney<<endl<<endl;
	}
};

class FruitBuyer
{
	int myMoney;		// private:
	int numOfApples;	// private:

public:
	void InitMembers(int money)
	{
		myMoney=money;
		numOfApples=0;
	}
	void BuyApples(FruitSeller &seller, int money)
	{
		numOfApples+=seller.SaleApples(money);
		myMoney-=money;
	}
	void ShowBuyResult()
	{
		cout<<"현재 잔액: "<<myMoney<<endl;
		cout<<"사과 개수: "<<numOfApples<<endl<<endl;
	}
};

int main(void)
{
	FruitSeller seller;
	seller.InitMembers(1000, 20, 0);
	FruitBuyer buyer;
	buyer.InitMembers(5000);
	buyer.BuyApples(seller, 2000);
	
	cout<<"과일 판매자의 현황"<<endl;
	seller.ShowSalesResult();
	cout<<"과일 구매자의 현황"<<endl;
	buyer.ShowBuyResult();
	return 0;
}

2) 변경 코드

<FruitSaleSimEdit.cpp>

// FruitSaleSimEdit.cpp
#include <iostream>
using namespace std;

class FruitSeller
{
private:
	int APPLE_PRICE;
	int numOfApples;
	int myMoney;
	
public:
	void InitMembers(int price, int num, int money)
	{
		APPLE_PRICE=price;
		numOfApples=num;
		myMoney=money;
	}
	int SaleApples(int money)
	{
		int num=money/APPLE_PRICE;
		numOfApples-=num;
		myMoney+=money;
		return num;
	}
	void ShowSalesResult() const // const 추가
	{
		cout<<"남은 사과: "<<numOfApples<<endl;
		cout<<"판매 수익: "<<myMoney<<endl<<endl;
	}
};

class FruitBuyer
{
	int myMoney;		// private:
	int numOfApples;	// private:

public:
	void InitMembers(int money)
	{
		myMoney=money;
		numOfApples=0;
	}
	void BuyApples(FruitSeller &seller, int money)
	{
        if(money <= 0) {
            cout << "구매 금액은 0보다 커야 합니다." << endl;
            return;
        } // 추가된 부분
		numOfApples+=seller.SaleApples(money);
		myMoney-=money;
	}
	void ShowBuyResult() const // const 추가
	{
		cout<<"현재 잔액: "<<myMoney<<endl;
		cout<<"사과 개수: "<<numOfApples<<endl<<endl;
	}
};

int main(void)
{
	FruitSeller seller;
	seller.InitMembers(1000, 20, 0);
	FruitBuyer buyer;
	buyer.InitMembers(5000);
	buyer.BuyApples(seller, 0);
	
	cout<<"과일 판매자의 현황"<<endl;
	seller.ShowSalesResult();
	cout<<"과일 구매자의 현황"<<endl;
	buyer.ShowBuyResult();
	return 0;
}

변경된 부분 1.

class FruitBuyer의 void BuyApples의 0원 이하의 money를 인자로 전달시에는 아래의 로직을 수행할 수 없게 함

변경된 부분 2.

class FruitSeller의 void ShowSalesResult, class FruitBuyer의 void ShowBuyResult 함수에 각각 const를 추가함


C++ 문제 04-2

#include<iostream>
using namespace std;

// 좌표를 표현하는 클래스, Point
class Point{
    private:
        int xpos, ypos;
    public:
        void Init(int x, int y){
            xpos = x;
            ypos = y;
        }
        void ShowPointInfo() const {
            cout << "[" << xpos << ", " << ypos << "]" << endl;
        }
};
// Point 클래스를 이용하여 원을 표현하는 클래스, Circle
class Circle{
    private:
        Point center; // 원의 중심좌표
        int radius; // 원의 반지름
    public:
        void Init(int x, int y, int r){
            center.Init(x, y);
            radius = r;
        }
        void ShowCircleInfo() const {
            cout << "Radius: " << radius << endl;
            cout << "중심좌표 : ";
            center.ShowPointInfo();
        }
};
// Circle 클래스를 이용하여 고리를 표현하는 클래스, Ring
class Ring{
    private :
        Circle inner; // 내부 원
        Circle outer; // 외부 원
    public :
        void Init(int x1, int y1, int r1, int x2, int y2, int r2){
            inner.Init(x1, y1, r1);
            outer.Init(x2, y2, r2);
        }
        void ShowRingInfo() {
            cout << "Inner Circle info ..." << endl;
            inner.ShowCircleInfo();
            cout << "Outter Circle info ..." << endl;
            outer.ShowCircleInfo();
        }
};
// 메인 함수
int main(void){
    Ring ring;
    ring.Init(1, 1, 4, 2, 2, 9);
    ring.ShowRingInfo();
    return 0;
}

C++ 문제 04-3

#include<iostream>
using namespace std;

// 좌표를 표현하는 클래스, Point
class Point{
    private:
        int xpos, ypos;
    public:
        Point(int x, int y) : xpos(x), ypos(y) {}
        void ShowPointInfo() const {
            cout << "[" << xpos << ", " << ypos << "]" << endl;
        }
};
// Point 클래스를 이용하여 원을 표현하는 클래스, Circle
class Circle{
    private:
        Point center; // 원의 중심좌표
        int radius; // 원의 반지름
    public:
        Circle(int x, int y, int r) : center(x, y), radius(r) {}
        void ShowCircleInfo() const {
            cout << "Radius: " << radius << endl;
            cout << "중심좌표 : ";
            center.ShowPointInfo();
        }
};
// Circle 클래스를 이용하여 고리를 표현하는 클래스, Ring
class Ring{
    private :
        Circle inner; // 내부 원
        Circle outer; // 외부 원
    public :
        Ring(int x1, int y1, int r1, int x2, int y2, int r2)
            : inner(x1, y1, r1), outer(x2, y2, r2){}
        void ShowRingInfo() {
            cout << "Inner Circle info ..." << endl;
            inner.ShowCircleInfo();
            cout << "Outter Circle info ..." << endl;
            outer.ShowCircleInfo();
        }
};
// 메인 함수
int main(void){
    Ring ring(1, 1, 4, 2, 2, 9);
    ring.ShowRingInfo();
    return 0;
}

#include<iostream>
#include<cstring>
using namespace std;

// 조건 enum을 사용하여 직급을 정의
// main의 COMP_POS::enum을 위해 namespace를 사용
namespace COMP_POS {
    enum {CLERK=1, SENIOR, ASSIST, MANAGER};
}

class NameCard{
    private:
        char*name; // 이름
        char*company; // 회사
        char*phone; // 전화번호
        int rank; // 입력받은 enum에 의한 정수를 저장
        char*rankName; // switch문을 통해 직급 이름을 저장
    public:
        // 생성자, 멤버 이니셜라이저를 사용하여 먼저 초기화 후 입력받은 값을 차례대로 저장
        NameCard(const char* input_name, const char* input_company, const char* input_phone, int rank)
            : name(), company(), phone(), rank(rank) {
            name = new char[strlen(input_name) + 1];
            strcpy(name, input_name);
            company = new char[strlen(input_company) + 1];
            strcpy(company, input_company);
            phone = new char[strlen(input_phone) + 1];
            strcpy(phone, input_phone);
            rankName = new char[20];
            switch (rank){
                case COMP_POS::CLERK:
                    rank = COMP_POS::CLERK;
                    strcpy(rankName, "사원");
                    break;
                case COMP_POS::SENIOR:
                    rank = COMP_POS::SENIOR;
                    strcpy(rankName, "대리");
                    break;
                case COMP_POS::ASSIST:
                    rank = COMP_POS::ASSIST;
                    strcpy(rankName, "주임");
                    break;
                case COMP_POS::MANAGER:
                    rank = COMP_POS::MANAGER;
                    strcpy(rankName, "보조자");
            } // switch(rank)
        } // NameCard(const char* input_name, ...)
        // 직급 이름을 출력하는 함수
        void ShowNameCardInfo() const{
            cout << "이름: " << name << endl;
            cout << "회사: " << company << endl;
            cout << "전화: " << phone << endl;
            cout << "직급: " << rankName << endl;
            cout << endl;
        }
        ~NameCard() {
            delete[] name;
            delete[] company;
            delete[] phone;
            delete[] rankName; // 동적 할당된 메모리 해제
        }
};

int main(void){
    NameCard manClerk("Lee", "ABCEng", "010-1111-2222", COMP_POS::CLERK);
    NameCard manSenior("Hong", "OrangeEng", "010-3333-4444", COMP_POS::SENIOR);
    NameCard manAssist("Kim", "SoGoodComp", "010-5555-6666", COMP_POS::ASSIST);
    manClerk.ShowNameCardInfo();
    manSenior.ShowNameCardInfo();
    manAssist.ShowNameCardInfo();
    return 0;
}

 

'LMS 7 > 개발일지' 카테고리의 다른 글

25.07.31 학습개발일지 C++ Study5  (11) 2025.07.31
25.07.30 개발학습일지 / C++ Study4(chapter 5, chapter 6)  (2) 2025.07.30
25.07.28 학습개발일지 / Makefile+  (2) 2025.07.30
25.07.27 학습개발일지 / C++ Study3  (3) 2025.07.30
25.07.25 개발일지 / 빌드 및 Make  (3) 2025.07.29
'LMS 7/개발일지' 카테고리의 다른 글
  • 25.07.31 학습개발일지 C++ Study5
  • 25.07.30 개발학습일지 / C++ Study4(chapter 5, chapter 6)
  • 25.07.28 학습개발일지 / Makefile+
  • 25.07.27 학습개발일지 / C++ Study3
m_Dev
m_Dev
  • m_Dev
    m_Dev
    m_Dev
  • 전체
    오늘
    어제
    • 분류 전체보기
      • MAIN STUDY
        • 정보보안
        • 빅데이터
        • 정보처리
        • 컴퓨터 구조
        • 기타
      • JOB
        • Study
        • Project
      • LMS 7
        • 개발일지
      • FRAMEWORK
        • Qt
        • MFC
        • Winform
        • WPF
        • MAUI
      • NETWORK
        • Study
        • Assignment
      • PYTHON
        • Set
        • Study
        • Assignment
        • Project
      • C
        • Set
        • Study
        • Assignment
        • Project
      • C++
        • Set
        • Study
        • Assignment
        • Project
      • C#
        • Set
        • Study
        • Assignment
        • Project
      • DATABASE
        • MySQL
        • SQLite
      • IDE
        • VisualStudioCode
        • VisualStudio
        • Pycharm
        • Colab
      • 기타
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
m_Dev
25.07.29 학습개발일지 / C++ Study4
상단으로

티스토리툴바