Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to write when binary number is converted into octal number in C++?
How to write when binary number is converted into octal number in C++?
I can't sleep. I get up at 4 o'clock. Thank you for your question, which helped me to exercise my brain.

Method 1: Do it yourself and have plenty of food and clothing.

Tip: Just convert the input binary number string into decimal, so the focus is on the to_decimal function.

# include & ltiostream & gt

# include & lt string & gt

# include & ltiomanip & gt

# include & lt restrictions & gt

Use namespace std

//Use long integer to correctly express the integer represented by the binary number string to the maximum extent.

Typedef unsigned long ulong

//The maximum bit of a long integer is usually 32 bits, which is the same as an int on windows.

const ulong max _ digit = numeric _ limits & lt; Ulong & gt* number;

//Check the input

The string check _ input ();

//Convert a string binary number into a decimal number

Ulong to_decimal (constant string & binary);

int main()

{

string binary = check _ input();

Ulong decimal = to_decimal (binary);

cout & lt& ltendl

Cout.setf (IOs _ base:: left | IOs _ base:: showbase | IOs _ base:: uppercase);

cout & lt& ltsetw( 15)& lt; & lt" Binary "<& lt":" & lt& lt binary & lt& ltendl.

cout & lt& ltsetw( 15)& lt; & lt" Decimal "<& lt":" & lt& lt decimal & lt& ltendl.

//You can use different input/output operators to control input/output formats, such as oct and hex.

cout & lt& ltsetw( 15)& lt; & lt "octal"< ":"<< oct<< decimal & lt& ltendl.

cout & lt& ltsetw( 15)& lt; & lt "hexadecimal"<& lt":" & lt& lt hexadecimal & lt& lt decimal & lt& ltendl.

cout & lt& ltendl

}

//Check whether the input binary number string is qualified.

String check_input ()

{

Cout & lt& lt "Please enter a binary number:";

String binary;

do

{

Getline(cin, binary);

//Check whether the input is empty.

if(binary.empty())

Cout & lt& lt "null input! Try again:';

//Check whether characters other than 0 and 1 are entered.

else if(binary . find _ first _ not _ of(" 10 ")! = string::NPO)

Cout & lt& lt "You cannot enter other characters"

& lt& lt "except 0 and 1. \ nPlease try again:';

//Check whether the length of the input character exceeds the maximum number of digits that can be represented.

else if(binary . size()>; max_digit)

Cout & lt& lt "You can only enter"<& ltmax _ digit & lt& lt "the number of digits."

& lt< \ nPlease try again: ";

other

Break;

}while (true);

//Returns the checked binary number string.

Returns binary;

}

//Converts a string of binary digits to decimal digits.

Ulong to_decimal (constant string & binary)

{

typedef string::const _ reverse _ iterator rite;

ulong decimal = 0;

for(rite rit = binary . Rb egin(); rit! = binary . rend(); ++rit)

{

if(*rit == '0 ')

{

Decimal & gt& gt= 1;

}

other

{

Decimal & gt& gt= 1;

decimal | = 0x80000000

}

}

Decimal & gt> = max _ digit-binary. size ();

Returns a decimal;

}

Method 2: Use the powerful standard library of C++ to realize it.

# include & ltiostream & gt

# include & lt string & gt

# include & ltiomanip & gt

# include & lt restrictions & gt

# include & ltbitset & gt// Use standard location set.

Use namespace std

//Use long integer to correctly express the integer represented by the binary number string to the maximum extent.

Typedef unsigned long ulong

//The maximum bit of a long integer is usually 32 bits, which is the same as an int on windows.

const ulong max _ digit = numeric _ limits & lt; Ulong & gt* number;

//Check the input

The string check _ input ();

int main()

{

string binary(check _ input());

//Convert to 10 decimal number

Ulong decimal = bitset & lt numerical limit & ltulong & gt* number & gt (binary). to _ ulong();

cout & lt& ltendl

Cout.setf (IOs _ base:: left | IOs _ base:: showbase | IOs _ base:: uppercase);

cout & lt& ltsetw( 15)& lt; & lt" Binary "<& lt":" & lt& lt binary & lt& ltendl.

cout & lt& ltsetw( 15)& lt; & lt" Decimal "<& lt":" & lt& lt decimal & lt& ltendl.

//You can use different input/output operators to control input/output formats, such as oct and hex.

cout & lt& ltsetw( 15)& lt; & lt "octal"< ":"<< oct<< decimal & lt& ltendl.

cout & lt& ltsetw( 15)& lt; & lt "hexadecimal"<& lt":" & lt& lt hexadecimal & lt& lt decimal & lt& ltendl.

cout & lt& ltendl

}

//Check whether the input binary number string is qualified.

String check_input ()

{

Cout & lt& lt "Please enter a binary number:";

String binary;

do

{

Getline(cin, binary);

//Check whether the input is empty.

if(binary.empty())

Cout & lt& lt "null input! Try again:';

//Check whether characters other than 0 and 1 are entered.

else if(binary . find _ first _ not _ of(" 10 ")! = string::NPO)

Cout & lt& lt "You cannot enter other characters"

& lt& lt "except 0 and 1. \ nPlease try again:';

//Check whether the length of the input character exceeds the maximum number of digits that can be represented.

else if(binary . size()>; max_digit)

Cout & lt& lt "You can only enter"<& ltmax _ digit & lt& lt "the number of digits."

& lt< \ nPlease try again: ";

other

Break;

}while (true);

//Returns the checked binary number string.

Returns binary;

}