Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void checkMemoryAllocation(char* array, const char* type) {
- if (array == NULL) {
- printf("ERROR: memory allocation error for %s.\n", type);
- exit(0);
- }
- }
- void printBinary(unsigned long long num, int bits) {
- for (int i = bits - 1; i >= 0; i--) {
- printf("%d", (num >> i) & 1);
- }
- }
- char* getIntegerBinary(long long num) {
- if (num < 0) num = -num;
- char* buffer = (char*)malloc(65);
- checkMemoryAllocation(buffer, "buffer");
- int i = 0;
- while (num > 0) {
- buffer[i] = num % 2 ? '1' : '0';
- num /= 2;
- i++;
- }
- buffer[i] = '\0';
- char* result = (char*)malloc(i + 1);
- checkMemoryAllocation(result, "result (integer)");
- for (int j = 0; j < i; j++) {
- result[j] = buffer[i - j - 1];
- }
- result[i] = '\0';
- if (i == 0) {
- result[0] = '0';
- result[1] = '\0';
- }
- free(buffer);
- return result;
- }
- int getBinaryToDecimalInteger(char* binary) {
- int decimal = 0;
- int length = strlen(binary);
- int base = 1; // Represents 2^(length - i - 1)
- for (int i = length - 1; i >= 0; i--) {
- if (binary[i] == '1') {
- decimal += base;
- } else if (binary[i] != '0') {
- printf("ERROR: Incorrect symbol '%c' in binary\n", binary[i]);
- return -1;
- }
- base *= 2;
- }
- return decimal;
- }
- char* getFractionalBinary(double fractional) {
- if (fractional < 0) fractional = -fractional;
- char* result = (char*)malloc(1053); // Увеличен размер массива для уменьшения погрешности
- checkMemoryAllocation(result, "result (fractional)");
- for (int i = 0; i < 1052; i++) {
- fractional *= 2;
- if (fractional >= 1.0) {
- result[i] = '1';
- fractional -= 1.0;
- } else {
- result[i] = '0';
- }
- if (fractional == 0.0) {
- result[i + 1] = '\0';
- return result;
- }
- }
- result[1052] = '\0';
- return result;
- }
- double getBinaryToDecimalFractional(char* binary) {
- double decimal = 0.0;
- double base = 0.5; // Represents 2^-(i + 1)
- for (int i = 0; binary[i] != '\0'; i++) {
- if (binary[i] == '1') {
- decimal += base;
- } else if (binary[i] != '0') {
- printf("ERROR: Incorrect symbol '%c' in binary\n", binary[i]);
- return 0.0;
- }
- base /= 2;
- }
- return decimal;
- }
- int main() {
- double number;
- printf("\n\n------------------------------------------------------------------------------------\n");
- printf("\n Enter number: ");
- scanf("%lf", &number);
- union {
- float f;
- unsigned int i;
- } converter32;
- converter32.f = (float)number;
- unsigned int sign32 = (converter32.i >> 31) & 1;
- unsigned int exponent32 = (converter32.i >> 23) & 0xFF;
- unsigned int mantissa32 = converter32.i & 0x7FFFFF;
- int exponentValue32 = exponent32 - 127;
- printf("\n---------------------------------------Float----------------------------------------\n");
- printf("\n Number: %f", (float)number);
- printf("\n Machine code: ");
- printBinary(converter32.i, 32);
- printf("\n Sign: %u", sign32);
- printf("\n Order: %d", exponentValue32);
- printf("\n Exponent: %d", exponent32);
- printf("\n Characteristic: ");
- printBinary(exponent32, 9);
- printf("\n Mantissa: ");
- printBinary(mantissa32, 23);
- union {
- double f;
- unsigned long long i;
- } converter;
- converter.f = number;
- unsigned long long sign = (converter.i >> 63) & 1;
- unsigned long long exponent = (converter.i >> 52) & 0x7FF;
- unsigned long long mantissa = converter.i & 0xFFFFFFFFFFFFF;
- int exponentValue = exponent - 1023;
- printf("\n\n---------------------------------------Double---------------------------------------\n");
- printf("\n Number: %lf", number);
- printf("\n Machine code: ");
- printBinary(converter.i, 64);
- printf("\n Sign: %llu", sign);
- printf("\n Order: %d", exponentValue);
- printf("\n Exponent: %llu", exponent);
- printf("\n Characteristic: ");
- printBinary(exponent, 11);
- printf("\n Mantissa: ");
- printBinary(mantissa, 52);
- long long integerPart = (long long)number;
- double fractionalPart = number - integerPart;
- char* integerBinary = getIntegerBinary(integerPart);
- char* fractionalBinary = getFractionalBinary(fractionalPart);
- printf("\n\n------------------------------------------------------------------------------------\n");
- printf("\n Integer part in binary: %s", integerBinary);
- printf("\n Fractional part in binary: %s", fractionalBinary);
- printf("\n Both parts in binary: %s.%s", integerBinary, fractionalBinary);
- printf("\n Integer part in decimal: %d", getBinaryToDecimalInteger(integerBinary));
- printf("\n Fractional part in decimal: %.64lf", getBinaryToDecimalFractional(fractionalBinary));
- printf("\n Both parts in decimal: %.64lf", getBinaryToDecimalInteger(integerBinary) + getBinaryToDecimalFractional(fractionalBinary));
- printf("\n\n------------------------------------------------------------------------------------\n\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement