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[0].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];
double avg = 0;
cout << "정수 5개 입력 >> ";
for(int i = 0; i < 5; i++){
cin >> arr[i];
avg += arr[i];
}
cout << "평균 " << avg / 5;
delete [] arr;
}
3-1번
#include<iostream>
using namespace std;
int main(){
cout << "문자열 입력 >> ";
string str;
getline(cin, str);
int cnt = 0;
for(int i = 0; i < str.length(); i++){
if(str[i] == 'a')
cnt++;
}
cout << "문자 a는 " << cnt << "개 있습니다.";
}
3-2번
#include<iostream>
using namespace std;
int main(){
cout << "문자열 입력 >> ";
string str;
getline(cin, str);
int cnt = 0;
for(int i = 0;;){
i = str.find('a', i+1);
if(i == -1) break;
else cnt++;
}
cout << "문자 a는 " << cnt << "개 있습니다.";
}
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();
};
void Sample::read(){
for(int i = 0; i < size; i++){
cin >> p[i];
}
cout << endl;
}
void Sample::write(){
for(int i = 0; i < size; i++){
cout << p[i] << " ";
}
cout << endl;
}
int Sample::big(){
int big = p[0];
for(int i = 0; i < size; i++){
if(big < p[i]){
big = p[i];
}
}
return big;
}
Sample::~Sample(){
}
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(){
string str;
while(1){
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;
}
}
6번
#include<iostream>
#include <string>
using namespace std;
int main(){
string str;
while(1){
getline(cin, str);
if(str.compare("exit") == 0) break;
for(int i = str.length() - 1; i >= 0; i--){
cout << str[i];
}
cout << endl;
}
}
7번
#include<iostream>
#include <string>
using namespace std;
class Circle{
int radius;
public:
void setRadius(int radius);
double getArea();
};
void Circle::setRadius(int radius){
this->radius = radius;
}
double Circle::getArea(){
return 3.14 * radius * radius;
}
int main(){
Circle circles[3];
int cnt = 0;
for(int i = 0; i < 3; i++){
int r;
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> r;
circles[i].setRadius(r);
if(circles[i].getArea() > 100){
cnt++;
}
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다." << endl;
}
8번
#include<iostream>
#include <string>
using namespace std;
class Circle{
int radius;
public:
void setRadius(int radius);
double getArea();
};
void Circle::setRadius(int radius){
this->radius = radius;
}
double Circle::getArea(){
return 3.14 * radius * radius;
}
int main(){
cout << "원의 개수 >> ";
int n;
cin >> n;
Circle circles[n];
int cnt = 0;
for(int i = 0; i < n; i++){
int r;
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> r;
circles[i].setRadius(r);
if(circles[i].getArea() > 100){
cnt++;
}
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다." << endl;
}
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);
};
Person::Person(){
}
void Person::set(string name, string tel){
this->name = name;
this->tel = tel;
}
int main(){
cout << "이름과 전화번호를 입력해주세요." << endl;
Person p [3];
for(int i = 0; i < 3; i++){
string temp1, temp2;
cout << "사람 "<< i + 1 << " >>";
cin >> temp1 >> temp2;
p[i].set(temp1, temp2);
}
cout << "모든 사람의 이름은 ";
for(int i = 0; i < 3; i++){
cout << p[i].getName() << " ";
}
cout << endl;
cout << "전화번호를 검색합니다. 이름을 입력하세요 >>";
string str;
cin >> str;
for(int i = 0; i < 3; i++){
if(str.compare(p[i].getName()) == 0){
cout << "전화번호는 " << p[i].getTel();
break;
}
}
}
10번
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
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;
public:
Family(string name, int size);
void show();
void setName(int index, string name) { p[index].setName(name); }
~Family();
};
Family::Family(string name, int size) {
p = new Person[size];
this->familyName = name;
this->size = size;
}
void Family::show() {
cout << familyName << "가족은 다음과 같이 " << size << "명 입니다." << endl;
for (int i = 0; i < size; i++) {
cout << p[i].getName() << '\t';
}
cout << endl;
}
Family::~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;
return 0;
}
11번
#include <iostream>
#include <string>
using namespace std;
class Container {
int size;
public:
Container(){
size = 10;
}
void fill(){size = 10;};
void consume(){size -= 1;};
int getSize(){return size; };
};
class CoffeeVendingMachine {
Container tong[3];
void fill();
void selectEspresso();
void selectAmericano();
void selectSugarCoffee();
void show();
public:
void run();
};
void CoffeeVendingMachine::fill(){
tong[0].fill();
tong[1].fill();
tong[2].fill();
}
void CoffeeVendingMachine::selectEspresso(){
if(tong[0].getSize() > 0 && tong[1].getSize() > 0){
tong[0].consume();
tong[1].consume();
}
else{
cout << "원료가 부족합니다.";
}
}
void CoffeeVendingMachine::selectAmericano(){
if(tong[0].getSize() > 0 && tong[1].getSize() > 1){
tong[0].consume();
tong[1].consume();
tong[1].consume();
}
else{
cout << "원료가 부족합니다.";
}
}
void CoffeeVendingMachine::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();
}
else{
cout << "원료가 부족합니다.";
}
}
void CoffeeVendingMachine::show(){
cout << "커피: "<< tong[0].getSize() << ", 물: " << tong[1].getSize() << ", 설탕: " << tong[2].getSize();
}
void CoffeeVendingMachine::run(){
cout << "****** 커피 자판기 작동합니다. *****" << endl;
while(1){
cout << "메뉴를 눌러주세요(1:에스프레소, 2:아메리카노, 3:설탕커피, 4:잔량보기, 5:채우기)>> ";
int a;
cin >> a;
switch (a)
{
case 1:
selectEspresso();
cout << "에스프레소 드세요" << endl;
break;
case 2:
selectAmericano();
cout << "아메리카노 드세요" << endl;
break;
case 3:
selectSugarCoffee();
cout << "설탕커피 드세요" << endl;
break;
case 4:
show();
break;
case 5:
fill();
show();
break;
default:
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);
double getArea();
string getName();
};
void Circle::setCircle(string name, int radius){
this->name = name;
this->radius = radius;
}
double Circle::getArea(){
return 3.14 * radius * radius;
}
string Circle::getName(){
return name;
}
class CircleManager{
Circle *p;
int size;
public:
CircleManager(int size);
~CircleManager(){delete []p;};
Circle * getCircle() {return p;};
void searchByName();
void searchByArea();
};
CircleManager::CircleManager(int size){
p = new Circle[size];
this->size = size;
}
void CircleManager::searchByName(){
string temp;
cout << "검색하고자 하는 원의 이름 >> ";
cin >> temp;
for(int i = 0; i < size; i++){
if(p[i].getName().compare(temp) == 0){
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << endl;
break;
}
}
}
void CircleManager::searchByArea(){
int a;
cout << "최소 면적을 정수로 입력하세요 >> ";
cin >> a;
cout << a << "보다 큰 원을 검색합니다.";
for(int i = 0; i < size; i ++){
if(p[i].getArea() > a){
cout << p[i].getName() << "의 면적은 "<< p[i].getArea() << ", ";
}
}
}
int main(){
cout << "원의 개수 >> ";
int temp;
cin >> temp;
CircleManager cm(temp);
for(int i = 0; i < temp; i++){
string temp1;
int temp2;
cout << "원 " << i + 1 << "의 이름과 반지름 >>";
cin >> temp1 >> temp2;
cm.getCircle()[i].setCircle(temp1, temp2);
}
cm.searchByName();
cm.searchByArea();
}
#include <iostream>
#include <string>
using namespace std;
class Histogram{
string str;
int spell[26] = {0};
public:
Histogram(string str){
this->str = str;
};
void put(string n) {this->str.append(n);};
void putc(char n) {this->str += n;};
void print();
int cntSpell();
};
void Histogram::print(){
cout << this->str << endl;
cout << "총 알파벳 수" << cntSpell() << endl;
for(int i = 'a'; i <= 'z'; i++){
cout << char(i) << "(" << spell[(int)i - 'a'] << ")\t:";
for(int j = 0 ; j < spell[(int)i - 'a']; j++){
cout << "*";
}
cout << endl;
}
}
int Histogram::cntSpell(){
for(int i = 0; i < str.length(); i++){
if(str[i] >= 'A' && str[i] <= 'Z'){
int index = str[i] - 'A';
spell[index]++;
}
if(str[i] >= 'a' && str[i] <= 'z'){
int index = str[i] - 'a';
spell[index]++;
}
}
int cnt = 0;
for(int i = 0; i < str.length(); i++){
if((str[i] <= 'z' && str[i] >= 'a') || (str[i] <= 'Z' && str[i] >= 'A')){
cnt++;
}
}
return cnt;
}
int main(){
Histogram elvisHisto("Wise men say, only fools rush in But I can't help, ");
elvisHisto.put("falling in love with you");
elvisHisto.putc('-');
elvisHisto.put("Elvis Presley");
elvisHisto.print();
}
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
class Player {
int card[3];
string name;
public:
Player() :Player("플레이어") { }
Player(string name) { this->name = name; }
string getName() { return name; }
bool playGambling();
};
bool Player::playGambling() {
for (int i = 0; i < 3; i++) {
card[i] = rand() % 3;
cout << "\t" << card[i];
}
for (int i = 0; i < 2; i++) {
if (card[i] != card[i + 1]) {
return false;
}
}
return true;
}
class GamblingGame {
Player player[2];
bool isGameCompleted = false;
public:
GamblingGame();
void play();
};
GamblingGame::GamblingGame() {
cout << "*****갬블링 게임을 시작합니다. *****" << endl;
string name;
cout << "첫번째 선수 이름>>";
cin >> name;
player[0] = Player(name);
cout << "두번째 선수 이름>>";
cin >> name;
player[1] = Player(name);
getchar();
}
void GamblingGame::play() {
int i = 0;
while (!isGameCompleted) {
cout << player[i % 2].getName() << ":<Enter>";
getchar();
if (player[i % 2].playGambling()) {
isGameCompleted = true;
cout << "\t" << player[i % 2].getName() << "님 승리!!" << endl;
}
else {
cout << "\t아쉽군요!" << endl;
}
i++;
}
}
int main() {
GamblingGame game;
game.play();
}
'대학교 수업 > C++ 프로그래밍' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 6장 (0) | 2022.06.07 |
---|---|
[C++] 명품 c++ 프로그래밍 5장 (0) | 2022.04.20 |
[C++] 명품 C++ 프로그래밍 3장 (0) | 2022.04.11 |
[C++] 명품 C++ 프로그래밍 2장 (0) | 2022.04.10 |
[C++] 명품 C++ 프로그래밍 1장 (0) | 2022.04.10 |