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

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

by starfish22 2022. 6. 4.
728x90

1.

#include <iostream>
using namespace std;

int main() {
	int ch, count = 0;

	while ((ch = cin.get()) != EOF) {
		if (ch == 'a') count++;
		else if (ch == '\n') break;
	}

	cout << count << endl;
}

2.

#include <iostream>
using namespace std;

int main() {
	char ch;
	int count = 0;

	while (true) {
		cin.get(ch);
		if (cin.eof() || ch=='\n') break;
		if (ch == ' ') count++;
	}
    
	cout << count << endl;
}

3.

#include <iostream>
using namespace std;

int main() {
	int ch;
	cin.ignore(100, ';');

	while ((ch=cin.get())!=EOF) {
		cout.put(ch);
		if (ch == '\n') cin.ignore(100, ';');
	}
}

4.

#include <iostream>
using namespace std;

int main() {
	int ch;

	while ((ch=cin.get())!=EOF) {
		if (ch == ';') {
			cin.ignore(100, '\n');
			cout.put('\n');
		}
		else cout.put(ch);
	}
	
}

5.

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

int main() {
	string cmd;
	cout << "getline(istream&, string&)로 문자열을 읽습니다." << endl;

	while (true) {
		cout << "종료하려면 exit을 입력하세요 >> ";
		getline(cin, cmd);
		if (cmd == "exit") {
			cout << "프로그램을 종료합니다....";
			return 0;
		}
	}
}

6.

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

int main() {
	cout << setw(15) << left << "Number";
	cout << setw(15) << left << "Square";
	cout << setw(15) << left << "Square Root" << endl;

	for (int i = 0; i < 50; i += 5) {
		cout << setw(15) << setfill('_') << i;
		cout << setw(15) << setfill('_') << i * i;
		cout << setw(15) << setfill('_') << setprecision(3) << sqrt(i) << endl;
	}
}

7.

#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

int main() {
	cout << left << setw(7) << "dec" << setw(8) << "hexa" << setw(8) << "char";
	cout << left << setw(7) << "dec" << setw(8) << "hexa" << setw(8) << "char";
	cout << left << setw(7) << "dec" << setw(8) << "hexa" << setw(8) << "char";
	cout << left << setw(7) << "dec" << setw(8) << "hexa" << setw(8) << "char" << endl;
	cout << left << setw(7) << "---" << setw(8) << "----" << setw(8) << "----";
	cout << left << setw(7) << "---" << setw(8) << "----" << setw(8) << "----";
	cout << left << setw(7) << "---" << setw(8) << "----" << setw(8) << "----";
	cout << left << setw(7) << "---" << setw(8) << "----" << setw(8) << "----";

	for (int i = 0; i < 128; i++) {
		if (i % 4 == 0) cout << endl;
		cout << dec << setw(7) << i << setw(8) << hex << i << setw(8);
		if (isprint(i)) cout << (char)i;
		else cout << '.';
	}
}

8.

#include <iostream>
using namespace std;

class Circle {
	string name;
	int radius;
public:
	Circle(int radius = 1, string name = "") {
		this->radius = radius; this->name = name;
	}
	friend istream& operator>>(istream& ins, Circle& c);
	friend ostream& operator<<(ostream& outs, Circle& c);
};

istream& operator>>(istream& ins, Circle& c) {
	cout << "반지름 >> ";
	ins >> c.radius;
	cout << "이름 >> ";
	ins >> c.name;
	return ins;
}

ostream& operator<<(ostream& outs, Circle& c) {
	outs << "(반지름" << c.radius << "인 " << c.name << ")";
	return outs;
}

int main() {
	Circle d, w;
	cin >> d >> w;
	cout << d << w << endl;
}

9.

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

class Phone {
	string name, telnum, address;
public:
	Phone(string name = "", string telnum = "", string address = "") {
		this->name = name;
		this->telnum = telnum;
		this->address = address;
	}
	friend istream& operator>>(istream& ins, Phone& p);
	friend ostream& operator<<(ostream& outs, Phone& p);
};

istream& operator>>(istream& ins, Phone& p) {
	cout << "이름:";
	getline(ins, p.name);
	cout << "전화번호:";
	getline(ins, p.telnum);
	cout << "주소:";
	getline(ins, p.address);
	return ins;
}

ostream& operator<<(ostream& outs, Phone& p) {
	outs << '(' << p.name << ',' << p.telnum << ','<<p.address<<')';
	return outs;
}

int main() {
	Phone girl, boy;
	cin >> girl >> boy;
	cout << girl << endl << boy << endl;
}

10.

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

istream& prompt(istream& ins) {
	cout << "암호?";
	return ins;
}

int main() {
	string password;
	while (true) {
		cin >> prompt >> password;
		if (password == "C++") {
			cout << "login success!!" << endl;
			break;
		}
		else cout << "login fail. try again!!" << endl;
	}
}

11.

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

istream& pos(istream& ins) {
	cout << "위치는? ";
	return ins;
}

int main() {
	int x, y;
	cin >> pos >> x;
	cin >> pos >> y;
	cout << x << ',' << y << endl;
}

12.

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

class Machine {
	map<string, int> m;
	string str[5]= { "Coffee","Sugar","CREAM", "Water","Cup" };
public:
	Machine() {
		cout << "------명품 커피 자판기켭니다.------" << endl;
		for (int i = 0; i < 5; i++) {
			m[str[i]] = 3;
		}
		show();
		menu();
	}

	void coffee() {
		if (!ingredient()) return;
		cout << "맛있는 커피 나왔습니다~~" << endl;
		m[str[0]]--;
		m[str[3]]--;
		m[str[4]]--;
	}
	void sugarCoffee() {
		if (!ingredient()) return;
		cout << "맛있는 설탕 커피 나왔습니다~~" << endl;
		m[str[0]]--;
		m[str[1]]--;
		m[str[3]]--;
		m[str[4]]--;
	}
	void blackCoffee() {
		if (!ingredient()) return;
		cout << "맛있는 커피 나왔습니다~~" << endl;
		m[str[0]]--;
		m[str[3]]--;
		m[str[4]]--;
	}

	void fill() {
		cout << "모든 통을 채웁니다.~~" << endl;
		for (int i = 0; i < 5; i++) {
			m[str[i]] = 3;
		}
	}
	void show() {
		for (int i = 0; i < 5; i++) {
			cout << setw(8) << left << str[i];
			for (int j = 0; j < m[str[i]]; j++) cout << '*';
			cout << endl;
		}
		cout << endl;
	}
	int ingredient() {
		int t = 0;
		for (int i = 0; i < 5; i++) {
			if (m[str[i]] == 0) {
				t = 1;
				break;
			}
		}
		if (t == 0) return 1;
		else {
			cout << "재료가 부족합니다..." << endl;
			return 0;
		}
	}
    
	void menu() {
		int n;
		while (true) {
			cout << "보통 커피:0, 설탕 커피:1, 블랙 커피:2, 채우기:3, 종료:4>> ";
			cin >> n;
			switch (n) {
			case 0: coffee(); break;
			case 1: sugarCoffee(); break;
			case 2: blackCoffee(); break;
			case 3: fill(); break;
			case 4: return;
			}
			show();
		}
	}
};

int main() {
	Machine machine;
}
728x90

댓글