본문 바로가기
프로그램/C++ 1000제

C++ 115제] 인하대 과제4

by 건티 2024. 10. 6.
728x90

출처 : 반크_백제역사 유전지구와 이스탐블 역사지구

 

참고풀이1] 하나의 객체 파일로 구현하기

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include<cstring>
#include <cstdlib>
#include <iomanip> //setw(), setfill()

using namespace std;

class Point
{
private:
   int x, y;
   char code[3];
   char data[100];
public:
   Point(int x = 0, int y = 0) : x(x), y(y) { };
   int getX() { return x; }
   int getY() { return y; }
   bool operator==(const Point& p2)
   {
      return this->x == p2.x && this->y == p2.y;
   }
   bool operator!=(const Point& p2)
   {
      return !(*this == p2);
   }
   bool operator>(const Point& p2)
   {
      return this->x > p2.x && this->y > p2.y;
   }
   bool operator<=(const Point& p2)
   {
      return this->x <= p2.x && this->y <= p2.y;
   }

   Point operator+(const Point& p2);
   Point operator*(const Point& p2);

   Point& operator++() //전위 연산자 중복 정의
   {
      ++(this->x);
      ++(this->y);
      return *this;
   }

   const Point operator++(int) //후위 연산자 중복정의
   {
      Point temp = { *this };
      ++(this->x);
      ++(this->y);
      return temp;
   }

   Point operator+=(const Point& p2);
   Point operator-=(const Point& p2);

   friend istream& operator >>(istream& input, Point& in)
   {
      input.getline(in.data,100, ')'); //(5, 5) (3, 3)

      int i;
      int index = 0;
      int num[2] = { 0 };
      for (i = 1; i < strlen(in.data); i++)
      {
         if (num[1] != 0) break;
         if (in.data[i] >= '0' && in.data[i] <= '9')
         {
            num[index] = (num[index] * 10) + (int)in.data[i] - '0';
         }
         else if (in.data[i] == ' ') continue;
         else if (in.data[i] == ',') index++;
      }
      in.x = num[0];
      in.y = num[1];

      return input;
   }

   friend ostream& operator<<(ostream& output, const Point& v)
   {
      if (strcmp(v.code, "*") == 0)
         output << v.x + v.y;
      else
         output << "(" << v.x << ", " << v.y << ")";

      return output;
   }
};

Point Point::operator+(const Point& p2)
{
   Point v;
   strcpy(v.code, "+");
   v.x = this->x + p2.x;
   v.y = this->y + p2.y;

   return v;
}

Point Point::operator*(const Point& p2)
{
   Point v;
   strcpy(v.code, "*");
   v.x = this->x * p2.x;
   v.y = this->y * p2.y;

   return v;
}

Point Point::operator+=(const Point& p2)
{
   strcpy(this->code, "+=");
   this->x = this->x + p2.x;
   this->y = this->y + p2.y;

   return *this;
}

Point Point::operator-=(const Point& p2)
{
   strcpy(this->code, "-=");
   this->x = this->x - p2.x;
   this->y = this->y - p2.y;

   return *this;
}

int main()
{
   cout << "Input 2 points: ";
   Point p1, p2; //p1(5, 5), p2(3, 3);

   cin >> p1 >> p2;

   if (p1 == p2) cout << "p1 == p2" << endl;
   if (p1 != p2) cout << "p1 != p2" << endl;
   if (p1 > p2) cout << "p1 > p2" << endl;
   if (p1 <= p2) cout << "p1 <= p2" << endl;

   cout << endl;
   cout << "p1 + p2 = " << (p1 + p2) << endl;
   cout << "p1 * p2 = " << (p1 * p2) << endl;

   cout << endl;
   p1 -= p2++;
   cout << "p1 -= p2++" << endl;
   ++p1 += p2;
   cout << "++p1 += p2" << endl;
   cout << "p1 = " << p1 << endl;

   return 0;
}

 

참고풀이1 결과]

 

참고풀이2] 헤더, 멤버 함수 정의, main()로 구분하기

Point.h)

//Point.h
#ifndef POINT_H
#define POINT_H

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include<cstring>
#include <cstdlib>
using namespace std;

class Point
{
private:
   int x, y;
   char code[3];
   char data[100];
public:
   Point(int x = 0, int y = 0) : x(x), y(y) { };
   int getX() { return x; }
   int getY() { return y; }
   bool operator==(const Point& p2)
   {
      return this->x == p2.x && this->y == p2.y;
   }

 

   bool operator!=(const Point& p2)
   {
      return !(*this == p2);
   }

 

   bool operator>(const Point& p2)
   {
      return this->x > p2.x && this->y > p2.y;
   }

 

   bool operator<=(const Point& p2)
   {
      return this->x <= p2.x && this->y <= p2.y;
   }

   Point& operator++() //전위 연산자 중복 정의
   {
      ++(this->x);
      ++(this->y);
      return *this;
   }

   const Point operator++(int) //후위 연산자 중복정의
   {
      Point temp = { *this };
      ++(this->x);
      ++(this->y);
      return temp;
   }

   friend istream& operator >>(istream& input, Point& in)
   {
      input.getline(in.data, 100, ')'); //(5, 5) (3, 3)

      int i;
      int index = 0;
      int num[2] = { 0 };
      for (i = 1; i < strlen(in.data); i++)
      {
         if (num[1] != 0) break;
         if (in.data[i] >= '0' && in.data[i] <= '9')
         {
             num[index] = (num[index] * 10) + (int)in.data[i] - '0';
         }
         else if (in.data[i] == ' ') continue;
         else if (in.data[i] == ',') index++;
      }
      in.x = num[0];
      in.y = num[1];

      return input;
   }

   friend ostream& operator<<(ostream& output, const Point& v)
   {
      if (strcmp(v.code, "*") == 0)
         output << v.x + v.y;
      else
         output << "(" << v.x << ", " << v.y << ")";

      return output;
   }

 

   Point operator+(const Point& p2);
   Point operator*(const Point& p2);

   Point operator+=(const Point& p2);
   Point operator-=(const Point& p2);

};

#endif

 

Point.cpp)

#define _CRT_SECURE_NO_WARNINGS
#include "Point.h"
using namespace std;

Point Point::operator+(const Point& p2)
{
   Point v;
   strcpy(v.code, "+");
   v.x = this->x + p2.x;
   v.y = this->y + p2.y;

   return v;
}

Point Point::operator*(const Point& p2)
{
   Point v;
   strcpy(v.code, "*");
   v.x = this->x * p2.x;
   v.y = this->y * p2.y;

   return v;
}

Point Point::operator+=(const Point& p2)
{
   strcpy(this->code, "+=");
   this->x = this->x + p2.x;
   this->y = this->y + p2.y;

   return *this;
}

Point Point::operator-=(const Point& p2)
{
   strcpy(this->code, "-=");
   this->x = this->x - p2.x;
   this->y = this->y - p2.y;

   return *this;
}

 

Main.cpp)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Point.h"
using namespace std;

int main()
{
   cout << "Input 2 points: ";
   Point p1, p2; //p1(5, 5), p2(3, 3);

   cin >> p1 >> p2;

   if (p1 == p2) cout << "p1 == p2" << endl;
   if (p1 != p2) cout << "p1 != p2" << endl;
   if (p1 > p2) cout << "p1 > p2" << endl;
   if (p1 <= p2) cout << "p1 <= p2" << endl;

   cout << endl;
   cout << "p1 + p2 = " << (p1 + p2) << endl;
   cout << "p1 * p2 = " << (p1 * p2) << endl;

   cout << endl;
   p1 -= p2++;
   cout << "p1 -= p2++" << endl;
   ++p1 += p2;
   cout << "++p1 += p2" << endl;
   cout << "p1 = " << p1 << endl;

   return 0;
}

 

참고풀이2 결과]

 

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 겨울

 

 

반응형

댓글