Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ===================================
- Program: Calculate version 1.0
- Author: Manish Raj
- Website: http://www.technoslab.in/
- Description: Simple CLI calculator for beginners
- ===================================
- */
- /* Include header files */
- #include <stdio.h>
- #include <conio.h>
- /* Begin main method */
- int main(){
- /* Display program information */
- printf("\nCalculate v1 by Manish");
- /* Declare variables */
- char cmd, oprt;
- int num1, num2;
- /* Loop infinitely till 'x' is pressed */
- while(1){
- /* Initialize variables to default value on each iteration */
- cmd = '?';
- oprt = '?';
- num1 = 0;
- num2 = 0;
- /* Ask user for action to be performed */
- printf("\nPress x to exit, Press c to calculate: ");
- cmd = getch();
- if(cmd == 'X' || cmd == 'x'){
- /* If 'x' was pressed -> exit */
- printf("%c", cmd);
- break;
- }
- else if(cmd == 'c' || cmd == 'C'){
- /* If 'c' was pressed -> calculate */
- printf("%c", cmd);
- /* Display instructions */
- printf("\nEnter expression in this form - number1 operator number2.\nAllowed operators: + , -, *, /, %.\nExample: 2 + 2\nEnter now:");
- scanf("%d %c %d", &num1, &oprt, &num2);
- /* Begin calculation */
- switch(oprt){
- case '+' :
- printf("%d %c %d = %d", num1, oprt, num2, num1 + num2);
- break;
- case '-' :
- printf("%d %c %d = %d", num1, oprt, num2, num1 - num2);
- break;
- case '*' :
- printf("%d %c %d = %d", num1, oprt, num2, num1 * num2);
- break;
- case '/' :
- if( num2 == 0){
- printf("%d %c %d = Math Error. You can not divide any number by 0", num1, oprt, num2);
- }else{
- printf("%d %c %d = %d", num1, oprt, num2, num1 / num2);
- }
- break;
- case '%' :
- printf("%d %c %d = %d", num1, oprt, num2, num1 % num2);
- break;
- default:
- printf("Invalid expression. Operator %c is not allowed. Try again!", oprt);
- break;
- }
- }else{
- /* Display error */
- printf("\nInvalid selection. Try again!");
- }
- }
- /* Display exit message */
- printf("\nThank you for using the program.");
- /* Return 0 to OS */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment