Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <time.h>
- double solve(double x) {
- double res = 1;
- double tmp = 1;
- for (int i = 1; i <= 100; ++i) {
- tmp *= x / i;
- res += tmp;
- }
- return res;
- }
- int main(int argc, char **argv) {
- double x, res;
- if (argc == 0) {
- printf("Please enter one of the possible execution keys: -r, -f, -c\n");
- return 0;
- }
- if (strcmp(argv[1], "-r") == 0) {
- srand(time(0));
- x = rand() % 31;
- clock_t begin = clock();
- res = solve(x);
- clock_t end = clock();
- printf("e^%lf = %lf\n", x, res);
- double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
- printf("Program execution time: %lf\n", time_spent);
- } else if (strcmp(argv[1], "-c") == 0) {
- scanf("%lf", &x);
- clock_t begin = clock();
- res = solve(x);
- clock_t end = clock();
- printf("%lf\n", res);
- double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
- printf("Program execution time: %lf\n", time_spent);
- } else if (strcmp(argv[1], "-f") == 0) {
- if (argc != 4) {
- printf("Incorrect input\n");
- return 0;
- }
- FILE *input, *output;
- input = fopen(argv[2], "r");
- output = fopen(argv[3], "w");
- if ((input == NULL) || (output == NULL)) {
- printf("Incorrect input\n");
- return 0;
- }
- fscanf(input, "%lf", &x);
- clock_t begin = clock();
- res = solve(x);
- clock_t end = clock();
- fprintf(output, "%lf\n", res);
- double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
- printf("Program execution time: %lf\n", time_spent);
- fclose(input);
- fclose(output);
- } else {
- printf("Please enter one of the possible execution keys: -r, -f, -c\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement