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