#include <stdio.h>
int calc(int op1, int op2, char op);
short op_v(char op);
int main(void)
{
/* Variable declarations */
char op;
int op1, op2;
/* Get the expression */
printf("%s", "Enter a simple expression: ");
/* Break the expression down into tokens and solve it */
if(scanf("%d%c%d", &op1, &op, &op2) == 3 && op_v(op))
printf("%s%d", "Result= ", calc(op1, op2, op));
else
printf("%s", "Invalid expression.\n");
return 0;
}
/* Calculate the result */
int calc(int op1, int op2, char op)
{
switch(op)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': return op1/op2;
}
}
/* Validate the operator */
short op_v(char op)
{
switch(op)
{
case '+': return 1;
case '-': return 1;
case '*': return 1;
case '/': return 1;
default: return 0;
}
}
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.