Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using polymorphism.

Status
Not open for further replies.

johndw

Technical User
Nov 15, 2001
8
0
0
US
I want to read a file (ie: C++ source code file or other) and count the characters that are used in pairs including ( ), [ ] and { }. The program must read all the lines of the file and count all occurrences of these characters. As its output, the program should summarize these counts and it
should provide suitable error messages in the event that the right and left members of the pairs are not balanced.
(Maybe even get the file specification for the target file from the command line.)I'm most interested in the number of lines in the file, the number of left and right parentheses, brackets, and curly braces. Error messages should be printed in the event of a mismatch.
I have had some success with this using other methods, but not writting the classes and using polymorphism. Can anyone help ?
 
I would look into designing a Finite State Machine. This sounds like the perfect use for one. Sorry for the short answer I will find some links and post em but I dont have the time right now.

Matt
 
What ever is a Finite State machine ?
I was invisioning a base class with a virtual function and a couple of derived classes, with main() opening up the file and stepping though it a line at a time. Each time the character read matches a specified type, the counter from that class would increment. Does that sound like the right direction ?
 
I thought if I posted this mess, someone might be able to help me put this together. It's new to me, so I'm looking for an assist. It will step through a file now and display the contents of the buffer. Reworking it into a polymorphic app shouldn't be so tough. Can anyone help ?
#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

class base {
private:
int left, right; //to be inherited by derived class(es)
public:
virtual void Count() {cout << &quot;Called base class.&quot;;}
};
//--------------------------------------------------
class derived : public base
{ // need functions to count characters.


};
//------------------------------
void check_characters(char *);

//-------------------------
int main () {

char buffer[256];
base* bPtr = new derived(); // I think this is right ???

ifstream examplefile (&quot;Text1.cpp&quot;); //eventually to get file from command line.

int number; //store the number

while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
ifstream temp (buffer);

temp >> number;
cout << endl;
cout << &quot;Contents of buffer: &quot; << buffer << endl;
getch();
check_characters(buffer);

}
return 0;
}

/*FYI
lp = left par
rp = right par
lb = left braket
rb = right braket
lc = left curly
rc = right curly
*/
void check_characters(char *line) // this works now.
{
int len = 0,no = 0,nolp = 0,norp = 0,nolb = 0,norb = 0,nolc = 0, norc = 0;
len = strlen(line);

for(int i=0; i < len; i++){
if(line == '(' || line == ')' || line == '[' || line == ']' || line == '{' || line == '}')
no++;
}
printf(&quot;\nNo of characters is %d\n&quot;,no);
for(i=0;i < len;i++){
if(line == '(')
nolp++;
if(line == ')')
norp++;
if(line == '[')
nolb++;
if(line == ']')
norb++;
if(line == '{')
nolc++;
if(line == '}')
norc++;
}

printf(&quot;No of '(' is %d\n&quot;,nolp);
printf(&quot;No of ')' is %d\n&quot;,norp);
printf(&quot;No of '[' is %d\n&quot;,nolb);
printf(&quot;No of ']' is %d\n&quot;,norb);
printf(&quot;No of '{' is %d\n&quot;,nolc);
printf(&quot;No of '}' is %d\n&quot;,norc);
}/*end of check_characters function*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top