728x90
1.
#include <iostream>
using namespace std;
class Tower{
int meter;
public:
Tower() : Tower(1) {}
Tower(int m) : meter(m) {}
int getHeight() { return meter; }
};
int main()
{
Tower myTower;
Tower seoulTower(100);
cout << "높이는 " << myTower.getHeight() << "미터" << endl;
cout << "높이는 " << seoulTower.getHeight() << "미터" << endl;
}
2.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Date{
int year, mon, day;
public:
Date(int y, int m, int d) : year(y), mon(m), day(d) {}
Date(string s)
{
char c[101];
strcpy(c, s.c_str());
year = stoi(strtok(c, "/"));
mon = stoi(strtok(NULL, "/"));
day = stoi(strtok(NULL, "/"));
}
void show()
{
cout << year << "년" << mon << "월" << day << "일" << endl;
}
int getYear() { return year; }
int getMonth() { return mon; }
int getDay() { return day; }
};
int main()
{
Date birth(2014, 3, 20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
}
3.
#include <iostream>
#include <cstring>
using namespace std;
class Account{
char name[20];
int id, balance;
public:
char *getOwner() { return name; }
void deposit(int n) { balance += n; }
int withdraw(int n){
balance -= n;
return balance;
}
int inquiry() { return balance; }
Account(const char *s, int n, int m) : id(n), balance(m)
{
strcpy(name, s);
}
};
int main()
{
Account a("kitae", 1, 5000);
a.deposit(50000);
cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
int money = a.withdraw(20000);
cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
}
4.
#include <iostream>
using namespace std;
class CoffeeMachine
{
int coffee, water, sugar;
public:
CoffeeMachine(int co, int w, int s) : coffee(co), water(w), sugar(s) {}
void drinkEspresso(){
coffee--;
water--;
}
void drinkAmericano(){
coffee--;
water -= 2;
}
void drinkSugarCoffee(){
coffee--;
water -= 2;
sugar--;
}
void show(){
cout << "커피 머신 상태, 커피:" << coffee << "\t물:" << water << "\t설탕:" << sugar << endl;
}
void fill(){
coffee = 10;
water = 10;
sugar = 10;
}
};
int main()
{
CoffeeMachine java(5, 10, 3);
java.drinkEspresso();
java.show();
java.drinkAmericano();
java.show();
java.drinkSugarCoffee();
java.show();
java.fill();
java.show();
}
5.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random{
public:
Random() { srand((unsigned)time(0)); }
int next(){
return rand();
}
int nextInRange(int a, int b){
int n = rand() % (b - a + 1);
return n + a;
}
};
int main()
{
Random r;
cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.next();
cout << n << ' ';
}
cout << endl << endl << "-- 2에서 " << "4 까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.nextInRange(2, 4);
cout << n << ' ';
}
cout << endl;
}
6.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class EvenRandom{
public:
EvenRandom() { srand((unsigned)time(0)); }
int next(){
int n = rand();
if(n % 2 == 1 && n + 1 < RAND_MAX) n++;
return n;
}
int nextInRange(int a, int b){
int n = rand() % (b - a + 1) + a;
if(n % 2 == 1 && n + 1 <= b) n++;
return n;
}
};
int main()
{
EvenRandom r;
cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.next();
cout << n << ' ';
}
cout << endl << endl << "-- 2에서 " << "10 까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.nextInRange(2, 10);
cout << n << ' ';
}
cout << endl;
}
7.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class SelectableRandom{
public:
SelectableRandom() { srand((unsigned)time(0)); }
int evenRandom(){
int n = rand();
if(n % 2 == 1 && n + 1 < RAND_MAX) n++;
return n;
}
int oddRandom(int a, int b){
int n = rand() % (b - a + 1) + a;
if(n % 2 == 0 && n + 1 <= b) n++;
return n;
}
};
int main()
{
SelectableRandom r;
cout << "-- 0에서 " << RAND_MAX << "까지의 짝수 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.evenRandom();
cout << n << ' ';
}
cout << endl << endl << "-- 2에서 " << "9 까지의 랜덤 홀수 정수 10개 --" << endl;
for (int i = 0; i < 10; i++){
int n = r.oddRandom(2, 9);
cout << n << ' ';
}
cout << endl;
}
8.
#include <iostream>
#include <string>
using namespace std;
class Integer{
int num;
public:
Integer(int n) : num(n) {}
Integer(const char *str) : num(stoi(str)) {}
int get() { return num; }
void set(int n) { num = n; }
bool isEven(){
if (num % 2 == 0) return true;
else return false;
}
};
int main()
{
Integer n(30);
cout << n.get() << ' ';
n.set(50);
cout << n.get() << ' ';
Integer m("300");
cout << m.get() << ' ';
cout << m.isEven();
}
9.
#include <iostream>
using namespace std;
class Oval{
int width, height;
public:
Oval(int a, int b);
Oval();
~Oval();
int getWidth();
int getHeight();
void set(int w, int h);
void show();
};
Oval::Oval(int a, int b) : width(a), height(b) {}
Oval::Oval() : width(1), height(1) {}
Oval::~Oval(){
cout<<"Oval 소멸 : width = "<<width<<", height = "<<height<<endl;
}
int Oval::getWidth(){ return width; }
int Oval::getHeight(){ return height; }
void Oval::set(int w, int h){
width=w;
height=h;
}
void Oval::show(){
cout<<"width = "<<width<<", height = "<<height<<endl;
}
10.
(1)
#include <iostream>
using namespace std;
class Add{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Sub{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Mul{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Div{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
void Add::setValue(int x, int y)
{
a = x;
b = y;
}
int Add::calculate() { return a + b; }
void Sub::setValue(int x, int y)
{
a = x;
b = y;
}
int Sub::calculate() { return a - b; }
void Mul::setValue(int x, int y)
{
a = x;
b = y;
}
int Mul::calculate() { return a * b; }
void Div::setValue(int x, int y)
{
a = x;
b = y;
}
int Div::calculate() { return a / b; }
int main()
{
Add a;
Sub s;
Mul m;
Div d;
int num1, num2;
char str;
while (1)
{
cout << "두 정수와 연산자를 입력하세요>>";
cin >> num1 >> num2 >> str;
switch (str)
{
case '+':
a.setValue(num1, num2);
cout << a.calculate() << endl;
break;
case '-':
s.setValue(num1, num2);
cout << s.calculate() << endl;
break;
case '*':
m.setValue(num1, num2);
cout << m.calculate() << endl;
break;
case '/':
d.setValue(num1, num2);
cout << d.calculate() << endl;
break;
}
}
}
(2)
Calculator.h
#pragma once
class Add{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Sub{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Mul{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
class Div{
int a, b;
public:
void setValue(int x, int y);
int calculate();
};
Calculator.cpp
#include <iostream>
#include "Calculator.h"
using namespace std;
void Add::setValue(int x, int y)
{
a = x;
b = y;
}
int Add::calculate() { return a + b; }
void Sub::setValue(int x, int y)
{
a = x;
b = y;
}
int Sub::calculate() { return a - b; }
void Mul::setValue(int x, int y)
{
a = x;
b = y;
}
int Mul::calculate() { return a * b; }
void Div::setValue(int x, int y)
{
a = x;
b = y;
}
int Div::calculate() { return a / b; }
int main()
{
Add a;
Sub s;
Mul m;
Div d;
int num1, num2;
char str;
while (1)
{
cout << "두 정수와 연산자를 입력하세요>>";
cin >> num1 >> num2 >> str;
switch (str)
{
case '+':
a.setValue(num1, num2);
cout << a.calculate() << endl;
break;
case '-':
s.setValue(num1, num2);
cout << s.calculate() << endl;
break;
case '*':
m.setValue(num1, num2);
cout << m.calculate() << endl;
break;
case '/':
d.setValue(num1, num2);
cout << d.calculate() << endl;
break;
}
}
}
11.
Box.h
#pragma once
class Box {
int width, height;
char fill;
public:
Box(int w, int h);
void setFill(char f);
void setSize(int w, int h);
void draw();
};
Box.cpp
#include <iostream>
#include "Box.h"
using namespace std;
Box::Box(int w, int h)
{
setSize(w, h);
fill = '*';
}
void Box::setFill(char f)
{
fill = f;
}
void Box::setSize(int w, int h)
{
width = w;
height = h;
}
void Box::draw()
{
for (int n = 0; n < height; n++)
{
for (int m = 0; m < width; m++)
{
cout << fill;
}
cout << endl;
}
}
main.cpp
#include <iostream>
#include "Box.h"
using namespace std;
int main()
{
Box b(10, 2);
b.draw();
cout << endl;
b.setSize(7, 4);
b.setFill('^');
b.draw();
}
12.
Ram.h
#pragma once
class Ram{
char mem[100 * 1024];
int size;
public:
Ram();
~Ram();
char read(int address);
void write(int address, char value);
};
Ram.cpp
#include <iostream>
#include "Ram.h"
using namespace std;
Ram::Ram()
{
for (int i = 0; i < 100 * 1024; i++)
{
mem[i] = 0;
}
size = 100 * 1024;
}
Ram::~Ram()
{
cout << "메모리 제거됨" << endl;
}
char Ram::read(int address)
{
return mem[address];
}
void Ram::write(int address, char value)
{
mem[address] = value;
}
main.cpp
#include <iostream>
#include "Ram.h"
using namespace std;
int main()
{
Ram ram;
ram.write(100, 20);
ram.write(101, 30);
char res = ram.read(100) + ram.read(101);
ram.write(102, res);
cout << "102 번지의 값 = " << (int)ram.read(102) << endl;
}
728x90
'C++ > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ 5장 연습문제 - 실습 문제 (0) | 2022.01.10 |
---|---|
명품 C++ 5장 연습문제 - 이론 문제 (5) | 2022.01.08 |
명품 C++ 4장 연습문제 - 실습 문제 (0) | 2022.01.03 |
명품 C++ 4장 연습문제 - 이론 문제 (0) | 2022.01.01 |
명품 C++ 3장 연습문제 - 이론 문제 (0) | 2021.12.06 |
댓글