hugDog
Android DevLog
hugDog
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๐Ÿ™Œ Hello? (162)
    • ๐Ÿงฉ์•ˆ๋“œ๋กœ์ด๋“œ (12)
      • ๊ฐœ๋… ์ •๋ฆฌ (5)
      • ๋ฒ„๊ทธ ํ•ด๊ฒฐ (4)
      • ๊ธฐํƒ€ (3)
    • ๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜ (54)
      • ๊ฐœ๋… (0)
      • ๋ฐฑ์ค€ (48)
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (6)
    • ๐Ÿ“„๊ฐœ๋ฐœ ์ผ์ง€ (0)
      • FINPO (0)
    • ๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (71)
      • C++ ์ •๋ฆฌ (49)
      • C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ (20)
      • Kotlin (2)
    • โญProject (1)
    • ๐ŸšดTIL (13)
      • Clean Code (13)
    • ๐Ÿšฉ๊ธฐํƒ€ (9)
      • ๋ชฉํ‘œ (6)
      • ์ผ์ƒ (3)
      • ๋ฌธ์„œ (0)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
hugDog

Android DevLog

๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ

(C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 10 ๊ฐ์ฒด์™€ ํด๋ž˜์Šค p.705~ 1๋ฒˆ~8๋ฒˆ

2020. 9. 10. 11:36
728x90

1๋ฒˆ

//account.h
#ifndef account_H_
#define account_H_

class BankAccount
{
private:
	char name[40];
	char acctnum[25];
	double balance;
public:
	BankAccount(const char* client, const char* num, double bal = 0.0);
	void show(void) const;
	void deposit(double cash);
	void withdraw(double cash);
};
#endif

//account.cpp
#include<iostream>
#include<cctype>
#include"account.h"

BankAccount::BankAccount(const char* client, const char* num, double bal)
{
	strcpy(name, client);
	strcpy(acctnum, num);
	balance = bal;
}
void BankAccount::show(void) const
{
	std::cout << "์ด๋ฆ„ : " << name << "\n๊ณ„์ขŒ : " << acctnum 
		<< "\n์ž”์•ก: " << balance << '\n';
}
void BankAccount::deposit(double cash)
{
	balance += cash;
}
void BankAccount::withdraw(double cash)
{
	if ((balance - cash) < 0)
		std::cout << "์ถœ๊ธˆํ•  ๊ธˆ์•ก์ด ์ž”์•ก๋ณด๋‹ค ๋งŽ์Šต๋‹ˆ๋‹ค.\n";
	else
		balance -= cash;
}

//useaccnt.cpp
#include<iostream>
#include"account.h"

int main()
{
	BankAccount test = { "test name","test account num",1000.0 };
	test.show();
	test.deposit(250);
	test.show();
	test.withdraw(1500);
	test.withdraw(300);
	test.show();

	return 0;
}

 

 

 

2๋ฒˆ

//person.h
#ifndef PERSON_H_
#define PERSON_H_
class Person {
private:
	static const int LIMIT = 25;
	std::string lname;
	char fname[LIMIT];
public:
	Person() { lname = "", fname[0] = '\0'; } //#1
	Person(const std::string& ln, const char* fn = "Heyyou"); //#2
	void Show() const; //์ด๋ฆ„ ์„ฑ์”จ ํ˜•์‹
	void FormalShow() const; //์„ฑ์”จ, ์ด๋ฆ„ ํ˜•์‹
};
#endif

//person.cpp
#include<iostream>
#include"person.h"

Person::Person(const std::string& ln, const char* fn) //#2
{
	lname = ln;
	strcpy(fname, fn);
}
void Person::Show() const //์ด๋ฆ„ ์„ฑ์”จ ํ˜•์‹
{
	std::cout << "์ด๋ฆ„ : " << fname << " " << lname << '\n';
}
void Person::FormalShow() const //์„ฑ์”จ, ์ด๋ฆ„ ํ˜•์‹
{
	std::cout << "์ด๋ฆ„ : " << lname << " " << fname << '\n';
}


//useperson.cpp
#include<iostream>
#include"person.h"

int main()
{
	using std::cout;
	using std::endl;
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	one.FormalShow();
	two.Show();
	two.FormalShow();
	three.Show();
	three.FormalShow();
	
	return 0;
}

 

 

3๋ฒˆ

//golf.cpp
#include<iostream>
#include"golf.h"

Golf::Golf()
{
	strcpy(fullname, "");
	this->handicap = 0;
}

Golf::Golf(const char* name, int hc)
{
	strcpy(fullname, name);
	this->handicap = hc;
}
int Golf::setgolf() // ar[1].setgolf(); 
{
	using std::cout;
	cout << "์ด๋ฆ„ ์ž…๋ ฅ : ";
	std::cin.getline(this->fullname, 40);
	if (this->fullname[0] == '\0')
	{
		cout << "์ด๋ฆ„์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.\n";
		return 0;
	}
	else
	{
		cout << "ํ•ธ๋””์บก ์ž…๋ ฅ : ";
		(std::cin >> this->handicap).get();
		return 1;
	}
}
void Golf::f_handicap(int hc)
{
	this->handicap = hc;
}
void Golf::showgolf() const
{
	using std::cout;
	using std::endl;
	cout << "์ด๋ฆ„ : " << fullname << endl
		<< "ํ•ธ๋””์บก : " << this->handicap << endl;
}




//golf.h
#ifndef GOLF_H_
#define GOLF_H_
#include<iostream>
class Golf {
private:
	static const int Len = 40; 
	char fullname[Len];
	int handicap;
public:
	Golf();
	Golf(const char* name, int hc = 0);
	int setgolf();
	void f_handicap(int hc);
	void showgolf() const;
};
#endif




//usegolf.cpp
#include<iostream>
#include"golf.h"

int main()
{
	using std::cout;
	Golf ann("Ann Birdfree",24);
	ann.showgolf();
	cout << "ann์˜ ํ•ธ๋””์บก ์ˆ˜์ •\n";
	ann.f_handicap(30);
	ann.showgolf();

	cout << "\n\ngolf ๊ตฌ์กฐ์ฒด์˜ ๋ฐฐ์—ด์— ์ž…๋ ฅ์„ ์š”๊ตฌ\n\n";

	Golf ar[5];
	int i;
	for (i = 0; i < 5; i++)
	{
		if ((ar[i].setgolf()) == 0)
			break;
	}

	cout << "\n\ngolf ๊ตฌ์กฐ์ฒด์˜ ๋ฐฐ์—ด์ถœ๋ ฅ\n\n";
	for (int j = 0; j < i; j++)
		ar[j].showgolf();
}

 

 

4๋ฒˆ

//sale.h

namespace N_SALES
{
	class SALES
	{
	private:
		static const int QUARTERS = 4;
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	public:
		SALES();
		SALES(const double ar[], int n);
		void setSales();
		void showSales() const;
	};
}

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

namespace N_SALES
{
	SALES::SALES()
	{
		double sales[QUARTERS] = { 0.0,0.0,0.0,0.0 };
	}
	SALES::SALES(const double ar[], int n)
	{
		if (n < 4)
		{
			for (int i = n; i < 4; i++)
				sales[i] = 0;
		}
		else n = 4;
		for (int i = 0; i < n; i++)
			sales[i] = ar[i];
		max = min = average = sales[0];
		for (int i = 1; i < n; i++)
		{
			if (sales[i] > max) max = sales[i];
			if (sales[i] < min) min = sales[i];
			average += sales[i];
		}
		average /= n;
	}

	void SALES::setSales()
	{
		for (int i = 0; i < 4; i++)
		{
			cout << i + 1 << "๋ถ„๊ธฐ๋ณ„ ํŒ๋งค์•ก : ";
			cin >> sales[i];
		}
		max = min = average = sales[0];
		for (int i = 1; i < 4; i++)
		{
			if (sales[i] > max) max = sales[i];
			if (sales[i] < min) min = sales[i];
			average += sales[i];
		}
		average /= 4;
	}

	void SALES::showSales() const
	{
		for (int i = 0; i < 4; i++)
			cout << i + 1 << "๋ถ„๊ธฐ ํŒ๋งค์•ก : " << sales[i] << endl;
		cout << "์ตœ๋Œ€๊ฐ’ : " << max << endl
			<< "์ตœ์†Œ๊ฐ’ : " << min << endl
			<< "ํ‰๊ท ๊ฐ’ : " << average << endl;
	}
}

//main.cpp
#include<iostream>
#include"sale.h"
using namespace N_SALES;

int main()
{
	using namespace std;
	double test1[2] = { 3.1,9.7 };
	double test2[4] = { 1.4,2.5,3.2,6.5 };
	SALES a1(test1, 2); SALES a2(test2, 4); SALES a3;
	a1.showSales(); a2.showSales();

	cout << endl << endl;
	a3.setSales();
	a3.showSales();

}

 

 

5๋ฒˆ

//stack.h
#ifndef STACK_H_
#define STACK_H_

struct customer {
	char fullname[35];
	double payment;
};

typedef struct customer Item;

class Stack {
private:
	enum {MAX=10};
	Item items[MAX];
	int top;
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	bool push(const Item& item);
	bool pop(Item& item);
};



#endif

//stack.cpp
#include "stack.h"

Stack::Stack()
{
	top = 0;
}

bool Stack::isempty() const
{
	return top == 0;
}

bool Stack::isfull() const
{
	return top == MAX;
}

bool Stack::push(const Item& item)
{
	if (top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
		return false;
}

bool Stack::pop(Item& item)
{
	if (top > 0)
	{
		item = items[--top];
		return true;
	}
	else
		return false;
}


//usestack.cpp
#include<iostream>
#include<cctype>
#include"stack.h"
int main()
{
	using namespace std;
	Stack st;
	char ch;
	customer po;
	double total = 0.0;
	cout << "๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด A, ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด P,\n"
		<< "์ข…๋ฃŒํ•˜๋ ค๋ฉด Q๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค.\n";
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
			continue;
		if (!isalpha(ch))
		{
			cout << '\a';
			continue;
		}
		switch (ch)
		{
		case 'A':
		case 'a':
			cout << "์ถ”๊ฐ€ํ•  ๊ณ ๊ฐ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
			cin.getline(po.fullname, 35);
			cout << "์ถ”๊ฐ€ํ•  ๊ณ ๊ฐ์˜ ์ง€๋ถˆ ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
			cin >> po.payment;
			cin.get();
			if (st.isfull())
				cout << "์Šคํƒ์ด ๊ฐ€๋“ ์ฐจ ์žˆ์Šต๋‹ˆ๋‹ค.\n";
			else
				st.push(po);
			break;
		case 'P':
		case 'p':
			if (st.isempty())
				cout << "์Šคํƒ์ด ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.\n";
			else
			{
				st.pop(po);
				cout << "#" << po.fullname << " ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.\n";
				total += po.payment;
				cout << "ํ˜„์žฌ๊นŒ์ง€์˜ ์ด ์ˆ˜์ž… : " << total << endl;
			}
			break;
		}
		cout << "๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด A, ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด P,\n"
			<< "์ข…๋ฃŒํ•˜๋ ค๋ฉด Q๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค.\n";
	}
	cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n";
	return 0;
}

 

 

6๋ฒˆ

//move.h
#ifndef MOVE_H_
#define MOVE_H_
class Move
{
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);
	void showmove() const;
	Move add(const Move& m) const;
	void reset(double a = 0, double b = 0);
};
#endif

//move.cpp
#include<iostream>
#include "move.h"

Move::Move(double a, double b)
{
	x = a;
	y = b;
}
void Move::showmove() const
{
	using std::cout;
	cout << "x์ขŒํ‘œ : " << x << ", Y์ขŒํ‘œ : " << y<<'\n';
}
Move Move::add(const Move& m) const
{
	return Move(x + m.x, y + m.y);
}
void Move::reset(double a, double b)
{
	x = a;
	y = b;
}


//usemove.cpp
#include<iostream>
#include"move.h"

int main()
{
	Move a;
	Move b(5, 1);
	a.showmove();
	b.showmove();
	a.reset(2, 3);
	a.showmove();
	a = a.add(b);
	a.showmove();
	return 0;
}

 

 

7๋ฒˆ

//plorg.h
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
	char pname[20];
	int ci;
public:
	Plorg(const char m_pname[] = "Plorga");
	void change_ci(int m_ci);
	void showplorg();
};
#endif

//plorg.cpp
#include<iostream>
#include<cctype>
#include"plorg.h"

Plorg::Plorg(const char m_pname[])
{
	strncpy(pname, m_pname,19);
	pname[20] = '\0';
	ci = 50;
}
void Plorg::change_ci(int m_ci)
{
	ci = m_ci;
}
void Plorg::showplorg()
{
	std::cout << "์ด๋ฆ„ : " << pname << ", ci ์ง€์ˆ˜ : " << ci << '\n';
}


//useplorg.cpp
#include<iostream>
#include"plorg.h"

int main()
{
	Plorg a1("asdf's plorg");
	Plorg a2;
	a2.change_ci(20);
	a1.showplorg();
	a2.showplorg();
	return 0;
}

 

 

8๋ฒˆ

//list.h
#ifndef LIST_H_
#define LIST_H_
typedef unsigned int Item;

class List
{
private:
	Item *items;
	int top,num;
public:
	List(int n=10);
	~List();
	bool add(Item &);
	bool isempty();
	bool isfull();
	void show();
	void visit(void (*pf)(Item&));
};
#endif

//list.cpp

#include<iostream>
#include"list.h"


List::List(int n)
{
	num = n;
	items = new Item[num];
	top = 0;
}
List::~List()
{
	delete[]items;
}
bool List::add(Item& item)
{
	if (top < num)
	{
		items[top++] = item;
		return true;
	}
	else
		return false;

}
bool List::isempty()
{
	return top == 0;
}
bool List::isfull()
{
	return top == num;
}
void List::visit(void (*pf)(Item&))
{
	for (int i = 0; i < num; i++)
		(*pf)(items[i]);
}

void List::show()
{
	for (int i = 0; i < top; i++)
		std::cout << items[i] << '\n';
}

//uselist.cpp
#include<iostream>
#include"list.h"

void half(Item& a);

int main()
{
	using namespace std;
	cout << "์ƒ์„ฑํ•  ๋ฆฌ์ŠคํŠธ์˜ ์ˆ˜ : ";
	int n;
	Item item;
	(cin >> n).get();
	List a(n);
	cout << n << "๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n";
	if (a.isempty())
	{
		cout << "๋ฆฌ์ŠคํŠธ์— ํ•ญ๋ชฉ ์ž…๋ ฅ\n";
		for (int i=1;!a.isfull();i++)
		{
			cout << i << "๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ : ";
			cin >> item;
			a.add(item);
		}
		cout << "์ž…๋ ฅ ์™„๋ฃŒ\n";
	}
	cout << "์ž…๋ ฅ๋œ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ :\n";
	a.show();
	cout << "๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ๋“ค์„ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ”\n";
	a.visit(half);
	cout << "๋‚˜๋ˆ ์ง„ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ:\n";
	a.show();
}

void half(Item& a)
{
	a /= 2;
}
728x90
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    '๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 12 ํด๋ž˜์Šค์™€ ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์ž… p.892~ 1๋ฒˆ~6๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 11 ํด๋ž˜์Šค์˜ ํ™œ์šฉ p.787~ 1๋ฒˆ~7๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 09 ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋ธ๊ณผ ์ด๋ฆ„ ๊ณต๊ฐ„ p.630~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 08 ํ•จ์ˆ˜์˜ ํ™œ์šฉ p.555~ 1๋ฒˆ~7๋ฒˆ
    hugDog
    hugDog
    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ ์ค‘์ธ ํ•™์ƒ์ž…๋‹ˆ๋‹ค!

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”