728x90
개인적으로 C++을 공부할 때 작성해놓았던 코드들을 찾았다.
혼자 책을 사서 독학하던 시절에, 그리고 학부생 시절에 복습하면서 했던 문제들이라서
어떤 문제들은 깔끔히 잘 정리되어 있고, 어떤 문제들은 허접한 버그가 있을 수도 있다.
확인은 해뒀지만, 확인하지 못하거나 고려해야 할 버그, 오탈자 등은 댓글을 남겨주시라.
그간 공부한 것들을 정리하는 블로그이기 때문에 올려놓는다.
지금까지는 실습문제 문제 내용까지 적었지만, 귀찮기도 하고, 굳이 입력할 필요성은 느끼지 않아서 적지 않았다. 차후 게시물을 대대적으로 수정할 때면 추가될지도 모른다.
실습문제 1.
#include <iostream> #include <string> using namespace std; class Book { string title; int price, pages; public: Book(string title = "", int price = 0, int pages = 0) { this->title = title, this->price = price, this->pages = pages; } void show() { cout << title << ' ' << price << "¿ø " << pages << " ÆäÀÌÁö" << endl; } string getTitle() { return title; } // (1) /* void operator+=(int n) { this->price += n; } void operator-=(int n) { this->price -= n; } */ // (2) friend Book operator+=(Book& b, int n); friend Book operator-=(Book& b, int n); }; Book operator+=(Book& b, int n) { b.price += n; return b; } Book operator-=(Book& b, int n) { b.price -= n; return b; } int main(void) { Book a("ûÃá", 2000, 300), b("¹Ì·¡", 30000, 500); a += 500; b -= 500; a.show(); b.show(); }
실습문제 2.
#include <iostream> #include <string> using namespace std; class Book { string title; int price, pages; public: Book(string title = "", int price = 0, int pages = 0) { this->title = title, this->price = price, this->pages = pages; } void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } // (1) /* bool operator==(int p) { return this->price == p; } bool operator==(string t) { return this->title == t; } bool operator==(Book b) { return ((this->title == b.title) && (this->price == b.price) && (this->pages == b.pages)); } */ // (2) friend bool operator==(Book& b, int p); friend bool operator==(Book& b, string t); friend bool operator==(Book& b, Book other); }; bool operator==(Book& b, int p) { return b.price == p; } bool operator==(Book& b, string t) { return b.title == t; } bool operator==(Book& b, Book other) { return ((b.title == other.title) && (b.price == other.price) && (b.pages == other.pages)); } int main(void) { Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500); if (a == 30000) cout << "정가 30000원" << endl; if (a == "명품 C++") cout << "명품 C++ 입니다." << endl; if (a == b) cout << "두 책이 같은 책입니다." << endl; }
실습문제 3.
#include <iostream> #include <string> using namespace std; class Book { string title; int price, pages; public: Book(string title = "", int price = 0, int pages = 0) { this->title = title, this->price = price, this->pages = pages; } void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } bool operator!() { return !(this->price); } }; int main(void) { Book book("벼룩시장", 0, 50); if (!book) cout << "공짜다" << endl; }
실습문제 4.
#include <iostream> #include <string> using namespace std; class Book { string title; int price, pages; public: Book(string title = "", int price = 0, int pages = 0) { this->title = title, this->price = price, this->pages = pages; } void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } friend bool operator<(string s, Book& b); }; bool operator<(string s, Book &b) { return s < b.title; } int main(void) { Book a("청춘", 20000, 300); string b; cout << "책 이름을 입력하세요>>"; getline(cin, b); if (b < a) cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl; }
실습문제 5.
#include <iostream> using namespace std; class Matrix { int a1, a2; int b1, b2; public: Matrix() { a1 = a2 = b1 = b2 = 0; } Matrix(int a1, int a2, int b1, int b2) { this->a1 = a1, this->a2 = a2; this->b1 = b1, this->b2 = b2; } void show() { cout << "Matrix = { " << a1 << ' ' << a2 << ' ' << b1 << ' ' << b2 << " }\n"; } //(1) /* Matrix operator+(Matrix m) { Matrix tmp; tmp.a1 = a1 + m.a1, tmp.a2 = a2 + m.a2; tmp.b1 = b1 + m.b1, tmp.b2 = b2 + m.b2; return tmp; } void operator+=(Matrix m) { a1 += m.a1, a2 += m.a2; b1 += m.b1, b2 += m.b2; } bool operator==(Matrix m) { return ((a1 == m.a1) && (a2 == m.a2) && (b1 == m.b1) && (b2 == m.b2)); } */ // (2) friend Matrix operator+(Matrix& m1, Matrix m2); friend void operator+=(Matrix& m1, Matrix m2); friend bool operator==(Matrix& m1, Matrix m2); }; Matrix operator+(Matrix& m1, Matrix m2) { Matrix tmp; tmp.a1 = m1.a1 + m2.a1, tmp.a2 = m1.a2 + m2.a2; tmp.b1 = m1.b1 + m2.b1, tmp.b2 = m1.b2 + m2.b2; return tmp; } void operator+=(Matrix& m1, Matrix m2) { m1.a1 += m2.a1, m1.a2 += m2.a2; m1.b1 += m2.b1, m1.b2 += m2.b2; } bool operator==(Matrix& m1, Matrix m2) { return ((m1.a1 == m2.a1) && (m1.a2 == m2.a2) && (m1.b1 == m2.b1) && (m1.b2 == m2.b2)); } int main(void) { Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c; c = a + b; a += b; a.show(), b.show(), c.show(); if (a == c) cout << "a and c are the same" << endl; }
실습문제 6.
#include <iostream> using namespace std; class Matrix { int a1, a2; int b1, b2; public: Matrix() { a1 = a2 = b1 = b2 = 0; } Matrix(int a1, int a2, int b1, int b2) { this->a1 = a1, this->a2 = a2; this->b1 = b1, this->b2 = b2; } void show() { cout << "Matrix = { " << a1 << ' ' << a2 << ' ' << b1 << ' ' << b2 << " }\n"; } // (1) /* void operator>>(int* arr) { arr[0] = a1, arr[1] = a2, arr[2] = b1, arr[3] = b2; } void operator<<(int *arr) { a1 = arr[0], a2 = arr[1], b1 = arr[2], b2 = arr[3]; } */ // (2) friend void operator>>(Matrix& m, int* arr); friend void operator<<(Matrix& m, int* arr); }; void operator>>(Matrix& m, int* arr) { arr[0] = m.a1, arr[1] = m.a2, arr[2] = m.b1, arr[3] = m.b2; } void operator<<(Matrix& m, int* arr) { m.a1 = arr[0], m.a2 = arr[1], m.b1 = arr[2], m.b2 = arr[3]; } int main(void) { Matrix a(4, 3, 2, 1), b; int x[4], y[4] = { 1,2,3,4 }; a >> x; b << y; for (int i = 0; i < 4; i++) cout << x[i] << ' '; cout << endl; b.show(); }
실습문제 7.
#include <iostream> using namespace std; class Circle { int radius; public: Circle(int radius = 0) { this->radius = radius; } void show() { cout << "radius = " << radius << "ÀÎ ¿ø" << endl; } friend Circle operator++(Circle& c); friend Circle operator++(Circle& c, int n); }; Circle operator++(Circle& c) { c.radius++; return c; } Circle operator++(Circle& c, int n) { Circle tmp = c; c.radius++; return tmp; } int main(void) { Circle a(5), b(4); ++a; b = a++; a.show(); b.show(); }
실습문제 8.
#include <iostream> using namespace std; class Circle { int radius; public: Circle(int radius = 0) { this->radius = radius; } void show() { cout << "radius = " << radius << "ÀÎ ¿ø" << endl; } friend Circle operator+(int n, Circle& c); friend Circle operator++(Circle& c); friend Circle operator++(Circle& c, int n); }; Circle operator+(int n, Circle& c) { Circle tmp; tmp = c.radius + n; return tmp; } Circle operator++(Circle& c) { c.radius++; return c; } Circle operator++(Circle& c, int n) { Circle tmp = c; c.radius++; return tmp; } int main(void) { Circle a(5), b(4); b = 1 + a; a.show(); b.show(); }
실습문제 9.
#include <iostream> using namespace std; class Statistics { int* arr; int idx; int size; public: Statistics() { arr = new int[5], idx = 0, size = 5; } ~Statistics() { delete[] arr; } bool operator!() { return !(this->idx); } Statistics& operator<<(int n) { if (idx >= size) { int* tmp = new int[size * 2]; for (int i = 0; i < size; i++) tmp[i] = arr[i]; delete[] arr; arr = tmp, size *= 2; } arr[idx] = n; idx++; return *this; } void operator~() { for (int i = 0; i < idx; i++) cout << arr[i] << ' '; cout << endl; } void operator>>(int& n) { int sum = 0; for (int i = 0; i < idx; i++) sum += arr[i]; n = sum / idx; } }; int main(void) { Statistics stat; if (!stat) cout << "현재 통계 데이타가 없습니다." << endl; int x[5]; cout << "5 개의 정수를 입력하라>>"; for (int i = 0; i < 5; i++) cin >> x[i]; for (int i = 0; i < 5; i++) stat << x[i]; stat << 100 << 200; ~stat; int avg; stat >> avg; cout << "avg=" << avg << endl; }
실습문제 10.
#include <iostream> using namespace std; class Stack { int* arr; int tos; int size; public: Stack() { arr = new int[10], size = 10, tos = -1; } ~Stack() { delete[] arr; } bool operator!() { return (tos < 0); } void operator>>(int& n) { if (tos < 0) cout << "stack is empty" << endl; else n = arr[tos--]; } Stack& operator<<(int n) { if (tos >= size) { int* tmp = new int[size * 2]; for (int i = 0; i < size; i++) tmp[i] = arr[i]; delete[] arr; arr = tmp, size *= 2; } arr[++tos] = n; return *this; } }; int main(void) { Stack stack; stack << 3 << 5 << 10; while (1) { if (!stack) break; int x; stack >> x; cout << x << ' '; } cout << endl; }
728x90
'Programming Language > C,C++' 카테고리의 다른 글
C++] 명품 C++ 프로그래밍 9장 실습문제 (3) | 2017.11.09 |
---|---|
C++] 명품 C++ 프로그래밍 8장 실습문제 (0) | 2017.11.08 |
C++] 명품 C++ 프로그래밍 6장 실습문제 (0) | 2017.10.28 |
C++] 명품 C++ 프로그래밍 5장 실습문제 (2) | 2017.10.27 |
C++] 명품 C++ 프로그래밍 4장 실습문제 (0) | 2017.10.26 |