본문 바로가기
C++/명품 C++ Programming

[C++/명품 C++ Programming]명품 C++ 10장 연습문제 - 실습 문제

by starfish22 2022. 5. 26.
728x90

1.

#include <iostream>
using namespace std;

template <class T>
T biggest(T *x, int n) {
    if (n <= 0) return 0;

    T big = x[0];
    for (int i = 1; i < n; i++) {
        if (big < x[i]) big = x[i];
    }
    return big;
}

int main() {
    int x[] = {1, 10, 100, 5, 4};
    cout << biggest(x, 5) << endl;
}

2.

#include <iostream>
using namespace std;

template <class T>
bool equalArray(T *x, T *y, int n) {
    for (int i = 0; i < n; i++) {
        if (x[i] != y[i]) return false;
    }
    return true;
}

int main() {
    int x[] = {1, 10, 100, 5, 4};
    int y[] = {1, 10, 100, 5, 4};
    if (equalArray(x, y, 5)) cout << "같다";
    else cout << "다르다";
}

3.

#include <iostream>
using namespace std;

template <class T>
void reverseArray(T* x, int n) {
	T temp;
	for (int i = 0; i < n / 2; i++) {
		temp = x[i];
		x[i] = x[n - i - 1];
		x[n - i - 1] = temp;
	}
}

int main() {
	int x[] = { 1, 10, 100, 5, 4 };
	reverseArray(x, 5);
	for (int i = 0; i < 5; i++) cout << x[i] << ' ';
}

4.

#include <iostream>
using namespace std;

template <class T>
bool search(T index, T* x, int n) {
	if (n <= 0) return false;
	for (int i = 0; i < n; i++) {
		if (x[i] == index) return true;
	}
	return false;
}

int main() {
	int x[] = { 1, 10, 100, 5, 4 };
	if (search(100, x, 5)) cout << "100이 배열 x에 포함되어 있다";
	else cout << "100이 배열 x에 포함되어 있지 않다";
}

5.

template <class T>
T* concat(T* a, int sizea, T* b, int sizeb) {
	T* arr = new T[sizea + sizeb];
	int len = 0;
	for (int i = 0; i < sizea; i++) arr[len++] = a[i];
	for (int i = 0; i < sizeb; i++) arr[len++] = b[i];
	return arr;
}

6.

template <class T>
T* remove(T* src, int sizeSrc, T* minus, int sizeMinus, int& retSize) {
	T* arr = new T[sizeSrc];
	retSize = 0;
	bool b;
	for (int i = 0; i < sizeSrc; i++) {
		b = false;
		for (int j = 0; j < sizeMinus; j++) {
			if (src[i] == minus[j]) {
				b = true;
				break;
			}
		}
		if (b == false) arr[retSize++] = src[i];
	}
	return arr;
}

7.

#include <iostream>
using namespace std;

class Circle {
	int radius;
public:
	Circle(int radius = 1) { this->radius = radius; }
	int getRadius() { return radius; }
};

template <class T>
T bigger(T a, T b) {
	if (a > b) return a;
	else return b;
}

Circle bigger(Circle a, Circle b) {
	if (a.getRadius() > b.getRadius()) return a;
	else return b;
}

int main() {
	int a = 20, b = 50, c;
	c = bigger(a, b);
	cout << "20과 50중 큰 값은 " << c << endl;
	Circle waffle(10), pizza(20), y;
	y = bigger(waffle, pizza);
	cout << "waffle과 pizza 중 큰 것의 반지름은 " << y.getRadius() << endl;
}

8.

#include <iostream>
using namespace std;

class Comparable {
public:
	virtual bool operator>(Comparable& op2) = 0;
	virtual bool operator<(Comparable& op2) = 0;
	virtual bool operator==(Comparable& op2) = 0;
};

class Circle : public Comparable {
	int radius;
public:
	Circle(int radius = 1) { this->radius = radius; }
	int getRadius() { return radius; }
	virtual bool operator>(Comparable& op2) {
		Circle* op = (Circle*)&op2;
		if (this->radius > op->radius) return true;
		else return false;
	}
	virtual bool operator<(Comparable& op2) {
		Circle* op = (Circle*)&op2;
		if (this->radius < op->radius) return true;
		else return false;
	}
	virtual bool operator==(Comparable& op2) {
		Circle* op = (Circle*)&op2;
		if (this->radius == op->radius) return true;
		else return false;
	}
};

template <class T>
T bigger(T a, T b) {
	if (a > b) return a;
	else return b;
}

int main() {
	int a = 20, b = 50, c;
	c = bigger(a, b);
	cout << "20과 50중 큰 값은 " << c << endl;
	Circle waffle(10), pizza(20), y;
	y = bigger(waffle, pizza);
	cout << "waffle과 pizza 중 큰 것의 반지름은 " << y.getRadius() << endl;
}

9.

#include <iostream>
#include <vector>
using namespace std;

int main() {
	vector<int> v;
	int sum = 0, n;
	while (true) {
		cout << "정수를 입력하세요(0을 입력하면 종료)>>";
		cin >> n;
		if (n == 0) break;
		v.push_back(n);
		for (int i = 0; i < v.size(); i++) cout << v[i] << ' ';
		sum += n;
		cout << "\n평균 = " << (double)sum / v.size() << endl;
	}
}

10.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Nation {
	string nation, capital;
public:
	Nation(string nation="", string capital="") {
		this->nation = nation; this->capital = capital;
	}
	void setNation(string nation, string capital) {
		this->nation = nation; this->capital = capital;
	}
	string getNation() { return nation; }
	string getCapital() { return capital; }
};

int main() {
	vector<Nation> v;
	Nation nation[9] = {
		Nation("한국","서울"),Nation("독일","베를린"), Nation("이탈리아","로마"),
		Nation("일본","도쿄"),Nation("중국","베이징"), Nation("러시아","모스크바"),
		Nation("미국","워싱턴"),Nation("프랑스","파리"), Nation("필리핀","마닐라"),
	};
	for (int i = 0; i < 9; i++) {
		v.push_back(nation[i]);
	}

	Nation temp;
	string str, str2;
	bool b;
	int n;

	cout << "***** 나라의 수도 맞추기 게임을 시작합니다. *****" << endl;
	while (true) {
		cout << "정보 입력 : 1, 퀴즈 : 2, 종료 : 3 >> ";
		cin >> n;
		if (n == 1) {
			cout << "현재 " << v.size() << "개의 나라가 입력되어 있습니다." << endl;
			cout << "나라와 수도를 입력하세요(no no 이면 입력끝)" << endl;
			while (true) {
				cout << v.size() + 1 << ">>";
				cin >> str >> str2;
				if (str == "no" && str2 == "no") break;
				b = false;
				for (auto it = v.begin(); it != v.end();it++) {
					if (it->getNation() == str) {
						cout << "already exists !!" << endl;
						b = true;
						break;
					}
				}
				if (b == false) {
					temp.setNation(str, str2);
					v.push_back(temp);
				}
			}
		}
		else if (n == 2) {
			for (auto it = v.begin(); it != v.end(); it++) {
				cout << it->getNation() << "의 수도는?";
				cin >> str;
				if (str == "exit") break;
				if (it->getCapital() == str) cout << "Correct !!" << endl;
				else cout << "NO !!" << endl;
			}
		}
		else if (n == 3) break;
	}
}

11.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Book {
	int year;
	string title, author;
public:
	Book (string title, string author, int year) {
		this->title = title; this->author = author; this->year = year;
	}
	int getYear() { return year; }
	string getTitle() { return title; }
	string getAuthor() { return author; }
};

int main() {
	vector<Book> v;
	string str, str2;
	int year;

	cout << "입고할 책을 입력하세요. 년도에 -1을 입력하면 입고를 종료합니다." << endl;
	while (true) {
		cout << "년도>>";
		cin >> year;
		cin.ignore();
		if (year == -1) break;
		cout << "책이름>>";
		getline(cin, str);
		cout << "저자>>";
		getline(cin, str2);
		Book book(str, str2, year);
		v.push_back(book);
	}
	cout << "총 입고된 책은 " << v.size() << "권입니다." << endl;

	cout << "검색하고자 하는 저자 이름을 입력하세요>>";
	getline(cin, str);
	for (auto it = v.begin();; it++) {
		if (it == v.end()) {
			cout << "찾을 수 없습니다." << endl;
			break;
		}
		if (it->getAuthor() == str) {
			cout << it->getYear() << "년도, " << it->getTitle() << ", " << it->getAuthor() << endl;
			break;
		}
	}

	cout << "검색하고자 하는 년도를 입력하세요>>";
	cin >> year;
	for (auto it = v.begin();; it++) {
		if (it == v.end()) {
			cout << "찾을 수 없습니다." << endl;
			break;
		}
		if (it->getYear() == year) {
			cout << it->getYear() << "년도, " << it->getTitle() << ", " << it->getAuthor() << endl;
			break;
		}
	}
}

12.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

class Word {
	string english, korean;
public:
	void setWord(string eng, string kor) {
		english = eng; korean = kor;
	}
	string getEnglish() { return english; }
	string getKorean() { return korean; }
};

int main() {
	srand((unsigned)time(NULL));
	vector<Word> v;
	Word word;
	string str, str2;
	int n, r;
	int random[4];

	cout << "***** 영어 어휘 테스트를 시작합니다. *****" << endl;
	while (true) {
		cout << "어휘 삽입 : 1, 어휘 테스트 : 2, 프로그램 종료 : 그외 키 >> ";
		cin >> n;
		if (n == 1) {
			cout << "영어 단어에 exit을 입력하면 입력 끝" << endl;
			while(true) {
				cout << "영어 >>";
				cin >> str;
				if (str == "exit") break;
				cout << "한글 >>";
				cin >> str2;
				word.setWord(str, str2);
				v.push_back(word);
			}
		}
		else if (n == 2) {
			cout << "영어 어휘 테스트를 시작합니다. 1~4 외 다른 입력시 종료." << endl;
			while (true) {
				for (int i = 0; i < 4; i++) {
					random[i] = rand() % v.size();
					for (int j = 0; j < i; j++) {
						if (random[j] == random[i]) {
							i--;
							break;
						}
					}
				}
				r = rand() % 4;
				cout << v[random[r]].getEnglish() << "?" << endl;
				for (int i = 0; i < 4; i++) {
					cout << "(" << i + 1 << ") " << v[random[i]].getKorean() << " ";
				}
				cout << ":>";
				cin >> n;
				if (n == r + 1) cout << "Excellent !!" << endl;
				else if (n >= 1 && n <= 4) cout << "NO. !!" << endl;
				else break;
			}
		}
		else break;
		cout << endl;
	}
}

13.

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
	map<string, int> manage;
	string name;
	int n, score;

	cout << "***** 점수관리 프로그램 HIGH SCORE을 시작합니다 *****" << endl;
	while (true) {
		cout << "입력:1, 조회:2, 종료:3 >> ";
		cin >> n;
		if (n == 1) {
			cout << "이름과 점수>> ";
			cin >> name >> score;
			manage[name] = score;
		}
		else if (n == 2) {
			cout << "이름>> ";
			cin >> name;
			if (manage.find(name) == manage.end()) cout << "찾을 수 없습니다" << endl;
			else cout << name << "의 점수는 " << manage[name] << endl;
		}
		else if (n == 3) break;
	}
}

14.

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
	map<string, string> password;
	string name, pw;
	int n;

	cout << "***** 암호 관리 프로그램 WHO를 시작합니다 *****" << endl;
	while (true) {
		cout << "삽입:1, 검사:2, 종료:3>> ";
		cin >> n;
		if (n == 1) {
			cout << "이름 암호>> ";
			cin >> name >> pw;
			password[name] = pw;
		}
		else if (n == 2) {
			cout << "이름? ";
			cin >> name;
			while (true) {
				cout << "암호? ";
				cin >> pw;
				if (password[name] == pw) {
					cout << "통과!!" << endl;
					break;
				}
				else cout << "실패~~" << endl;
			}
		}
		else if (n == 3) {
			cout << "프로그램을 종료합니다..." << endl;
			break;
		}
	}
}

15.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Circle {
	string name;
	int radius;
public:
	Circle(int radius, string name) {
		this->radius = radius; this->name = name;
	}
	double getArea() { return 3.14 * radius * radius; }
	string getName() { return name; }
};

int main() {
	vector<Circle*> v;
	string str;
	int n;

	cout << "원을 삽입하고 삭제하는 프로그램입니다." << endl;
	while (true) {
		cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >> ";
		cin >> n;
		if (n == 1) {
			cout << "생성하고자 하는 원의 반지름과 이름은 >> ";
			cin >> n >> str;
			v.push_back(new Circle(n, str));
		}
		else if (n == 2) {
			cout << "삭제하고자 하는 원의 이름은 >> ";
			cin >> str;
			for (auto it = v.begin(); it != v.end(); it++) {
				if ((*it)->getName() == str) {
					it = v.erase(it);
				}
			}
		}
		else if (n == 3) {
			for (auto it = v.begin(); it != v.end(); it++) {
				cout << (*it)->getName() << endl;
			}
			cout << endl;
		}
		else if (n == 4) break;
	}
}

16.

#include <iostream>
#include <vector>
#include "Shape.h"
#include "Circle.h"
#include "Line.h"
#include "Rect.h"
using namespace std;

int main() {
	vector<Shape*> v;
	int n;

	cout << "그래픽 에디터입니다." << endl;
	while (true) {
		cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >> ";
		cin >> n;
		if (n == 1) {
			cout << "선:1, 원:2, 사각형:3 >> ";
			cin >> n;
			switch (n) {
			case 1: v.push_back(new Line); break;
			case 2: v.push_back(new Circle); break;
			case 3: v.push_back(new Rect); break;
			}
		}
		else if (n == 2) {
			cout << "삭제하고자 하는 도형의 인덱스 >> ";
			cin >> n;
			vector<Shape*>::iterator it = v.begin();
			it += n;
			it = v.erase(it);
		}
		else if (n == 3) {
			for (int i = 0; i < v.size(); i++) {
				cout << i << ": ";
				v[i]->paint();
			}
		}
		else if (n == 4) break;
	}
}
728x90

댓글