Pages

Monday, July 08, 2013

Write a program of Evaluating Expressions in C++

Evaluating two complex Expressions

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
   #include <iostream.h>
   #include <conio.h>
   int main()
   { 
    int a = 3;
    int b = 4;
    int c = 5;
    int x, y;
    cout << " Initial values of the variable:\n"; 
    cout << "a = " << a << "    b = " << b << "     c = " << c << endl << endl ;
     x = a * 4 + b / 2 - c * b; // Expressions without side effects
     cout << " Value of   a * 4 + b / 2 - c * b    is  : " << x << endl;

    y = --a * (3 + b) / 2 - c++ * b; // Expressions with side effects
    cout << " Value of --a * (3 + b0 / 2 - c++ * b is : " << y << endl;

    cout << "\n Values of the variables are now :\n";
    cout << "a = " << a << "    b = " << b << "     c = " << c << endl ;
    getch ();
    return 0;
               }// main

Output:

 Initial values of the variable:
 a = 3    b = 4    c = 5

 Value of   a * 4 + b / 2 - c * b    is  : -6
 Value of --a * (3 + b0 / 2 - c++ * b is : -13

 Values of the variables are now :
 a = 2    b = 4    c = 5

No comments:

Post a Comment