Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. /*
  2. Nitish Sarin
  3. Thapar University
  4. */
  5.  
  6. #include<iostream>
  7. #include<cstring>
  8. using namespace std;
  9.  
  10. void ConvertPostfix(char*); // Takes the character array, and converts it to postfix
  11. float EvaluatePostfix(char*); // Asks you the values of the variables used at runtime and evaluates the infix expression
  12. // after converting it to postfix
  13. float Calc(char, float, float); // Performs the operation operator1 <operand> operator 2
  14. bool Precedence(char, char); // It will return whether operator1 has higher precedence than operator2
  15. int Opweight(char); // Returns the relative weight of the operators based on precedence order
  16. bool Operator(char); // Returns true if the given parameter belongs to { =, -, *, /, % } --> Left Associative
  17. bool Operand(char); // Returns true if the given parameter belongs to { a-z OR A-Z )
  18.  
  19.  
  20. char postfix[100]; // Global array
  21.  
  22.  
  23. class s{ // Class for character Stack
  24. public:
  25. char arr[100];
  26. int top = -1;
  27.  
  28. void pop(){ // Pop out the top element from the stack
  29. if(empty()){
  30. cout<<"Array is empty"<<endl<<endl;
  31. return;
  32. }
  33. top--;
  34. }
  35.  
  36. void show(){ // Show the current status of the stack
  37. cout<<endl<<endl;
  38. if(empty()){
  39. cout<<"Array is empty"<<endl<<endl;
  40. return;
  41. }
  42.  
  43. for(int i=0; i<=top; i++){
  44. cout<<arr[i]<<" ";
  45. }
  46.  
  47. cout<<endl<<endl;
  48. }
  49.  
  50. bool empty(){ // Returns true if the stack is empty
  51. if(top<0){
  52. return true;
  53. }
  54. return false;
  55. }
  56.  
  57. void push(char data){ // Push data into the stack
  58. top++;
  59. arr[top]=data;
  60. }
  61.  
  62. };
  63.  
  64. class cal{ // Class for storing numerical operands in Stack
  65. public:
  66. float arr[100];
  67. int top = -1;
  68.  
  69. void pop(){
  70. if(top==-1){
  71. cout<<endl;
  72. cout<<"Array is empty"<<endl<<endl;
  73. return;
  74. }
  75. top--;
  76. }
  77.  
  78. void show(){
  79. cout<<endl<<endl;
  80. if(top==-1){
  81. cout<<"Array is empty"<<endl<<endl;
  82. return;
  83. }
  84.  
  85. for(int i=0; i<=top; i++){
  86. cout<<arr[i]<<" ";
  87. }
  88.  
  89. cout<<endl<<endl;
  90. }
  91.  
  92. bool empty(){
  93. if(top<0){
  94. return true;
  95. }
  96. return false;
  97. }
  98.  
  99. void push(char data){
  100. top++;
  101. arr[top]=data;
  102. }
  103.  
  104. };
  105.  
  106.  
  107. int main(){
  108. int op;
  109. s st;
  110. char data;
  111. char infix[100];
  112.  
  113. // ***************************User Interface******************************
  114.  
  115. cout<<"Welcome to Stack Interface Arena!"<<endl<<endl;
  116. do{
  117. cout<<"1) Push an element into the Stack."<<endl;
  118. cout<<"2) Pop an element from the Stack."<<endl;
  119. cout<<"3) Show the current status of the Stack."<<endl;
  120. cout<<"4) Convert Infix to Postfix (Using BOMDAS rule)."<<endl;
  121. cout<<"5) Evaluate a given Infix expression by first converting it to Postfix."<<endl;
  122. cout<<"6) Exit";
  123. cin>>op;
  124. switch(op){
  125. case 1:
  126. cout<<"Please enter the element which you would like to push --> ";
  127. cin>>data;
  128. st.push(data);
  129. break;
  130. case 2:
  131. st.pop();
  132. break;
  133. case 3:
  134. st.show();
  135. break;
  136. case 4:
  137. cout<<endl<<endl;
  138. cout<<"Only + - * / \% operators are allowed!!"<<endl;
  139. cout<<"Please enter the expression(Infix) --> ";
  140. cin.ignore(); // Ignore the \n character stored from the previous cin
  141. cin.getline(infix, 100); // So that we can enter a line including spaces
  142. ConvertPostfix(infix);
  143. cout<<"The final Postfix form is "<<postfix<<endl;
  144. break;
  145. case 5:
  146. cout<<"Please enter the expression(Infix) --> ";
  147. cin.ignore();
  148. cin.getline(infix, 100);
  149. cout<< "The evaluated output is "<<EvaluatePostfix(infix)<<endl;
  150. break;
  151. default:
  152. cout<<"Please enter a valid choice"<<endl;
  153. }
  154. cout<<"----------------------------------------------------"<<endl<<endl;
  155. }while(op!=6);
  156.  
  157. }
  158.  
  159.  
  160. void ConvertPostfix(char* expression){ // Takes the character array, and converts it to postfix
  161. s st1;
  162. memset(&postfix[0], 0, sizeof(postfix)); // Flush out any previous stored data
  163. int j=0;
  164. for(int i=0; i<strlen(expression); i++){
  165. if(expression[i]==' '){ // Ignore the spaces
  166. continue;
  167. }
  168.  
  169. else if(Operator(expression[i])){ // If an operator, it is pushed into the stack after
  170. // popping out all higher precedence operators
  171.  
  172. while(!st1.empty() && st1.arr[st1.top]!='(' && Precedence(st1.arr[st1.top], expression[i])){
  173. postfix[j]=st1.arr[st1.top];
  174. st1.pop();
  175. j++;
  176. }
  177. st1.push(expression[i]);
  178. }
  179. else if(Operand(expression[i])){ // Appending operand to the postfix string
  180. postfix[j]=expression[i];
  181. j++;
  182. }
  183. else if(expression[i]=='('){
  184. st1.push(expression[i]);
  185. }
  186.  
  187. else if(expression[i]==')'){ // pop out operators till opening bracket of the closed one is found
  188. while(!st1.empty() && st1.arr[st1.top]!= '('){
  189. postfix[j]=st1.arr[st1.top];
  190. j++;
  191. st1.pop();
  192. }
  193. if(!st1.empty()){
  194. st1.pop();
  195. }
  196. }
  197. }
  198. while(!st1.empty()){ // Appending the rest of the stack to the postfix expression!
  199. postfix[j]=st1.arr[st1.top];
  200. j++;
  201. st1.pop();
  202. }
  203. }
  204.  
  205. bool Operator(char op){ // Returns true if the given parameter belongs to { =, -, *, /, % } --> Left Associative
  206.  
  207. if(op == '+' || op == '-' || op == '*' || op == '/' || op== '%'){
  208. return true;
  209. }
  210. return false;
  211. }
  212.  
  213. bool Operand(char op){ // Returns true if the given parameter belongs to { a-z OR A-Z )
  214.  
  215. if((op>='a' && op<='z') || (op>='A' && op <='Z')){
  216. return true;
  217. }
  218. return false;
  219. }
  220.  
  221. bool Precedence(char op1, char op2){ // It will return whether operator1 has higher precedence than operator2
  222. int weight1= Opweight(op1);
  223. int weight2= Opweight(op2);
  224.  
  225. if(weight1>=weight2){ // As all operators are left associative
  226. return true;
  227. }
  228. return false;
  229. }
  230.  
  231. int Opweight(char op){ // Returns the relative weight of the operators based on precedence order
  232. int weight = -1;
  233. switch(op){
  234. case '-':
  235. case '+':
  236. weight = 1;
  237. break;
  238. case '/':
  239. case '*':
  240. case '%':
  241. weight = 2;
  242. break;
  243. }
  244. return weight;
  245. }
  246.  
  247.  
  248. float EvaluatePostfix(char* infix){ // Asks you the values of the variables used at runtime and evaluates the infix expression
  249. // after converting it to postfix
  250. cal st;
  251. ConvertPostfix(infix); // First converting the infix to postfix before evaluating
  252. float eval=0, temp;
  253. for(int i=0; i<sizeof(postfix); i++){
  254. if(postfix[i] == ' '){
  255. continue;
  256. }
  257. else if(Operator(postfix[i])){
  258. float op2= st.arr[st.top];
  259. st.pop();
  260. float op1= st.arr[st.top];
  261. st.pop();
  262. float eval = Calc(postfix[i], op1, op2); // Sending first two elements of stack, and the operator as arguments to the function
  263. st.push(eval);
  264. }
  265. else if(Operand(postfix[i])){
  266. cout<<" What value would you assign to the variable "<<postfix[i]<<" ? "; // Asking the values of the variables, at runtime!
  267. cin>>temp;
  268. st.push(temp);
  269. }
  270. }
  271. return st.arr[st.top];
  272. }
  273.  
  274. float Calc(char op, float op1, float op2){ // Performs the operation operator1 <operand> operator 2
  275. if(op=='+'){
  276. return op1+op2;
  277. }
  278. else if(op=='-'){
  279. return op1-op2;
  280. }
  281. else if(op=='*'){
  282. return op1*op2;
  283. }
  284. else if(op=='/'){
  285. return op1/op2;
  286. }
  287. else if(op=='%'){
  288. return (int)op1 % (int)op2;
  289. }
  290. else{
  291. cout<< "Error! Please check the inputs Carefully"<<endl;
  292. return -1;
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement