You are required to write a C program to carry out a strict-left-to-right evalua
ID: 3674425 • Letter: Y
Question
You are required to write a C program to carry out a strict-left-to-right evaluation of an arithmetic expression consisting of integer constants and the operators +, -, * and /. Here, the operator / denotes integer division; that is, the remainder is discarded. In a strict-left-to-right evaluation, there is no notion of precedence. For example, the value of the expression 6+4*3 when evaluated in a strict-left-to-right fashion is 30. (Under usual precedence rules, where multiplication has higher precedence than addition, the value of the above expression would be 18.) Similarly, the value of the expression 6+4*3/7-9 when evaluated in a strict-left-to-right fashion is = -5.
Here is some additional information about the input.
Each input expression is terminated by the newline (' ') character.
There may be zero or more spaces between successive non-blank characters of an expression.
You may assume that the given expression is valid; that is, it satisfies all of the following
conditions.
The expression consists only of integer constants, the operators +, -, * and / and spaces.
In particular, the expression won't contain parentheses.
Each integer constant in the expression consists of only one decimal digit.
The expression begins with an integer constant, without any preceding sign.
In the expression, integer constants and operators alternate.
Note that an expression consisting of a single digit integer constant, without any operators, is a valid expression.
The outline of your C program is as follows.
1) Prompt the user for an expression.
2) Read the expression character by character and carry out a strict-left-to-right evaluation of the
expression.
3) Print the value of the result obtained in Step 2 and stop.
Thus, each time your program is executed, it should handle just one expression. No error checks are needed.
Explanation / Answer
#include <stdio.h>
#include<string.h>
int calculate(int num1, int num2, char op){
int result = 0;
switch(op){
case '+':
result= num1+num2;
break;
case '-':
result= num1-num2;
break;
case '*':
result= num1*num2;
break;
case '/':
result= num1/num2;
}
return result;
}
int skipSpaces(char exp[], int i, int length){
while(exp[i]==' '|| exp[i]==' ')
i++;
return i;
}
int main()
{
char exp[50] = {''};
printf("Enter expression: ");
fgets (exp, 100, stdin);
int length = strlen(exp);
int i, result;
bool flag = true;
if(length ==1)
printf("Result: %c ",exp[0]);
else{
for(i=0; i<length;){
// ignoring spaces
i = skipSpaces(exp, i, length);
if(i >= length) // if i crocess length, then break
break;
int num1;
if(flag){
num1 = exp[i++] - '0';
flag = false;
}else{
num1= result;
}
// ignoring spaces
i = skipSpaces(exp, i, length);
if(i >= length) // if i crocess length, then break
break;
char op = exp[i++];
// ignoring spaces
i = skipSpaces(exp, i, length);
if(i >= length)
break;
int num2 = exp[i++] - '0';
result = calculate(num1, num2, op);
}
}
printf("Result: %d ",result);
return 0;
}
/*
Output:
Enter expression: 5*6+4*4-3
Result: 133
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.