You are on page 1of 6

Assignment-01

2.Write a program to convert decimal number to binary number by


using stack.
1. #include <bits/stdc++.h>
2. using namespace std;
4.
5. //class for Decimal to binary conversion operations
6. class dtob
7. {
8. public:
9. int temp,i=0,carry=1;
10. string st;
11. double integ;
12. stack<int> s;
13. dtob(int no,bool type)
14. {
15. while(no>0)
16. {
17. temp=no%2;
18. no=no/2;
19. s.push(temp);
20. }
21. if(type==true)
22. {
23. while(!s.empty())
24. {
25. cout<<s.top();
26. s.pop();
27. }
28. }
29. else
30. {
31. while(!s.empty())
32. {
33. int x = s.top();
34. st += x + '0';
35. s.pop();
36. }
37.
38. for(int i=0;i<st.length();i++)
39. {
40. if(st[i] == '0')
41. {
42. st[i] = '1';
43. }

Umar Asghar-70076360
Assignment-01

Assignment-01

44. else
45. {
46. st[i] = '0';
47. }
48. }
49.
50. for(int i=st.length()-1;i>=0;i--)
51. {
52. if (st[i] == '1' && carry == 1)
53. {
54. s.push(0);
55. }
56. else if (st[i] == '0' && carry == 1)
57. {
58. s.push(1);
59. carry = 0;
60. }
61. else
62. {
63. s.push(st[i]-'0');
64. }
65. }
66. while(!s.empty())
67. {
68. cout<<s.top();
69. s.pop();
70. }
71. }
72. }
73. //function to convert fraction part
74. void fraction(double fract)
75. {
76. fract=fract*2;
77. temp=fract;
78. cout<<temp;
79. fract=modf(fract,&integ);
80. if(i<9)
81. {
82. i++;
83. return fraction(fract);
84. }
85. }
86. };
87.
88. int main() {

Umar Asghar-70076360
Assignment-01

Assignment-01

89. ios_base::sync_with_stdio(false);
90. cin.tie(NULL);
91. double no,integ,fract;
92. cin>>no;
93. if(no>0)
94. {
95. fract=modf(no,&integ);
96. if(fract>0)
97. {
98. dtob o(no,true);
99. cout<<".";
100. o.fraction(fract);
101. }
102. else
103. {
104. dtob o(no,true);
105. }
106. }
107. else
108. {
109. cout<<"-";
110. no=no*-1;
111. fract=modf(no,&integ);
112. if(fract>0)
113. {
114. dtob o(no,true);
115. cout<<".";
116. o.fraction(fract);
117. }
118. else
119. {
120. dtob o(no,false);
121. }
122. }
123. }
124.

Output:

Umar Asghar-70076360
Assignment-01

3. Write an Algorithm to reverse the given string using stack.

Method: This difficult can be resolved not only with the help of the strtok() but
also it can be solved by using Stack Bottle Class in STL C++ by following the given
steps:

Make an unfilled stack.


Traverse the whole string, while traversing add the characters of the string into a
provisional
variable until you get a space(‘ ‘) and push that provisional variable into the stack.
Duplication the above step until the end of the string.
Pop the words from the stack till the stack is not unfilled which will be in reverse
order.

Umar Asghar-70076360
Assignment-01

1. Draw the flow chart parsing for well-formed parenthesis.


Sol:
Algorithm:

Declare a character stack S.


Now traverse the expression string exp.
If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then
push it to stack.
If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then
pop from stack and if the popped character is the matching
starting bracket then fine else brackets are not balanced.
After complete traversal, if there is some starting bracket left
in stack then “not balanced”

Umar Asghar-70076360
Assignment-01

Umar Asghar-70076360

You might also like