Search
Duplicate
🔢

10장. 수업 코드

생성일
2022/05/09 08:20
태그
C++

10장. 수업 코드

Template - Generic Stack Class

Stack.h
#ifndef __STACK_H #define __STACK_H template <class T> class MyStack { int tos; T data[100]; public: MyStack(); void push(T element); T pop(); }; #endif
C++
복사
Point.h
#ifndef __POINT_H #define __POINT_H #include <iostream> #include <string> using namespace std; class Point { int x, y; public: Point(int x=0, int y=0) { this->x = x; this->y = y; } void print() { cout << '(' << x << ", " << y << ')' << endl; } }; #endif
C++
복사
Stack.cpp
#include "Stack.h" #include "Point.h" #include <iostream> using namespace std; template <class T> MyStack<T>::MyStack() { tos = 0; } template <class T> void MyStack<T>::push(T element) { if (tos >= 100) { cout << "Stack Full!!!" << endl; return; } data[tos++] = element; } template <class T> T MyStack<T>::pop() { if (tos <= 0) { cout << "Stack Empty!!!" << endl; } return data[--tos]; } template class MyStack<int>; template class MyStack<double>; template class MyStack<Point>; template class MyStack<Point*>;
C++
복사
main.cpp
#include <iostream> #include "Stack.h" #include "Point.h" using namespace std; int main() { MyStack<int> iStack; // int 타입의 스택을 만들것이다 iStack.push(10); iStack.push(20); cout << iStack.pop() << ", " << iStack.pop() << endl; MyStack<double> dStack; dStack.push(1.1234); dStack.push(3.14); dStack.push(0.101); cout << dStack.pop() << endl; MyStack<Point> pStack; Point p1(10, 2), p2(40, 70); pStack.push(p1); pStack.pop().print(); pStack.pop().print(); MyStack<Point*> ppStack; ppStack.push(new Point(10, 20)); ppStack.push(new Point(100, 200)); ppStack.pop()->print(); ppStack.pop()->print(); MyStack<Point*> *pppStack; pppStack = new MyStack<Point*>(); pppStack->push(new Point(500, 600)); pppStack->push(new Point(230, 240)); pppStack->pop()->print(); pppStack->pop()->print(); }
C++
복사

Vector 컨테이너 활용 스택

#include <iostream> #include "Stack.h" #include "Point.h" #include <vector> using namespace std; int main() { MyStack<int> iStack; // int 타입의 스택을 만들것이다 iStack.push(10); iStack.push(20); cout << iStack.pop() << ", " << iStack.pop() << endl; MyStack<double> dStack; dStack.push(1.1234); dStack.push(3.14); dStack.push(0.101); cout << dStack.pop() << endl; MyStack<Point> pStack; Point p1(10, 2), p2(40, 70); pStack.push(p1); pStack.pop().print(); pStack.pop().print(); MyStack<Point*> ppStack; ppStack.push(new Point(10, 20)); ppStack.push(new Point(100, 200)); ppStack.pop()->print(); ppStack.pop()->print(); MyStack<Point*> *pppStack; pppStack = new MyStack<Point*>(); pppStack->push(new Point(500, 600)); pppStack->push(new Point(230, 240)); pppStack->pop()->print(); pppStack->pop()->print(); vector<int> intV; intV.push_back(10); intV.push_back(20); intV.push_back(30); cout << intV.at(0) << endl; cout << intV[1] << endl; intV.at(1) = 300; cout << intV[1] << endl; vector<Point*> pointV; for (int i = 0; i < 1000; i++) { pointV.push_back(new Point(i*2, i*3)); } cout << pointV.size() << endl; cout << pointV.capacity() << endl; for (int i = 0; i < pointV.size(); i++) { pointV[i]->print(); } }
C++
복사

iterator를 사용하여 Vector 활용

#include <iostream> #include "Stack.h" #include "Point.h" #include <vector> using namespace std; int main() { MyStack<int> iStack; // int 타입의 스택을 만들것이다 iStack.push(10); iStack.push(20); cout << iStack.pop() << ", " << iStack.pop() << endl; MyStack<double> dStack; dStack.push(1.1234); dStack.push(3.14); dStack.push(0.101); cout << dStack.pop() << endl; MyStack<Point> pStack; Point p1(10, 2), p2(40, 70); pStack.push(p1); pStack.pop().print(); pStack.pop().print(); MyStack<Point*> ppStack; ppStack.push(new Point(10, 20)); ppStack.push(new Point(100, 200)); ppStack.pop()->print(); ppStack.pop()->print(); MyStack<Point*> *pppStack; pppStack = new MyStack<Point*>(); pppStack->push(new Point(500, 600)); pppStack->push(new Point(230, 240)); pppStack->pop()->print(); pppStack->pop()->print(); vector<int> intV; intV.push_back(10); intV.push_back(20); intV.push_back(30); cout << intV.at(0) << endl; cout << intV[1] << endl; intV.at(1) = 300; cout << intV[1] << endl; vector<Point*> pointV; for (int i = 0; i < 1000; i++) { pointV.push_back(new Point(i*2, i*3)); } cout << pointV.size() << endl; cout << pointV.capacity() << endl; for (int i = 0; i < pointV.size(); i++) { pointV[i]->print(); } vector<Point*>::iterator iP; for (iP = pointV.begin(); iP != pointV.end(); iP++) { (*iP)->print(); } iP = pointV.end() - 3; // 끝에서 3번째 출력 (*iP)->print(); pointV.erase(iP); // 지우기 for (iP = pointV.begin()+990; iP != pointV.end(); iP++) { (*iP)->print(); } cout << pointV.size() << endl; (*iP)->print(); pointV.insert(iP-3, new Point(30, 30)); for (iP = pointV.begin()+990; iP != pointV.end(); iP++) { (*iP)->print(); } }
C++
복사

map 예제 - 영한사전

#include <iostream> #include <map> #include <string> using namespace std; int main() { map<string, string> dic; dic.insert(make_pair("love", "사랑")); dic.insert(make_pair("apple", "사과")); dic["cherry"] = "체리"; cout << dic.size() << endl; string eng; while(true) { cout << "Word? "; cin >> eng; if (eng == "exit") break; if (dic.find(eng) == dic.end()) { // 만약 못찾았다면 cout << "Not Found this word" << endl; string newWord; cout << "Mean? "; cin >> newWord; dic[eng] = newWord; } else cout << dic[eng] << endl; } cout << "exit" << endl; }
C++
복사

Vector 와 Algorithm 이용한 정렬

#include <iostream> #include <map> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v; for (int i = 0; i < 5; i++) { int in; cin >> in; v.push_back(in); } // sort(v.begin()+1, v.end()); // 첫번째는 제외하고 sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } map<string, string> dic; dic.insert(make_pair("love", "사랑")); dic.insert(make_pair("apple", "사과")); dic["cherry"] = "체리"; cout << dic.size() << endl; string eng; while(true) { cout << "Word? "; cin >> eng; if (eng == "exit") break; if (dic.find(eng) == dic.end()) { // 만약 못찾았다면 cout << "Not Found this word" << endl; string newWord; cout << "Mean? "; cin >> newWord; dic[eng] = newWord; } else cout << dic[eng] << endl; } cout << "exit" << endl; }
C++
복사
10-14 Auto 들어가는지 안들어가는지 확실히 알기

lambda

#include <iostream> #include <map> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v; for (int i = 0; i < 5; i++) { int in; cin >> in; v.push_back(in); } // sort(v.begin()+1, v.end()); // 첫번째는 제외하고 sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } [](int x, int y)->void { cout << "sum : " << x + y << endl; } (10, 20); map<string, string> dic; dic.insert(make_pair("love", "사랑")); dic.insert(make_pair("apple", "사과")); dic["cherry"] = "체리"; cout << dic.size() << endl; string eng; while(true) { cout << "Word? "; cin >> eng; if (eng == "exit") break; if (dic.find(eng) == dic.end()) { // 만약 못찾았다면 cout << "Not Found this word" << endl; string newWord; cout << "Mean? "; cin >> newWord; dic[eng] = newWord; } else cout << dic[eng] << endl; } cout << "exit" << endl; }
C++
복사
#include <iostream> #include <map> #include <string> #include <vector> #include <algorithm> using namespace std; template<typename F> void callMyFn(F f) { f(1000, 2000); } int main() { vector<int> v; for (int i = 0; i < 5; i++) { int in; cin >> in; v.push_back(in); } // sort(v.begin()+1, v.end()); // 첫번째는 제외하고 sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } [](int x, int y)->void { cout << "sum : " << x + y << endl; } (10, 20); auto myFunction = [](int x, int y) { cout << "합 : " << x + y << endl; }; auto myFunction2 = [](int x, int y) { cout << "차 : " << x - y << endl; }; myFunction(300, 400); callMyFn(myFunction); callMyFn(myFunction2); map<string, string> dic; dic.insert(make_pair("love", "사랑")); dic.insert(make_pair("apple", "사과")); dic["cherry"] = "체리"; cout << dic.size() << endl; string eng; while(true) { cout << "Word? "; cin >> eng; if (eng == "exit") break; if (dic.find(eng) == dic.end()) { // 만약 못찾았다면 cout << "Not Found this word" << endl; string newWord; cout << "Mean? "; cin >> newWord; dic[eng] = newWord; } else cout << dic[eng] << endl; } cout << "exit" << endl; }
C++
복사

for_each()

#include <iostream> #include <map> #include <string> #include <vector> #include <algorithm> using namespace std; template<typename F> void callMyFn(F f) { f(1000, 2000); } void print(int n) { cout << n << " "; } int main() { vector<int> v; for (int i = 0; i < 5; i++) { int in; cin >> in; v.push_back(in); } // sort(v.begin()+1, v.end()); // 첫번째는 제외하고 sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } for (auto it = v.begin(); it != v.end(); it++) { print(*it); } for_each(v.begin(), v.end(), print); [](int x, int y)->void { cout << "sum : " << x + y << endl; } (10, 20); auto myFunction = [](int x, int y) { cout << "합 : " << x + y << endl; }; auto myFunction2 = [](int x, int y) { cout << "차 : " << x - y << endl; }; myFunction(300, 400); callMyFn(myFunction); callMyFn(myFunction2); map<string, string> dic; dic.insert(make_pair("love", "사랑")); dic.insert(make_pair("apple", "사과")); dic["cherry"] = "체리"; cout << dic.size() << endl; string eng; while(true) { cout << "Word? "; cin >> eng; if (eng == "exit") break; if (dic.find(eng) == dic.end()) { // 만약 못찾았다면 cout << "Not Found this word" << endl; string newWord; cout << "Mean? "; cin >> newWord; dic[eng] = newWord; } else cout << dic[eng] << endl; } cout << "exit" << endl; }
C++
복사

실습 문제 2번)

Book.h
#ifndef __BOOK_H #define __BOOK_H #include <string> using namespace std; class Book { string title; string author; int year; public: Book(string title, string author, int year) : title(title), author(author), year(year) {} // 멤버변수, 이름이 똑같아도 구분할 수 있다. void set(string title, string author, int year); string getAuthor() { return author; } int getYear() { return year; } void show(); }; #endif
C++
복사
Book.cpp
#include "Book.h" #include <iostream> void Book::set(string title, string author, int year) { this->title = title; this->author = author; this->year = year; } void Book::show() { cout << year << "년도, " << title << ", " << author << endl; }
C++
복사
BookManager.h
// book을 관리 #ifndef __BOOKMANAGER_H #define __BOOKMANAGER_H #include "Book.h" #include <vector> class BookManager { vector<Book> v; void searchByAuthor(); void searchByYear(); void bookIn(); public: void run(); }; #endif
C++
복사
BookManager.cpp
#include <iostream> #include "BookManager.h" using namespace std; void BookManager::bookIn() { string title, author; int year; cout << "입고할 책을 입력하세요. 년도에 -1을 입력하면 입고를 종료합니다." << endl; while (true) { cout << "년도>> "; cin >> year; if (year == -1) break; cout << "책이름>> "; //cin >> title;-> 띄어쓰기 안됨 cin.ignore(); getline(cin, title); cout << "저자>> "; // cin >> author; getline(cin, author); Book b(title, author, year); v.push_back(b); } cout << "총 입고된 책은 " << v.size() << "권 입니다." << endl; } void BookManager::searchByYear() { cout << "검색할 년도는?"; int year; cin >> year; for (int i = 0; i < v.size(); i++) { Book b = v[i]; if (b.getYear() == year) { b.show(); } } } void BookManager::searchByAuthor() { cout << "검색할 저자는?"; string author; cin.ignore(); // cin의 버퍼를 완전히 비워준다 getline(cin, author); for (int i = 0; i < v.size(); i++) { if (v[i].getAuthor() == author) { v[i].show(); } } } void BookManager::run() { /* bookIn(); searchByAuthor(); searchByYear(); */ bool end = false; while (!end) { cout << "메뉴 (1: 책 추가, 2: 저자 검색, 3: 년도 검색, 0: 종료" << endl; int menu; cin >> menu; if (menu == 0) break; switch(menu) { case 0: cout << "종료"; end = true; break; case 1: bookIn(); break; case 2: searchByAuthor(); break; case 3: searchByYear(); break; default: cout << "메뉴 선택 오류!!" << endl; } } }
C++
복사
main.cpp
#include <iostream> using namespace std; #include "BookManager.h" int main() { BookManager bm; bm.run(); }
C++
복사