Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- #define M_PI 3.14159265358979323846
- #define EPS 1e-14
- int main () {
- double res, input, x, prev_res, init;
- char choice;
- int isInput = 0, sign, i, isOut = 0, tmp;
- do {
- printf("---------------- SCIENTIFIC CALCULATOR ------------------\n");
- printf("N. Enter x\n");
- printf("Compute:\n");
- printf("\tS. Sin of x (sin(x))\n");
- printf("\tC. Square root of x (sqrt(x))\n");
- printf("\tE. Natural exponential (e^x)\n");
- printf("T. Exit\n");
- printf("\n---------------------------------------------------------\n");
- printf("\n>>>> Enter your choice (N/S/C/E/T): ");
- scanf("%c", &choice);
- while (getchar() != '\n');
- printf("---------------------------------------------------------\n");
- if (choice >= 'a' && choice <= 'z') {
- choice -= 32;
- }
- switch (choice) {
- case 'N':
- isInput = 0;
- do {
- printf("> Enter positive number x: ");
- tmp = scanf("%lf", &x);
- while (getchar() != '\n');
- if (tmp != 1) {
- printf("!!! Please enter a number x !!!\n");
- } else if (x < 0) {
- printf("!!! Please enter a positive number !!!\n");
- } else {
- isInput = 1;
- }
- } while (!isInput);
- putchar('\n');
- break;
- case 'S':
- case 'C':
- case 'E':
- if (!isInput) {
- printf("!!! Please enter x first !!!\n");
- } else switch (choice) {
- case 'S':
- sign = -1;
- i = 1;
- input = x;
- while (input > 2*M_PI) {
- input -= 2*M_PI;
- }
- init = input;
- res = input;
- do {
- input *= (init / (i+1)) * (init / (i+2));
- i += 2;
- res += sign * input;
- sign = - sign;
- } while (fabs(input) > EPS);
- printf("> sin(%.4lf) = %.15lf\n\n", x, res);
- break;
- case 'C':
- res = x;
- do {
- prev_res = res;
- res = (res + x / res) / 2;
- } while (fabs(prev_res - res) > EPS);
- printf("> sqrt(%.4lf) = %.15lf\n\n", x, res);
- break;
- case 'E':
- res = 1;
- i = 1;
- input = 1;
- do {
- prev_res = res;
- input *= x / i;
- ++i;
- res += input;
- } while (fabs(input) > EPS);
- printf("> e^%.4lf = %.15lf\n\n", x, res);
- break;
- break;
- }
- break;
- case 'T':
- isOut = 1;
- printf("\n!!! Good bye & See you later !!!\n");
- break;
- default:
- printf("!!! Your choice you entered is invalid. Please try again !!!\n");
- break;
- }
- } while (!isOut);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement