Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <math.h>
- #include <stdlib.h>
- // Performs operations listed below
- // Operation Key
- // 1: Addition
- // 2: Subtraction
- // 3: Multiplication
- // 4: Division
- // 5: Exponent
- double basic_operation(int operation) {
- double val1, val2, return_val;
- for (int i = 1; i <= 2; i++) {
- double val;
- printf("Please enter value %d: ", i);
- if (i == 1) {
- scanf("%lf", &val1);
- }
- else {
- scanf("%lf", &val2);
- }
- }
- switch (operation) {
- case 1:
- return_val = val1 + val2;
- break;
- case 2:
- return_val = val1 - val2;
- break;
- case 3:
- return_val = val1 * val2;
- break;
- case 4:
- return_val = val1 / val2;
- break;
- case 5:
- return_val = 1;
- for (int i = 0; i < val2; i++) {
- return_val *= val1;
- }
- break;
- }
- return return_val;
- }
- // Returns the square root of an entered value
- double square_root() {
- double val;
- printf("Please enter the value you wish to square root: ");
- scanf("%lf", &val);
- return sqrt(val);
- }
- void help_text(bool welcome) {
- printf(".... Simple C Calculator ....\n");
- printf("... Written by Liam Keeton ..\n");
- printf("Operations:\n[+] [-] [*] [/]\n[^] [√] [?] [q]\n");
- if (welcome) {
- printf("\n");
- }
- }
- void select_operation(int iteration) {
- char operation[50];
- int amount_vals;
- printf("Calculation[%d]: Please enter the operation you wish to use: ", iteration);
- scanf("%s", operation);
- if (strcmp(operation, "+") == 0) {
- printf("The sum: %lf\n", basic_operation(1));
- }
- else if (strcmp(operation, "-") == 0) {
- printf("The difference: %lf\n", basic_operation(2));
- }
- else if (strcmp(operation, "*") == 0) {
- printf("The product: %lf\n", basic_operation(3));
- }
- else if (strcmp(operation, "/") == 0) {
- printf("The quotient: %lf\n", basic_operation(4));
- }
- else if (strcmp(operation, "exp") == 0) {
- printf("The power: %lf\n", basic_operation(5));
- }
- else if (strcmp(operation, "sqrt") == 0) {
- printf("The square root: %lf\n", square_root());
- }
- else if (strcmp(operation, "?") == 0) {
- help_text(false);
- }
- else if (strcmp(operation, "q") == 0) {
- exit(0);
- }
- else {
- printf("Unrecognised Operation.\n");
- }
- printf("\n");
- select_operation(iteration + 1);
- }
- int main() {
- help_text(true);
- select_operation(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement