2016-11-02 2 views
-3

좋은 하루들. 그래서 과제를 위해, 나는 다양한 기능의 6 개의 클래스를 만드는 임무를 가졌다. 그러나 저는 특히 1에 문제가 있습니다. 따라서 2 진수를 취하고 10 진수로 변환하는 클래스를 만들어야합니다.C++ 바이너리 변환 클래스

 #include <iostream> 
    using std::cout; 
    using std::cin; 
    using std::endl; 


    #include "binaryconversion.h" 

    int main() 
    { 
    ///*********************************Testing BinaryConversion      Class************************************************/ 
    cout<<"Creating BinaryConversion object\n\n\n"; 
    BinaryConversion binary; 
    cout<<"Binary value 111 to decimal = 3\n";//7 
    cout<<"Actual function result: " << binary.binaryToDecimal(111); 
    cout<<"Binary value 101001 to decimal = 41\n"; 
    cout<<"Actual function result: " << binary.binaryToDecimal(101001); 
    cout<<"Binary value 11100 to decimal = 28\n"; 
    cout<<"Actual function result: " << binary.binaryToDecimal(11100); 

    return 0; 
    } 

나는이 가지고 .H 파일의 경우 : 아래는 내가 할당 제공된 드라이버 파일이 무엇

#ifndef BINARYCONVERSION_H 
    #define BINARYCONVERSION_H 

    #include <iostream> 
    using std::cout; 
    using std::cin; 
    using std::endl; 

    class BinaryConversion 
    { 
    public: 
     BinaryConversion(); 
     void binaryToDecimal(int); 


    }; 

    #endif // BINARYCONVERSION_H 

그리고 마지막으로을의 cpp를 위해,이 있습니다

#include "binaryconversion.h" 

    BinaryConversion::BinaryConversion() 
    { 

    } 

    void BinaryConversion::binaryToDecimal(int){ 

     int decimalNumber = 0, y = 0, remainder; 
     while (x!=0) 
     { 
      remainder = n%10; 
      n /= 10; 
      decimalNumber += remainder*pow(2,y); 
      ++y 
     } 
     return decimalNumber; 
    } 

이제 내 문제는, 내가 파일을 빌드 할 때, 나는 드라이버 파일에 다음과 같은 라인을 따라 오류를 얻을 :

,164,848,843,532 10

무엇을 잘못했을 수 있습니까?

편집 : 보정의 제안을 모두 수행 한 후 다음과 같이 현재의 코드는 다음과 같습니다

#include <iostream> 
    using std::cout; 
    using std::cin; 
    using std::endl; 

    class BinaryConversion 
    { 
    public: 
     BinaryConversion(); 
     int binaryToDecimal(int x); 

    private: 
     int x; 
     int n; 


    }; 

    #endif // BINARYCONVERSION_H 

통화 당 :

int BinaryConversion::binaryToDecimal(int x){ 

     int decimalNumber = 0, y = 0, remainder; 
     while (x!=0) 
     { 
      remainder = x%10; 
      x /= 10; 
      decimalNumber += remainder* 1 >> y; 
      ++y; 
     } 
     return decimalNumber; 
    } 
+2

'반환 유형은 당신이 전혀 값을 반환하지 않겠다고 약속을 의미 void'한다. 그런 다음 가서'return decimalNumber;', 약속을 어기십시오. 하나 또는 다른 사람들이 동의하도록 변경해야합니다. –

+0

그래서 나는 void를 제거하고 bool과 같은 것을 제공해야합니까? –

+0

'1 << y'는'y'가 정수일 때 2-to-the-pow-of-y를 계산하는 훨씬 더 빠른 방법입니다. floating에서 작동하는'pow' 함수를 호출하는 것입니다 - 포인트 번호. –

답변

0

귀하의 void BinaryConversion::binaryToDecimal(int) 기능이 올바르게 implemented.Firstly 아니라, 함수에 값을 전달했지만 매개 변수 이름이 올바르게 작성되지 않았습니다. 그것은 binaryToDecimal(int x)이어야합니다.이 때문에 당신은이 오류가 발생합니다.

두 번째로 반환 형식은 호출 함수에 정수 값을 반환하기 때문에 void가 아니어야합니다. 따라서 반환 유형은 int이어야합니다.

기능은 다음과 같이해야합니다

void BinaryConversion::binaryToDecimal(int x){ 
int decimalNumber = 0, y = 0, remainder; 
    while (x!=0){ 
    remainder = x%10; 
    x /= 10; 
    decimalNumber += remainder*pow(2,y); 
    ++y; 
    } 
    return decimalNumber; 
} 
+0

헤더 밑에 코드는 int로 읽혀야한다. binaryToDecimal (int x); 그런 다음 함수를 배치해야합니다. –

+0

내 결과 이진 값 (111) = 3 실제 함수 결과를 진수로 나온 : 1BINARY 값 101,001은 = 41 실제 함수 결과를 진수하는 : 1BINARY 값 11100은 = 28 실제 함수 결과를 진수하는 : 0Press –

+0

당신은 단지 코드가 구성 변경 함수 본문에. –