Guest User

Untitled

a guest
Jan 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio>
  3.  
  4. class ListStack
  5. {
  6. private:
  7. struct node
  8. {
  9.  
  10. int num;
  11. node *next;
  12. }*top;
  13. int operand_count;
  14.  
  15. public:
  16.  
  17. ListStack()
  18. {
  19. top=NULL;
  20. }
  21.  
  22. int push(int);
  23. int pop();
  24. void display();
  25. void get_expression();
  26. int get_value(int);
  27.  
  28. };
  29.  
  30. int ListStack::push(int c)
  31. {
  32. node *temp;
  33. temp = new node;
  34. temp->num=c;
  35. temp->next=top;
  36. top=temp;
  37. return c;
  38.  
  39. }
  40.  
  41. int ListStack::pop()
  42. { int c;
  43. if(top==NULL)
  44. cout<<"Stack UnderFlow"<<endl;
  45. else
  46. {
  47. node *temp;
  48. temp=top;
  49. cout<<"deleted Number from the stack = ";
  50. c=top->num;
  51. top=top->next;
  52. //return (temp->num);
  53. delete temp;
  54. return (c);
  55. }
  56. }
  57.  
  58.  
  59. void ListStack::display()
  60. { node*temp;
  61. temp=top;
  62. while(temp!=NULL)
  63. {
  64. cout<<"n"<<temp->num<<endl;
  65. temp=temp->next;
  66. }
  67. }
  68.  
  69. int ListStack::get_value(int c)
  70. { int operand_count;
  71. cout<<"how many number of operands you have?";
  72. cin>>operand_count;
  73. int numeric_array[5];
  74. int i;
  75.  
  76. for (i=0;i<=operand_count;i++)
  77. {
  78. cout<<"Enter Value: ";
  79. cin>>numeric_array[i];
  80. //return numeric_array;
  81. }
  82. }
  83.  
  84. void ListStack::get_expression()
  85. {
  86. char save;
  87. int i=0;
  88. int first_operand, second_operand, result;
  89. char postfix_array[50], No_operator[50];
  90. cout<<"Enter the numeric Postfix expression: ";
  91. cin.getline(postfix_array,50);
  92.  
  93. while (postfix_array[i]!='')
  94. save=postfix_array[i];
  95. if (save!= '+' && save!= '-' && save!= '*' &&save!= '/' &&save!= '^')
  96. {
  97.  
  98. for(i=0; i<=50; i++)
  99. {
  100.  
  101. cin>>No_operator[i];
  102. get_value( No_operator[i]);
  103. }
  104.  
  105. while (No_operator[i]!='')
  106.  
  107. push( No_operator[i]);
  108.  
  109. }
  110. else
  111. first_operand = pop();
  112. second_operand = pop();
  113. switch(save)
  114. {
  115. case '+':
  116. result=first_operand + second_operand;
  117. cout<<result;
  118. push(result);
  119. break;
  120.  
  121. case '-':
  122. result=first_operand - second_operand;
  123. cout<<result;
  124. push(result);
  125. break;
  126.  
  127. case '*':
  128. result=first_operand * second_operand;
  129. cout<<result;
  130. push(result);
  131. break;
  132.  
  133. case '/':
  134. result=first_operand / second_operand;
  135. cout<<result;
  136. push(result);
  137. break;
  138.  
  139. case '%':
  140. result=first_operand % second_operand;
  141. cout<<result;
  142. push(result);
  143. break;
  144. }
  145. }
  146.  
  147. void main()
  148. {
  149. ListStack LS;
  150. LS.get_expression();
  151. LS.display();
  152. getche();
  153. }
Add Comment
Please, Sign In to add comment