Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. // Program 4.4; Calculator with for loop
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5.  
  6. bool first = true;
  7. bool exited = false;
  8.  
  9. long double eq = 0.0L;
  10. double op = 0.0;
  11.  
  12. char operation, choice;
  13.  
  14. long double calculate(long double eq, double op, char operation);
  15. bool cont();
  16.  
  17. int main(void){
  18.     // Welcome words
  19.     printf("Hello, it\'s my calculator on C via for loop.\n");                                     
  20.     printf("Please choice your opeartion to do");
  21.    
  22.     for( ;; ){
  23.         // Loop to choose operation
  24.         printf("\n\t1. Plus\n\t2. Minus\n\t3. Multiplication\n\t4. Division\n\t5. Exit\nYour choice: ");
  25.         scanf("%c", &operation);
  26.         // Exit if user decided it
  27.         if(operation == '5') {
  28.             printf("\nBye :c\n");
  29.             return 0;
  30.         }
  31.        
  32.         for( ;; ) {
  33.             // Main loop of calculations
  34.             if((operation >= '6') || (operation <= '0')) {
  35.                 // Check on false operations
  36.                 printf("\nI don\'t know that command.\n");
  37.                 break;
  38.             }
  39.             if(first) {
  40.                 // Entering first number
  41.                 printf("\nEnter first value: ");
  42.                 scanf("%Lf", &eq);
  43.                 first = false;
  44.             }
  45.             // Entering second (next) number
  46.             printf("\nAnd: ");
  47.             scanf("%lf", &op);
  48.             // Calculating
  49.             eq = calculate(eq, op, operation);
  50.        
  51.             printf("\n = %Lf\n", eq);
  52.             exited = cont();
  53.             if(exited) {
  54.                 op = 0.0;
  55.                 eq = 0.0L;
  56.                 first = true;
  57.                 break; 
  58.             }      
  59.         }
  60.     }
  61.     return 0;
  62. }
  63.  
  64. long double calculate(long double eq, double op, char operation) { 
  65.     switch(operation) {
  66.         case '1':
  67.             eq += op;
  68.             break;
  69.         case '2':
  70.             eq -= op;
  71.             break;
  72.         case '3':
  73.             eq *= op;
  74.             break;
  75.         case '4':
  76.             eq /= op;
  77.             break;
  78.     }
  79.     return eq;
  80. }
  81.  
  82. bool cont() {
  83.     printf("Do you want to continue? [n|y] ");
  84.     scanf("%c", &choice);
  85.        
  86.     switch(tolower(choice)) {
  87.         case 'y':
  88.             exited = false;
  89.             break;
  90.         case 'n':
  91.             exited = true;
  92.             break;
  93.         default:
  94.             printf("\nI don\'t know that choice, try again.\n");
  95.             cont();
  96.             break;
  97.     }
  98.     return exited;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement