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
- #include<stdio.h> //needed for printf
- #include<cstring>
- using namespace std; //standard namespace
- int idx;
- 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
- //array of infix strings
- char ifx[NMAX];
- char pfx[NMAX];
- 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' };
- int opndval[NUMOPNDS] = { 3, 1, 2, 5, 2, 4, -1, 3, 7, 187 };
- int j;
- struct OPERAND_STACK
- {
- int top;
- double items[MAXSTACK];
- };
- OPERAND_STACK opndstk;
- void opndpush(double);
- double opndpop();
- bool opnEmpty();
- void convert(char[], char[]);
- double evaluation(char[],int[NUMOPNDS],double val);
- int main()
- {
- double val;
- opndstk.top = -1;
- /*************************************************************************
- PRINT OUT THE INFIX EXPRESSIONS
- **************************************************************************/
- 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("%5i", opndval[opnd[j] - 'A']); //is not SIMPLER to just use j ONLY as the subscript?
- //Q: why the more complex syntax? A: j ONLY assumes that operands are in lexical order
- printf("\n\n\n");
- printf("RESULTS:\n\n");
- printf("%25s%25s%15s\n", "INFIX EXPRESSION", "POSTFIX RESULT", "Value");
- printf("%25s%25s%15s\n", "-----------------", "---------------", "-----");
- for (idx = 0; idx < LSIZE; idx++) //display name strings & corresponding weights
- {
- strcpy(ifx, infix[idx]);
- convert(ifx, pfx);
- val = evaluation(postfix[NMAX], opndval, val);
- printf("%25s%25s%15g\n", ifx, pfx, val);
- }
- /*************************************************************************
- PRINT OUT THE OPERANDS AND THEIR VALUES
- **************************************************************************/
- system("PAUSE");
- return 0;
- }
- /****************************************************************************
- CONVERSION FUNCTION: INFIX TO POSTFIX NOTATION
- ****************************************************************************/
- void convert(char ifx[], char pfx[])
- {
- strcpy(pfx, postfix[idx]);
- }
- /****************************************************************************
- FUNCTION TO DISPLAY INFIX AND POSTFIX EXPRESSIONS
- ****************************************************************************/
- /****************************************************************************
- FUNCTION TO EVALUATE THE POSTFIX EXPRESSION
- ****************************************************************************/
- double evaluation(char postfix[NMAX], int opndval[NUMOPNDS], double val)
- {
- char s;
- double op1;
- double op2;
- 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 '+': val = op1 + op2;
- break;
- case '-': val = op1 - op2;
- break;
- case '/': val = op1 / op2;
- break;
- case '*': val = op1 * op2;
- break;
- case '$': val = pow(op1,op2);
- break;
- default: break;
- }
- opndpush(val);
- }
- k++;
- }
- return val = opndpop();
- }
- void opndpush(double c)
- {
- opndstk.top++;
- opndstk.items[opndstk.top] = c;
- }
- double opndpop()
- {
- double c;
- c = opndstk.items[opndstk.top];
- opndstk.top--;
- return c;
- }
- bool opnEmpty()
- {
- bool r;
- if (opndstk.top == -1)
- r = true;
- else
- r = false;
- return r;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement