fork download
  1. //Task1 stak
  2. #include<bits/stdc++.h>
  3. #define MAXSIZE 100
  4. using namespace std;
  5. class Stak{
  6. int items[MAXSIZE];
  7. int top;
  8. public:
  9. Stak(){
  10. top=-1;
  11. }
  12. bool is_empty();
  13. bool is_full();
  14. void push(int item);
  15. int pop();
  16. int min_element();
  17. int max_element();
  18. int count();
  19. int top_elemant();
  20. float eval_postfix(string s) ;
  21. string infix_postfix(string s) ;
  22. void desplay();
  23. }integer,chr,obj;
  24. bool Stak::is_empty() {
  25. if(top==-1)return 1;
  26. return 0;
  27. }
  28. bool Stak::is_full() {
  29. if(top==MAXSIZE-1)return 1;
  30. return 0;
  31. }
  32. void Stak::push(int item) {
  33. if(is_full()){
  34. cout<<"over flow";
  35. return;
  36. }
  37. top++;
  38. items[top]=item;
  39.  
  40. }
  41. int Stak::count() {
  42. return top + 1;
  43. }
  44. int Stak::pop(){
  45. if(is_empty()){
  46. cout<<"under flow";
  47. return 0;
  48. }
  49. int item =items[top];
  50. top--;
  51. return item;
  52. }
  53. int Stak::top_elemant(){
  54. if(is_empty()){
  55. cout<<"Stack is empty\n";
  56. return 0;
  57. }
  58. return items[top];
  59.  
  60. }
  61. int Stak::min_element(){
  62. if(is_empty()){
  63. cout<<"Stack is empty\n";
  64. return 0;
  65. }
  66. int min = INT16_MAX;
  67. for(int i=0;i<=top;i++){
  68. if(items[i]<min){
  69. min=items[i];
  70. }
  71. }
  72. return min;
  73. }
  74. int Stak:: max_element() {
  75. if (is_empty()) {
  76. cout << "Stack is empty!" << endl;
  77. return 0;
  78. } else {
  79. int max_elem = INT16_MIN;
  80. for (int i = 0; i <= top; ++i) {
  81. if (items[i] > max_elem) {
  82. max_elem = items[i];
  83. }
  84. }
  85. return max_elem;
  86. }
  87. }
  88. void Stak::desplay() {
  89. for(int i=0;i<=top;i++){
  90. cout<<items[i]<<" ";
  91. }
  92. cout<<"\n";
  93. }
  94. float Stak::eval_postfix(string s) {
  95. for(int i=0;i<s.size();i++){
  96. if(isdigit(s[i])){
  97. integer.push(s[i]-48);
  98. }
  99. else{
  100. float opr2=integer.pop();
  101. float opr1=integer.pop();
  102. float val;
  103. switch (s[i]) {
  104. case '+': val=opr1+opr2;break;
  105. case '/': val=opr1/opr2;break;
  106. case '*': val=opr1*opr2;break;
  107. case '-': val=opr1-opr2;break;
  108. case '^': val=pow(opr1,opr2);break;
  109. }
  110. integer.push(val);
  111. }
  112. }
  113. return integer.pop();
  114. }
  115. int precedence(char op) {
  116. if (op == '+' || op == '-')
  117. return 1;
  118. if (op == '*' || op == '/')
  119. return 2;
  120. return 0;
  121. }
  122. string Stak::infix_postfix(string s){
  123. string postfix="";
  124. for(int i=0;i<s.size();i++){
  125. if(isalnum(s[i])){
  126. postfix+=s[i];
  127. }
  128. else if(s[i]=='('){
  129. chr.push(s[i]);
  130. }
  131. else if(s[i]==')'){
  132. while(!chr.is_empty()&&chr.top_elemant()!='('){
  133. postfix+=chr.pop();
  134. }
  135. }
  136. else{
  137. while(!chr.is_empty()&&chr.top_elemant()!='('&& precedence(s[i])<=precedence(chr.top_elemant())){
  138. postfix+=chr.pop();
  139. }
  140. chr.push(s[i]);
  141. }
  142. }
  143. while(!chr.is_empty()){
  144. postfix+=chr.pop();
  145. }
  146. return postfix;
  147. }
  148.  
  149. int main(){
  150. #ifndef ONLINE_JUDGE
  151. freopen("out.out", "w", stdout);
  152. freopen("in.in", "r", stdin);
  153. #else
  154. #endif
  155. obj.push(5);
  156. obj.push(6);
  157. obj.push(7);
  158. obj.push(8);
  159. cout<<obj.is_full()<<"\n";
  160. obj.desplay();
  161. obj.pop();
  162. obj.desplay();
  163. cout<< obj.max_element()<<"\n";
  164. cout<< obj.min_element()<<"\n";
  165. cout<< obj.top_elemant()<<"\n";
  166. obj.pop();
  167. obj.pop();
  168. obj.pop();
  169. cout<<obj.is_empty()<<"\n";
  170.  
  171. string infix;
  172. cout << "Enter an infix expression: ";
  173. cin >> infix;
  174.  
  175. string postfix = obj.infix_postfix(infix);
  176. cout << "Postfix expression: " << postfix << endl;
  177. return 0;
  178. }
  179. /*******************************************/
  180.  
  181.  
  182.  
  183.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
0
5 6 7 8 
5 6 7 
7
5
7
1
Enter an infix expression: Postfix expression: