Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. char arg[256], item;
  9. int badArgument;
  10. double x; // an actual argument
  11.  
  12. int arrLength(char* string)
  13. {
  14.     int length = 0;
  15.     while (*string++) length++;
  16.     return length;
  17. }
  18.  
  19. /*
  20. void getArg() // v. 1
  21. {
  22.     scanf("%s", arg);
  23.  
  24.     while((getchar()) != '\n'); // an alternative way to flush stdin
  25.  
  26.     x = strtod(arg, NULL);
  27.  
  28.     if (errno == ERANGE || x == 0.0) // exception handling in strtod function
  29.     {
  30.         badArgument = 1; // so that the user's argument is bad
  31.     }
  32.  
  33.     badArgument = 0; // so that the user's argument is good
  34. }
  35. */
  36.  
  37. int checkArg()
  38. {
  39.     int dots = 0, weird_symbols = 0;
  40.  
  41.     for (int i = 0; i < arrLength(arg); i++)
  42.     {
  43.         if (arg[i] == '.') dots++;
  44.  
  45.         if (!isdigit(arg[i]) && dots > 1 && arg[i] != '-' && arg[i] != 'e' && arg[i] != 'E')
  46.         {
  47.             badArgument = 1;
  48.             return 0;
  49.         }
  50.     }
  51.  
  52.     badArgument = 0;
  53.     return 1;
  54. }
  55.  
  56. void displayMenu()
  57. {
  58.     printf("\n1. Compute a function.\n");
  59.     printf("2. Exit.\n");
  60.     printf("Choose a menu item: ");
  61. }
  62.  
  63. void getArg() // v. 2
  64. {
  65.     scanf("%s", arg);
  66.     if (checkArg())
  67.     {
  68.         x = strtod(arg, NULL);
  69.     }
  70. }
  71.  
  72. void compute(double x)
  73. {
  74.     printf("Z1 = %f\n", cos(x) + sin(x) + cos(3*x) + sin(3*x));
  75.     printf("Z2 = %f\n", 2 * sqrt(2) * cos(x) * sin(M_PI/4 + 2*x));
  76. }
  77.  
  78. void getItem()
  79. {
  80.     scanf(" %c", &item);
  81.     while((getchar()) != '\n'); // an alternative way to flush stdin
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87.     displayMenu();
  88.     getItem();
  89.  
  90.     while (item != '1' && item != '2')
  91.     {
  92.         printf("\nPlease, choose a correct menu item: ");
  93.         getItem();
  94.     }
  95.  
  96.     if (item == '2')
  97.     {
  98.         exit(0);
  99.     }
  100.  
  101.     while (1)
  102.     {
  103.         printf("\nZ1 = cosA + sinA + cos(3A) + sin(3A)\n");
  104.         printf("Z2 = 2 * sqrt(2) * cosA * sin(pi/4 + 2A)\n");
  105.         printf("Input argument A: ");
  106.  
  107.         getArg();
  108.  
  109.         if (badArgument)
  110.         {
  111.             printf("Wrong input argument A. Press 'y' to continue, another key to return back: ");
  112.             getItem();
  113.             if (item == 'y')
  114.             {
  115.                 continue;
  116.             } else break;
  117.         }
  118.  
  119.         compute(x);
  120.  
  121.         break;
  122.     }
  123.  
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement