You are here:

C++/problem in seperating files

Advertisement


Question
i have done this code and i dint know how to separate this code into two file infix.h and infix.cpp.  plz help me sir

#include <iostream>
#include <string>
#include <stack>
using namespace std;

void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);

int main(void)
{
char Reply;
do
{
string Infix, Postfix; // local to this loop
cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"<< endl;
cin >> Infix;
Convert(Infix, Postfix);
cout << "The equivalent postfix expression is:" << endl<< Postfix << endl;
cout << endl << "Do another (y/n)? ";
cin >> Reply;
}
while (tolower(Reply) == 'y');
return 0;
}

/*
Given: ch A character.
Task: To determine whether ch represents an operand (here
understood to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false
otherwise.

*/

bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}

/* Given:
OperatorA A character representing an operator or
parenthesis.
OperatorB A character representing an operator or
parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.

Return: In the function name: true, if OperatorA takes precedence
over OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;

}

/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/

void Convert(const string & Infix, string & Postfix)
{
stack<char> OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}

thank you in advance

Answer
Hello Vaanie

The general idea is to place all the function declarations into a header file.

Create an infix.h like this:
---------------------------------------------------------------------
#ifndef _INFIX_H
#define _INFIX_H

#include <string>

/*
Given: ch A character.
Task: To determine whether ch represents an operand (here
understood to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false
otherwise.
*/
bool IsOperand(char ch);


/* Given:
OperatorA A character representing an operator or
parenthesis.
OperatorB A character representing an operator or
parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.
Return: In the function name: true, if OperatorA takes precedence
over OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB);


/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/
void Convert(const std::string & Infix, std::string & Postfix);

#endif

---------------------------------------------------------------------

Notice that the entire contents of the header file are surrounded by ifndef/endif preprocessor commands. These are called include guards and they prevent the contents of the file from being included more than once. That prevents errors caused when the compiler sees structure definitions more than once. Using include guards is a standard practice. It is probably more important to have comments in the header file than in the index.cpp file because it is the header file that a user would look at to understand how to use the functions.

I included string into infix.h so that the header file can stand on its own and not depend on string being included before infix.h

Next, put your utility functions into an infix.cpp file like this:

---------------------------------------------------------------------
#include <string>
#include <stack>

#include "infix.h"

using namespace std;

/*
Given: ch A character.
Task: To determine whether ch represents an operand (here
understood to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false
otherwise.

*/

bool IsOperand(char ch)
{
   if (((ch >= 'a') && (ch <= 'z')) ||
       ((ch >= 'A') && (ch <= 'Z')) ||
       ((ch >= '0') && (ch <= '9')))
       return true;
   else
       return false;
}

/* Given:
OperatorA A character representing an operator or
parenthesis.
OperatorB A character representing an operator or
parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.

Return: In the function name: true, if OperatorA takes precedence
over OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
   if (OperatorA == '(')
       return false;
   else if (OperatorB == '(')
       return false;
   else if (OperatorB == ')')
       return true;
   else if ((OperatorA == '^') && (OperatorB == '^'))
       return false;
   else if (OperatorA == '^')
       return true;
   else if (OperatorB == '^')
       return false;
   else if ((OperatorA == '*') || (OperatorA == '/'))
       return true;
   else if ((OperatorB == '*') || (OperatorB == '/'))
       return false;
   else
       return true;

}

/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/

void Convert(const string & Infix, string & Postfix)
{
   stack<char> OperatorStack;
   char TopSymbol, Symbol;
   int k;
   for (k = 0; k < Infix.size(); k++)
   {
       Symbol = Infix[k];
       if (IsOperand(Symbol))
         Postfix = Postfix + Symbol;
       else
       {
         while ((! OperatorStack.empty()) &&
         (TakesPrecedence(OperatorStack.top(), Symbol)))
         {
         TopSymbol = OperatorStack.top();
         OperatorStack.pop();
         Postfix = Postfix + TopSymbol;
         }
         if ((! OperatorStack.empty()) && (Symbol == ')'))
         OperatorStack.pop(); // discard matching (
         else
         OperatorStack.push(Symbol);
       }
   }
   while (! OperatorStack.empty())
   {
       TopSymbol = OperatorStack.top();
       OperatorStack.pop();
       Postfix = Postfix + TopSymbol;
   }
}

---------------------------------------------------------------------

The infix.h is included into infix.cpp so that the compiler can check the function declarations against the function definitions.

Finally, the main.cpp contains just the main, and includes infix.h. Notice that in main.cpp, I have removed the function declarations because they are now in infix.h

---------------------------------------------------------------------

#include <iostream>
#include "infix.h"

using namespace std;

int main(void)
{
   char Reply;
   do
   {
       string Infix, Postfix; // local to this loop
       cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"<< endl;
       cin >> Infix;
       Convert(Infix, Postfix);
       cout << "The equivalent postfix expression is:" << endl<< Postfix << endl;
       cout << endl << "Do another (y/n)? ";
       cin >> Reply;
   }
   while (tolower(Reply) == 'y');
   return 0;
}

---------------------------------------------------------------------

I hope that helps you out. I did not test out your code. If you found this problem interesting, you might also be interested in the shunting algorithm. See http://en.wikipedia.org/wiki/Shunting-yard_algorithm

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.