Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. // asst5v1.cpp ALGORITHM DESIGN 306
  2. // arrays of infix strings, postfix strings, operands and operand-values
  3. #include <iostream>
  4. #include <iomanip> //for setw function to format output
  5. using namespace std; //standard namespace
  6. const int LMAX = 50; //maximum number of infix strings in array
  7. const int NMAX = 30; //maximum size of each infix string
  8. const int LSIZE = 5; //actual number of infix strings in the array infix
  9. const int NUMOPNDS = 10; //number of different operands i.e. A through J
  10. const int MAXSTACK = 100; //maximum number of items allowed in the stack structures
  11. string InToPost();
  12. void OPNDpush(double);
  13. double OPNDpop();
  14. bool emptystk();
  15. void dumpOPNDstack();
  16. double eValue(char[]);
  17. //array of infix strings
  18. char infix[LMAX][NMAX] = { "A+B-C",
  19. "(A+B)*(C-D)",
  20. "A$B*C-D+E/F/(G+H)",
  21. "((A+B)*C-(D-E))$(F+G)",
  22. "A-B/(C*D$E)" };
  23. //array of postfix strings
  24. char postfix[LMAX][NMAX] = { "AB+C-",
  25. "AB+CD-*",
  26. "AB$C*D-EF/GH+/+",
  27. "AB+C*DE--FG+$",
  28. "ABCDE$*/-" };
  29. //arrays for the operands and their values
  30. char opnd[NUMOPNDS] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
  31. double opndval[NUMOPNDS] = { 3, 1, 2, 5, 2, 4, -1, 3, 7, 187 };
  32. int j;
  33. int IDX;
  34. struct OPERAND_STRAND
  35. {
  36. int top = -1;
  37. double item[MAXSTACK];
  38. };
  39. OPERAND_STRAND opndstk;
  40. int main() {
  41.  
  42. /*************************************************************************
  43. PRINT OUT THE OPERANDS AND THEIR VALUES
  44. **************************************************************************/
  45. printf("%40s\n\n", "OPERANDS AND THEIR VALUES:");
  46. for (j = 0; j < NUMOPNDS; j++)
  47. printf("%5c", opnd[j]);
  48.  
  49. printf("\n\n");
  50. for (j = 0; j < NUMOPNDS; j++)
  51. printf("%5g", opndval[opnd[j] - 'A']);
  52. printf("\n\n");
  53. /*************************************************************************
  54. PRINT OUT THE INFIX EXPRESSIONS
  55. **************************************************************************/
  56. printf("%s\n\n\n","RESULTS");
  57. printf("%25s%30s%21s\n", "INFIX EXPRESSION", "POSTFIX RESULT", "eVALUE");
  58. printf("%25s%30s%21s\n", "--------------------", "-------------------", "-----");
  59.  
  60. for (j = 0, IDX = j; j < LSIZE; j++, IDX++) //display name strings & corresponding weights
  61. printf("%25s%30s%21g\n", infix[j], InToPost().c_str(), eValue(postfix[IDX]));
  62. system("PAUSE");
  63. return 0;
  64. }
  65. /****************************************************************************
  66. CONVERSION FUNCTION: INFIX TO POSTFIX NOTATION
  67. ****************************************************************************/
  68. string InToPost()
  69. {
  70. return postfix[IDX];
  71. }
  72.  
  73. /****************************************************************************
  74. FUNCTION TO DISPLAY INFIX AND POSTFIX EXPRESSIONS
  75. ****************************************************************************/
  76. /****************************************************************************
  77. FUNCTION TO EVALUATE THE POSTFIX EXPRESSION
  78. ****************************************************************************/
  79. double eValue(char postfix[NMAX])
  80. {
  81. double evalNum;
  82. char s;
  83. double op1;
  84. double op2;
  85. double tempVal;
  86. int k = 0;
  87.  
  88. while(postfix[k] != '/0')
  89. {
  90. s = postfix[k];
  91.  
  92. if (isalpha(s))
  93. OPNDpush(opndval[s - 'A']);
  94. else
  95. {
  96. op2 = OPNDpop();
  97. op1 = OPNDpop();
  98. switch (s)
  99. {
  100. case '+': evalNum = op1 + op2;
  101. break;
  102. case '-': evalNum = op1 - op2;
  103. break;
  104. case '/': evalNum = op1 / op2;
  105. break;
  106. case '*': evalNum = op1 * op2;
  107. break;
  108. case '$': evalNum = pow(op1, op2);
  109. break;
  110. }
  111. OPNDpush(evalNum);
  112. }
  113. k++;
  114. }
  115. return evalNum;
  116. }
  117.  
  118. void OPNDpush(double c)
  119. {
  120. opndstk.top++;
  121. opndstk.item[opndstk.top] = c;
  122. }
  123.  
  124. double OPNDpop()
  125. {
  126. double c;
  127. c = opndstk.item[opndstk.top--];
  128. return c;
  129. }
  130.  
  131. bool emptystk()
  132. {
  133. bool r;
  134. if (opndstk.top == -1)
  135. r = true;
  136. else
  137. r = false;
  138. return r;
  139. }
  140.  
  141. void dumpOPNDstack()
  142. {
  143. if (emptystk())
  144. printf("| Stack is empty |");
  145. else
  146. for (int m = 0; m <= opndstk.top; m++)
  147. printf("| %g |", opndstk.item[m]);
  148. printf("\n");
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement