좋은 하루들. 그래서 과제를 위해, 나는 다양한 기능의 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;
}
'반환 유형은 당신이 전혀 값을 반환하지 않겠다고 약속을 의미 void'한다. 그런 다음 가서'return decimalNumber;', 약속을 어기십시오. 하나 또는 다른 사람들이 동의하도록 변경해야합니다. –
그래서 나는 void를 제거하고 bool과 같은 것을 제공해야합니까? –
'1 << y'는'y'가 정수일 때 2-to-the-pow-of-y를 계산하는 훨씬 더 빠른 방법입니다. floating에서 작동하는'pow' 함수를 호출하는 것입니다 - 포인트 번호. –