Jam's story
명품 c++ programming 실습 문제 10장 본문
1
T big 이부분
main 안에서 배열을 다 int로 선언했던 실수
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
template <class T>
T biggest(T a[], int size) {
T big = a[0];
for (int i = 1; i < size; i++) {
if (a[i] > big) big = a[i];
}
return big;
}
int main() {
int x[] = { 1,2,3,4,5 };
cout << biggest(x,5) << endl;
double y[] = { 1.2 ,3.3, 4.5, 5.5,6.6 };
cout << biggest(y, 5) << endl;
char z[] = { 'a','b','c','d','e' };
cout << biggest(z, 5) << endl;
}
2
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
template <class T>
bool equalArray(T a[], T b[], int size) {
for (int i = 0; i <size; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
int main() {
int x[] = { 1,2,3,4,5 };
int y[] = { 2,3,4,3,2 };
int z[] = { 1,2,3,4,5 };
if (equalArray(x, y, 5))cout << "같다" << endl;
else cout << "다르다" << endl;
if (equalArray(x, z, 5))cout << "같다" << endl;
else cout << "다르다" << endl;
}
3
#include <iostream>
using namespace std;
template <class T>
void reverseArray(T array[], int n) {
int j = n - 1;
for (int i = 0; j > i; i++) {
T tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
}
}
int main() {
int x[] = { 1, 10, 100, 5, 4 };
reverseArray(x, 5);
for (int i = 0; i < 5; i++)
cout << x[i] << ' '; // 4 5 100 10 1이 출력된다.
}
4
#include <iostream>
using namespace std;
template <class T>
bool search(T s, T x[], int size) {
for (int i = 0; i < size; i++) {
if (s == x[i]) return true;
return false;
}
}
int main() {
int x[] = { 1, 10, 100, 5, 4 };
if (search(300, x, 5)) cout << "100이 배열 x에 포함되어 있다"; // 이 cout 실행
else cout << "100이 배열 x에 포함되어 있지 않다";
}
5
#include <iostream>
using namespace std;
template <class T>
T* concat(T a[], int sizea, T b[], int sizeb){
T* Array = new T[sizea + sizeb];
for (int i = 0; i < sizea + sizeb; i++) {
if (i < sizea) {
Array[i] = a[i];
}
else {
Array[i] = b[i-sizea];
}
}
return Array;
}
int main() {
int x[] = { 1,32,2,4,5 };
int y[] = {3,2,2,3 };
int* a = concat(x, 5, y, 4);
for (int i = 0; i < 9; i++)
cout << a[i] << ' ';
};
6 미완성
#include <iostream>
using namespace std;
template <class T>
T* remove( T src[], int sizeSrc, T minus[], int sizeMinus,int& retsize){
bool sw;
T* p = new T[sizeSrc];
for (int i = 0; i < sizeSrc; i++) {
sw = true;
for (int j = 0; j < sizeMinus; j++) {
if (src[i] == minus[j]) {
sw = false;
break;
}
}
}
}
7
Circle과 같은 클래스는 > 연산자가 구현되어 있지 않기 때문에 구체화에 실패하기 때문입니다.
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 1) { this->radius = radius; }
int getRadius() { return radius; }
bool operator > (Circle op2);
};
bool Circle:: operator >(Circle op2) {
if (this->radius > op2.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;
}
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; }
bool operator > (Comparable& op2);
bool operator < (Comparable& op2);
bool operator == (Comparable& op2);
};
bool Circle:: operator > (Comparable& op2) {
Circle* op;
op = (Circle*)&op2;
if (this->radius > op->getRadius()) return true;
else return false;
}
bool Circle:: operator < (Comparable& op2) {
Circle* op;
op = (Circle*)&op2;
if (this->radius < op->getRadius()) return true;
else return false;
}
bool Circle:: operator == (Comparable& op2) {
Circle* op;
op = (Circle*)&op2;
if (this->radius == op->getRadius()) 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;
}
10
명품 C++ programming 실습 문제 10장 10번 (tistory.com)
명품 C++ 10장 10번 :: 건호의 코딩공부 (tistory.com)
명품 C++ programming 실습 문제 10장 10번
문제 : 나라의 수도 맞추기 게임에 vector를 활용해보자. 나라 이름(nation)과 수도(capital) 문자열로 구성된 Nation 클래스를 만들고, vector v;로 생성한 벡터를 이용하여 나라 이름과 수도 이름을 삽입
sobamemil.tistory.com
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Nation {
string nation;
string capital;
public:
Nation(string n = 0, string c = 0) {
nation = n;
capital = c;
}
string getNation() {
return nation;
}
string getCapital() {
return capital;
}
bool nationCompare(string n) { //나라 이름을 비교하는 함수.
if (nation == n)
return true;
return false;
}
};
int main() {
vector<Nation> v;
v.push_back(Nation("대한민국", "서울"));
v.push_back(Nation("중국", "베이징"));
v.push_back(Nation("북한", "평양"));
v.push_back(Nation("미국", "와싱턴"));
v.push_back(Nation("대만", "타이베이"));
v.push_back(Nation("캐나다", "오타와"));
v.push_back(Nation("스위스", "제네바"));
v.push_back(Nation("프랑스", "파리"));
v.push_back(Nation("이집트", "카이로"));
cout << "***** 나라의 수도 맞추기 게임을 시작합니다. *****\n";
while (true) {
int num;
cout << "정보 입력: 1, 퀴즈: 2, 종료 3 >> ";
cin >> num;
switch (num) {
case 1:
cout << "현재 " << v.size() << "개의 나라가 입력되어 있습니다.\n";
cout << "나라와 수도를 입력하세요(no no 이면 입력끝)\n";
for (int i = v.size() + 1;; i++) {
string n, c;
bool b;
cout << i << ">>";
cin >> n >> c;
if (n == "no" && c == "no")
break;
for (int j = 0; j < v.size(); j++)
if (b = v.at(j).nationCompare(n)) {
cout << "already exists !!\n";
i--;
break;
}
if (b)
continue;
v.push_back(Nation(n, c)); // 정상적인 입력 값이면 vector v에 입력
}
break;
case 2:
int random;
while (true) {
string c;
random = rand() % v.size(); // v에 들어있는 원소의 개수보다 작은 숫자를 얻음
cout << v.at(random).getNation() << "의 수도는?";
cin >> c;
if (c == "exit")
break;
else if (v.at(random).getCapital() == c) // 입력받은 수도가 맞으면 Correct 출력
cout << "Correct !!\n";
else
cout << "No !!\n";
}
break;
case 3:
return 0;
}
}
}
'2021-2학기 > C++' 카테고리의 다른 글
명품 C++ programming 9장 실습 문제 (0) | 2021.11.22 |
---|---|
C++ programming 8장 실습문제 (0) | 2021.11.18 |
명품 c++programming 7장 실습문제 (0) | 2021.11.13 |
프렌드와 연산자 중복 (0) | 2021.11.12 |
4장 (0) | 2021.10.26 |
Comments