Page 185 - C++
P. 185
(e) Evaluate the following postfix expression showing stack status after every step (2)
8, 2, +, 5, 3, -, *, 4 /
Ans.
Element scanned from Stack status after Operation performed
postfix expression processing the scanned
token
8 8 Push 8
2 8, 2 Push 2
+ 10 Op2=pop() i.e 2
Op1=pop() i.e 8
Push(op1+op2) i.e. 8+2
5 10, 5 Push(5)
3 10, 5, 3 Push(3)
- 10, 2 Op2=pop() i.e. 3
Op1=pop() i.e. 5
Push(op1-op2) i.e. 5-3
* 20 Op2=pop() i.e. 2
Op1=pop() i.e. 10
Push(op1-op2) i.e. 10*2
4 20, 4 Push 4
/ 5 Op2=pop() i.e. 4
Op1=pop() i.e. 20
Push(op1/op2) i.e. 20/4
NULL Final result 5 Pop 5 and return 5
Q4 (a) Write a C++ program, which initializes a string variable to the content.”Time is a great teacher but
unfortunately it kills all its pupils. Berlioz”and output the string one character at a time to the disk file OUT.txt.
Include all the header files required. (2)
Ans.
#include<fstream.h>
int main()
{
ofstream fout(“OUT.TXT”);
char *str= “Time is a great teacher but unfortunately it kills all its pupils. Berlioz”;
int i=0;