Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- char arg[256], item;
- int badArgument;
- double x; // an actual argument
- int arrLength(char* string)
- {
- int length = 0;
- while (*string++) length++;
- return length;
- }
- /*
- void getArg() // v. 1
- {
- scanf("%s", arg);
- while((getchar()) != '\n'); // an alternative way to flush stdin
- x = strtod(arg, NULL);
- if (errno == ERANGE || x == 0.0) // exception handling in strtod function
- {
- badArgument = 1; // so that the user's argument is bad
- }
- badArgument = 0; // so that the user's argument is good
- }
- */
- int checkArg()
- {
- int dots = 0, weird_symbols = 0;
- for (int i = 0; i < arrLength(arg); i++)
- {
- if (arg[i] == '.') dots++;
- if (!isdigit(arg[i]) && dots > 1 && arg[i] != '-' && arg[i] != 'e' && arg[i] != 'E')
- {
- badArgument = 1;
- return 0;
- }
- }
- badArgument = 0;
- return 1;
- }
- void displayMenu()
- {
- printf("\n1. Compute a function.\n");
- printf("2. Exit.\n");
- printf("Choose a menu item: ");
- }
- void getArg() // v. 2
- {
- scanf("%s", arg);
- if (checkArg())
- {
- x = strtod(arg, NULL);
- }
- }
- void compute(double x)
- {
- printf("Z1 = %f\n", cos(x) + sin(x) + cos(3*x) + sin(3*x));
- printf("Z2 = %f\n", 2 * sqrt(2) * cos(x) * sin(M_PI/4 + 2*x));
- }
- void getItem()
- {
- scanf(" %c", &item);
- while((getchar()) != '\n'); // an alternative way to flush stdin
- }
- int main()
- {
- displayMenu();
- getItem();
- while (item != '1' && item != '2')
- {
- printf("\nPlease, choose a correct menu item: ");
- getItem();
- }
- if (item == '2')
- {
- exit(0);
- }
- while (1)
- {
- printf("\nZ1 = cosA + sinA + cos(3A) + sin(3A)\n");
- printf("Z2 = 2 * sqrt(2) * cosA * sin(pi/4 + 2A)\n");
- printf("Input argument A: ");
- getArg();
- if (badArgument)
- {
- printf("Wrong input argument A. Press 'y' to continue, another key to return back: ");
- getItem();
- if (item == 'y')
- {
- continue;
- } else break;
- }
- compute(x);
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement