how to separate this code?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
This code is used to convert from Infix to postfix expression. Ive created an empty project using visual c++ (vistual studio 2008) and I need your help to saperate the follwoing code to two files *.cpp and *.h . thanks in advance
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
#include <iostream.h><br/>
#include <stdio.h><br/>
#include <ctype.h><br/>
typedef char data;
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
template <class Item><br/>
class QUEUE<br/>
{<br/>
private:<br/>
struct node<br/>
{<br/>
Item item; node *next;<br/>
node(Item x)<br/>
{<br/>
item=x; next=0;<br/>
}<br/>
};
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
typedef node *link;<br/>
link head, tail;
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
void deletelist()<br/>
{<br/>
for(link t=head;t!=0;head=t)<br/>
{<br/>
t=head->next;<br/>
delete head;<br/>
}<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
public:<br/>
QUEUE(int)<br/>
{<br/>
head=0; <br/>
}<br/>
<br/>
QUEUE(const QUEUE& rhs)<br/>
{ head=0; *this=rhs;}<br/>
<br/>
~QUEUE()<br/>
{<br/>
deletelist();<br/>
}<br/>
QUEUE& operator=(const QUEUE& rhs)<br/>
{<br/>
if(this==&rhs)<br/>
return *this;<br/>
deletelist();<br/>
link t=rhs.head;<br/>
while(t!=0)<br/>
{<br/>
put(t->item);<br/>
t=t->next;<br/>
}<br/>
return *this;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
int empty() const<br/>
{<br/>
return head==0;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
void put(Item x)<br/>
{<br/>
link t=tail;<br/>
tail=new node(x);<br/>
if(head==0)<br/>
head=tail;<br/>
else<br/>
t->next=tail;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
Item get()<br/>
{<br/>
Item v=head->item;<br/>
link t=head->next; <br/>
delete head;<br/>
head=t;<br/>
return v;<br/>
}<br/>
<br/>
Item front()<br/>
{<br/>
return head->item;<br/>
}<br/>
};
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
template<class Item><br/>
class STACK<br/>
{<br/>
private:<br/>
struct node<br/>
{<br/>
Item item;<br/>
node *next;<br/>
node(Item x, node* t)<br/>
{ item=x; next=t; }<br/>
};<br/>
typedef node *link;<br/>
link head;
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
public:<br/>
STACK(int)<br/>
{<br/>
head=0;<br/>
}<br/>
<br/>
int empty() const<br/>
{<br/>
return head==0;<br/>
}<br/>
<br/>
void push(Item x)<br/>
{<br/>
head=new node(x,head);<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
Item pop()<br/>
{<br/>
Item v=head->item;<br/>
link t=head->next;<br/>
delete head;<br/>
head=t;<br/>
return v;<br/>
}<br/>
<br/>
Item top()<br/>
{<br/>
return head->item;<br/>
}<br/>
};
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
bool operator1(data x) <br/>
{<br/>
if ((x == =)||(x == +)||(x == -)||(x ==*)||(x == ^)||(x==()||(x==))||(x==/))<br/>
return 1;<br/>
else<br/>
return 0;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
QUEUE<data> get_Qin(void) <br/>
{<br/>
data ch;<br/>
QUEUE<data> Qin(0);<br/>
ch = getchar();<br/>
while (ch != n)<br/>
{<br/>
if (isalnum(ch)||operator1(ch)||(ch == ()||(ch ==)))<br/>
Qin.put(ch);<br/>
ch = getchar();<br/>
}<br/>
return (Qin);<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
int isp(data x) //table for comparing operators<br/>
{<br/>
if (x == =)<br/>
return 1;<br/>
else if ((x == +)||(x == -))<br/>
return 4;<br/>
else if ((x == *)||(x == /))<br/>
return 13;<br/>
else if (x == ^)<br/>
return 16;<br/>
else if (x == ()<br/>
return 2;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
int icp(data x) //second table for comparing<br/>
{<br/>
if (x == =)<br/>
return 2;<br/>
else if ((x == +)||(x == -))<br/>
return 3;<br/>
else if ((x == *)||(x == /))<br/>
return 11;<br/>
else if (x == ^)<br/>
return 19;<br/>
else if (x == ()<br/>
return 1;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
QUEUE<data> postfix(QUEUE<data> In) // infix to postfix form<br/>
<br/>
{<br/>
QUEUE<data> Qout(0);<br/>
STACK<data> stack(0);<br/>
while (!(In.empty()))<br/>
{<br/>
if (isalnum(In.front()))<br/>
Qout.put(In.get());<br/>
else if (In.front() == ()<br/>
stack.push(In.get());<br/>
else if (In.front() == ))<br/>
{<br/>
Qout.put(stack.pop());<br/>
stack.pop();<br/>
In.get();<br/>
}<br/>
else if (operator1(In.front()))<br/>
{<br/>
if (stack.empty())<br/>
stack.push(In.get());
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
else if (icp(In.front()) > isp(stack.top()))<br/>
stack.push(In.get());<br/>
else<br/>
Qout.put(stack.pop());<br/>
}<br/>
}<br/>
while (!stack.empty())<br/>
Qout.put(stack.pop());<br/>
return Qout;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
void printQ(QUEUE<data> queue) //prints the queue<br/>
{<br/>
while (!queue.empty())<br/>
cout<<queue.get();<br/>
cout<<endl;<br/>
}
<p style="font:13px/normal Arial,Helvetica,sans-serif; margin:20px 0px 0.5em; text-align:left; color:#2f4551; text-transform:none; text-indent:0px; letter-spacing:normal; word-spacing:0px; white-space:normal; orphans:2; widows:2; background-color:#e7effa
int main(int argc, char* argv[])<br/>
{<br/>
QUEUE<data> Out_queue(0);<br/>
QUEUE<data> In_queue(0);<br/>
cout<<"Type the Infix expression below"<< endl;<br/>
In_queue = get_Qin();<br/>
cout<<"The Postfix expression is "<< endl;<br/>
Out_queue = postfix(In_queue);<br/>
printQ(Out_queue);<br/>
return 0;<br/>
}

View the full article
 
Back
Top