Hi, I really need some help with this code. I am really stuck on this and have no clue where to go, any help would be much appreciated.
here is my objective:
Write a c program thatcalculates the value of a mathematical expression comprised ofpositive numbers and the operations ‘+’ and ‘-’.Specifically, first prompt the user to input an expression, read itin as a string, and then print the value of the expression. You mayassume that the expression does not contain spaces, maximumsize of the expression (including digits and operators) can be 20,and that all numbers are single digit numbers.
Hints: Note that, thedigits would be read in as characters; you will need to translatethem to numbers (recall the ASCII table).
Implementation Requirements
- Write a function called “evaluate” that takes as input a mathematical expression (as a string) and returns the value of the expression. The prototype of the function is—“int evaluate(char expr[]);”
Sample Output:
Input: 4+2-1+7
Output:12
here is the code:
the program runs, but the output is not coming out correct.Code:int main() { char expr[21]; int a,ssum; printf("Input: "); scanf("%20s", expr); ssum = evaluate(&a); printf("\nOutput: %d", ssum); } int evaluate(char expr[]) { int i, sum=0; for(i=0; i<21; i++) { sum = sum + expr[i]; } return sum; }
Thanks for all the help in advance.