#include #include float num(int);/*a function that gets characters anf returns a number out of them*/ int op(void);/*a function that gets a characters and check if it is a valid mathematical operator*/ void print(float,char,float);/*a function that prints the equation*/ void claculator(void);/*a function that gets the input from the user*/ int my_math(void); const int TEN = 10; /*get number function*/ float num(int c) { int negative= 1; float num1 = 0; float num2 = 0; float tens = 10.0; while ((c != (' '))&& (c != '\n')) { if (c== '-') negative = -1; if (isdigit(c)) num1 = (num1 * TEN) + (c - '0'); if (c== '.') { while (isdigit((c=getchar()))) { num2= num2+((c-'0') / tens); tens = tens * TEN; } break; } c= getchar(); } return ((num1+num2) * negative); } /*get opreation function*/ int op() { char ch = 0; while((ch=getchar())!=EOF) { if(ch==' ')/*continue the loop if the user types space*/ { continue; } if(ch == '+' || ch == '-' || ch == '*' || ch == '/')/*returns the character if it is a valid mathematical operator, returns EOF otherwise*/ { return ch; } } return EOF;/*returns EOF if the user types other character*/ } /*print calculator function*/ void print (float n1, char op, float n2) { switch(op)/*checks which mathematical operator the user entered and prints the equation*/ { case '+': printf("%g + %g = %g\n",n1,n2,n1+n2);return; case '-': printf("%g - %g = %g\n",n1,n2,n1-n2);return; case '*': printf("%g * %g = %g\n",n1,n2,n1*n2);return; case '/': printf("%g / %g = %g\n",n1,n2,n1/n2);return; default: printf("invalid operator");return; } return; } /*calculator function*/ void calculator() { char c = 0; float n1 = 0, n2 = 0;/*the numbers that the user types*/ int operator = 0;/*the mathmatical operator that the user types*/ printf("enter a string contains two operands and an operator\n"); n1 = num(c);/*the first number*/ if((operator = op())==EOF)/*the operator*/ { printf("invalid operator"); return; } n2 = num(c);/*the second number*/ print(n1,operator,n2);/*activates the print function*/ } int my_math() { calculator(); return 0; }