You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.4 KiB

  1. // programmingMode
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include "programmingMode.h"
  5. // Function to convert an integer to binary representation
  6. char* binaryRepresentation(int num);
  7. // get the NUmbers from the user
  8. int getIntegerInput(const char* prompt) {
  9. int num;
  10. printf("%s", prompt);
  11. scanf("%d", &num);
  12. return num;
  13. }
  14. // Display the diffrent operations and scan them
  15. char getOperatorInput() {
  16. char operator;
  17. printf("Enter operator (+, -, *, /): ");
  18. scanf(" %c", &operator);
  19. return operator;
  20. }
  21. // Calculation
  22. int performOperation(int num1, char operatorType, int num2) {
  23. switch (operatorType) {
  24. case '+':
  25. return num1 + num2;
  26. case '-':
  27. return num1 - num2;
  28. case '*':
  29. return num1 * num2;
  30. case '/':
  31. if (num2 != 0) {
  32. return num1 / num2;
  33. } else {
  34. return 0;
  35. }
  36. default:
  37. printf("Invalid operator\n");
  38. return 0;
  39. }
  40. }
  41. void programmingMode() {
  42. char choice;
  43. do {
  44. int num1 = getIntegerInput("Enter first integer: ");
  45. char operator = getOperatorInput();
  46. int num2 = getIntegerInput("Enter second integer: ");
  47. // Calculation
  48. int result = performOperation(num1, operator, num2);
  49. // Display the result in different bases based on user choice
  50. int base;
  51. printf("Choose base to print the result (2 for binary, 8 for octal, 16 for hexadecimal): ");
  52. scanf("%d", &base);
  53. switch (base) {
  54. case 2:
  55. printf("Result (Binary): %s\n", binaryRepresentation(result));
  56. break;
  57. case 8:
  58. printf("Result (Octal): %o\n", result);
  59. break;
  60. case 16:
  61. printf("Result (Hexadecimal): %x\n", result);
  62. break;
  63. default:
  64. printf("Invalid base choice\n");
  65. break;
  66. }
  67. // Ask user if they want to continue
  68. printf("Do you want to continue? (y/n): ");
  69. scanf(" %c", &choice);
  70. } while (choice == 'y' || choice == 'Y');
  71. }
  72. // Function to convert an integer to binary representation
  73. char* binaryRepresentation(int num) {
  74. static char binary[32];
  75. for (int i = 31; i >= 0; i--) {
  76. binary[i] = (num & 1) + '0';
  77. num >>= 1;
  78. }
  79. binary[32] = '\0';
  80. return binary;
  81. }