I am trying to figure out a way to write a function in C where the user enters i
ID: 671749 • Letter: I
Question
I am trying to figure out a way to write a function in C where the user enters in an integer, then a operand (+,*,/, or -), then enters another integer.
It then needs to carryout the operation entered in
for example the user enters in 10 * 4
I understand how to take in the integeras and the character for the operand, I am not quite sure what the function should look like
Here is what I have so fare
int evaluate (int firstnumber, char operator, int secondnumber)
{
int result;
result = firstnumber operator secondnumber;
return result;
}
Explanation / Answer
int evaluate (int firstnumber, char operator, int secondnumber)
{
int result;
switch(operator)
{
case '+':
result = firstnumber + secondnumber;
break;
case '*':
result = firstnumber * secondnumber;
break;
case '-':
result = firstnumber - secondnumber;
break;
case '/':
result = firstnumber / secondnumber;
break;
}
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.