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
๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ

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

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

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

2020. 9. 21. 14:21
728x90

1๋ฒˆ

//cow.h
#ifndef COW_H_
#define COW_H_
class Cow
{
private:
	char name[20];
	char* hobby;
	double weight;
public:
	Cow();
	Cow(const char* nm, const char* ho, double wt);
	Cow(const Cow& c);
	~Cow();
	Cow& operator=(const Cow& c);
	void ShowCow() const;
};
#endif

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

Cow::Cow()
{
	strcpy(name, "no name");
	hobby = new char[1];
	hobby[0] = '\0';
	weight = 0.0;
}
Cow::Cow(const char* nm, const char* ho, double wt)
{
	strncpy(name, nm, 19);
	name[19] = '\0';
	hobby = new char[strlen(ho) + 1];
	strcpy(hobby, ho);
	weight = wt;
}
Cow::Cow(const Cow& c)
{
	strcpy(name, c.name);
	delete[] hobby;
	hobby = new char[strlen(c.hobby) + 1];
	strcpy(hobby, c.hobby);
	weight = c.weight;
}
Cow::~Cow()
{ 
	delete[] hobby;
}
Cow& Cow::operator=(const Cow& c)
{
	if (this == &c)
		return *this;
	delete[]hobby;
	strcpy(name, c.name);
	hobby = new char[strlen(c.hobby) + 1];
	strcpy(hobby, c.hobby);
	weight = c.weight;
	return *this;
}
void Cow::ShowCow() const
{
	std::cout << "์ด๋ฆ„ : " << name << '\n'
		<< "์ทจ๋ฏธ : " << hobby << '\n'
		<< "๋ชธ๋ฌด๊ฒŒ : " << weight << '\n';
}

//usecow.cpp
#include"cow.h"

int main()
{
	Cow h("hong gil dong", "soccer", 72.4);
	Cow n;
	h.ShowCow();
	n.ShowCow();

	n = h;
	n.ShowCow();
}

 

 

2๋ฒˆ

// string2.h -- ํ™•๋Œ€ ๊ฐœ์„ ๋œ String ํด๋ž˜์Šค ์ •์˜

#ifndef STRING2_H_
#define STRING2_H_
#include <iostream>
using std::ostream;
using std::istream;

class String
{
private:
    char* str;             // ๋ฌธ์ž์—ด์„ ์ง€์‹œํ•˜๋Š” ํฌ์ธํ„ฐ
    int len;                // ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
    static int num_strings; // ๊ฐ์ฒด์˜ ์ˆ˜
    static const int CINLIM = 80;  // cin ์ž…๋ ฅ ํ•œ๊ณ„
public:
    // ์ƒ์„ฑ์ž์™€ ๊ธฐํƒ€ ๋ฉ”์„œ๋“œ
    String(const char* s); // ์ƒ์„ฑ์ž
    String();               // ๋””ํดํŠธ ์ƒ์„ฑ์ž
    String(const String&); // ๋ณต์‚ฌ ์ƒ์„ฑ์ž
    ~String();              // ํŒŒ๊ดด์ž
    int length() const { return len; }
    void stringlow(); //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
    void stringup(); //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
    int has(char); //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
    // ์˜ค๋ฒ„๋กœ๋”ฉ ์—ฐ์‚ฐ์ž ๋ฉ”์„œ๋“œ
    String& operator=(const String&);
    String& operator=(const char*);
    char& operator[](int i);
    const char& operator[](int i) const;
    // ์˜ค๋ฒ„๋กœ๋”ฉ ์—ฐ์‚ฐ์ž ํ”„๋ Œ๋“œ
    friend bool operator<(const String& st, const String& st2);
    friend bool operator>(const String& st1, const String& st2);
    friend bool operator==(const String& st, const String& st2);
    friend ostream& operator<<(ostream& os, const String& st);
    friend istream& operator>>(istream& is, String& st);
    friend String operator+(const String&,const String&); //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
    // static ํ•จ์ˆ˜
    static int HowMany();
};
#endif

// string2.cpp -- String ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ
#include <cstring>                 // ์–ด๋–ค C++ ์‹œ์Šคํ…œ์—์„œ๋Š” string.h
#include "string2.h" // <iostream>์„ ํฌํ•จํ•œ๋‹ค
#include<cctype>

using std::cin;
using std::cout;

// static ํด๋ž˜์Šค ๋ฉค๋ฒ„๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค

int String::num_strings = 0;

// static ๋ฉ”์„œ๋“œ
int String::HowMany()
{
    return num_strings;
}

// ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ
String::String(const char* s)     // C ์Šคํƒ€์ผ์˜ ๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ String์„ ์ƒ์„ฑํ•œ๋‹ค
{
    len = std::strlen(s);          // ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•œ๋‹ค
    str = new char[len + 1];       // ๊ธฐ์–ต ๊ณต๊ฐ„์„ ๋Œ€์ž…ํ•œ๋‹ค
    std::strcpy(str, s);           // ํฌ์ธํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค
    num_strings++;                 // ๊ฐ์ฒด ์นด์šดํŠธ ์„ค์ •
}

String::String()                   // ๋””ํดํŠธ ์ƒ์„ฑ์ž
{
    len = 4;
    str = new char[1];
    str[0] = '\0';                 // ๋””ํดํŠธ ๋ฌธ์ž์—ด
    num_strings++;
}

String::String(const String& st)
{
    num_strings++;             // static ๋งด๋ฒ„ ๊ฐฑ์‹ ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค
    len = st.len;              // ๊ฐ™์€ ํฌ๊ธฐ๋กœ ์„ค์ •ํ•œ๋‹ค
    str = new char[len + 1];  // ๊ธฐ์–ต ๊ณต๊ฐ„์„ ๋Œ€์ž…ํ•œ๋‹ค
    std::strcpy(str, st.str);  // ๋ฌธ์ž์—ด์„ ์ƒˆ ์œ„์น˜์— ๋ณต์‚ฌํ•œ๋‹ค
}

String::~String()                     // ๊ผญ ํ•„์š”ํ•œ ํŒŒ๊ดด์ž
{
    --num_strings;                    // ํ•„์š”ํ•˜๋‹ค
    delete[] str;                    // ํ•„์š”ํ•˜๋‹ค
}

// ์˜ค๋ฒ„๋กœ๋“œ ์—ฐ์‚ฐ์ž ๋ฉ”์„œ๋“œ 

    // String์„ String์— ๋Œ€์ž…ํ•œ๋‹ค
String& String::operator=(const String& st)
{
    if (this == &st)
        return *this;
    delete[] str;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);
    return *this;
}

// C ์Šคํƒ€์ผ์˜ ๋ฌธ์ž์—ด์„ String์— ๋Œ€์ž…ํ•œ๋‹ค
String& String::operator=(const char* s)
{
    delete[] str;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    return *this;
}

// const๊ฐ€ ์•„๋‹Œ String์— ์ฝ๊ธฐ-์“ฐ๊ธฐ ๊ฐœ๋ณ„ ๋ฌธ์ž ์ ‘๊ทผ
char& String::operator[](int i)
{
    return str[i];
}

// const String์— ์ฝ๊ธฐ ์ „์šฉ ๊ฐœ๋ณ„ ๋ฌธ์ž ์ ‘๊ทผ
const char& String::operator[](int i) const
{
    return str[i];
}

// ์˜ค๋ฒ„๋กœ๋“œ ์—ฐ์‚ฐ์ž ํ”„๋ Œ๋“œ

bool operator<(const String& st1, const String& st2)
{
    return (std::strcmp(st1.str, st2.str) < 0);
}

bool operator>(const String& st1, const String& st2)
{
    return st1.str > st2.str;
}

bool operator==(const String& st1, const String& st2)
{
    return (std::strcmp(st1.str, st2.str) == 0);
}

ostream& operator<<(ostream& os, const String& st)
{
    os << st.str;
    return os;
}

istream& operator>>(istream& is, String& st)
{
    char temp[String::CINLIM];
    is.get(temp, String::CINLIM);

    if (is)
        st = temp;
    while (is && is.get() != '\n')
        continue;
    return is;
}

void String::stringlow() //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
{
    for (int i = 0; i < len; i++)
            str[i] = tolower(str[i]);
}
void String::stringup() //ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต 2๋ฒˆ
{
    for (int i = 0; i < len; i++)
            str[i] = toupper(str[i]);
}

String operator+(const String& s1, const String& s2)
{
    int slen = s1.len + s2.len + 1;
    char *temp = new char[slen];
    strcpy(temp, s1.str);
    strcat(temp, s2.str);
    String s(temp);
    return s;
}

int String::has(char a)
{
    int count = 0;
    for (int i = 0; i < len; i++)
        if (a == str[i])
            count++;
    return count;
}

//usestring2.cpp
#include<iostream>
#include"string2.h"
using namespace std;
int main()
{
	String s1(" and I am a C++ student.");
	String s2 = "์˜๋ฌธ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
	String s3;
	cout << s2;
	cin >> s3;
	s2 = "My name is " + s3;
	cout << s2 << ".\n";
	s2 = s2 + s1;
	s2.stringup();
	cout << "๋‹ค์Œ ๋ฌธ์ž์—ด์—๋Š”\n" << s2 << "\n๋ฌธ์ž 'A'๊ฐ€ "
		<< s2.has('A') << "๊ฐœ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.\n";
	s1 = "red";
	String rgb[3] = { String(s1),String("green"),String("blue") };
	cout << "๋น›์˜ ์‚ผ์›์ƒ‰์˜ ์ด๋ฆ„์„ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
	String ans;
	bool success = false;
	while (cin >> ans)
	{
		ans.stringlow();
		for (int i = 0; i < 3; i++)
		{
			if (ans == rgb[i])
			{
				cout << "๋งž์•˜์Šต๋‹ˆ๋‹ค!\n";
				success = true;
				break;
			}
		}
		if (success)
			break;
		else
			cout << "๋‹ค์‹œ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
	}
	cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n";
	return 0;
}

 

 

3๋ฒˆ

// stock3.h -- ๊ฐœ์ •๋œ ํ—ค๋” ํŒŒ์ผ
#ifndef STOCK3_H_
#define STOCK3_H_
#include<iostream>
#include <cstring>

class Stock
{
private:
    char * company;
    int shares;
    double share_val;
    double total_val;
    void set_tot() { total_val = shares * share_val; }
public:
    Stock();        // ๋””ํดํŠธ ์ƒ์„ฑ์ž
    Stock(const char * co, long n = 0, double pr = 0.0);
    ~Stock();       // ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๋Š” ํŒŒ๊ดด์ž
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    friend std::ostream& operator<<(std::ostream& os, const Stock& c);
    const Stock& topval(const Stock& s) const;
};

#endif

// stock3.cpp -- ๊ฐœ์ •ํŒ
#include "stock3.h"

// ์ƒ์„ฑ์ž๋“ค
Stock::Stock()        // ๋””ํดํŠธ ์ƒ์„ฑ์ž
{
    strcpy(company, "no name");
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}

Stock::Stock(const char* co, long n, double pr)
{
    company = new char[strlen(co) + 1];
    strcpy(company, co);

    if (n < 0)
    {
        std::cout << "์ฃผ์‹ ์ˆ˜๋Š” ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, "
            << company << " shares๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}

// ํด๋ž˜์Šค ํŒŒ๊ดด์ž
Stock::~Stock()        // ๋ง ์—†๋Š” ํด๋ž˜์Šค ํŒŒ๊ดด์ž
{
    delete[] company;
}

// ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋“ค
void Stock::buy(long num, double price)
{
    if (num < 0)
    {
        std::cout << "๋งค์ž… ์ฃผ์‹ ์ˆ˜๋Š” ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, "
            << "๊ฑฐ๋ž˜๊ฐ€ ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(long num, double price)
{
    using std::cout;
    if (num < 0)
    {
        cout << "๋งค๋„ ์ฃผ์‹ ์ˆ˜๋Š” ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, "
            << "๊ฑฐ๋ž˜๊ฐ€ ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n";
    }
    else if (num > shares)
    {
        cout << "๋ณด์œ  ์ฃผ์‹๋ณด๋‹ค ๋งŽ์€ ์ฃผ์‹์„ ๋งค๋„ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, "
            << "๊ฑฐ๋ž˜๊ฐ€ ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val = price;
    set_tot();
}

std::ostream& operator<<(std::ostream& os, const Stock& c)
{
    using std::ios_base;
    // ํฌ๋งท์„ #.###์— ์„ธํŒ…ํ•œ๋‹ค
    ios_base::fmtflags orig =
        os.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = os.precision(3);

    os << "ํšŒ์‚ฌ๋ช…: " << c.company
        << "  ์ฃผ์‹ ์ˆ˜: " << c.shares << '\n';
    os << "  ์ฃผ๊ฐ€: $" << c.share_val;
    // ํฌ๋งท์„ #.##์— ์„ธํŒ…ํ•œ๋‹ค
    os.precision(2);
    os << "  ์ฃผ์‹ ์ด ๊ฐ€์น˜: $" << c.total_val << '\n';

    // ์›๋ณธ ํฌ๋งท์„ ์ €์žฅํ•œ๋‹ค
    os.setf(orig, ios_base::floatfield);
    os.precision(prec);

    return os;
}



const Stock& Stock::topval(const Stock& s) const
{
    if (s.total_val > total_val)
        return s;
    else
        return *this;
}

// usestok3.cpp -- Stock ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
// stock3.cpp์™€ ํ•จ๊ป˜ ์ปดํŒŒ์ผํ•œ๋‹ค

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

const int STKS = 4;
int main()
{
    // ์ดˆ๊ธฐํ™”๋œ ๊ฐ์ฒด๋“ค์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค
    Stock stocks[STKS] = {
        Stock("NanoSmart", 12, 20.0),
        Stock("Boffo Objects", 200, 2.0),
        Stock("Monolithic Obelisks", 130, 3.25),
        Stock("Fleep Enterprises", 60, 6.5)
    };

    std::cout << "๋ณด์œ  ์ฃผ์‹ ๋ฆฌ์ŠคํŠธ:\n";
    int st;
    for (st = 0; st < STKS; st++)
        std::cout << stocks[st];
    // ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ํฌ์ธํ„ฐ ์ง€์ •
    const Stock* top = &stocks[0];
    for (st = 1; st < STKS; st++)
        top = &top->topval(stocks[st]);
    // ๊ฐ€์žฅ ๊ฐ€์น˜ ์žˆ๋Š” ์ฃผ์‹์˜ ์ตœ๊ณ ์น˜
    std::cout << "\n์ตœ๊ณ  ๊ฐ€์น˜์˜ ์ฃผ์‹:\n";
    std::cout << *top;
    return 0;
}

 

 

4๋ฒˆ

//stack1.h
#ifndef STACK1_H_
#define STACK1_H_
typedef unsigned long Item;

class Stack
{
private:
	enum{MAX=10}; //ํด๋ž˜์Šค์šฉ ์ƒ์ˆ˜
	Item* pitems; //์Šคํƒ ํ•ญ๋ชฉ๋“ค์„ ์ €์žฅ
	int size; //์Šคํƒ์— ๋“ค์–ด ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜
	int top; //์Šคํƒ์˜ ๊ผญ๋Œ€๊ธฐ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค
public:
	Stack(int n = MAX); //n๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ์Šคํƒ์„ ์ƒ์„ฑ
	Stack(const Stack& st);
	~Stack();
	bool isempty() const;
	bool isfull() const;
	//push() ๋Š” ์Šคํƒ์ด ๊ฐ€๋“ ์ฐจ ์žˆ์œผ๋ฉด false๋ฅผ ์•„๋‹ˆ๋ฉด true๋ฅผ ๋ฆฌํ„ด
	bool push(const Item& item); // ์Šคํƒ์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€
	//pop()์€ ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด false๋ฅผ, ์•„๋‹ˆ๋ฉด true๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค
	bool pop(Item& item); //๊ผญ๋Œ€๊ธฐ ํ•ญ๋ชฉ์„ ๊บผ๋‚ด item์— ๋„ฃ๋Š”๋‹ค
	Stack& operator=(const Stack& st);
};
#endif

//stack1.cpp
#include "stack1.h"
Stack::Stack(int n)
{
	pitems = new Item[n];
	size = n;
	top = 0;
}
Stack::Stack(const Stack& st)
{
	delete[]pitems;
	pitems = new Item[st.size];
	for (int i = 0; i < st.size; i++)
		pitems[i] = st.pitems[i];
	size = st.size;
	top = st.top;
}
Stack::~Stack()
{
	delete[]pitems;
}
bool Stack::isempty() const
{
	return top == 0;
}
bool Stack::isfull() const
{
	return top == size;
}
bool Stack::push(const Item& item)
{
	if (top < size)
	{
		pitems[top++] = item;
		return true;
	}
	else
		return false;
}
bool Stack::pop(Item& item)
{
	if (top > 0)
	{
		item = pitems[--top];
		return true;
	}
	else
		return false;
}
Stack& Stack::operator=(const Stack& st)
{
	if (this == &st)
		return *this;

	delete[]pitems;
	pitems = new Item[st.size];
	for (int i = 0; i < st.size; i++)
		pitems[i] = st.pitems[i];
	size = st.size;
	top = st.top;
	return *this;
}


//usestack1.h
#include<iostream>
#include<cctype>
#include"stack1.h"

int main()
{
    using namespace std;
    Stack st; // ๋น„์–ด ์žˆ๋Š” ์Šคํƒ์„ ์ƒ์„ฑํ•œ๋‹ค
    Stack st2;
    char ch;
    unsigned long po;
    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 >> po;
            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 << "์ฃผ๋ฌธ์„œ๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.\n";
        }
                break;
        }
        cout << "์ฃผ๋ฌธ์„œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด A, ์ฃผ๋ฌธ์„œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด P,\n"
            << "์ข…๋ฃŒํ•˜๋ ค๋ฉด Q๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค.\n";
    }

    st2 = st;
    for (int i = 0; i < 10; i++)
    {
        if (st.isempty())
            cout << "์Šคํƒ์ด ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.\n";
        else {
            st.pop(po);
            cout << "#" << po << "์ฃผ๋ฌธ์„œ๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.\n";
        }
    }

    return 0;
}

 

 

5๋ฒˆ

// bank2.cpp -- Queue ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
// queue.cpp์™€ ํ•จ๊ป˜ ์ปดํŒŒ์ผํ•œ๋‹ค
#include <iostream>
#include <cstdlib> // rand()์™€ srand()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include <ctime>   // time()์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include "queue.h"
const int MIN_PER_HR = 60;

bool newcustomer(double x); // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋Š”๊ฐ€?

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios_base;
    // ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์ค€๋น„ํ•œ๋‹ค
    std::srand(std::time(0));    //  rand()์˜ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”

    cout << "์‚ฌ๋ก€ ์—ฐ๊ตฌ: ํžˆ์„œ ์€ํ–‰์˜ ATM\n";
    cout << "ํ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int qs;
    cin >> qs;

    cout << "์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int hours;              // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜
    cin >> hours;
    // ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ 1๋ถ„์— 1์ฃผ๊ธฐ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค
    long cyclelimit = MIN_PER_HR * hours; // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์ฃผ๊ธฐ ์ˆ˜

    double perhour;         //  ์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜
    double min_per_cust;    //  ํ‰๊ท  ๊ณ ๊ฐ ๋„์ฐฉ ๊ฐ„๊ฒฉ(๋ถ„ ๋‹จ์œ„)

    Item temp;              //  ์ƒˆ ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ
    long turnaways;     //  ํ๊ฐ€ ๊ฐ€๋“ ์ฐจ์„œ ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜
    long customers;     //  ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜
    long served;        //  ์‹œ๋ฎฌ๋ ˆ์ด์…˜์—์„œ ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜
    long sum_line;      //  ํ์˜ ๋ˆ„์  ๊ธธ์ด
    int wait_time;      //  ATM์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ์‹œ๊ฐ„
    long line_wait;     //  ๊ณ ๊ฐ๋“ค์ด ์ค„์„ ์„œ์„œ ๋Œ€๊ธฐํ•œ ๋ˆ„์  ์‹œ๊ฐ„
    double line_wait_aver = 0;

    for (perhour = 1; line_wait_aver < 1; perhour++)
    {
        Queue line(qs);         // line ํ์—๋Š” ์ตœ๋Œ€ qs๋ช…๊นŒ์ง€ ๋Œ€๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค
        turnaways = 0;
        customers = 0;
        served = 0;
        sum_line = 0;
        wait_time = 0;
        line_wait = 0;
        line_wait_aver = 0;
        min_per_cust = MIN_PER_HR / perhour;
        for (int cycle = 0; cycle < cyclelimit; cycle++)
        {
            if (newcustomer(min_per_cust))  // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋‹ค
            {
                if (line.isfull())
                    turnaways++;
                else
                {
                    customers++;
                    temp.set(cycle);    // cycle์ด ๋„์ฐฉ ์‹œ๊ฐ„์ด ๋œ๋‹ค
                    line.enqueue(temp); // ํ์— ์ƒˆ ๊ณ ๊ฐ์„ ์ถ”๊ฐ€ํ•œ๋‹ค
                }
            }
            if (wait_time <= 0 && !line.isempty())
            {
                line.dequeue(temp);      // ๋‹ค์Œ ๊ณ ๊ฐ์„ ๋ฐ›๋Š”๋‹ค
                wait_time = temp.ptime(); // wait_time์„ ์„ค์ •ํ•œ๋‹ค
                line_wait += cycle - temp.when();
                served++;
            }
            if (wait_time > 0)
                wait_time--;
            sum_line += line.queuecount();
        }
        line_wait_aver = (double)line_wait / served;
        // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
        if (customers > 0)
        {
            cout << "perhour = " << perhour << '\n';
            cout << " ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜: " << customers << endl;
            cout << " ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜: " << served << endl;
            cout << " ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜: " << turnaways << endl;
            cout << "          ํ‰๊ท  ํ์˜ ๊ธธ์ด: ";
            cout.precision(2);
            cout.setf(ios_base::fixed, ios_base::floatfield);
            cout.setf(ios_base::showpoint);
            cout << (double)sum_line / cyclelimit << endl;
            cout << "       ํ‰๊ท  ๋Œ€๊ธฐ ์‹œ๊ฐ„: "
                << (double)line_wait_aver << "๋ถ„\n\n";
        }
        else
            cout << "๊ณ ๊ฐ์ด ํ•œ ๋ช…๋„ ์—†์Šต๋‹ˆ๋‹ค!\n";
    }
    cout << "์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜๊ฐ€ " << perhour-1 << "์ด์ƒ์ด ๋˜๋ฉด ํ‰๊ท  ๋Œ€๊ธฐ์‹œ๊ฐ„์ด 1๋ถ„์„ ๋„˜๋Š”๋‹ค.\n";
    return 0;
}

//  x๋Š” ๊ณ ๊ฐ ๊ฐ„์˜ ํ‰๊ท  ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์ด๋‹ค(๋ถ„ ๋‹จ์œ„)
//  ์ด ์‹œ๊ฐ„ ๋‚ด์— ๊ณ ๊ฐ์ด ๋„์ฐฉํ•˜๋ฉด ๋ฆฌํ„ด๊ฐ’์€ true์ด๋‹ค
bool newcustomer(double x)
{
    return (std::rand() * x / RAND_MAX < 1);
}

 

 

6๋ฒˆ

// bank3.cpp -- Queue ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
// queue.cpp์™€ ํ•จ๊ป˜ ์ปดํŒŒ์ผํ•œ๋‹ค
#include <iostream>
#include <cstdlib> // rand()์™€ srand()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include <ctime>   // time()์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include "queue.h"
const int MIN_PER_HR = 60;
Item temp;              //  ์ƒˆ ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ
struct information
{
    long customers;     //  ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜
    long served;        //  ์‹œ๋ฎฌ๋ ˆ์ด์…˜์—์„œ ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜
    long sum_line;      //  ํ์˜ ๋ˆ„์  ๊ธธ์ด
    int wait_time;      //  ATM์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ์‹œ๊ฐ„
    long line_wait;     //  ๊ณ ๊ฐ๋“ค์ด ์ค„์„ ์„œ์„œ ๋Œ€๊ธฐํ•œ ๋ˆ„์  ์‹œ๊ฐ„
};

bool newcustomer(double x); // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋Š”๊ฐ€?
void choose_line(Queue& line, information& info, int cycle, int ne);



int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios_base;
    // ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์ค€๋น„ํ•œ๋‹ค
    std::srand(std::time(0));    //  rand()์˜ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”

    cout << "์‚ฌ๋ก€ ์—ฐ๊ตฌ: ํžˆ์„œ ์€ํ–‰์˜ ATM\n";
    cout << "ํ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int qs;
    cin >> qs;

    cout << "์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int hours;              // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜
    cin >> hours;
    // ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ 1๋ถ„์— 1์ฃผ๊ธฐ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค
    long cyclelimit = MIN_PER_HR * hours; // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์ฃผ๊ธฐ ์ˆ˜

    double perhour;         //  ์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜
    double line_wait_aver = 0;
    double min_per_cust;    //  ํ‰๊ท  ๊ณ ๊ฐ ๋„์ฐฉ ๊ฐ„๊ฒฉ(๋ถ„ ๋‹จ์œ„)
    int newcust;
    long turnaways;     //  ํ๊ฐ€ ๊ฐ€๋“ ์ฐจ์„œ ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜

    for (perhour = 1; line_wait_aver < 1; perhour++)
    {
        Queue line(qs);         // line ํ์—๋Š” ์ตœ๋Œ€ qs๋ช…๊นŒ์ง€ ๋Œ€๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค
        Queue line2(qs);

        information info = {};
        information info2 = {};
        min_per_cust = MIN_PER_HR / perhour;
        for (int cycle = 0; cycle < cyclelimit; cycle++)
        {
            if (newcustomer(min_per_cust))  // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋‹ค
            {
                if (line.isfull()&&line2.isfull())
                    turnaways++;
                else
                {
                    temp.set(cycle);
                    if (line.queuecount() < line2.queuecount())
                    {
                        choose_line(line, info, cycle, 1);
                        choose_line(line2, info2, cycle, 0);
                    }
                    else
                    {
                        choose_line(line, info, cycle, 0);
                        choose_line(line2, info2, cycle, 1);
                    }
                }
            }
            else
            {
                choose_line(line, info, cycle, 0);
                choose_line(line2, info2, cycle, 0);
            }
        }
        line_wait_aver = (double)((info.line_wait + info2.line_wait)/2)
            / (info.served + info2.served);
        // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
        if (info.customers+info2.customers > 0)
        {
            cout << "perhour = " << (int)perhour << '\n';
            cout << " ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜: " << info.customers + info2.customers << endl;
            cout << " ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜: " << info.served + info2.served << endl;
            cout << " ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜: " << turnaways << endl;
            cout << "          ํ‰๊ท  ํ์˜ ๊ธธ์ด: ";
            cout.precision(2);
            cout.setf(ios_base::fixed, ios_base::floatfield);
            cout.setf(ios_base::showpoint);
            cout << ((double)info.sum_line / cyclelimit +
                (double)info2.sum_line / cyclelimit) / 2 << endl;
            cout << "       ํ‰๊ท  ๋Œ€๊ธฐ ์‹œ๊ฐ„: "
                << (double)line_wait_aver << "๋ถ„\n\n";
        }
        else
            cout << "๊ณ ๊ฐ์ด ํ•œ ๋ช…๋„ ์—†์Šต๋‹ˆ๋‹ค!\n";
    }
    cout << "์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜๊ฐ€ " << perhour - 1
        << "์ด์ƒ์ด ๋˜๋ฉด ํ‰๊ท  ๋Œ€๊ธฐ์‹œ๊ฐ„์ด 1๋ถ„์„ ๋„˜๋Š”๋‹ค.\n";
    return 0;
}

//  x๋Š” ๊ณ ๊ฐ ๊ฐ„์˜ ํ‰๊ท  ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์ด๋‹ค(๋ถ„ ๋‹จ์œ„)
//  ์ด ์‹œ๊ฐ„ ๋‚ด์— ๊ณ ๊ฐ์ด ๋„์ฐฉํ•˜๋ฉด ๋ฆฌํ„ด๊ฐ’์€ true์ด๋‹ค
bool newcustomer(double x)
{
    return (std::rand() * x / RAND_MAX < 1);
}

void choose_line(Queue& line, information& info, int cycle, int ne)
{
    if (ne == 1)  // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋‹ค
    {
    
        {
            info.customers++;
            line.enqueue(temp); // ํ์— ์ƒˆ ๊ณ ๊ฐ์„ ์ถ”๊ฐ€ํ•œ๋‹ค
        }
    }
    if (info.wait_time <= 0 && !line.isempty())
    {
        line.dequeue(temp);      // ๋‹ค์Œ ๊ณ ๊ฐ์„ ๋ฐ›๋Š”๋‹ค
        info.wait_time = temp.ptime(); // wait_time์„ ์„ค์ •ํ•œ๋‹ค
        info.line_wait += cycle - temp.when();
        if (cycle < temp.when())
        {
            std::cout << cycle << " " << temp.when()<<'\n';
        }

        info.served++;
    }
    if (info.wait_time > 0)
        info.wait_time--;
    info.sum_line += line.queuecount();
}
728x90
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    '๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 14 C++ ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ p.1103~ 1๋ฒˆ~5๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 13 ํด๋ž˜์Šค์˜ ์ƒ์† p.989~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 11 ํด๋ž˜์Šค์˜ ํ™œ์šฉ p.787~ 1๋ฒˆ~7๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 10 ๊ฐ์ฒด์™€ ํด๋ž˜์Šค p.705~ 1๋ฒˆ~8๋ฒˆ
    hugDog
    hugDog
    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ ์ค‘์ธ ํ•™์ƒ์ž…๋‹ˆ๋‹ค!

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

    ๊ฐœ์ธ์ •๋ณด

    • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
    • ํฌ๋Ÿผ
    • ๋กœ๊ทธ์ธ

    ๋‹จ์ถ•ํ‚ค

    ๋‚ด ๋ธ”๋กœ๊ทธ

    ๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
    Q
    Q
    ์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
    W
    W

    ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

    ๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
    E
    E
    ๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
    C
    C

    ๋ชจ๋“  ์˜์—ญ

    ์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
    S
    S
    ๋งจ ์œ„๋กœ ์ด๋™
    T
    T
    ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
    H
    H
    ๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
    Shift + /
    โ‡ง + /

    * ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.