4-1
#include <iostream>
using namespace std;
int main()
{
int *p = new int[5];
int sum = 0;
double m=0.0;
cout << "정수 5개 입력>> ";
for (int i = 0; i < 5; i++)
{
cin >> p[i];
sum += p[i];
}
m = (double)sum / 5;
cout << "평균 " << m << endl;
delete[] p;
}
4-2
4-3
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int cnt = 0, ind = 0;
cout << "문자열 입력>> ";
getline(cin, str);
while(true)
{
ind = str.find('a', ind+1);
if (ind == -1)
break;
else
cnt++;
}
cout << "문자 a는 " << cnt << "개 있습니다." << endl;
}
4-4
#include <iostream>
#include <string>
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();
};
int main()
{
Sample s(10);
s.read();
s.write();
cout << "가장 큰 수는 " << s.big() << endl;
}
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 ind = 0;
for (int i = 0; i < size; i++)
{
if (ind < p[i])
ind = p[i];
}
return ind;
}
Sample::~Sample()
{
}
4-5
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
string str;
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
while(true)
{
cout << ">>";
getline(cin, str);
if (str.compare("exit") == 0)break;
srand((unsigned)time(0));
int n = rand() % (str.length());
srand((unsigned)time(0));
char ch = 'a' + rand() % 26;
str[n] = ch;
cout << str << endl;
}
}
4-6
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
string str;
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
while (true) {
cout << ">>";
getline(cin, str);
if (str.compare("exit") == 0) break;
for (int i = str.length(); i >= 0; i--)
{
cout << str[i];
}
cout << endl;
}
return 0;
}
4-7
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
void setRadius(int radius);
double getArea();
};
int main()
{
int r1, r2, r3;
Circle circles[3];
cout << "원 1의 반지름 >> ";
cin >> r1;
circles[0].setRadius(r1);
cout << "원 2의 반지름 >> ";
cin >> r2;
circles[1].setRadius(r2);
cout << "원 3의 반지름 >>";
cin >> r3;
circles[2].setRadius(r3);
int cnt = 0;
for(int i=0;i<3;i++)
{
if (circles[i].getArea() > 100)
cnt++;
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
}
void Circle::setRadius(int radius)
{
this->radius = radius;
}
double Circle::getArea()
{
return 3.14 * radius * radius;
}
4-8
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
void setRadius(int radius);
double getArea();
};
int main()
{
int r;
int n;
int cnt = 0;
cout << "원의 개수 >> ";
cin >> n;
Circle* p = new Circle[n];
for (int i = 0; i < n; i++)
{
cout << "원 " << i+1 << "의 반지름 >> ";
cin >> r;
p[i].setRadius(r);
if (p[i].getArea() > 100)
cnt++;
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
}
void Circle::setRadius(int radius)
{
this->radius = radius;
}
double Circle::getArea()
{
return 3.14 * radius * radius;
}
4-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);
};
int main()
{
Person people[3];
string name;
string tel;
cout << "이름과 전화 번호를 입력해 주세요" << endl;
for (int i = 0; i < 3; i++)
{
cout << "사람 " << i + 1 << ">> ";
cin >> name >> tel;
people[i].set(name, tel);
}
cout << "모든 사람의 이름은 ";
for (int i = 0; i < 3; i++)
{
cout<<people[i].getName() << ' ';
}
cout<< endl;
cout << "전화번호 검색합니다. 이름을 입력하세요>>";
string str;
cin >> str;
/*switch (str)
{
case '스폰지밥':
cout << "전화 번호는 " << people[0].getTel();
case '뚱이':
cout << "전화 번호는 " << people[1].getTel();
case '징징이':
cout << "전화 번호는 "<<people[2].getTel();
}스위치문 쓰지 말기. */
for (int i = 0; i < 3; i++)
{
if(str.compare(people[i].getName())==0)
{
cout << "전화 번호는 " << people[i].getTel() << endl;
break;
}
}
}
Person::Person()
{
}
void Person::set(string name, string tel)
{
this->name = name;
this->tel = tel;
}
4-10
#include <iostream>
#include <string>
using namespace std;
class Person {
string name;
public:
Person();
Person(string name) { this->name = name; }
string getName() { return name; }
void setName(string name) { this->name = name; }
};
class Family {
Person *p;
string familyName;
int size = 0;
public:
Family(string name, int size);
void show();
void setName(int index, string name) { p[index].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;
}
Person::Person()
{
}
Family::Family(string name, int size)
{
p = new Person[size];
this->familyName = name;
this->size = size;
}
void Family::show()
{
cout << "Simpson가족은 다음과 같이 " << size << "명입니다." << endl;
for (int i = 0; i < size; i++)
{
cout << p[i].getName() << '\t';
}
cout << endl;
}
4-11
#include <iostream>
#include <string>
using namespace std;
class CoffeeVendingMachine { //커피자판기를 표현하는 클래스
Container tong[3]; //tong[o]은 커피, tong[1]은 물, tong[2]는 설탕통을 나타냄
void fill(); //3개의 통을 모두 10으로 채움
void selectEspresso();//에스프레소를 선택한 경우, 커피1, 물 1 소모
void selectAmericano(); //아메리카노를 선택한 경우, 커피1, 물 2 소모
void selectSugarCoffee();//설탕커피를 선택한 경우, 커피 1, 물2, 설탕 1 소모
void show(); //현재 커피, 물, 설탕의 잔량 출력
public:
void run();//커피 자판기 작동
};
class Container {//통 하나를 나타내는 클래스
int size;//현재 저장 량, 최대 저장량은 10
public:
Container() { size = 10; }
void fill() { size = 10; }
void consume() {size -= 1;}
int getSize() { return size; }
};
void CoffeeVendingMachine::fill()
{
for (int i = 0; i < 3; i++)
{
tong[i].fill();
}
show();
}
void CoffeeVendingMachine::selectEspresso()
{
if (tong[0].getSize() >= 1 && tong[1].getSize() >= 1)
{
tong[0].consume();
tong[1].consume();
cout << "에스프레소 드세요" << endl;
}
else
{
cout << "원료가 부족합니다." << endl;
}
}
void CoffeeVendingMachine::selectAmericano()
{
if (tong[0].getSize() >= 1 && tong[1].getSize() >= 2)
{
tong[0].consume();
tong[1].consume();
tong[1].consume();
cout << "아메리카노 드세요" << endl;
}
else
{
cout << "원료가 부족합니다." << endl;
}
}
void CoffeeVendingMachine::selectSugarCoffee()
{
if (tong[0].getSize() >= 1 && tong[1].getSize() >= 2 && tong[2].getSize() >= 1)
{
tong[0].consume();
tong[1].consume();
tong[1].consume();
tong[2].consume();
cout << "아메리카노 드세요" << endl;
}
else
{
cout << "원료가 부족합니다." << endl;
}
}
void CoffeeVendingMachine::show()
{
cout << "커피 " << tong[0].getSize() << ", 물 " << tong[1].getSize() << ", 설탕 " << tong[2].getSize() << endl;
}
void CoffeeVendingMachine::run()
{
cout << "***** 커피자판기를 작동합니다. *****" << endl;
while (true)
{
int menu;
cout << "메뉴를 눌러주세요(1:에스프레소, 2:아메리카노, 3:설탕커피, 4:잔량보기, 5:채우기)>>";
cin >> menu;
switch (menu) {
case 1:
selectEspresso();
break;
case 2:
selectAmericano();
break;
case 3:
selectSugarCoffee();
break;
case 4:
show();
break;
case 5:
fill();
break;
default:
break;
}
}
}
int main()
{
CoffeeVendingMachine coffeeVendingMachine;
coffeeVendingMachine.run();
}
4-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 3.14 * radius * radius; }
string getName() { return name; }
};
class CircleManager {
Circle *p;
int size;
public:
CircleManager(int size) { this->size = size; p = new Circle[size]; }
~CircleManager() { delete [] p; }
Circle* getCircle() { return p; } //이걸 추가해야함.
void searchByName();
void searchByArea();
};
void CircleManager::searchByName()
{
cout << "검색하고자 하는 원의 이름 >> ";
string name;
cin >> name;
for (int i = 0; i <size; i++) //num이 아니라 size로 하면 됨.
{
if (p[i].getName() == name) {
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << endl;
break;
}
}
}
void CircleManager::searchByArea()
{
cout << "최소 면적을 정수로 입력하세요 >> ";
int area;
cin >> area;
cout << area << "보다 큰 원을 검색합니다." << endl;
for (int i = 0; i < size; i++) //num이 아니라 size로 하면됨
{
if (p[i].getArea() > area)
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << ",";
}
cout << endl;
}
int main()
{
int num;
cout << "원의 개수 >> ";
cin >> num;
CircleManager circles(num);
for (int i = 0; i < num; i++)
{
cout << "원 " << i + 1 << "의 이름과 반지름 >> ";
string name;
int radius;
cin >> name >> radius;
circles.getCircle()[i].setCircle(name, radius); //p[i]로 직접적으로 쓸 수 없음.
}
circles.searchByName();
circles.searchByArea();
}
4-13
#include <iostream>
#include <string>
#include <string>
#include <locale>
using namespace std;
class Histogram
{
//int cnt = 0;
string str;
int alphabet[26] = { 0 };
public:
Histogram(string str) { this->str = str; };
void put(string str) { this->str += str; };
void putc(char ch) { this->str += ch; };
void print();
int getAlphabetSize();
void countAlphabet();
};
int Histogram::getAlphabetSize()
{
int cnt = 0;
for (int i = 0; i<str.length(); i++)
{
str[i] = tolower(str[i]);
if (str[i] >= 'a' && str[i] <= 'z')
cnt++;
}
return cnt;
}
void Histogram::countAlphabet()
{
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
int ind = str[i] - 'a';
alphabet[ind]++;
}
}
}
void Histogram::print()
{
cout << str << endl << endl;
cout << "총 알파벳 수 " << getAlphabetSize() << endl << endl;
countAlphabet();
char ch = 'a';
while (ch <= 'z') {
cout << ch << " (" << alphabet[(int)ch - 'a'] << ")\t:";
for (int i = 0; i < alphabet[(int)ch - 'a']; i++)
{
cout << "*";
}
cout << endl;
ch++;
}
}
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();
}
4-14
#include <iostream>
#include <string>
using namespace std;
class Player
{
string players[2];
public:
void setName();
};
class GamblingGame
{
};
void Player::setName()
{
string str;
cout << "첫번째 선수 이름>>";
cin >> str;
this->players[0] = str;
cout << "두번째 선수 이름>>";
cin >> str;
this->players[1] = str;
}
int main()
{
cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
Player player;
player.setName();
}