• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jun 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <string.h>
3. #include <stdbool.h>
4. #include <math.h>
5. #include <stdlib.h>
6.
7. // Performs operations listed below
8.         // Operation Key
10.         // 2: Subtraction
11.         // 3: Multiplication
12.         // 4: Division
13.         // 5: Exponent
14. double basic_operation(int operation) {
15.         double val1, val2, return_val;
16.         for (int i = 1; i <= 2; i++) {
17.                 double val;
18.                 printf("Please enter value %d: ", i);
19.                 if (i == 1) {
20.                         scanf("%lf", &val1);
21.                 }
22.                 else {
23.                         scanf("%lf", &val2);
24.                 }
25.         }
26.         switch (operation) {
27.                 case 1:
28.                         return_val = val1 + val2;
29.                         break;
30.                 case 2:
31.                         return_val = val1 - val2;
32.                         break;
33.                 case 3:
34.                         return_val = val1 * val2;
35.                         break;
36.                 case 4:
37.                         return_val = val1 / val2;
38.                         break;
39.                 case 5:
40.                         return_val = 1;
41.                         for (int i = 0; i < val2; i++) {
42.                                 return_val *= val1;
43.                         }
44.                         break;
45.         }
46.         return return_val;
47. }
48.
49. // Returns the square root of an entered value
50. double square_root() {
51.         double val;
52.         printf("Please enter the value you wish to square root: ");
53.         scanf("%lf", &val);
54.         return sqrt(val);
55. }
56.
57. void help_text(bool welcome) {
58.         printf(".... Simple C Calculator ....\n");
59.         printf("... Written by Liam Keeton ..\n");
60.         printf("Operations:\n[+] [-] [*] [/]\n[^] [√] [?] [q]\n");
61.         if (welcome) {
62.                 printf("\n");
63.         }
64. }
65.
66. void select_operation(int iteration) {
67.         char operation;
68.         int amount_vals;
69.         printf("Calculation[%d]: Please enter the operation you wish to use: ", iteration);
70.         scanf("%s", operation);
71.         if (strcmp(operation, "+") == 0) {
72.                 printf("The sum: %lf\n", basic_operation(1));
73.         }
74.         else if (strcmp(operation, "-") == 0) {
75.                 printf("The difference: %lf\n", basic_operation(2));
76.         }
77.         else if (strcmp(operation, "*") == 0) {
78.                 printf("The product: %lf\n", basic_operation(3));
79.         }
80.         else if (strcmp(operation, "/") == 0) {
81.                 printf("The quotient: %lf\n", basic_operation(4));
82.         }
83.         else if (strcmp(operation, "exp") == 0) {
84.                 printf("The power: %lf\n", basic_operation(5));
85.         }
86.         else if (strcmp(operation, "sqrt") == 0) {
87.                 printf("The square root: %lf\n", square_root());
88.         }
89.         else if (strcmp(operation, "?") == 0) {
90.                 help_text(false);
91.         }
92.         else if (strcmp(operation, "q") == 0) {
93.                 exit(0);
94.         }
95.         else {
96.                 printf("Unrecognised Operation.\n");
97.         }
98.         printf("\n");
99.         select_operation(iteration + 1);
100. }
101.
102. int main() {
103.         help_text(true);
104.         select_operation(1);
105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top