fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. // Declare yylval as a union type to hold numbers (doubles)
  7. union {
  8. double num;
  9. } yylval;
  10.  
  11. // Function to handle errors
  12. void yyerror(const char *s);
  13. %}
  14.  
  15. %option noyywrap
  16.  
  17. DIGIT [0-9]
  18. NUMBER {DIGIT}+(\.{DIGIT}+)?
  19. ID [a-zA-Z_][a-zA-Z0-9_]*
  20. %%
  21.  
  22. "1" { return 1; }
  23. "2" { return 2; }
  24. "3" { return 3; }
  25. "4" { return 4; }
  26. "5" { return 5; }
  27. "6" { return 6; }
  28. "7" { return 7; }
  29. "+" { return '+'; }
  30. "-" { return '-'; }
  31. "*" { return '*'; }
  32. "SQRT" { return 'S'; }
  33. "CQRT" { return 'C'; }
  34. {NUMBER} { yylval.num = atof(yytext); return 'N'; }
  35. . { return yytext[0]; }
  36.  
  37. %%
  38.  
  39. // Main function to interact with the user and execute the chosen operation
  40. int main() {
  41. int choice;
  42. double num1, num2, result;
  43.  
  44. printf("Select an operation:\n");
  45. printf("1. Addition (+)\n");
  46. printf("2. Subtraction (-)\n");
  47. printf("3. Multiplication (*)\n");
  48. printf("4. Square Root (SQRT)\n");
  49. printf("5. Cube Root (CQRT)\n");
  50. printf("6. Exit\n");
  51.  
  52. // Read the user's choice
  53. scanf("%d", &choice);
  54.  
  55. switch(choice) {
  56. case 1: // Addition
  57. printf("Enter two numbers: ");
  58. scanf("%lf %lf", &num1, &num2);
  59. result = num1 + num2;
  60. printf("Result: %.2f\n", result);
  61. break;
  62.  
  63. case 2: // Subtraction
  64. printf("Enter two numbers: ");
  65. scanf("%lf %lf", &num1, &num2);
  66. result = num1 - num2;
  67. printf("Result: %.2f\n", result);
  68. break;
  69.  
  70. case 3: // Multiplication
  71. printf("Enter two numbers: ");
  72. scanf("%lf %lf", &num1, &num2);
  73. result = num1 * num2;
  74. printf("Result: %.2f\n", result);
  75. break;
  76.  
  77. case 4: // Square Root
  78. printf("Enter a number: ");
  79. scanf("%lf", &num1);
  80. if (num1 < 0) {
  81. printf("Error: Cannot take square root of a negative number.\n");
  82. } else {
  83. result = sqrt(num1);
  84. printf("Square Root: %.2f\n", result);
  85. }
  86. break;
  87.  
  88. case 5: // Cube Root
  89. printf("Enter a number: ");
  90. scanf("%lf", &num1);
  91. result = cbrt(num1);
  92. printf("Cube Root: %.2f\n", result);
  93. break;
  94.  
  95. case 6: // Exit
  96. printf("Exiting the program.\n");
  97. return 0;
  98.  
  99. default:
  100. printf("Invalid choice!\n");
  101. break;
  102. }
  103.  
  104. return 0;
  105. }
  106.  
  107. // Error handling function
  108. void yyerror(const char *s) {
  109. fprintf(stderr, "%s\n", s);
  110. exit(1);
Success #stdin #stdout #stderr 0.03s 6888KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/3LdNwb/prog:2:1: Syntax error: Operator expected
ERROR: /home/3LdNwb/prog:35:5: Syntax error: End of file in quoted atom
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit