diff --git a/src/main/c/programmingMode.c b/src/main/c/programmingMode.c index f9a4ebb..48bc867 100644 --- a/src/main/c/programmingMode.c +++ b/src/main/c/programmingMode.c @@ -5,6 +5,10 @@ #include "calculator.h" + +// Function to convert an integer to binary representation +char* binaryRepresentation(int num); + // get the NUmbers from the user int getIntegerInput(const char* prompt) { int num; @@ -45,11 +49,52 @@ int performOperation(int num1, char operatorType, int num2) { void programmingMode() { - int num1 = getIntegerInput("Enter first integer: "); - char operator = getOperatorInput(); - int num2 = getIntegerInput("Enter second integer: "); +char choice; + do { + int num1 = getIntegerInput("Enter first integer: "); + char operator = getOperatorInput(); + int num2 = getIntegerInput("Enter second integer: "); + + // Calculation + int result = performOperation(num1, operator, num2); - // Calculation + Display the result + // Display the result in different bases based on user choice + int base; + printf("Choose base to print the result (2 for binary, 8 for octal, 16 for hexadecimal): "); + scanf("%d", &base); - printf("Result: %d\n", performOperation(num1, operator, num2)); + switch (base) { + case 2: + printf("Result (Binary): %s\n", binaryRepresentation(result)); + break; + case 8: + printf("Result (Octal): %o\n", result); + break; + case 16: + printf("Result (Hexadecimal): %x\n", result); + break; + default: + printf("Invalid base choice\n"); + break; + } + + // Ask user if they want to continue + printf("Do you want to continue? (y/n): "); + scanf(" %c", &choice); + + } while (choice == 'y' || choice == 'Y'); } + + + +// Function to convert an integer to binary representation +char* binaryRepresentation(int num) { + static char binary[32]; + for (int i = 31; i >= 0; i--) { + binary[i] = (num & 1) + '0'; + num >>= 1; + } + binary[32] = '\0'; + return binary; +} +