C++

[C++]명품 C++ 프로그래밍 실습문제 10장 10번

Hs’s Coding vlog 2023. 1. 25. 21:38

[C++]명품 C++ 프로그래밍 실습문제 장 번

[C++]명품 C++ programming 실습문제 장 번

명품 C++ 프로그래밍실습문제/연습문제 /C++

 

#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include<cstdlib>
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() {
	srand((unsigned int)time(NULL));

	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 << "**************나라 수도맞추기 게임을 시작합니다.***********" << endl;
	
	string s1, s2;
	int num;
	bool check;
	while (1)
	{
		cout << "정보입력: 1, 퀴즈: 2 , 종료:3 >>";
		cin >> num;
		switch (num)
		{
		case 1:

			cout << "현재 " << v.size() << "개의 나라가 입력되어 있습니다." << endl;
			cout << "나라와 수도를 입력하세요 (no no 이면 입력끝)" << endl;
			while (1)
			{
				check = false;
				cout << v.size()+1 << ">>";
				cin >> s1 >> s2;
				if (s1 == "no" && s2 == "no") break;
			
				for (int i = 0; i < v.size(); i++) {
					if (v[i].nationCompare(s1) == true) {
						cout << "already exist" << endl;
						check = true;
						break;
					}
				}
				if (check == false) {
					v.push_back(Nation(s1, s2));
		
				}
					

			}
			break;
		case 2: //퀴즈 푸는 케이스
			while (1)
			{
				string capital;
				int ran = rand() % (v.size() - 1);
				cout << v[ran].getNation() << "의 수도는???";
				cin >> capital;
				if (v[ran].getCapital() == capital)
					cout << "correct!!" << endl;
				else if (capital == "exit")
					break;
				else
					cout << "NO!!" << endl;


			}
			
			break;	


		case 3:
			return 0;
		}
	}
}