All pastes #25708 Raw Edit

Stuff

public text v1 · immutable
#25708 ·published 2005-10-17 00:00 UTC
rendered paste body
/****************************/
/*         lab2.c           */
/*A Simple Base-7 Calculator*/
/****************************/

/* Adapted from figure 6.8 */

#include<stdio.h>
#define TRUE 1
#define FALSE !TRUE

int main(void){
	//declarations with descriptive variable names
	double		operand1, operand2;
	char		operator, ask;
	int 		done = FALSE, problemflag = FALSE;
	float		ans

	// Title Prompt
	printf("A Simple Base-7 Calculator\n");
	do {// Loop while variable done is FALSE
	    //Prompt user for arithmetic expression
	    printf("Enter calculator command in the following form:\n");
	    printf("<operand> <operator> <operand>\n");
	    printf("valid operators are +, -, *, and /.\n");
	    scanf ("%lf %c %lf", &operand1, &operator, &operand2);

	    // Based on operation, perfom calculation
	    switch(operator) {
	    	case '+':
	    	    ans = operand1 + operand2;
	    	    break;
	    	case '-':
	    	    ans = operand1 - operand2;
	    	    break;
	    	case '*':
		    ans = operand1 * operand2;
	    	    break;
	    	case '/':
		    if (operand2 == 0.0){
		    	printf("Error. Cannot divide by zero\n");
		    	ans = 0;
		    	problemflag = TRUE;
		    } else {
		    	ans = operand1/operand2;
		    }
		    break;
		default:
	            printf("*** Invalid operator. *** \n");
	            problemflag = TRUE;
	            break;
}
	if (problemflag) {
		// if there was a problem encountered
		printf("Arithmatic operation not performed.\n");
		problemflag = FALSE; // Our error checking flag reset
    } else {
		// no problem encountered, calc present result
		printf("%f %c %f = %f", operand1, operator, operand2, ans);
    }
	printf("\n\n Enter '1' to continue or '0' to quit: ");
	scanf ("%d", &ask);

	if (ask == 0)			//ask if done
		done = TRUE;
  }while (!done);
  return(0);
}