Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // asst5v1.cpp ALGORITHM DESIGN 306
- // arrays of infix strings, postfix strings, operands and operand-values
- #include <iostream>
- #include <iomanip> //for setw function to format output
- using namespace std; //standard namespace
- const int LMAX = 50; //maximum number of infix strings in array
- const int NMAX = 30; //maximum size of each infix string
- const int LSIZE = 5; //actual number of infix strings in the array infix
- const int NUMOPNDS = 10; //number of different operands i.e. A through J
- const int MAXSTACK = 100; //maximum number of items allowed in the stack structures
- string InToPost();
- void OPNDpush(double);
- double OPNDpop();
- bool emptystk();
- void dumpOPNDstack();
- double eValue(char[]);
- //array of infix strings
- char infix[LMAX][NMAX] = { "A+B-C",
- "(A+B)*(C-D)",
- "A$B*C-D+E/F/(G+H)",
- "((A+B)*C-(D-E))$(F+G)",
- "A-B/(C*D$E)" };
- //array of postfix strings
- char postfix[LMAX][NMAX] = { "AB+C-",
- "AB+CD-*",
- "AB$C*D-EF/GH+/+",
- "AB+C*DE--FG+$",
- "ABCDE$*/-" };
- //arrays for the operands and their values
- char opnd[NUMOPNDS] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
- double opndval[NUMOPNDS] = { 3, 1, 2, 5, 2, 4, -1, 3, 7, 187 };
- int j;
- int IDX;
- struct OPERAND_STRAND
- {
- int top = -1;
- double item[MAXSTACK];
- };
- OPERAND_STRAND opndstk;
- int main() {
- /*************************************************************************
- PRINT OUT THE OPERANDS AND THEIR VALUES
- **************************************************************************/
- printf("%40s\n\n", "OPERANDS AND THEIR VALUES:");
- for (j = 0; j < NUMOPNDS; j++)
- printf("%5c", opnd[j]);
- printf("\n\n");
- for (j = 0; j < NUMOPNDS; j++)
- printf("%5g", opndval[opnd[j] - 'A']);
- printf("\n\n");
- /*************************************************************************
- PRINT OUT THE INFIX EXPRESSIONS
- **************************************************************************/
- printf("%s\n\n\n","RESULTS");
- printf("%25s%30s%21s\n", "INFIX EXPRESSION", "POSTFIX RESULT", "eVALUE");
- printf("%25s%30s%21s\n", "--------------------", "-------------------", "-----");
- for (j = 0, IDX = j; j < LSIZE; j++, IDX++) //display name strings & corresponding weights
- printf("%25s%30s%21g\n", infix[j], InToPost().c_str(), eValue(postfix[IDX]));
- system("PAUSE");
- return 0;
- }
- /****************************************************************************
- CONVERSION FUNCTION: INFIX TO POSTFIX NOTATION
- ****************************************************************************/
- string InToPost()
- {
- return postfix[IDX];
- }
- /****************************************************************************
- FUNCTION TO DISPLAY INFIX AND POSTFIX EXPRESSIONS
- ****************************************************************************/
- /****************************************************************************
- FUNCTION TO EVALUATE THE POSTFIX EXPRESSION
- ****************************************************************************/
- double eValue(char postfix[NMAX])
- {
- double evalNum;
- char s;
- double op1;
- double op2;
- double tempVal;
- int k = 0;
- while(postfix[k] != '/0')
- {
- s = postfix[k];
- if (isalpha(s))
- OPNDpush(opndval[s - 'A']);
- else
- {
- op2 = OPNDpop();
- op1 = OPNDpop();
- switch (s)
- {
- case '+': evalNum = op1 + op2;
- break;
- case '-': evalNum = op1 - op2;
- break;
- case '/': evalNum = op1 / op2;
- break;
- case '*': evalNum = op1 * op2;
- break;
- case '$': evalNum = pow(op1, op2);
- break;
- }
- OPNDpush(evalNum);
- }
- k++;
- }
- return evalNum;
- }
- void OPNDpush(double c)
- {
- opndstk.top++;
- opndstk.item[opndstk.top] = c;
- }
- double OPNDpop()
- {
- double c;
- c = opndstk.item[opndstk.top--];
- return c;
- }
- bool emptystk()
- {
- bool r;
- if (opndstk.top == -1)
- r = true;
- else
- r = false;
- return r;
- }
- void dumpOPNDstack()
- {
- if (emptystk())
- printf("| Stack is empty |");
- else
- for (int m = 0; m <= opndstk.top; m++)
- printf("| %g |", opndstk.item[m]);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement