Browse Source

Improve programmingMode to display results in binary (hexadecimal format) and prompt user for continuation

remotes/origin/kabrel
fdai7782 11 months ago
parent
commit
8718b9d507
  1. 49
      src/main/c/programmingMode.c

49
src/main/c/programmingMode.c

@ -5,6 +5,10 @@
#include "calculator.h" #include "calculator.h"
// Function to convert an integer to binary representation
char* binaryRepresentation(int num);
// get the NUmbers from the user // get the NUmbers from the user
int getIntegerInput(const char* prompt) { int getIntegerInput(const char* prompt) {
int num; int num;
@ -45,11 +49,52 @@ int performOperation(int num1, char operatorType, int num2) {
void programmingMode() { void programmingMode() {
char choice;
do {
int num1 = getIntegerInput("Enter first integer: "); int num1 = getIntegerInput("Enter first integer: ");
char operator = getOperatorInput(); char operator = getOperatorInput();
int num2 = getIntegerInput("Enter second integer: "); int num2 = getIntegerInput("Enter second integer: ");
// Calculation + Display the result
// Calculation
int result = performOperation(num1, operator, num2);
// 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);
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);
printf("Result: %d\n", performOperation(num1, operator, num2));
} 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;
}
Loading…
Cancel
Save