25.07.27 학습개발일지 / C++ Study3

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

과제1, C++ 문제 03-1 까지

https://cafe.naver.com/startdev?iframe_url_utf8=%2FArticleRead.nhn%253Fclubid%3D28969626%2526articleid%3D58442%2526referrerAllArticles%3Dtrue

 

Start Developer : 네이버 카페

대한상공회의소 인력개발사업단 국비무료교육, 개발자에 도전하는 이들을 위한 Start Developer 카페

cafe.naver.com


C++ 문제 03-2(p135)

#include<iostream>
using namespace std;

class Calculator{
	private:
		int Add_count;
		int Min_count;
		int Mul_count;
		int Div_count;
	public:
		void Init()
		{
			Add_count = 0;
			Min_count = 0;
			Mul_count = 0;
			Div_count = 0;
		}
		double Add(double a, double b)
		{
			Add_count++;
			return a + b;
		}
		double Min(double a, double b)
		{
			Min_count++;
			return a - b;
		}
		double Mul(double a, double b)
		{
			Mul_count++;
			return a * b;
		}
		double Div(double a, double b)
		{
			Div_count++;
			return a / b;
		}
		void ShowOpCount()
		{
			cout<<"덧셈: "<<Add_count<<" ";
			cout<<"뺄셈: "<<Min_count<<" ";
			cout<<"곱셈: "<<Mul_count<<" ";
			cout<<"나눗셈: "<<Div_count<<" "<<endl;
		}
};

int main(void)
{
	Calculator cal;
	cal.Init();
	cout<<"3.2 + 2.4 = "<<cal.Add(3.2, 2.4)<<endl;
	cout<<"3.5 / 1.7 = "<<cal.Div(3.5, 1.7)<<endl;
	cout<<"2.2 - 1.5 = "<<cal.Min(2.2, 1.5)<<endl;
	cout<<"4.9 / 1.2 = "<<cal.Div(4.9, 1.2)<<endl;
	cal.ShowOpCount();
	return 0;
}


#include<iostream>
using namespace std;

class Printer
{
	private:
		string str;
	public:
		void SetString(string s)
		{
			str = s;
		}
		void ShowString()
		{
			cout << str << endl;
		}
};
int main()
{
	Printer pnt;
	pnt.SetString("Hello, World!");
	pnt.ShowString();

	pnt.SetString("I love C++");
	pnt.ShowString();
	return 0;
}​

 


과제3. 파일 분할 & Makefile 스크립트 만들기

1.

p177

 

<point.h>

// point.h
#ifndef __POINT_H_
#define __POINT_H_

class Point
{
	int x; 
	int y;    
public:
	Point(const int &xpos, const int &ypos);
	int GetX() const;
	int GetY() const;
	bool SetX(int xpos);
	bool SetY(int ypos);
};

#endif

 

<point.cpp>

// point.cpp
#include <iostream>
#include "Point.h"
using namespace std;

Point::Point(const int &xpos, const int &ypos)
{
	x=xpos;
	y=ypos;
}

int Point::GetX() const {return x;}
int Point::GetY() const {return y;}

bool Point::SetX(int xpos)
{
	if(0>xpos || xpos>100)
	{
		cout<<"벗어난 범위의 값 전달"<<endl;
		return false;
	}

	x=xpos;
	return true;
}	
bool Point::SetY(int ypos)
{
	if(0>ypos || ypos>100)
	{
		cout<<"벗어난 범위의 값 전달"<<endl;
		return false;
	}

	y=ypos;
	return true;
}

 

<Rectangle.h>

// Rectangle.h
#ifndef __RECTANGLE_H_
#define __RECTANGLE_H_

#include "Point.h"

class Rectangle
{
	Point upLeft;
	Point lowRight;

public:
	Rectangle(const int &x1, const int &y1, const int &x2, const int &y2);
	void ShowRecInfo() const;
};

#endif

 

<Rectangle.cpp>

#include <iostream>
#include "Rectangle.h"
using namespace std;

Rectangle::Rectangle(const int &x1, const int &y1, const int &x2, const int &y2)
			:upLeft(x1, y1), lowRight(x2, y2)
{
	// empty
}

void Rectangle::ShowRecInfo() const
{
	cout<<"좌 상단: "<<'['<<upLeft.GetX()<<", ";
	cout<<upLeft.GetY()<<']'<<endl;
	cout<<"우 하단: "<<'['<<lowRight.GetX()<<", ";
	cout<<lowRight.GetY()<<']'<<endl<<endl;
}

 

<RectangleConstructor.cpp>

// RectangleConstructor.cpp
#include<iostream>
#include "Point.h"
#include "Rectangle.h"
using namespace std;

int main(void)
{

	Rectangle rec(1, 1, 5, 5);
	rec.ShowRecInfo();
	return 0;
}

 

<Makefile>

main : RectangleConstructor.o Rectangle.o Point.o
	g++ RectangleConstructor.o Rectangle.o Point.o -o main
RectangleConstructor.o : RectangleConstructor.cpp
	g++ -c RectangleConstructor.cpp
Rectangle.o : Rectangle.cpp Rectangle.h
	g++ -c Rectangle.cpp
Point.o : Point.cpp Point.h
	g++ -c Point.cpp
clean :
	rm -f main *.o


2.

181p

1) 원본 파일

 

<FruitSaleSim3.cpp>

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

class FruitSeller
{
	const int APPLE_PRICE;
	int numOfApples;
	int myMoney;
public:
	FruitSeller(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
	{
		cout<<"남은 사과: "<<numOfApples<<endl;
		cout<<"판매 수익: "<<myMoney<<endl<<endl;
	}
};

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

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

 

2) 파일 분할

 

<FruitSub.h>

// FruitSub.h
class FruitSeller
{
	const int APPLE_PRICE;
	int numOfApples;
	int myMoney;
public:
	FruitSeller(int price, int num, int money);
	int SaleApples(int money);
	void ShowSalesResult() const;
};

class FruitBuyer
{
	int myMoney;
	int numOfApples;
public:
	FruitBuyer(int money);
	void BuyApples(FruitSeller &seller, int money);
	void ShowBuyResult() const;
};

 

<FruitSub.cpp>

// FruitSub.cpp
#include <iostream>
#include "FruitSub.h"
using namespace std;

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

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

 

<FruitSaleMain.cpp>

// FruitSaleMain.cpp
#include <iostream>
#include "FruitSub.h"
using namespace std;

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

 

<Makefile>

main : FruitSaleMain.o FruitSub.o
	g++ FruitSaleMain.o FruitSub.o -o main
FruitSaleMain.o: FruitSaleMain.cpp
	g++ -c FruitSaleMain.cpp
FruitSub.o : FruitSub.cpp FruitSub.h
	g++ -c FruitSub.cpp
clean:
	rm -f *.o main

3.

203p

1) 파일 분할

 

<BankingMain.cpp>

// BankingMain.cpp
#include "Account.h"
int main(void)
{
	int choice;
    
    // 전역변수 초기화
    Account * accArr[100];   // Account 저장을 위한 배열
    int accNum=0;        // 저장된 Account 수
    const int NAME_LEN = 20;
	
	while(1)
	{
		ShowMenu();
		cout<<"선택: "; 
		cin>>choice;
		cout<<endl;
		
		switch(choice)
		{
		case MAKE:
			MakeAccount(); 
			break;
		case DEPOSIT:
			DepositMoney(); 
			break;
		case WITHDRAW: 
			WithdrawMoney(); 
			break;
		case INQUIRE:
			ShowAllAccInfo(); 
			break;
		case EXIT:
			return 0;
		default:
			cout<<"Illegal selection.."<<endl;
		}
	}

	for(int i=0; i<accNum; i++)
		delete accArr[i];
	return 0;
}

 

<Account.h>

// Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

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

void ShowMenu(void);       // 메뉴출력
void MakeAccount(void);    // 계좌개설을 위한 함수
void DepositMoney(void);       // 입    금
void WithdrawMoney(void);      // 출    금
void ShowAllAccInfo(void);     // 잔액조회

enum {MAKE=1, DEPOSIT, WITHDRAW, INQUIRE, EXIT};

class Account
{
private:
	int accID;      // 계좌번호
	int balance;    // 잔    액
	char * cusName;   // 고객이름

public:
	Account(int ID, int money, char * name)
		: accID(ID), balance(money)
	{
		cusName=new char[strlen(name)+1];
		strcpy(cusName, name);
	}

	int GetAccID() { return accID; }

	void Deposit(int money)
	{
		balance+=money;
	}

	int Withdraw(int money)    // 출금액 반환, 부족 시 0
	{
		if(balance<money)
			return 0;
		
		balance-=money;
		return money;
	}

	void ShowAccInfo()
	{
		cout<<"계좌ID: "<<accID<<endl;
		cout<<"이  름: "<<cusName<<endl;
		cout<<"잔  액: "<<balance<<endl;
	}

	~Account()
	{
		delete []cusName;
	}	
};

// 전역변수들
extern Account * accArr[100];   // Account 저장을 위한 배열
extern int accNum;        // 저장된 Account 수
extern const int NAME_LEN;
    
#endif

 

<Account.cpp>

// Account.cpp
#include "Account.h"

// 전역변수 초기화
Account * accArr[100];   // Account 저장을 위한 배열
int accNum=0;        // 저장된 Account 수
const int NAME_LEN = 20;

void ShowMenu(void)
{
	cout<<"-----Menu------"<<endl;
	cout<<"1. 계좌개설"<<endl;
	cout<<"2. 입    금"<<endl;
	cout<<"3. 출    금"<<endl;
	cout<<"4. 계좌정보 전체 출력"<<endl;
	cout<<"5. 프로그램 종료"<<endl;
}

void MakeAccount(void) 
{
	int id;
	char name[NAME_LEN];
	int balance;
	
	cout<<"[계좌개설]"<<endl;
	cout<<"계좌ID: ";	cin>>id;
	cout<<"이  름: ";	cin>>name;
	cout<<"입금액: ";	cin>>balance;
	cout<<endl;

	accArr[accNum++]=new Account(id, balance, name);
}

void DepositMoney(void)
{
	int money;
	int id;
	cout<<"[입    금]"<<endl;
	cout<<"계좌ID: ";	cin>>id;
	cout<<"입금액: ";	cin>>money;
	
	for(int i=0; i<accNum; i++)
	{
		if(accArr[i]->GetAccID()==id)
		{
			accArr[i]->Deposit(money);
			cout<<"입금완료"<<endl<<endl;
			return;
		}
	}
	cout<<"유효하지 않은 ID 입니다."<<endl<<endl;
}

void WithdrawMoney(void)
{
	int money;
	int id;
	cout<<"[출    금]"<<endl;
	cout<<"계좌ID: ";	cin>>id;
	cout<<"출금액: ";	cin>>money;
	
	for(int i=0; i<accNum; i++)
	{
		if(accArr[i]->GetAccID()==id)
		{
			if(accArr[i]->Withdraw(money)==0)
			{
				cout<<"잔액부족"<<endl<<endl;
				return;
			}

			cout<<"출금완료"<<endl<<endl;
			return;
		}
	}
	cout<<"유효하지 않은 ID 입니다."<<endl<<endl;
}

void ShowAllAccInfo(void)	
{
	for(int i=0; i<accNum; i++)
	{
		accArr[i]->ShowAccInfo();
		cout<<endl;
	}
}

 

<Makefile>

main : BankingMain.o Account.o
	g++ -o main BankingMain.o Account.o

BankingMain.o : BankingMain.cpp Account.h
	g++ -c BankingMain.cpp

Account.o : Account.cpp Account.h
	g++ -c Account.cpp

clean :
	rm -f main *.o

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

25.07.29 학습개발일지 / C++ Study4  (2) 2025.07.30
25.07.28 학습개발일지 / Makefile+  (2) 2025.07.30
25.07.25 개발일지 / 빌드 및 Make  (3) 2025.07.29
25.07.24 학습개발일지 / C++  (5) 2025.07.29
25.07.23 학습일지 / C++  (3) 2025.07.29
'LMS 7/개발일지' 카테고리의 다른 글
  • 25.07.29 학습개발일지 / C++ Study4
  • 25.07.28 학습개발일지 / Makefile+
  • 25.07.25 개발일지 / 빌드 및 Make
  • 25.07.24 학습개발일지 / C++
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.27 학습개발일지 / C++ Study3
상단으로

티스토리툴바