Advertisement
pdaogu

HW8.1

Oct 30th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. #define M_PI 3.14159265358979323846
  6. #define EPS 1e-14
  7.  
  8. int main () {
  9.     double res, input, x, prev_res, init;
  10.     char choice;
  11.     int isInput = 0, sign, i, isOut = 0, tmp;
  12.     do {
  13.         printf("---------------- SCIENTIFIC CALCULATOR ------------------\n");
  14.         printf("N. Enter x\n");
  15.         printf("Compute:\n");
  16.         printf("\tS. Sin of x (sin(x))\n");
  17.         printf("\tC. Square root of x (sqrt(x))\n");
  18.         printf("\tE. Natural exponential (e^x)\n");
  19.         printf("T. Exit\n");
  20.         printf("\n---------------------------------------------------------\n");
  21.         printf("\n>>>> Enter your choice (N/S/C/E/T): ");
  22.         scanf("%c", &choice);
  23.         while (getchar() != '\n');
  24.         printf("---------------------------------------------------------\n");
  25.         if (choice >= 'a' && choice <= 'z') {
  26.             choice -= 32;
  27.         }
  28.         switch (choice) {
  29.             case 'N':
  30.                 isInput = 0;
  31.                 do {
  32.                     printf("> Enter positive number x: ");
  33.                     tmp = scanf("%lf", &x);
  34.                     while (getchar() != '\n');
  35.                     if (tmp != 1) {
  36.                         printf("!!! Please enter a number x !!!\n");
  37.                     } else if (x < 0) {
  38.                         printf("!!! Please enter a positive number !!!\n");
  39.                     } else {
  40.                         isInput = 1;
  41.                     }
  42.                 } while (!isInput);
  43.                 putchar('\n');
  44.                 break;
  45.             case 'S':
  46.             case 'C':
  47.             case 'E':
  48.                 if (!isInput) {
  49.                     printf("!!! Please enter x first !!!\n");
  50.                 } else switch (choice) {
  51.                     case 'S':
  52.                         sign = -1;
  53.                         i = 1;
  54.                         input = x;
  55.                         while (input > 2*M_PI) {
  56.                             input -= 2*M_PI;
  57.                         }
  58.                         init = input;
  59.                         res = input;
  60.                         do {
  61.                             input *= (init / (i+1)) * (init / (i+2));
  62.                             i += 2;
  63.                             res += sign * input;
  64.                             sign = - sign;
  65.                         } while (fabs(input) > EPS);
  66.                         printf("> sin(%.4lf) = %.15lf\n\n", x, res);
  67.                         break;
  68.                     case 'C':
  69.                         res = x;
  70.                         do {
  71.                             prev_res = res;
  72.                             res = (res + x / res) / 2;
  73.                         } while (fabs(prev_res - res) > EPS);
  74.                         printf("> sqrt(%.4lf) = %.15lf\n\n", x, res);
  75.                         break;
  76.                     case 'E':
  77.                         res = 1;
  78.                         i = 1;
  79.                         input = 1;
  80.                         do {
  81.                             prev_res = res;
  82.                             input *= x / i;
  83.                             ++i;
  84.                             res += input;
  85.                         } while (fabs(input) > EPS);
  86.                         printf("> e^%.4lf = %.15lf\n\n", x, res);
  87.                         break;
  88.                     break;
  89.                     }
  90.                 break;
  91.             case 'T':
  92.                 isOut = 1;
  93.                 printf("\n!!! Good bye & See you later !!!\n");
  94.                 break;
  95.             default:
  96.                 printf("!!! Your choice you entered is invalid. Please try again !!!\n");
  97.                 break;
  98.         }
  99.     } while (!isOut);
  100.     getch();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement