Advertisement
Karim_Gabr

Untitled

Aug 19th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int tos = -1;
  6. int tos2 = -1;
  7.  
  8. char input[100];
  9. int size1 = strlen(input);
  10.  
  11. int size2 = size1+5;
  12. char output[size2];
  13. char operator[size2];
  14.  
  15. int result_s[100];
  16.  
  17. int k = 0;
  18. for(k = 0 ; k < 100 ; k++)
  19. {
  20.     input[k] = NULL;
  21.     result_s[k] = 0;
  22. }
  23. for(k = 0 ; k < size2 ; k++)
  24. {
  25.     output[k] = NULL;
  26.     operator[k] = NULL;
  27. }
  28.  
  29.  
  30. int is_empty(char string[], int maximum_size)
  31. {
  32.     if (tos < 0 || string[0] == NULL) return 1;
  33.     else return 0;
  34. }
  35.  
  36. void push(char string[] , char element)
  37. {
  38.     tos++;
  39.     string[tos] = element;
  40.     return;
  41. }
  42.  
  43. char pop(char string[])
  44. {
  45.     char element;
  46.     element = array[tos];
  47.     string[tos] = NULL;
  48.     tos--;
  49.     return element;
  50. }
  51.  
  52. void infix_to_postfix(char string[])
  53. {
  54.     int i = 0;
  55.     int j = 0;
  56.    
  57.     while (i != size1)
  58.     {
  59.         if (string[i] >= '0' && string[i] <= '9')
  60.         {
  61.             output[j] = string[i];
  62.             j++;
  63.             i++;
  64.             continue;  
  65.         }
  66.        
  67.         else switch(string[i])
  68.         {
  69.             case '(':case '+': case '-': case '*': case '/':
  70.                 while ( !is_empty(operator , size2) && strchr (string[i] , "+-") != NULL && strchr(operator[tos] , "*/" != NULL) )
  71.                     {
  72.                         if ( strchr(operator[tos] , "(" != NULL) )    break;
  73.                        
  74.                         output[j] = pop(operator);
  75.                         j++;
  76.                     }
  77.                 push(operator,string[i]);
  78.                 i++;
  79.                 continue;
  80.            
  81.             case ')':
  82.                 while (!isempty(operator , size2))
  83.                 {
  84.                     output[j] = pop(operator,size2);
  85.                     j++;
  86.                    
  87.                     if ( strchr(operator[tos] , "(" != NULL) )
  88.                     {
  89.                         pop(operator,size2);
  90.                         continue;
  91.                     }
  92.                 }
  93.         }
  94.     }
  95.  
  96.     while (!isempty(operator , size2))
  97.     {
  98.         if ( strchr(operator[tos] , "(" != NULL) )    pop(operator,size2);
  99.  
  100.         else
  101.         {
  102.             output[j] = pop(operator,size2);
  103.             j++;
  104.         }        
  105.     }
  106. }
  107.  
  108. void push_to_result(int array[], int number)
  109. {
  110.     tos2++;
  111.     array[tos] = number;
  112.     return;
  113. }
  114.  
  115. void pop_from_result(int array[])
  116. {
  117.     int element;
  118.     element = array[tos];
  119.     array[tos] = 0;
  120.     tos--;
  121.     return element;
  122. }
  123.  
  124. int evaluate_postfix(char string[])
  125. {
  126.     int operand1;
  127.     int operand2;
  128.  
  129.     int temp_result = 0;
  130.  
  131.     int i = 0;
  132.  
  133.     while(i != size2)
  134.     {
  135.         if (string[i] >= '0' && string[i] <= '9')
  136.         {
  137.             push_to_result(result_s,0);
  138.             i++;
  139.             continue;  
  140.         }
  141.  
  142.         switch(string[i])
  143.         {
  144.             case '0':
  145.                 push_to_result(result_s,0);
  146.                 i++;
  147.                 continue;
  148.             case '1':
  149.                 push_to_result(result_s,1);
  150.                 i++;
  151.                 continue;
  152.             case '2':
  153.                 push_to_result(result_s,2);
  154.                 i++;
  155.                 continue;
  156.             case '3':
  157.                 push_to_result(result_s,3);
  158.                 i++;
  159.                 continue;
  160.             case '4':
  161.                 push_to_result(result_s,4);
  162.                 i++;
  163.                 continue;
  164.             case '5':
  165.                 push_to_result(result_s,5);
  166.                 i++;
  167.                 continue;
  168.             case '6':
  169.                 push_to_result(result_s,6);
  170.                 i++;
  171.                 continue;
  172.             case '7':
  173.                 push_to_result(result_s,7);
  174.                 i++;
  175.                 continue;
  176.             case '8':
  177.                 push_to_result(result_s,8);
  178.                 i++;
  179.                 continue;
  180.             case '9':
  181.                 push_to_result(result_s,9);
  182.                 i++;
  183.                 continue;
  184.             case '+':
  185.                 operand2 = pop_from_result(result_s);
  186.                 operand1 = pop_from_result(result_s);
  187.                 temp_result = operand1 + operand2;
  188.                 push_to_result(result_s,temp_result);
  189.                 i++;
  190.                 continue;
  191.             case '-':
  192.                 operand2 = pop_from_result(result_s);
  193.                 operand1 = pop_from_result(result_s);
  194.                 temp_result = operand1 - operand2;
  195.                 push_to_result(result_s,temp_result);
  196.                 i++;
  197.                 continue;
  198.             case '*':
  199.                 operand2 = pop_from_result(result_s);
  200.                 operand1 = pop_from_result(result_s);
  201.                 temp_result = operand1 * operand2;
  202.                 push_to_result(result_s,temp_result);
  203.                 i++;
  204.                 continue;
  205.             case '/';
  206.                 operand2 = pop_from_result(result_s);
  207.                 operand1 = pop_from_result(result_s);
  208.                 temp_result = operand1 / operand2;
  209.                 push_to_result(result_s,temp_result);
  210.                 i++;
  211.                 continue;
  212.         }
  213.     }
  214.  
  215.     return temp_result;
  216. }
  217.  
  218. int main()
  219. {
  220.     int result;
  221.    
  222.     puts("Infix to Postfix Evaluation Program :");
  223.     puts("--------------------------------------\n");
  224.    
  225.     printf("Enter Mathematical Expression : ");
  226.     gets(input);
  227.    
  228.     infix_to_postfix(input);
  229.    
  230.     result = evaluate_postfix(output);
  231.     printf("\nResult = %d", result);
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement