Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Reads an expression and evaluates

C Basic Declarations and Expressions: Exercise-150 with Solution

Write a C program that reads an expression and evaluates it.

Terms and conditions:
The expression consists of numerical values, operators and parentheses, and the ends with '='.
The operators includes +, -, *, / where, represents, addition, subtraction, multiplication and division.
When two operators have the same precedence, they are applied to left to right.
You may assume that there is no division by zero.
All calculation is performed as integers, and after the decimal point should be truncated
Length of the expression will not exceed 100.
-1 × 10 9 <= intermediate results of computation <= 10 9
Sample Input:
4
10-2*3=
8*(8+2-5)=

Sample Solution:

C Code:

#include <stdio.h>
#include<string.h>

int addsub();
int muldiv();
int term();

char input[101];
int pos = 0;

int term(){
  int n = 0;

  if(input[pos] == '('){
    pos++;
    n = addsub();
    
    if(input[pos] == ')'){
      pos++;
      return n;
    }
  }else{
    while('0' <= input[pos] && input[pos] <= '9'){
      n = n*10 + (input[pos] - '0');
      pos++;
    }
  }
  return n;
}

int muldiv(){
  int first,second;
  
  first = term();
  for(;;){
    if(input[pos] == '*'){
      pos++;
      second = term();
      first *= second; 
    }else if(input[pos] == '/'){
      pos++;
      second = term();
      first /= second;
    }else{
      return first;
    }
  }
}

int addsub(){
  int first,second;
  
  first = muldiv();
  
  for(;;){
    if(input[pos] == '+'){
      pos++;
      second = muldiv();
      first += second; 
    }else if(input[pos] == '-'){
      pos++;
      second = muldiv();
      first -= second;
    }else{
      return first;
    }
  }
  
}

int main(){
  int n,i,j;
  printf("Input an expression using +, -, *, / operators:\n");
  scanf("%s",input);
  printf("%d\n",addsub());  
  return 0;
  }

Sample Output:

Input an expression using +, -, *, / operators:
1+6*8-4/2
47

Sample Output:

Input an expression using +, -, *, / operators:
25/5-6*7+2
-35

Sample Output:

Input an expression using +, -, *, / operators:
9+6+(5*2)-5
20

Flowchart:

C Programming Flowchart: Reads an expression and evaluates.
C Programming Flowchart: Reads an expression and evaluates.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program, which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
Next: C Variable Type Exercises Home

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP