# Calculator program to perform arithmetic operations

//The below program work just how our scientific calculator works.
//Enter an arithmetic string which is to be processed and evaluate the string.

```//INCLUDING HEADER FILES
#include<stdio.h>

//THIS ONE FOR MATHEMATICAL FUNCTIONS
#include<math.h>

#include<string.h>
char operator[10];
float operand[10];

{
operand[j+1]+=operand[j];
}

//FUNCTION TO PERFORM SUBTRACTION
void subtract(int j)
{
operand[j+1]=operand[j]-operand[j+1];

}

//FUNCTION TO PERFORM MULTIPLICATION
void multiply(int j)
{
operand[j+1]*=operand[j];

}

//FUNCTION TO PERFORM DIVISION
void divide(int j)
{
if(operand[j+1]==0)
{       printf("\n DIVISION BY 0--UNDEFINED");
goto l;
}
operand[j+1]=operand[j]/operand[j+1];
l:
}

main()
{
int i,j,l,dec,check,index1,index2,sign1,sign2,fault;
char calc[20];
float oper,mul;
printf("\nenter the mathematical string to be evaluated\n");
scanf("%s",calc);
l=strlen(calc);                        //CALCULATING LENGTH
index1=index2=sign2=fault=0;
sign1=1;
for(i=0;i<l;)                            //CHECKING EACH CHARACTER
{
oper=check=0;
if((calc[i]=='-')&&((i==0)||(calc[i-1]<48)||(calc[i-1]>57)))  //UNARY MINUS
{
sign1*=-1;
i++;
continue;
}
if((calc[i]=='l')&&((i==0)||(calc[i-1]<48)||(calc[i-1]>57)))  //LOG BASE e
{
sign2=1;
i++;
continue;
}
if((calc[i]=='s')&&((i==0)||(calc[i-1]<48)||(calc[i-1]>57)))  //SQUARE ROOT
{
sign2=2;
i++;
continue;
}
dec=mul=1;
for(j=i;j<l;j++)                          //GETTING THE OPERAND
{
if(calc[j]=='.')
{
dec=0;
continue;
}
if(calc[j]>=48&&calc[j]<=57)
{
check=1;
if(dec)
oper=oper*10+(calc[j]-48);
else
{
mul/=10.0;
oper+=mul*(calc[j]-48);
}
}
else
break;
}
if(check)                              //IF  IT IS OPERAND
{

operand[index1]=oper;
if(sign1==-1)                          //UNARY MINUS
operand[index1]=-operand[index1];

if(sign2==1)                          //LOG BASE e
{
if(operand[index1]<=0)
{
printf("\nlog is defined for positive no only");
fault=1;
}
if(!fault)
operand[index1]=log(operand[index1]);

}
if(sign2==2)                         //SQUARE ROOT
{
if(operand[index1]<=0)
{
printf("\nsquare root is defined for positive no only");
fault=1;
}
if(!fault)
operand[index1]=sqrt(operand[index1]);

}
index1++;

}
else
{    operator[index2]=calc[i];            //STORING THE OPERATOR
index2++;
}
if(check)
i=j;
else
i++;
if(fault)
goto l1;
sign1=1;
sign2=fault=0;
}

j=0;

//PERFORMING OPERATION ACCORDING TO OPERATOR
for(i=0;i<index2;i++)
{
switch(operator[i])
{
break;
case '-': subtract(j++);    //CALLING FUNCTION SUBTRACT()
break;
case '*': multiply(j++);    //CALLING FUNCTION MULTIPY()
break;
case '/': divide(j++);        //CALLING FUNCTION DIVIDE()
break;
default:  printf("%c is not a defined operator",operator[i]);
goto l1;        //PRINT ERROR AND LEAVE
}
}
//printf("\n%d%d",index1,index2);
printf("\n result");                //PRINTING THE RESULT
printf("\n%s = %f\n",calc,operand[index1-1]);
l1:
}```