C++-Input integers to display hexadecimal and octal (?: the use of conditional operators)

Write a program, enter an integer, and display its decimal, hexadecimal and octal forms. Such as input -31, output:

-31 -1f -37

Input: an integer, note that it may be a negative number.

Output: Three numbers, decimal, hexadecimal and octal, separated by spaces.

Prompt: Set the input integer as n. (1) Use? : The conditional operator evaluates the absolute value and assigns it to another variable. (2) Decimal, hexadecimal and octal forms, respectively output dec, hex, oct before output, such as cout<<oct<<m; (3) If n is less than 0, output a negative sign first, without line break, Then output an integer.

Note that even if you know the if statement, you should not use it here.

Sample 1 input:

-31

Sample 1 output:

-31 -1f -37

Time limit: 500ms Memory limit: 32000kb

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    
    
int c;
while(1)
{
    
    cin>>c;
c>=0?cout<<dec<<c<<" "<<hex<<c<<" "<<oct<<c:cout<<dec<<"-"<<-c<<" "<<"-"<<hex<<-c<<" "<<oct<<"-"<<-c;
    return 0;
}
}

Mainly right? : The use of conditional operators The
expression is: expression 1? Expression 2: Expression 3
For example:

max=(a>b)?a:b

The semantics is that if a>b is true, then a is assigned to max, otherwise b is assigned to max.
Equivalent to

if(a>b)
max=a;
else
max=b;

Guess you like

Origin blog.csdn.net/qq_41017444/article/details/104566489