Create a calculator in C, again in C and again in C. This is a textual calculato
ID: 3769956 • Letter: C
Question
Create a calculator in C, again in C and again in C. This is a textual calculator not a GUI. a. Your calculator should begin by displaying the functions your calculator is able to perform. b. The user should then be able to insert a formula for which you calculate and display the answer. c. The following functions need to be handled by your calculator: i. Sin ii. Cos iii. Tan iv. X^2 v. X^Y vi. Square root vii. Addition viii. Subtraction ix. Multiplication x. Division xi. Parentheses d. The user should be able to enter a formula like: 2 * (3 + 4) – 21 / 7 Your calculator should honor the order of operations and give the correct answer of 11. thank you
Explanation / Answer
#include <stdio.h>
#include <math.h>
void postfix(char str[]);
float evaluate(char str[]);
int main()
{
int option,x,y;
char str[20];
printf("enter option 1. sine 2.cosine 3.Tan
4.square your number find x power y
5.squareroot of your number 6.Addition
7. subtraction 8. multiplication 9. divison
10. calculate given infix epression 0. stop ");
scanf("%d",&option);
while(1)
{
switch(option)
{
case 1:
printf("enter x which is an integer");
scanf("%d", &x);
printf("sin(%d)=%lf",x,sin(x));
break;
case 2:
printf("enter x which is an integer");
scanf("%d", &x);
printf("cos(%d)=%lf",x,cos(x));
break;
case 3:
printf("enter x which is an integer");
scanf("%d",&x);
printf("tan(%d)=%lf",x,tan(x));
break;
case 4:
printf("enter x");
scanf("%d", &x);
printf("square(%d)=%lf",x, x*x);
break;
case 5:
printf("enter x and y values");
scanf("%d %d", &x,&y);
printf("power(%d,%d)=%d",x, pow(x,y));
break;
case 6:
printf("enter x and y values");
scanf("%d %d", &x,&y);
printf("sum (%d,%d)=%d",x,y, x+x);
break;
case 7:
printf("enter x and y values");
scanf("%d %d", &x,&y);
printf("difference (%d,%d)=%d",x,y, x-x);
break;
case 8:
printf("enter x and y values");
scanf("%d %d", &x,&y);
printf("product (%d,%d)=%d",x,y, x*x);
break;
case 9:
printf("enter x and y values");
scanf("%d %d", &x,&y);
printf("divison (%d,%d)=%d",x,y, x/y);
break;
case 10:
printf("enter an infix expression");
scanf("%s"str);
postfix(str);
break;
default:printf("invalid option");
exit(0);
}
return 0;
}
void postfix(char str[])
{
char token;
while((token=getchar())!='')
{
if(isalnum(token))
str1[i++]=token;
else
if(token == '(')
push(&s,'(');
else
{
if(token == ')')
while((x=pop(&s))!='(')
str1[i++]=x;
else
{
while(priority(token)< =priority(top(&s)) && !empty(&s))
{
x=pop(&s);
str1[i++]=x;
}
push(&s,token);
}
}
}
while(!empty(&s))
{
x=pop(&s);
str1[i++]=x;
}
printf("%f",evaluate(str1));
}
float evaluate(char str[])
{
char[] x;
int i=0;
while(str[i]!='')
{
x=getchar();
if(isalnum(x))
{
a=atoi(x);
push(&s,a);
i++;
}
else
{a=pop(&s);
b=pop(&s);
push(&s,operate(a,b,x));
}
return(pop(&s));
}
/* function to check prority of operators*/
int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
/* function to initialize stack*/
void init(stack *s)
{
s->top=-1;
}
/* function to check whether stack is empty or not*/
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
/*function to check whether stack is full or not*/
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
/* function to push a character onto stack*/
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
//The function to pop character from top of stack*/
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
/*this function returns char top of stack*/
char top(stack * s)
{
return(s->data[s->top]);
}
please contact me for furthur queries.