You are on page 1of 1

ASSIGNMENT NO.

11 CALCULATOR
#include<stdio.h> #include<stdlib.h> #include<math.h> int calc(char str[]) { int result,i=0,opnd1,opnd2; char op; while(str[i]!='\0') { if(i==0) { opnd1=str[i]-'0'; op=str[i+1]; opnd2=str[i+2]-'0'; i+=3; } else { opnd1=result; op=str[i]; opnd2=str[i+1]-'0'; i+=2; } switch(op) { case '+':result=opnd1+opnd2; break; case '-':result=opnd1-opnd2; break; case '*':result=opnd1*opnd2; break; case '/':result=opnd1/opnd2; break; case '%':result=opnd1%opnd2; break; case '$':result=(int)pow(opnd1,opnd2); break; } } return result; } int main() { system("cls"); char str[100]; int result; printf("Enter the expression to be evaluated: "); scanf("%s",str); result=calc(str); printf("\nThe result is: %d\n\n",result); system("pause"); return 0;}