CONVERT 문제 오답처리에 관한 문제

  • yhb0730
    yhb0730

    문제의 예제에서는 출력이 똑같이 나오는데 오답처리가 되서 질문드립니다.
    소숫점 정밀도가 문제인가 싶어서 단위변환을 소숫점 6자리까지 늘려서 해봤는데 그렇게되면 문제의 예제와 다른 답이 나와서 문제에서 주어진 환산으로 다시 바꿨습니다. 혹시 어떤게 문제인지 알 수 있을까요? 어떤게 문제인지 잘 모르겠습니다

    #include<iostream>
    #include<cstring>
    #include<iomanip>
    
    using namespace std;
    
    
    class Converter
    {
    private :
        double data;
        char unit[3];
    public :
        Converter(double data, char* unit) { this->setData(data); this->setUnit(unit); }
        virtual ~Converter(){};
        virtual void convert(){};
        double getData() {  return this->data;}
        void setData(double data) { this->data = data; }
        char* getUnit() { return this->unit; }
        void setUnit(char *unit);
    };
    
    void Converter::setUnit(char *unit)
    {
        int i = 0;
        while (1)
        {
            if (unit[i] == '\0')
            {
                this->unit[i] = '\0';
                break;
            }
            this->unit[i] = unit[i++];
        }
    }
    
    class Kg_Pound : public Converter
    {
    private :
        static double KtoP;
        static double PtoK;
    public :
        Kg_Pound(double data, char* unit) : Converter(data, unit) { }
        void convert();
    };
    
    void Kg_Pound::convert()
    {
        char *unit = this->getUnit();
        double data = this->getData();
        cout << showpoint << fixed << setprecision(4);
        if (!strcmp(unit, "kg"))
        {
            cout << data * this->KtoP << " lb" << endl;
        }
        else
        {
            cout << data * this->PtoK << " kg" << endl;;
        }
    }
    
    class Liter_Gallon : public Converter
    {
    private :
        static double LtoG;
        static double GtoL;
    public : 
        Liter_Gallon(double data, char* unit) : Converter(data, unit){}
        void convert();
    };
    
    void Liter_Gallon::convert()
    {
        char *unit = this->getUnit();
        double data = this->getData();
        cout << fixed;
        cout.precision(4);
        if (!strcmp(unit, "l"))
        {
            cout << data * this->LtoG << " g" << endl;
        }
        else
        {
            cout << data * this->GtoL << " l" << endl;
        }
    }
    
    double Kg_Pound::KtoP = 2.2046;
    double Kg_Pound::PtoK = 0.4536;
    double Liter_Gallon::GtoL = 3.7854;
    double Liter_Gallon::LtoG = 0.2642;
    
    
    int main(void)
    {
        int testcase;
        double data;
        char unit[3];
        Converter* con;
        cin >> testcase;
        for (int i = 0; i < testcase; ++i)
        {
            cin >> data >> unit;
            if (!strcmp(unit, "kg") || !strcmp(unit, "lb"))
            {
                con = new Kg_Pound(data, unit);
            }
            else
            {
                con = new Liter_Gallon(data, unit);
            }
            cout << i+1 << " ";
            con->convert();
            delete con;
        }
    }
    

    7년 전
3개의 댓글이 있습니다.
  • Corea
    Corea

    다음 입력을 넣어보세요.

    2
    0 l
    0 g

    7년 전 link
  • yhb0730
    yhb0730

    두개를 넣었을때
    1 0.0000 g
    2 0.0000 l
    이 출력되는데 혹시 이렇게 출력되면 안되나요?


    7년 전 link
  • Corea
    Corea

    아.. 컴파일러마다 다를 것 같네요.

    this->unit[i] = unit[i++];

    이 부분이 잘못되었습니다.


    7년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.