Skip to content

Commit c8201bc

Browse files
authored
Add files via upload
1 parent c1b7473 commit c8201bc

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

infix.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(3 + 4)
2+
((5 + 4) * 8)
3+
((0 * (1 + 2)) / (3 - (4 + 5)) * (6 / 7 / (8 * 9)))
4+
(1 - 3 * (4 + 5 * 6) + 7)

myBet.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <iostream>
2+
#include "stack.h"
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
string postfix(string in);
8+
string prefix(string pf);
9+
bool isFunct(char &curr);
10+
int comp(char x);
11+
12+
int main()
13+
{
14+
ifstream input("infix.dat");
15+
if(input.is_open()){ // if file is open
16+
string in;
17+
while(getline(input, in)){ // read line by line
18+
string pf = postfix(in); // convert to postfix first
19+
cout<< prefix(pf)<<endl; // convert from postfix to infix
20+
} // end of read
21+
input.close();
22+
}
23+
24+
return 0;
25+
}
26+
/////////////////////////////////////////////////////////////////////
27+
28+
// parameter: infix input from infix.dat
29+
// output: returns string in postfix form
30+
string postfix(string in){
31+
stack<char> bet;
32+
string result;
33+
34+
for(int i = 0; i<in.length(); i++){
35+
char x = in[i];
36+
if(isdigit(x))
37+
result += x;
38+
else if(x == '(')
39+
bet.push('(');
40+
else if(x == ')'){
41+
while(bet.top()!='('){
42+
char y = bet.topAndPop();
43+
result += y;
44+
}
45+
bet.pop();
46+
}
47+
else if(isFunct(x)){
48+
while(!bet.isEmpty() && bet.top() != '(' && comp(in[i]) <= comp(bet.top())){
49+
char z = bet.topAndPop();
50+
result += z;
51+
}
52+
bet.push(x);
53+
}
54+
}
55+
while(!bet.isEmpty()){
56+
result += bet.top();
57+
bet.pop();
58+
}
59+
return result;
60+
}
61+
62+
// parameter: postfix string after conversion
63+
// output: returns string in prefix form
64+
string prefix(string pf){
65+
stack<string> temp;
66+
string pref;
67+
for(int i=0; i<pf.length(); i++){
68+
if(isFunct(pf[i])){
69+
string a = temp.topAndPop();
70+
string b = temp.topAndPop();
71+
string c = pf[i]+ b+ a;
72+
temp.push(c);
73+
}
74+
else{
75+
temp.push(string(1, pf[i]));
76+
}
77+
}
78+
while(!temp.isEmpty()){
79+
pref += temp.topAndPop(); // build string to return
80+
}
81+
return pref;
82+
}
83+
84+
bool isFunct(char &curr){
85+
if(curr == '+'|| curr == '-' || curr == '*' || curr == '/') // from postfix.cpp
86+
return true;
87+
else
88+
return false;
89+
}
90+
int comp(char x){ // order of operation precedence s.t. comparable
91+
if(x == '*' || x == '/')
92+
return 2;
93+
else
94+
return 1;
95+
}
96+

stack.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
/**
3+
* Construct the stack.
4+
*/
5+
template <class Object>
6+
stack<Object>::stack( int capacity ) : theArray( capacity )
7+
{
8+
topOfstack = -1;
9+
}
10+
11+
/**
12+
* Test if the stack is logically empty.
13+
* Return true if empty, false otherwise.
14+
*/
15+
template <class Object>
16+
bool stack<Object>::isEmpty( ) const
17+
{
18+
return topOfstack == -1;
19+
}
20+
21+
/**
22+
* Test if the stack is logically full.
23+
* Return true if full, false otherwise.
24+
*/
25+
template <class Object>
26+
bool stack<Object>::isFull( ) const
27+
{
28+
return topOfstack == theArray.size( ) - 1;
29+
}
30+
31+
/**
32+
* Make the stack logically empty.
33+
*/
34+
template <class Object>
35+
void stack<Object>::makeEmpty( )
36+
{
37+
topOfstack = -1;
38+
}
39+
40+
/**
41+
* Get the most recently inserted item in the stack.
42+
* Does not alter the stack.
43+
* Return the most recently inserted item in the stack.
44+
* Exception Underflow if stack is already empty.
45+
*/
46+
template <class Object>
47+
const Object & stack<Object>::top( ) const
48+
{
49+
if( isEmpty( ) )
50+
throw Underflow( );
51+
return theArray[ topOfstack ];
52+
}
53+
54+
/**
55+
* Remove the most recently inserted item from the stack.
56+
* Exception Underflow if stack is already empty.
57+
*/
58+
template <class Object>
59+
void stack<Object>::pop( )
60+
{
61+
if( isEmpty( ) )
62+
throw Underflow( );
63+
topOfstack--;
64+
}
65+
66+
/**
67+
* Insert x into the stack, if not already full.
68+
* Exception Overflow if stack is already full.
69+
*/
70+
template <class Object>
71+
void stack<Object>::push( const Object & x )
72+
{
73+
if( isFull( ) )
74+
throw Overflow( );
75+
theArray[ ++topOfstack ] = x;
76+
}
77+
78+
/**
79+
* Return and remove most recently inserted item from the stack.
80+
* Return most recently inserted item.
81+
* Exception Underflow if stack is already empty.
82+
*/
83+
template <class Object>
84+
Object stack<Object>::topAndPop( )
85+
{
86+
if( isEmpty( ) )
87+
throw Underflow( );
88+
return theArray[ topOfstack-- ];
89+
}

stack.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using namespace std;
2+
#include "sexceptions.h"
3+
#include <vector>
4+
5+
// stack class -- array implementation
6+
//
7+
// CONSTRUCTION: with or without a capacity; default is 10
8+
//
9+
// ******************PUBLIC OPERATIONS*********************
10+
// void push( x ) --> Insert x
11+
// void pop( ) --> Remove most recently inserted item
12+
// Object top( ) --> Return most recently inserted item
13+
// Object topAndPop( ) --> Return and remove most recently inserted item
14+
// bool isEmpty( ) --> Return true if empty; else false
15+
// bool isFull( ) --> Return true if full; else false
16+
// void makeEmpty( ) --> Remove all items
17+
// ******************ERRORS********************************
18+
// Overflow and Underflow thrown as needed
19+
20+
template <class Object>
21+
class stack
22+
{
23+
public:
24+
explicit stack( int capacity = 10 );
25+
26+
bool isEmpty( ) const;
27+
bool isFull( ) const;
28+
const Object & top( ) const;
29+
30+
void makeEmpty( );
31+
void pop( );
32+
void push( const Object & x );
33+
Object topAndPop( );
34+
35+
private:
36+
vector<Object> theArray;
37+
int topOfstack;
38+
};
39+
40+
#include "stack.cpp"

0 commit comments

Comments
 (0)