Advertisement
_takumi

idz3

Nov 19th, 2022 (edited)
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5.  
  6. double solve(double x) {
  7.     double res = 1;
  8.     double tmp = 1;
  9.     for (int i = 1; i <= 100; ++i) {
  10.         tmp *= x / i;
  11.         res += tmp;
  12.     }
  13.     return res;
  14. }
  15.  
  16. int main(int argc, char **argv) {
  17.     double x, res;
  18.     if (argc == 0) {
  19.         printf("Please enter one of the possible execution keys: -r, -f, -c\n");
  20.         return 0;
  21.     }
  22.     if (strcmp(argv[1], "-r") == 0) {
  23.         srand(time(0));
  24.         x = rand() % 31;
  25.         clock_t begin = clock();
  26.         res = solve(x);
  27.         clock_t end = clock();
  28.         printf("e^%lf = %lf\n", x, res);
  29.         double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
  30.         printf("Program execution time: %lf\n", time_spent);
  31.     } else if (strcmp(argv[1], "-c") == 0) {
  32.         scanf("%lf", &x);
  33.         clock_t begin = clock();
  34.         res = solve(x);
  35.         clock_t end = clock();
  36.         printf("%lf\n", res);
  37.         double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
  38.         printf("Program execution time: %lf\n", time_spent);
  39.     } else if (strcmp(argv[1], "-f") == 0) {
  40.         if (argc != 4) {
  41.             printf("Incorrect input\n");
  42.             return 0;
  43.         }
  44.         FILE *input, *output;
  45.         input = fopen(argv[2], "r");
  46.         output = fopen(argv[3], "w");
  47.         if ((input == NULL) || (output == NULL)) {
  48.             printf("Incorrect input\n");
  49.             return 0;
  50.         }
  51.         fscanf(input, "%lf", &x);
  52.         clock_t begin = clock();
  53.         res = solve(x);
  54.         clock_t end = clock();
  55.         fprintf(output, "%lf\n", res);
  56.         double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
  57.         printf("Program execution time: %lf\n", time_spent);
  58.         fclose(input);
  59.         fclose(output);
  60.     } else {
  61.         printf("Please enter one of the possible execution keys: -r, -f, -c\n");
  62.     }
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement