실습문제
1번
(1)
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int money, page;
public:
Book(string title = "", int money = 0, int page = 0) {
this->title = title;
this->money = money;
this->page = page;
}
Book operator+=(int x) {
money += x;
return *this;
}
Book operator-=(int x) {
money -= x;
return *this;
}
void show() {
cout << title << " " << money << "원 " << page << " 페이지" << endl;
}
};
int main() {
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a += 500;
b -= 500;
a.show();
b.show();
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int money, page;
public:
Book(string title = "", int money = 0, int page = 0) {
this->title = title;
this->money = money;
this->page = page;
}
friend void operator+=(Book &book, int x);
friend void operator-=(Book& book, int x);
void show() {
cout << title << " " << money << "원 " << page << " 페이지" << endl;
}
};
void operator+=(Book& book, int x) {
book.money += x;
}
void operator-=(Book& book, int x) {
book.money -= x;
}
int main() {
Book a("청춘", 20000, 300), b("미래", 30000, 500);
a += 500;
b -= 500;
a.show();
b.show();
}
2번
(1)
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int money, page;
public:
Book(string title = "", int money = 0, int page = 0) {
this->title = title;
this->money = money;
this->page = page;
}
bool operator==(int x) {
if (money == x)
return true;
return false;
}
bool operator==(string x) {
if (title.compare(x) == 0)
return true;
return false;
}
bool operator==(Book x) {
if (title == x.title && money == x.money && x.page == page) {
return true;
}
return false;
return false;
}
void show() {
cout << title << " " << money << "원 " << page << " 페이지" << endl;
}
};
int main() {
Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
if (a == 30000) cout << "정가 30000원" << endl;
if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
if (a == b) cout << "두 책이 같은 책입니다." << endl;
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int money, page;
public:
Book(string title = "", int money = 0, int page = 0) {
this->title = title;
this->money = money;
this->page = page;
}
friend bool operator==(Book& book, int x);
friend bool operator==(Book& book, string x);
friend bool operator==(Book& book, Book x);
void show() {
cout << title << " " << money << "원 " << page << " 페이지" << endl;
}
};
bool operator==(Book& book, int x) {
if (book.money == x)
return true;
return false;
}
bool operator==(Book& book, string x) {
if (book.title.compare(x) == 0)
return true;
return false;
}
bool operator==(Book& book, Book x) {
if (book.title == x.title && book.money == x.money && x.page == book.page) {
return true;
}
return false;
}
int main() {
Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
if (a == 30000) cout << "정가 30000원" << endl;
if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
if (a == b) cout << "두 책이 같은 책입니다." << endl;
}
3번
#include <iostream>
#include <string>
using namespace std;
class Book {
string name;
int price, pages;
public:
Book(string name = "", int price = 0, int pages = 0) {
this->name = name;
this->price = price;
this->pages = pages;
}
bool operator !() {
if (price == 0)
return true;
return false;
}
};
int main() {
Book book("벼룩시장", 0, 50);
if (!book) cout << "공짜다" << endl;
}
4번
#include <iostream>
#include <string>
using namespace std;
class Book {
string title;
int money, page;
public:
Book(string title = "", int money = 0, int page = 0) {
this->title = title;
this->money = money;
this->page = page;
}
string getTitle() {
return title;
}
friend bool operator <(Book a, Book b);
};
bool operator <(Book a, Book b) {
if (a.getTitle() < b.getTitle()) {
return true;
}
return false;
}
int main() {
Book a("청춘", 20000, 300);
string b;
cout << "책 이름을 입력하세요>> ";
getline(cin, b);
if (b < a) {
cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}
}
5번
(1)
#include <iostream>
#include <string>
using namespace std;
class Color {
int r, g, b;
public:
Color(int r = 0, int g = 0, int b = 0) {
this->r = r;
this->g = g;
this->b = b;
}
Color operator+ (Color op) {
;
r = r + op.r;
b = b + op.b;
g = g + op.g;
return *this;
}
bool operator == (Color op) {
if (r == op.r && g == op.g && b == op.b)
return true;
return false;
}
void show() {
cout << r << " " << g << " " << b << endl;
}
};
int main() {
Color red(255, 0, 0), blue(0, 0, 255), c;
c = red + blue;
c.show();
Color fuchsia(255, 0, 255);
if (c == fuchsia)
cout << "보라색 맞음";
else
cout << "보라색 아님";
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Color {
int r, g, b;
public:
Color(int r = 0, int g = 0, int b = 0) {
this->r = r;
this->g = g;
this->b = b;
}
friend Color operator+ (Color op1, Color& op2);
friend bool operator == (Color op1, Color op2);
void show() {
cout << r << " " << g << " " << b << endl;
}
};
Color operator+ (Color op1, Color &op2) {
op2.r = op1.r + op2.r;
op2.b = op1.b + op2.b;
op2.g = op1.g + op2.g;
return op2;
}
bool operator == (Color op1, Color op2) {
if (op2.r == op1.r && op2.g == op1.g && op2.b == op1.b)
return true;
return false;
}
int main() {
Color red(255, 0, 0), blue(0, 0, 255), c;
c = red + blue;
c.show();
Color fuchsia(255, 0, 255);
if (c == fuchsia)
cout << "보라색 맞음";
else
cout << "보라색 아님";
}
6번
(1)
#include <iostream>
#include <string>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix(int arr1 = 0, int arr2 = 0, int arr3 = 0, int arr4 = 0) {
arr[0] = arr1;
arr[1] = arr2;
arr[2] = arr3;
arr[3] = arr4;
}
Matrix operator +(Matrix op) {
Matrix tmpMatrix;
for (int i = 0; i < 4; i++) {
tmpMatrix.arr[i] = arr[i] + op.arr[i];
}
return tmpMatrix;
}
void operator += (Matrix op) {
for (int i = 0; i < 4; i++) {
arr[i] += op.arr[i];
}
}
bool operator == (Matrix op) {
for (int i = 0; i < 4; i++) {
if (arr[i] != op.arr[i]) return false;
}
return true;
}
void show() {
cout << "Matrix = {";
for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
cout << "}" << endl;
}
};
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show();
b.show();
c.show();
if (a == c)
cout << "a and c are the same" << endl;
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix(int arr1 = 0, int arr2 = 0, int arr3 = 0, int arr4 = 0) {
arr[0] = arr1;
arr[1] = arr2;
arr[2] = arr3;
arr[3] = arr4;
}
friend Matrix operator +(Matrix op1, Matrix op2);
friend void operator += (Matrix &op1, Matrix op2);
friend bool operator == (Matrix op1, Matrix op2);
void show() {
cout << "Matrix = {";
for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
cout << "}" << endl;
}
};
Matrix operator +(Matrix op1, Matrix op2) {
Matrix tmpMatrix;
for (int i = 0; i < 4; i++) {
tmpMatrix.arr[i] = op1.arr[i] + op2.arr[i];
}
return tmpMatrix;
}
void operator += (Matrix &op1, Matrix op2) {
for (int i = 0; i < 4; i++) {
op1.arr[i] += op2.arr[i];
}
}
bool operator == (Matrix op1, Matrix op2) {
for (int i = 0; i < 4; i++) {
if (op2.arr[i] != op1.arr[i]) return false;
}
return true;
}
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show();
b.show();
c.show();
if (a == c)
cout << "a and c are the same" << endl;
}
7번
(1)
#include <iostream>
#include <string>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix(int arr1 = 0, int arr2 = 0, int arr3 = 0, int arr4 = 0) {
arr[0] = arr1;
arr[1] = arr2;
arr[2] = arr3;
arr[3] = arr4;
}
void operator >> (int *arrs) {
for (int i = 0; i < 4; i++) {
arrs[i] = arr[i];
}
}
void operator << (int *arrs) {
for (int i = 0; i < 4; i++) {
arr[i] = arrs[i];
}
}
void show() {
cout << "Matrix = {";
for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
cout << "}" << endl;
}
};
int main() {
Matrix a(4, 3, 2, 1), b;
int x[4], y[4] = { 1,2,3,4 };
a >> x;
b << y;
for (int i = 0; i < 4; i++) cout << x[i] << ' ';
cout << endl;
b.show();
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix(int arr1 = 0, int arr2 = 0, int arr3 = 0, int arr4 = 0) {
arr[0] = arr1;
arr[1] = arr2;
arr[2] = arr3;
arr[3] = arr4;
}
friend void operator >> (Matrix op, int* arrs);
friend void operator << (Matrix &op, int* arrs);
void show() {
cout << "Matrix = {";
for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
cout << "}" << endl;
}
};
void operator >> (Matrix op, int* arrs) {
for (int i = 0; i < 4; i++) {
arrs[i] = op.arr[i];
}
}
void operator << (Matrix &op, int* arrs) {
for (int i = 0; i < 4; i++) {
op.arr[i] = arrs[i];
}
}
int main() {
Matrix a(4, 3, 2, 1), b;
int x[4], y[4] = { 1,2,3,4 };
a >> x;
b << y;
for (int i = 0; i < 4; i++) cout << x[i] << ' ';
cout << endl;
b.show();
}
8번
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { this->radius = radius; }
void show() { cout << "radius = " << radius << "인 원" << endl;}
friend void operator ++(Circle &r);
friend Circle operator ++(Circle &r, int x);
};
void operator ++(Circle &r) {
r.radius += 1;
}
Circle operator ++(Circle &r, int x) {
Circle tempCircle = r;
r.radius += 1;
return tempCircle;
}
int main() {
Circle a(5), b(4);
++a;
b = a++;
a.show();
b.show();
}
9번
#include <iostream>
#include <string>
using namespace std;
class Circle {
int radius;
public:
Circle(int radius = 0) { this->radius = radius; }
void show() { cout << "radius = " << radius << "인 원" << endl;}
friend Circle operator +(int x, Circle op);
};
Circle operator +(int x, Circle op) {
op.radius += x;
return op;
}
int main() {
Circle a(5), b(4);
b = 1 + a;
a.show();
b.show();
}
10번
#include <iostream>
#include <string>
using namespace std;
class Statistics {
int *arr;
int size;
int max;
public:
Statistics(int max = 10) {
size = 0;
arr = new int[max];
}
bool operator!() {
if (size == 0) return true;
else false;
}
void operator~() {
for (int i = 0; i < size; i++) {
if (arr[i] != NULL) {
cout << arr[i] << ' ';
}
}
cout << endl;
}
void operator >> (int& a) {
int sum = 0;
for (int i = 0; i < size; i++) sum += arr[i];
a = sum / size;
}
Statistics& operator << (int b) {
arr[size] = b;
size++;
return *this;
}
~Statistics() { delete[] arr; }
};
int main() {
Statistics stat;
if (!stat) cout << "현재 통계 데이타가 없습니다." << endl;
int x[5];
cout << "5 개의 정수를 입력하라>>";
for (int i = 0; i < 5; i++) cin >> x[i];
for (int i = 0; i < 5; i++) stat << x[i];
stat << 100 << 200;
~stat;
int avg;
stat >> avg;
cout << "avg=" << avg << endl;
}
11번
#include <iostream>
#include <string>
using namespace std;
class Stack {
int arr[10];
int idx;
public:
Stack() { for (int i = 0; i < 10; i++) arr[i] = 0; idx = 0; }
Stack& operator << (int n) {
arr[idx++] = n;
return *this;
}
bool operator !() {
if (idx <= 0) return true;
return false;
}
void operator >>(int& n) {
n = arr[idx - 1];
idx--;
}
};
int main() {
Stack stack;
stack << 3 << 5 << 10;
while (true) {
if (!stack) break;
int x;
stack >> x;
cout << x << ' ';
}
cout << endl;
}
12번
#include <iostream>
#include <string>
using namespace std;
class SortedArray {
int size;
int* p;
void sort();
public:
SortedArray();
SortedArray(SortedArray& src);
SortedArray(int p[], int size);
~SortedArray();
SortedArray operator+(SortedArray& op2);
SortedArray& operator=(const SortedArray& op2);
void show();
};
void SortedArray::sort() {
int temp;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - 1; j++) {
if (p[j] > p[j + 1]) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
SortedArray::SortedArray() {
p = nullptr;
size = 0;
}
SortedArray::SortedArray(SortedArray& src) {
this->size = src.size;
this->p = new int[src.size];
for (int i = 0; i < src.size; i++)
this->p[i] = src.p[i];
}
SortedArray::SortedArray(int p[], int size) {
this->size = size;
this->p = new int[size];
for (int i = 0; i < size; i++)
this->p[i] = p[i];
}
SortedArray::~SortedArray() {
delete[] p;
}
SortedArray SortedArray::operator+(SortedArray& op2) {
SortedArray tempArray;
tempArray.p = new int[this->size + op2.size];
for (int i = 0; i < this->size; i++)
tempArray.p[tempArray.size++] = this->p[i];
for (int i = 0; i < op2.size; i++)
tempArray.p[tempArray.size++] = op2.p[i];
return tempArray;
}
SortedArray& SortedArray::operator= (const SortedArray& op2) {
delete[] p;
this->size = op2.size;
this->p = new int[this->size];
for (int i = 0; i < this->size; i++) {
this->p[i] = op2.p[i];
}
return *this;
}
void SortedArray::show() {
sort();
cout << "배열 출력 : ";
for (int i = 0; i < size; i++)
cout << p[i] << ' ';
cout << endl;
}
int main() {
int n[] = { 2, 20, 6 };
int m[] = { 10, 7, 8, 30 };
SortedArray a(n, 3), b(m, 4), c;
c = a + b;
a.show();
b.show();
c.show();
}
'대학교 수업 > C++ 프로그래밍' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 7장 OpenChallenge (0) | 2022.06.09 |
---|---|
[C++] 명품 C++ 프로그래밍 8장 (0) | 2022.06.08 |
[C++] 명품 C++ 프로그래밍 6장 (0) | 2022.06.07 |
[C++] 명품 c++ 프로그래밍 5장 (0) | 2022.04.20 |
[C++] 명품 C++ 프로그래밍 4장 (0) | 2022.04.11 |