1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
***Program Goals***
1. convert infix expression to RPN.
2. Read input file into a string /done
3. use stack to convert input string into RPN output string
4. Separte push pop functions /done
5. create function to determine priority of the operators.
6. output both infix and RPN strings into output file. /infix done
***Algortithm***
1. Function for create stack. /done
2. functions to determine empty and full stack /done
3. functions for push and pop /done
4. Read data from input file /done
5. convert infix to rpn.
6. function to determine priority of operators // may be involved in rpn conversion, ask for clarification.
7. output infix and RPN strings to Program3RPN.ot
8. main function to call createStack and readEm functions. (personal goal keep main function as clean and short as possible.)
9. E.C. include parentheses in conversion, and add extra date to input file to be read in.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctype.h>
using namespace std;
const int maxstack = 20;
const char initstack = 'z';
void createStack(char stack[], int &top)
{
int i;
top = -1;
for (i = 0; i < maxstack; i++)
stack[i] = initstack;
}
bool fullStack(int top)
{
return top >= maxstack - 1;
}
bool emptyStack(int top)
{
return top < 0;
}
void push(char stack[], int &top, char ch)
{
if (!fullStack(top))
{
top++;
stack[top] = ch;
}
else
cout << "The stack is full, can't push" << endl;
}
char pop(char stack[], int &top)
{
char ch = initstack;
if (!emptyStack(top))
{
ch = stack[top];
stack[top] = initstack;
top--;
}
return ch;
}
int priority(char ch)
{
switch (ch)
{
case '+':
return 1;
break;
case '-':
return 1;
break;
case '*':
return 5;
break;
case '/':
return 5;
break;
}
}
void readEm(char stack[], int &top, char ch)
{
string inString, outString;
int i, len;
ifstream inf("Program3RPN.dat");
ofstream outf("Program3RPN.ot");
while (!inf.eof())
{
inf >> inString;
ch = inString[i];
outString += ch;
len = inString.length();
for (i = 0; i < len; i++)
{
ch = inString[i];
if(ch == isalpha(i))
outString += ch;
else if(emptyStack(top))
push(stack, top, ch);
else if (priority(ch)== priority(stack[top]))
push(stack, top, ch);
else if(emptyStack(top))
push(stack, top, ch);
else if(priority(ch) >= priority(stack[top]))
push (stack, top, ch);
}
}
while (!emptyStack(top) && priority(ch) <= priority(stack[top]))
{
push(stack, top, ch);
}
cout << inString << " " << outString << endl;
outf << inString << " " << outString << endl;
//cout << inString << " " << outString << endl;
}
int main()
{
char ch;
char stack[maxstack];
int top;
createStack(stack, top);
readEm(stack, top, ch);
system("pause");
return 0;
}
|