728x90
1.
#include <iostream>
using namespace std;
class Color{
int red, green, blue;
public:
Color() { red = green = blue = 0; }
Color(int r, int g, int b) { red = r; green = g; blue = b; }
void setColor(int r, int g, int b) { red = r; green = g; blue = b; }
void show() { cout << red << ' ' << green << ' ' << blue << endl; }
};
int main() {
Color screenColor(255, 0, 0);
Color *p;
p = &screenColor;
p->show();
Color colors[3];
p = colors;
p->setColor(255, 0, 0);
(p + 1)->setColor(0, 255, 0);
(p + 2)->setColor(0, 0, 255);
for (int i = 0; i < 3; i++) {
(p + i)->show();
}
}
2.
#include <iostream>
using namespace std;
int main() {
int *arr = new int[5];
cout << "정수 5개 입력>> ";
double sum = 0;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "평균 " << sum / 5 << endl;
delete[] arr;
}
3.
#include <iostream>
#include <string>
using namespace std;
// (1)
int main() {
string text;
cout << "문자열 입력>> ";
getline(cin, text, '\n');
int cnt = 0;
for (int i = 0; i < text.length(); i++) {
if (text[i] == 'a') cnt++;
}
cout << "문자 a는 " << cnt << "개 있습니다." << endl;
}
// (2)
int main() {
string text;
cout << "문자열 입력>> ";
getline(cin, text, '\n');
int cnt = 0, index = 0;
while (true)
{
int fIndex = text.find('a', index);
if (fIndex == -1) break;
cnt++;
index = fIndex + 1;
}
cout << "문자 a는 " << cnt << "개 있습니다." << endl;
}
4.
#include <iostream>
using namespace std;
class Sample {
int *p;
int size;
public:
Sample(int n) { size = n; p = new int[n]; }
void read();
void write();
int big();
~Sample() { delete[] p; }
};
void Sample::read() {
for (int i = 0; i < size; i++) {
cin >> p[i];
}
}
void Sample::write() {
for (int i = 0; i < size; i++) {
cout << p[i] << " ";
}
cout << endl;
}
int Sample::big() {
int max = p[0];
for (int i = 1; i < size; i++) {
if (max < p[i]) max = p[i];
}
return max;
}
int main() {
Sample s(10);
s.read();
s.write();
cout << "가장 큰 수는 " << s.big() << endl;
}
5.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand((unsigned)time(0));
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)\n";
string text;
while (true)
{
cout << ">>";
getline(cin, text, '\n');
if (text == "exit") break;
int r = rand();
int index = r % text.length();
int alpha = r % ('z' - 'a');
text[index] = 'a' + alpha;
cout << text << endl;
}
}
6.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
string text;
while (true)
{
cout << ">>";
getline(cin, text, '\n');
if (text == "exit") break;
int start = 0, end = text.length() - 1, temp;
while (start < end)
{
temp = text[start];
text[start] = text[end];
text[end] = temp;
start++;
end--;
}
cout << text << endl;
}
}
7.
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
void setRadius(int radius) { this->radius = radius; }
double getArea() { return radius * radius * 3.14; }
};
int main() {
Circle circle[3];
int r;
for (int i = 0; i < 3; i++) {
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> r;
circle[i].setRadius(r);
}
int cnt = 0;
for (int i = 0; i < 3; i++) {
if (circle[i].getArea() > 100) {
cnt++;
}
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
}
8.
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
void setRadius(int radius) { this->radius = radius; }
double getArea() { return radius * radius * 3.14; }
};
int main() {
int n;
cout << "원의 개수 >> ";
cin >> n;
Circle *circle = new Circle [n];
int r;
for (int i = 0; i < n; i++) {
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> r;
circle[i].setRadius(r);
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (circle[i].getArea() > 100) {
cnt++;
}
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
delete [] circle;
}
9.
#include <iostream>
#include <string>
using namespace std;
class Person {
string name;
string tel;
public:
Person() {}
string getName() { return name; }
string getTel() { return tel; }
void set(string name, string tel) { this->name = name; this->tel = tel; }
};
int main() {
cout << "이름과 전화 번호를 입력해 주세요" << endl;
Person person[3];
string name, tel;
for (int i = 0; i < 3; i++) {
cout << "사람 " << i + 1 << ">> ";
cin >> name >> tel;
person[i].set(name, tel);
}
cout << "모든 사람의 이름은 ";
for (int i = 0; i < 3; i++) {
cout << person[i].getName() << " ";
}
cout << "\n전화번호 검색합니다. 이름을 입력하세요>>";
cin >> name;
for (int i = 0; i < 3; i++) {
if (person[i].getName() == name) {
cout << "전화 번호는 " << person[i].getTel() << endl;
break;
}
}
}
10.
#include <iostream>
#include <string>
using namespace std;
class Person {
string name;
public:
void setName(string name) { this->name = name; }
string getName() { return name; }
};
class Family {
Person *p;
string name;
int size;
public:
Family(string name, int size) {
p = new Person[size];
this->name = name;
this->size = size;
}
void show() {
cout << "Simpson가족은 다음과 같이 " << size << "명 입니다." << endl;
for (int i = 0; i < size; i++) {
cout << (p + i)->getName() << '\t';
}
}
void setName(int num, string name) { (p + num)->setName(name); }
~Family() { delete[] p; }
};
int main() {
Family *simpson = new Family("Simpson", 3);
simpson->setName(0, "Mr. Simpson");
simpson->setName(1, "Mrs. Simpson");
simpson->setName(2, "Bart Simpson");
simpson->show();
delete simpson;
}
11.
#include <iostream>
using namespace std;
class Container {
int size;
public:
Container() { size = 10; }
void fill() { size = 10; }
void consume() { size--; }
int getSize() { return size; }
};
class CoffeeVendingMachine {
Container tong[3];
void fill() {
tong[0].fill();
tong[1].fill();
tong[2].fill();
}
void selectEspresso() {
if (tong[0].getSize() > 0 && tong[1].getSize() > 0) {
tong[0].consume();
tong[1].consume();
cout << "에스프레소 드세요" << endl;
}
else cout << "원료가 부족합니다." << endl;
}
void selectAmericano() {
if (tong[0].getSize() > 0 && tong[1].getSize() > 1) {
tong[0].consume();
tong[1].consume();
tong[1].consume();
cout << "아메리카노 드세요" << endl;
}
else cout << "원료가 부족합니다." << endl;
}
void selectSugarCoffee() {
if (tong[0].getSize() > 0 && tong[1].getSize() > 1 && tong[2].getSize() > 0) {
tong[0].consume();
tong[1].consume();
tong[1].consume();
tong[2].consume();
cout << "설탕커피 드세요" << endl;
}
else cout << "원료가 부족합니다." << endl;
}
void show() {
cout << "커피 " << tong[0].getSize() << ", 물 " << tong[1].getSize() << ", 설탕 " << tong[2].getSize() << endl;
}
public:
void run() {
cout << "***** 커피자판기를 작동합니다. *****" << endl;
while (true)
{
cout << "메뉴를 눌러주세요(1:에스프레소, 2:아메리카노, 3:설탕커피, 4:잔량보기, 5:채우기)>> ";
int n;
cin >> n;
switch (n)
{
case 1: selectEspresso(); break;
case 2: selectAmericano(); break;
case 3: selectSugarCoffee(); break;
case 4: show(); break;
case 5: fill(); show(); break;
}
}
}
};
int main() {
CoffeeVendingMachine coffee;
coffee.run();
}
12.
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
string name;
public:
void setCircle(string name, int radius) {
this->name = name;
this->radius = radius;
}
double getArea() { return radius * radius * 3.14; }
string getName() { return name; }
};
class CircleManager {
Circle *p;
int size;
public:
CircleManager(int size) {
p = new Circle[size];
this->size = size;
string name;
int radius;
for (int i = 0; i < size; i++) {
cout << "원 " << i + 1 << "의 이름과 반지름 >> ";
cin >> name >> radius;
p[i].setCircle(name, radius);
}
}
~CircleManager() { delete[] p; }
void searchByName();
void searchByArea();
};
void CircleManager::searchByName() {
cout << "검색하고자 하는 원의 이름 >> ";
string name;
cin >> name;
for (int i = 0; i < size; i++) {
if (p[i].getName() == name) {
cout << "도넛의 면적은 " << p[i].getArea() << endl;
break;
}
}
}
void CircleManager::searchByArea() {
cout << "최소 면적을 정수로 입력하세요 >> ";
int n;
cin >> n;
cout << n << "보다 큰 원을 검색합니다." << endl;
for (int i = 0; i < size; i++) {
if (p[i].getArea() > n) {
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << ",";
}
}
}
int main() {
cout << "원의 개수 >> ";
int n;
cin >> n;
CircleManager circle(n);
circle.searchByName();
circle.searchByArea();
}
13.
#include <iostream>
#include <string>
using namespace std;
class Histogram {
string text;
public:
Histogram(string text) { this->text = text; }
void put(string str) { text.append(str); }
void putc(char ch) { text += ch; }
void print();
};
void Histogram::print() {
cout << text << endl << endl;
int arr[26] = {0};
int cnt = 0;
for (int i = 0; i < text.length(); i++) {
if(isalpha(text[i])) {
arr[tolower(text[i]) - 'a']++;
cnt++;
}
}
cout << "총 알파벳 수 " << cnt << endl << endl;
for (int i = 0; i < 26; i++) {
cout << char('a'+i) << " (" << arr[i] << ")\t: ";
for (int j = 0; j < arr[i]; j++) {
cout << '*';
}
cout << endl;
}
}
int main() {
Histogram elvisHisto("Wise men say, only fools rush in But I can't help,\n");
elvisHisto.put("falling in love with you");
elvisHisto.putc('-');
elvisHisto.put("Elvis Presley");
elvisHisto.print();
}
14.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Player {
string name;
public:
void setName(string name) { this->name = name; }
string getName() { return name; }
};
class GamblingGame {
Player player[2];
public:
void start();
};
void GamblingGame::start() {
srand((unsigned)time(0));
cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
string name;
cout << "첫번째 선수 이름>>";
cin >> name;
player[0].setName(name);
cout << "두번째 선수 이름>>";
cin >> name;
player[1].setName(name);
cin.ignore();
for (int i = 0;; i++) {
int index = i % 2;
cout << player[index].getName() << ":<Enter>";
if (getchar() == '\n')
{
int arr[3] = {rand() % 3, rand() % 3, rand() % 3};
cout << '\t' << arr[0] << '\t' << arr[1] << '\t' << arr[2] << '\t';
if (arr[0] == arr[1] && arr[1] == arr[2]) {
cout << player[index].getName() << "님 승리!!" << endl;
break;
}
else cout << "아쉽군요!" << endl;
}
}
}
int main() {
GamblingGame game;
game.start();
}
728x90
'C++ > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ 5장 연습문제 - 실습 문제 (0) | 2022.01.10 |
---|---|
명품 C++ 5장 연습문제 - 이론 문제 (5) | 2022.01.08 |
명품 C++ 4장 연습문제 - 이론 문제 (0) | 2022.01.01 |
명품 C++ 3장 연습문제 - 실습 문제 (0) | 2021.12.06 |
명품 C++ 3장 연습문제 - 이론 문제 (0) | 2021.12.06 |
댓글