1번
#include<iostream>
using namespace std;
class Tower{
int height;
public:
Tower();
Tower(int a);
int getHeight();
};
Tower::Tower(){
height = 1;
}
Tower::Tower(int a){
height = a;
}
int Tower::getHeight(){
return height;
}
int main()
{
Tower myTower;
Tower seoulTower(100);
cout << "높이는 " << myTower.getHeight() << "미터" << endl;
cout << "높이는 " << seoulTower.getHeight() << "미터" << endl;
}
2번
#include<iostream>
using namespace std;
class Date{
int year;
int month;
int day;
public:
Date(int a, int b, int c);
Date(string str);
void show();
int getYear();
int getMonth();
int getDay();
};
Date::Date(int a, int b, int c){
year = a;
month = b;
day = c;
}
Date::Date(string str){
year = stoi(str);
month = stoi(str.substr(5,6));
day = stoi(str.substr(7,9));
}
void Date::show(){
cout << year << "년" << month << "월" << day << "일" << endl;
}
int Date::getYear(){
return year;
}
int Date::getMonth(){
return month;
}
int Date::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>
using namespace std;
class Account{
int money;
string name;
int id;
public:
Account(string a, int b, int c);
void deposit(int a);
string getOwner();
int inquiry();
int withdraw(int a);
};
Account::Account(string a, int b, int c){
name = a;
money = c;
id = b;
}
void Account::deposit(int a){
money += a;
}
int Account::withdraw(int a){
money -= a;
return money;
}
string Account::getOwner(){
return name;
}
int Account::inquiry(){
return money;
}
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 water;
int coffee;
int sugar;
public:
CoffeeMachine(int a, int b, int c);
void drinkEspresso();
void drinkAmericano();
void drinkSugerCoffee();
void fill();
void show();
};
CoffeeMachine::CoffeeMachine(int a, int b, int c){
coffee = a;
water = b;
sugar = c;
}
void CoffeeMachine::drinkEspresso(){
coffee -= 1;
water -= 1;
}
void CoffeeMachine::drinkAmericano(){
coffee -= 1;
water -= 2;
}
void CoffeeMachine::drinkSugerCoffee(){
coffee -= 1;
water -= 2;
sugar -= 1;
}
void CoffeeMachine::show(){
cout << "커피 마신 상태, 커피:" << coffee << " 물:" << water << " 설탕:" << sugar << endl;
}
void CoffeeMachine::fill(){
water = 10;
sugar = 10;
coffee = 10;
}
int main()
{
CoffeeMachine java(5, 10, 3);
java.drinkEspresso();
java.show();
java.drinkAmericano();
java.show();
java.drinkSugerCoffee();
java.show();
java.fill();
java.show();
return 0;
}
5번
#include<iostream>
#include <cstdlib>
using namespace std;
class Random{
public:
int next();
int nextInRange(int a, int b);
};
int Random::next(){
int n = rand();
return n;
}
int Random::nextInRange(int a, int b){
int n = rand() % (b - a + 1) + a;
return n;
}
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;
return 0;
}
6번
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class EvenRandom{
public:
int next();
int nextInRange(int a, int b);
};
int EvenRandom::next(){
while(1){
int n = rand();
if(n % 2 == 0){
return n;
}
}
}
int EvenRandom::nextInRange(int a, int b){
while(1){
int n = rand() % (b - a + 1) + a;
if(n % 2 == 0){
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;
return 0;
}
7번
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class SelectableRandom{
public:
int evenNext();
int oddNextInRange(int a, int b);
};
int SelectableRandom::evenNext(){
while(1){
int n = rand();
if(n % 2 == 0){
return n;
}
}
}
int SelectableRandom::oddNextInRange(int a, int b){
while(1){
int n = rand() % (b - a + 1) + a;
if(n % 2 == 1){
return n;
}
}
}
int main()
{
SelectableRandom r;
cout << "-- 0에서 " << RAND_MAX << "까지의 정수 10개--" << endl;
for (int i = 0; i < 10; i++) {
int n = r.evenNext();
cout << n << ' ';
}
cout << endl << endl << "-- 2에서 9까지의 랜덤 정수 10개 --" << endl;
for (int i = 0; i < 10; i++) {
int n = r.oddNextInRange(2, 9);
cout << n << ' ';
}
cout << endl;
return 0;
}
8번
#include<iostream>
using namespace std;
class Integer{
public:
int n;
Integer(int a);
Integer(string a);
int get();
void set(int a);
int isEven();
};
Integer::Integer(int a){
n = a;
}
Integer::Integer(string a){
n = stoi(a);
}
int Integer::get(){
return n;
}
void Integer::set(int a){
n = a;
}
int Integer::isEven(){
if(n % 2 == 0) return 1;
else return 0;
}
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();
void set(int a, int b);
void show();
int getWidth();
int getHeight();
~Oval();
};
Oval::Oval(){
width = 0;
height = 0;
}
Oval::Oval(int a, int b){
width = a;
height = b;
}
void Oval::set(int a, int b){
width = a;
height = b;
}
void Oval::show(){
cout << "width = " << width << ", height = " << height << endl;
}
int Oval::getHeight(){
return height;
}
int Oval::getWidth(){
return width;
}
Oval::~Oval(){
cout << "Oval 소멸 : width = " << width << ", height = " << height << endl;
}
int main()
{
Oval a, b(3,4);
a.set(10, 20);
a.show();
cout << b.getWidth() << "," << b.getHeight() << endl;
}
10번
#include<iostream>
using namespace std;
class Add{
int a;
int b;
public:
void setValue(int a, int b){
this->a = a;
this->b = b;
};
void calculate(){
cout << a + b << endl;
};
};
class Sub{
int a;
int b;
public:
void setValue(int a, int b){
this->a = a;
this->b = b;
};
void calculate(){
cout << a - b << endl;
};
};
class Mul{
int a;
int b;
public:
void setValue(int a, int b){
this->a = a;
this->b = b;
};
void calculate(){
cout << a * b << endl;
};
};
class Div{
int a;
int b;
public:
void setValue(int a, int b){
this->a = a;
this->b = b;
};
void calculate(){
cout << a / b << endl;
};
};
int main()
{
Add a;
Sub s;
Mul m;
Div d;
int x, y;
char z;
while(1){
cout << "두 정수와 연산자를 입력하세요>> ";
cin >> x >> y >> z;
switch (z)
{
case '+':
a.setValue(x,y);
a.calculate();
break;
case '-':
s.setValue(x,y);
s.calculate();
break;
case '*':
m.setValue(x,y);
m.calculate();
break;
case '/':
d.setValue(x,y);
d.calculate();
break;
default:
break;
}
}
}
'대학교 수업 > C++ 프로그래밍' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 6장 (0) | 2022.06.07 |
---|---|
[C++] 명품 c++ 프로그래밍 5장 (0) | 2022.04.20 |
[C++] 명품 C++ 프로그래밍 4장 (0) | 2022.04.11 |
[C++] 명품 C++ 프로그래밍 2장 (0) | 2022.04.10 |
[C++] 명품 C++ 프로그래밍 1장 (0) | 2022.04.10 |