chundoong-lab-ta/SHPC2022/hw1_answer/convert.c

102 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fallback_print_usage() {
printf("Usage: ./convert [int|long|float|double] number\n");
printf("Example: ./convert float 3.14\n");
exit(0);
}
void print_int(int x) {
// one character per one bit
char output[32 + 1] = {
0,
};
/* YOUR CODE START HERE */
int k;
unsigned int value = *((unsigned int *)&x);
size_t bits = 8 * sizeof(int);
for (k = 0; k < bits; ++k) {
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
}
/* YOUR CODE END HERE */
printf("%s\n", output);
}
void print_long(long x) {
// one character per one bit
char output[64 + 1] = {
0,
};
/* YOUR CODE START HERE */
int k;
unsigned long value = *((unsigned long *)&x);
size_t bits = 8 * sizeof(long);
for (k = 0; k < bits; ++k) {
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
}
/* YOUR CODE END HERE */
printf("%s\n", output);
}
void print_float(float x) {
// one character per one bit
char output[32 + 1] = {
0,
};
/* YOUR CODE START HERE */
int k;
unsigned int value = *((unsigned int *)&x);
size_t bits = 8 * sizeof(int);
for (k = 0; k < bits; ++k) {
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
}
/* YOUR CODE END HERE */
printf("%s\n", output);
}
void print_double(double x) {
// one character per one bit
char output[64 + 1] = {
0,
};
/* YOUR CODE START HERE */
int k;
unsigned long value = *((unsigned long *)&x);
size_t bits = 8 * sizeof(long);
for (k = 0; k < bits; ++k) {
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
}
/* YOUR CODE END HERE */
printf("%s\n", output);
}
int main(int argc, char **argv) {
if (argc != 3)
fallback_print_usage();
if (strcmp(argv[1], "int") == 0) {
print_int(atoi(argv[2]));
} else if (strcmp(argv[1], "long") == 0) {
print_long(atol(argv[2]));
} else if (strcmp(argv[1], "float") == 0) {
print_float(atof(argv[2]));
} else if (strcmp(argv[1], "double") == 0) {
print_double(atof(argv[2]));
} else {
fallback_print_usage();
}
}