42Seoul/CPP Module 06

ex00

재윤 2024. 1. 26. 17:10
반응형

들어가기 전 공부하고 가자

 

리터널, 상수

https://wo-dbs.tistory.com/164

 

리터널, 상수

이번에는 프로그래밍 언어에서 리터널, 상수라는 이야기가 나오게 되는데 이걸 공부해보자. 리터널 리터널(literal)은 프로그래밍 언어에서 고정된 값을 나타내는 표현 방식 코드 상에서 직접 사

wo-dbs.tistory.com

 

 

 inff, inf, nan, NAN

https://wo-dbs.tistory.com/165

 

inff, inf, nan, NAN

inff, inf inff, inf는 양의 무한대를 나타내는 부동 소수점 리터널 상수이다. double, float에서 사용하는 상수가 나뉘어지는데 밑 표를 보고 이히해보자. double float 양의 무한대 부동 소수점 리터널 상

wo-dbs.tistory.com

 

static_cast 

https://wo-dbs.tistory.com/166

 

static_cast

static_cast는 언어에서 지원하는 명시적 변환을 수행한다. → 작은 데이터 타입에서 큰 데이터 타입으로 바꾸기 예를 들어 다음 코드처럼 정수에 대한 나눗셈이 아닌 부동소수점에 대한 나눗셈으

wo-dbs.tistory.com

  • ex00은 static_cast를 사용해보라는 문제이다. 여기서 하나의 수를 받아서 char, int, float, double로 바 꿀 때 static_cast를 사용해서 나타내보는 문제이다.
  • 이 문제는 잘 생각해야 될 게 예외처리이다..
  • 생각하게 될 예외처리는 float, double에서 나타낼 때 처리를 잘 해주어야한다.

Convert.cpp

#include "Convert.hpp"
#include <cmath>

Convert::Convert(void) : value("None"), doubleValue(0), endptr(NULL)
{

}

Convert::Convert(std::string value) : value(value)
{
    this->doubleValue = strtod(this->value.c_str(), &(this->endptr));
}

Convert::Convert(Convert const &convert)
{
    *this = convert;    
}

Convert::~Convert(void)
{

} 

Convert& Convert::operator=(Convert const &convert) 
{
    if (this == &convert)
        return *this;
    this->value = convert.getValue();
    this->doubleValue = convert.getDoubleValue();
    this->endptr = convert.getEndPtr();
    return *this;
}

void Convert::charConvert(void) const
{
    std::cout << "char: ";

	if(std::isnan(this->doubleValue) || std::isinf(this->doubleValue))
		std::cout << "impossible\\n";
	else if (this->doubleValue <= 31 || this->doubleValue > 128)
	{
		if ((this->doubleValue >= 0 && this->doubleValue <= 31) || \\
			this->doubleValue == 127)
			std::cout << "Non displayable\\n";
		else
			std::cout << "impossible\\n";
    }
	else
	{
        std::cout << "'" << static_cast<char>(doubleValue) << "'\\n";
    }
}

void Convert::intConvert(void) const
{
    std::cout << "int: ";

	if(std::isnan(this->doubleValue) || std::isinf(this->doubleValue))
		std::cout << "impossible\\n";
	else if (this->doubleValue < INT_MIN || this->doubleValue > INT_MAX) 
	{
		std::cout << "impossible\\n";
	} 
	else 
	{
		std::cout << static_cast<int>(doubleValue) << "\\n";
	}
}

void Convert::floatConvert(void) const
{
    std::cout << "float: ";

   	if(std::isnan(this->doubleValue) || std::isinf(this->doubleValue))
		std::cout << std::showpos << static_cast<float>(doubleValue) << "\\n";
    else if (static_cast<int>(doubleValue) == doubleValue) 
	{
		if (value.length() <= 6)
          std::cout << static_cast<float>(doubleValue) << ".0f\\n";
        else
          std::cout << static_cast<float>(doubleValue) << "f\\n";
    } 
    else if(value.find(".") != std::string::npos)
    {
		std::cout << static_cast<float>(doubleValue) << "f\\n";
    }
    else
	{
		std::cout << static_cast<float>(doubleValue) << "f\\n";
    }
}

void Convert::doubleConvert(void) const
{
    std::cout << "double: ";

	if(std::isnan(this->doubleValue) || std::isinf(this->doubleValue))
		std::cout << std::showpos << static_cast<float>(doubleValue) << "\\n";
	else if (static_cast<int>(doubleValue) == doubleValue)
    {      
        if (value.length() <= 6)
          std::cout << doubleValue << ".0\\n";
        else
          std::cout << static_cast<float>(doubleValue) << "\\n";
    } else if (this->value.find(".") != std::string::npos) {
        std::cout << doubleValue << "\\n";
    } else {
        std::cout << doubleValue << "\\n";
    }
}

bool Convert::checkInfo(void) const
{
    if (*(this->endptr) && !(*(this->endptr) == 'f'))
        return false;
    return true;
}

void Convert::convertAll(void) const
{
    if (!checkInfo())
        std::cout << "Wrong inputs\\n";
    else
    {
        this->charConvert();
        this->intConvert();
        this->floatConvert();
        this->doubleConvert();
    }

}

std::string Convert::getValue(void) const
{
    return this->value;
}

double Convert::getDoubleValue(void) const
{
    return this->doubleValue;
}

char *Convert::getEndPtr(void) const
{
    return this->endptr;
}

 

hpp

#ifndef CONVERT_HPP
#define CONVERT_HPP

#include <iostream>
#include <limits.h>
#include <cstdlib>

class Convert {
private:
    std::string value;
    double doubleValue;
    char *endptr;
public:
    Convert();
    Convert(std::string value);
    Convert(Convert const &convert);
    ~Convert();
    Convert& operator=(Convert const &convert);

    void charConvert() const;
    void intConvert() const;
    void floatConvert() const;
    void doubleConvert() const;
    void convertAll() const;

   // bool checkValid(std::string value) const;
    bool checkInfo() const;

    std::string getValue(void) const;
    double getDoubleValue(void) const;
    char *getEndPtr(void) const;
};

#endif
반응형

'42Seoul > CPP Module 06' 카테고리의 다른 글

ex02  (1) 2024.01.26
ex01  (1) 2024.01.26