Advertisement
palmerstone

Postfix lab

Jul 31st, 2011
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. int t, i, j, k, l, d, x, y, z;
  6. char str[100000], str2[100000], str3[100000];
  7.  
  8. void do_it()
  9. {
  10. puts("Enter your expression or press Any key to continue");
  11. }
  12.  
  13. int isoperator(char ch)
  14. {
  15. if (ch == '+' || ch == '-' || ch == '/' || ch == '^' || ch == '*') return 1;
  16. return 0;
  17. }
  18.  
  19. void check()
  20. {
  21. for (k = l - 1; ;k--)
  22. {
  23. if (str2[k] == ')' || str2[k] == '(') break;
  24. if (str2[k] == '.') continue;
  25.  
  26. if ((str[j] == '-' || str[j] == '+' && ( (str2[k] == '*' || str2[k] == '/') || str2[k] == '^' )) || ( (str[j] == '*' || str[j] == '/') && str2[k] == '^'))
  27. {
  28. str3[d] = str2[k];
  29. d++;
  30. str2[k] = '.';
  31. }
  32. }
  33. str2[l] = str[j];
  34. l++;
  35. }
  36.  
  37. void exodus()
  38. {
  39. for (j = strlen(str2); j >= 0; j--)
  40. {
  41. if (isoperator(str2[j]))
  42. {
  43. str3[d] = str2[j];
  44. d++;
  45. str2[j] = '.';
  46. }
  47. }
  48. str3[d] = '\0';
  49. }
  50.  
  51. int main()
  52. {
  53. for (; ;)
  54. {
  55. do_it();
  56. gets(str);
  57. z = strcmp(str, "0");
  58. if (z == 0) break;
  59.  
  60. d = 0, l = 1;
  61. str2[0] = ')';
  62.  
  63. for (j = 0; str[j] != '\0'; j++)
  64. {
  65. if (str[j] == '(')
  66. {
  67. str2[l] = str[j];
  68. l++;
  69. }
  70. if (isalnum(str[j]))
  71. {
  72. str3[d] = str[j];
  73. d++;
  74. }
  75.  
  76. if (isoperator(str[j])) check();
  77.  
  78. if (str[j] == ')')
  79. {
  80. for (k = l - 1; str2[k] != '('; k--)
  81. {
  82. if (str2[k] == '.') continue;
  83. str3[d] = str2[k];
  84. d++;
  85. str2[k] = '.';
  86. }
  87. }
  88. }
  89. str2[l] = '\0';
  90. exodus();
  91.  
  92. printf("%s\n", str3);
  93. puts("");
  94. }
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement