Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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. #include<stdio.h> //needed for printf
  6. #include<cstring>
  7. using namespace std; //standard namespace
  8.  
  9. int idx;
  10. const int LMAX = 50; //maximum number of infix strings in array
  11. const int NMAX = 30; //maximum size of each infix string
  12. const int LSIZE = 5; //actual number of infix strings in the array infix
  13. const int NUMOPNDS = 10; //number of different operands i.e. A through J
  14. const int MAXSTACK = 100; //maximum number of items allowed in the stack structures
  15. //array of infix strings
  16. char ifx[NMAX];
  17. char pfx[NMAX];
  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.  
  30. //arrays for the operands and their values
  31. char opnd[NUMOPNDS] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
  32. int opndval[NUMOPNDS] = { 3, 1, 2, 5, 2, 4, -1, 3, 7, 187 };
  33. int j;
  34.  
  35. struct OPERAND_STACK
  36. {
  37. int top;
  38. double items[MAXSTACK];
  39. };
  40. OPERAND_STACK opndstk;
  41.  
  42. void opndpush(double);
  43. double opndpop();
  44. bool opnEmpty();
  45. void convert(char[], char[]);
  46. double evaluation(char[],int[NUMOPNDS],double val);
  47.  
  48. int main()
  49. {
  50. double val;
  51. opndstk.top = -1;
  52. /*************************************************************************
  53. PRINT OUT THE INFIX EXPRESSIONS
  54. **************************************************************************/
  55. printf("%40s\n\n", "OPERANDS AND THEIR VALUES:");
  56. for (j = 0; j<NUMOPNDS; j++)
  57. printf("%5c", opnd[j]);
  58. printf("\n\n");
  59. for (j = 0; j<NUMOPNDS; j++)
  60. printf("%5i", opndval[opnd[j] - 'A']); //is not SIMPLER to just use j ONLY as the subscript?
  61. //Q: why the more complex syntax? A: j ONLY assumes that operands are in lexical order
  62. printf("\n\n\n");
  63. printf("RESULTS:\n\n");
  64.  
  65. printf("%25s%25s%15s\n", "INFIX EXPRESSION", "POSTFIX RESULT", "Value");
  66. printf("%25s%25s%15s\n", "-----------------", "---------------", "-----");
  67. for (idx = 0; idx < LSIZE; idx++) //display name strings & corresponding weights
  68. {
  69. strcpy(ifx, infix[idx]);
  70. convert(ifx, pfx);
  71. val = evaluation(postfix[NMAX], opndval, val);
  72. printf("%25s%25s%15g\n", ifx, pfx, val);
  73. }
  74. /*************************************************************************
  75. PRINT OUT THE OPERANDS AND THEIR VALUES
  76. **************************************************************************/
  77. system("PAUSE");
  78. return 0;
  79. }
  80. /****************************************************************************
  81. CONVERSION FUNCTION: INFIX TO POSTFIX NOTATION
  82. ****************************************************************************/
  83. void convert(char ifx[], char pfx[])
  84. {
  85. strcpy(pfx, postfix[idx]);
  86. }
  87. /****************************************************************************
  88. FUNCTION TO DISPLAY INFIX AND POSTFIX EXPRESSIONS
  89. ****************************************************************************/
  90. /****************************************************************************
  91. FUNCTION TO EVALUATE THE POSTFIX EXPRESSION
  92. ****************************************************************************/
  93. double evaluation(char postfix[NMAX], int opndval[NUMOPNDS], double val)
  94. {
  95. char s;
  96. double op1;
  97. double op2;
  98. int k = 0;
  99.  
  100. while(postfix[k] != '/0')
  101. {
  102. s = postfix[k];
  103. if(isalpha(s))
  104. opndpush(opndval[s - 'A']);
  105. else
  106. {
  107. op2 = opndpop();
  108. op1 = opndpop();
  109. switch(s)
  110. {
  111. case '+': val = op1 + op2;
  112. break;
  113. case '-': val = op1 - op2;
  114. break;
  115. case '/': val = op1 / op2;
  116. break;
  117. case '*': val = op1 * op2;
  118. break;
  119. case '$': val = pow(op1,op2);
  120. break;
  121. default: break;
  122. }
  123. opndpush(val);
  124. }
  125. k++;
  126. }
  127. return val = opndpop();
  128. }
  129.  
  130. void opndpush(double c)
  131. {
  132. opndstk.top++;
  133. opndstk.items[opndstk.top] = c;
  134. }
  135.  
  136. double opndpop()
  137. {
  138. double c;
  139. c = opndstk.items[opndstk.top];
  140. opndstk.top--;
  141. return c;
  142. }
  143.  
  144. bool opnEmpty()
  145. {
  146. bool r;
  147. if (opndstk.top == -1)
  148. r = true;
  149. else
  150. r = false;
  151. return r;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement