```
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define LEN 100
void initstack(int);
void push(int, int); // second argument is a flag
int pop(int); // argument is a flag
static int *s1; // stack for numbers
static int *s2; // stack for operators
static int N1; // number stack counter
static int N2; // operator stack counter
int PolishReverse(char polish[]) //after changing the expresion to polish notation it shows
{ //sin with s,cos with by c,tan with t,cot with C.
// flag = 0 will do operations on s1
// flag = 1 will do operations on s2
char input[LEN];
puts("Enter an expresion to calculate:");
scanf("%s", input);
int maxN = strlen(input);
initstack(maxN);
int i = 0;
int k = 0;
while (i < maxN)
{
if (input[i] == '*' || input[i] == '+'|| input[i]=='-'|| input[i]=='/'|| input[i]=='s'|| input[i]=='c'|| input[i]=='t'|| input[i]=='C'){
push(input[i], 1);
if(input[i]=='s'|| input[i]=='c'|| input[i]=='t'|| input[i]=='C'){
i=i+2;
}
}
if ((input[i] >= '0'&& input[i]<='9')||input[i]=='.')
{
push(input[i], 0);
polish[k]=pop(0);
printf("%c", polish[k]);
if(input[i+1]=='+'|| input[i+1]=='*'|| input[i+1]=='/'|| input[i+1]=='-'|| input[i+1]=='s'|| input[i+1]=='c'|| input[i+1]=='t'|| input[i+1]=='C'){
k++;
polish[k]=' ';
printf("%c",polish[k]);
}
k++;
}
if (input[i] == ')'){
polish[k]=' ';
printf("%c",polish[k]);
k++;
polish[k]=pop(1);
printf("%c", polish[k]);
k++;
polish[k]=' ';
printf("%c",polish[k]);
k++;
}
i++;
}
puts("");
return k;
}
void initstack(int maxN)
{
s1 = (int *)malloc(maxN*sizeof(int));
s2 = (int *)malloc(maxN*sizeof(int));
}
void push(int item, int flag)
{
if (flag == 0)
s1[N1++] = item;
else
s2[N2++] = item;
}
int pop(int flag)
{
if (flag == 0)
return s1[--N1];
else
return s2[--N2];
}
int convert_num(char a){
int numbers[]={0,1,2,3,4,5,6,7,8,9};
char characters[]={"0123456789"};
int b;
int i;
for(i=0;i<10;i++){
if(a==characters[i]){
b=numbers[i];
}
}
return b;
}
main(){
clrscr();
char input[100]=""; //for storing the input.
int fraction[10]={0}; //for holdint fraction part of the numbers given.
int round[10]={0}; //for holding the integer part of the numbers given.
float num[5]={0};
float a=0;
float b=0;
float integer=0;
float fract=0;
float result=0; //for storing the result in each state.
int j=0;
int k=0;
int i=0; //input counter.
int x=0;
int count;
int PolishReverse(char input[]); //this function changes the expresion given by the user to polish notation and prints the polish expresion.
int convert_num(char); //this function changes the characters to numbers.
PolishReverse(input);
while(input[i]!=NULL){
if((input[i]=='0')||(input[i]=='1')||(input[i]=='2')||(input[i]=='3')||(input[i]=='4')||(input[i]=='5')||(input[i]=='6')||(input[i]=='7')||(input[i]=='8')||(input[i]=='9')){
round[j]=convert_num(input[i]);
while((input[i+1]=='0')||(input[i+1]=='1')||(input[i+1]=='2')||(input[i+1]=='3')||(input[i+1]=='4')||(input[i+1]=='5')||(input[i+1]=='6')||(input[i+1]=='7')||(input[i+1]=='8')||(input[i+1]=='9')){
i++;
j++;
round[j]=convert_num(input[i]);
}
i++;
if(input[i]=='.'){
i++;
while((input[i]=='0')||(input[i]=='1')||(input[i]=='2')||(input[i]=='3')||(input[i]=='4')||(input[i]=='5')||(input[i]=='6')||(input[i]=='7')||(input[i]=='8')||(input[i]=='9')){
fraction[k]=convert_num(input[i]);
i++;
k++;
}
}
while(j>=0){
a=round[x]*(pow(10,j));
x++;
j--;
integer=integer+a;
}
x=0;
while(x<k){
b=fraction[x]*(pow(10,-(x+1)));
x++;
fract=fract+b;
}
num[count]=integer+fract;
count++;
i++;
j=0;
k=0;
x=0;
fract=0;
integer=0;
}
else if((input[i]=='+')||(input[i]=='-')||(input[i]=='*')||(input[i]=='/')||(input[i]=='c')||(input[i]=='s')||(input[i]=='t')||(input[i]=='C')){
switch(input[i]){
case '+':
result=num[count-1]+num[count-2];
i=i+2;
num[count-1]=result;
break;
case '-':
result=num[count-1]+num[count-2];
i=i+2;
num[count-1]=result;
break;
case '*':
result=num[count-1]*num[count-2];
i=i+2;
num[count-1]=result;
break;
case '/':
if(num[count-1]==0){
printf("zero diversion,illegal input\n");
getch();
return 0;
}
else{
result=(float)num[count-2]/num[count-1];
i=i+2;
num[count-1]=result;
break;
}
case 's':
result=sin(num[count-1]);
i=i+2;
num[count-1]=result;
break;
case 'c':
result=cos(num[count-1]);
i=i+2;
num[count-1]=result;
break;
case 't':
result=tan(num[count-1]);
i=i+2;
num[count-1]=result;
break;
case 'C':
if(count[num-1]==0){
printf("cot(0) is undefined.\n");
getch();
return 0;
}
else{
result=(float)1/tan(num[count-1]);
i=i+2;
num[count-1]=result;
break;
}
}
}
else{
printf("error,illegal input\n");
getch();
return 0;
}
}
printf("the result is %f",result);
getch();
return 0;
}
```