Write My Paper Button

WhatsApp Widget

I want you to use Test-Driven Development and add it to the code below.I have tw

Share this post on:

I want you to use Test-Driven Development and add it to the code below.I have two files one of them the header file.This is the code #include
#include
#include “conversion.h”
int main() {
char choice;
printf(RED”Decimal ” BLUE “to” WHITE ” Different Base Conversionn”);
// Main loop for the program
do {
displayMenu();
printf(WHITE”Enter your choice (” RED “1” WHITE “/” RED “2” WHITE “/” RED “3” WHITE “/” RED “4” WHITE “): “);
scanf(” %c”, &choice);
// Switch statement to handle user choice
switch(choice) {
case ‘1’:
convertToBinary(getDecimalInput());
break;
case ‘2’:
convertToOctal(getDecimalInput());
break;
case ‘3’:
convertToHexadecimal(getDecimalInput());
break;
case ‘4’:
printf(“Exiting program…n”);
exit(0);
default:
printf(“Invalid choice. Please choose again.n”);
}
printf(BLUE”Do”WHITE” you want”BLUE ” to” WHITE” convert another number? (y/n): “);
scanf(” %c”, &choice);
} while(choice == ‘y’ || choice == ‘Y’);
return 0;
}
// Display menu function
void displayMenu() {
printf(“n”);
printf(RED “1” WHITE”. Convert ” BLUE “to Binaryn”);
printf(RED “2”WHITE “. Convert ” BLUE “to ” WHITE “Octaln”);
printf(RED “3”WHITE”. Convert ” BLUE “to ” WHITE “Hexadecimaln”);
printf(RED “4” WHITE “.” BLUE” Exitn” );
printf(“n”);
}
// Get decimal input from user
int getDecimalInput() {
int decimal;
printf(“Enter a ” RED “decimal” WHITE ” number: “);
scanf(“%d”, &decimal);
// Clear input buffer
while (getchar() != ‘n’);
// Error handling for negative numbers
if (decimal 0) {
binary[i] = decimal % 2;
decimal /= 2;
i++;
}
// Print binary equivalent
for (int j = i – 1; j >= 0; j–) {
printf(RED “%d” WHITE, binary[j]);
}
}
printf(“n”);
}
// Convert decimal to octal
void convertToOctal(int decimal) {
printf(“Octal equivalent: “);
if (decimal == 0)
printf(RED “0” WHITE);
else {
int octal[32];
int i = 0;
// Decimal to octal conversion
while (decimal > 0) {
octal[i] = decimal % 8;
decimal /= 8;
i++;
}
// Print octal equivalent
for (int j = i – 1; j >= 0; j–) {
printf(RED “%d” WHITE, octal[j]);
}
}
printf(“n”);
}
// Convert decimal to hexadecimal
void convertToHexadecimal(int decimal) {
printf(“Hexadecimal equivalent: “);
if (decimal == 0)
printf(RED “0” WHITE);
else {
char hex[32];
int i = 0;
// Decimal to hexadecimal conversion
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder = 0; j–) {
printf(RED “%c” WHITE, hex[j]);
}
}
printf(“n”);
}AND THIS IS THE HEADER FILE #ifndef CONVERSION_H
#define CONVERSION_H
#include
#include
// ANSI color escape codes
#define RED “x1b[31m”
#define BLUE “x1b[34m”
#define WHITE “x1b[0m”
// Function prototypes
void displayMenu();
int getDecimalInput();
void convertToBinary(int decimal);
void convertToOctal(int decimal);
void convertToHexadecimal(int decimal);
#endif /* CONVERSION_H */

The post I want you to use Test-Driven Development and add it to the code below.I have tw appeared first on Scholars Hub Blog.

Share this post on:

Affordable and Dependable Platform for Your Academic Assignments

X