Techno

Basic Command Line Calculator source in C

Aug 19th, 2012
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. /*
  2. ===================================
  3. Program: Calculate version 1.0
  4. Author: Manish Raj
  5. Website: http://www.technoslab.in/
  6. Description: Simple CLI calculator for beginners
  7. ===================================
  8. */
  9.  
  10. /* Include header files */
  11. #include <stdio.h>
  12. #include <conio.h>
  13.  
  14. /* Begin main method */
  15. int main(){
  16.  
  17.     /* Display program information */
  18.     printf("\nCalculate v1 by Manish");
  19.    
  20.     /* Declare variables */
  21.     char cmd, oprt;
  22.     int num1, num2;
  23.    
  24.     /* Loop infinitely till 'x' is pressed */
  25.     while(1){
  26.    
  27.         /* Initialize variables to default value on each iteration */
  28.         cmd = '?';
  29.         oprt = '?';
  30.         num1 = 0;
  31.         num2 = 0;
  32.        
  33.         /* Ask user for action to be performed */
  34.         printf("\nPress x to exit, Press c to calculate: ");
  35.         cmd = getch();
  36.         if(cmd == 'X' || cmd == 'x'){
  37.        
  38.             /* If 'x' was pressed -> exit */
  39.             printf("%c", cmd);
  40.             break;
  41.         }
  42.         else if(cmd == 'c' || cmd == 'C'){
  43.            
  44.             /* If 'c' was pressed -> calculate */
  45.             printf("%c", cmd);
  46.            
  47.             /* Display instructions */
  48.             printf("\nEnter expression in this form - number1 operator number2.\nAllowed operators: + , -, *, /, %.\nExample: 2 + 2\nEnter now:");
  49.             scanf("%d %c %d", &num1, &oprt, &num2);
  50.            
  51.             /* Begin calculation */
  52.             switch(oprt){
  53.                 case '+' :
  54.                     printf("%d %c %d = %d", num1, oprt, num2, num1 + num2);
  55.                     break;
  56.                 case '-' :
  57.                     printf("%d %c %d = %d", num1, oprt, num2, num1 - num2);
  58.                     break;
  59.                 case '*' :
  60.                     printf("%d %c %d = %d", num1, oprt, num2, num1 * num2);
  61.                     break;
  62.                 case '/' :
  63.                     if( num2 == 0){
  64.                         printf("%d %c %d = Math Error. You can not divide any number by 0", num1, oprt, num2);
  65.                     }else{
  66.                         printf("%d %c %d = %d", num1, oprt, num2, num1 / num2);
  67.                     }
  68.                     break;
  69.                 case '%' :
  70.                     printf("%d %c %d = %d", num1, oprt, num2, num1 % num2);
  71.                     break;
  72.                 default:
  73.                     printf("Invalid expression. Operator %c is not allowed. Try again!", oprt);
  74.                     break;
  75.             }
  76.         }else{
  77.            
  78.             /* Display error */
  79.             printf("\nInvalid selection. Try again!");
  80.         }
  81.     }
  82.  
  83.     /* Display exit message */
  84.     printf("\nThank you for using the program.");
  85.  
  86.     /* Return 0 to OS */
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment