Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 22

Thread: C++ Help Part Two

  1. #1
    Posts, posts EVERYWHERE! Warsaw's Avatar
    Join Date
    May 2007
    Location
    State of Pandemonium
    Posts
    8,647

    Exclamation C++ Help Part Two

    Ok, so I've been taking Object Oriented Programming in C++ at the local community university and they haven't really done a good job teaching said course.

    So, I have to write a program that takes an HTML file, parses it into a text only document, and then lists the key words in order of frequency. I can do the parsing just fine, it's the listing key words in order of frequency that gets me. If anybody can lend me a hand with a piece of sample coding showing me how to sort words in order of frequency then output them to another file, there is +rep and some virtual cookies in it for you.
    Reply With Quote

  2. #2
    Senior Member hry's Avatar
    Join Date
    May 2009
    Posts
    207

    Re: C++ Help Part Two

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    printf("I don't know dude");
    getch;
    }
    Last edited by hry; March 13th, 2010 at 10:09 PM.
    Reply With Quote

  3. #3
    Posts, posts EVERYWHERE! Warsaw's Avatar
    Join Date
    May 2007
    Location
    State of Pandemonium
    Posts
    8,647

    Re: C++ Help Part Two

    What I have so far:

    Code:
    #include<iostream>  // Required for cin, cout, and cerr.
    #include<fstream>   // Required for ifstream and ofstream.
    #include<string>      // Required for string.
    using namespace std;
    
    int main()
    {
        // Declare Objects.
        char character;
        bool text_state(true);
        string infile;
        string outfile;
        ifstream html;
        ofstream htmltext;
        
        // Prompt user for name of input file.
        cout << "Enter the name of the input file:";
        cin >> infile;
        
        // Prompt user for name of output file.
        cout << "Enter name of the output file:";
        cin >> outfile;
        
        // Open files.
        html.open(infile.c_str());
        if(html.fail())
        {
            cerr << "Error opening the input file\n";
            exit(1);
        }
        htmltext.open(outfile.c_str());
        
        //Read first character from html file.
        html.get(character);
        
        while(html.eof())
        {
            //Check state
            if(text_state)
            {
                if(character =='<')                     // Beginning of a tag.
                    text_state=false;                    // Change states.
                else
                    htmltext << character;           // Still text, write to file.
            }
            
            else
            
            {
                // Command state, no output required.
                if(character =='>')                     // End of a tag.
                    text_state=true;                     // Change states.
            }
        
            // Read next character from html file.
            html.get(character);
        }
        html.close();
        htmltext.close();
        return 0;
    }
    I need the function prototypes to sort through the text that was just parsed above and list the key words in order of frequency. I can handle the exclusion of things like "to", "the", etc. I do realise that what's up there will end up being a module and not the main function, so disregard the "int main()" for now. Also, I'm using Dev-C++ because the instructor wants us to, so no Visual Studio shortcuts/vernacular please.

    E: I also know that this will end up in an array. First I need to make a list of all the key words (column a, if you will), then it needs to list how many times each of those appears (column b). I then need to take column b and apply a sorting algorithm to it to order it from most common to least common. I know the methodology. What I don't know are the commands, operators, syntax, etc. to do those steps.
    Last edited by Warsaw; March 13th, 2010 at 10:33 PM.
    Reply With Quote

  4. #4
    Kid in the Hall Kornman00's Avatar
    Join Date
    Sep 2006
    Location
    ◕‿◕, ┌( ಠ_ಠ)┘
    Posts
    3,126

    Re: C++ Help Part Two

    If this specific assignment is suppose to be for an OOP subject in your class then I want you to go into class on monday and smack your teacher

    I'm assuming you're allowed to use anything in the STL library. All you would have to do use a <string,int> dictionary (or map is it is called in stl) and then when you enter a keyword (found by the first character after a '<' then terminated either by the first white space or a matching '>') you'd either add it to the map or it would already exist so you'd just increment the "int" data of the dictionary (which represents the count of how many times you found it)

    You said in the first post that this is only for HTML keywords so it shouldn't be too difficult to modify your parsing code for those conditions
    Reply With Quote

  5. #5

    Re: C++ Help Part Two

    Your teacher is missing the OO in OOP :P

    For me: HTML Parser class with Keyword list.
    Last edited by CrAsHOvErRide; March 14th, 2010 at 10:51 AM.
    Reply With Quote

  6. #6
    Posts, posts EVERYWHERE! Warsaw's Avatar
    Join Date
    May 2007
    Location
    State of Pandemonium
    Posts
    8,647

    Re: C++ Help Part Two

    ^ That's too easy, and the teacher would probably give me a D for that. I'd do it too, but I have to use the same program with at least two different HTML files, which means either an extra long (versus an already long) keyword list. I think the point of this exercise (which is my mid-term) is to get us to use arrays and strings. The only problem is that she didn't teach us jack all about this shit.

    @Korn: Yeah, she didn't impose any limitations on how we achieve the end result, just that we get there with one program. Also, I got that down, I just don't know how to structure it. The teacher basically named out classes and things, but never showed us how to implement any of it. You could say that I'm a total nub at C++, since the most I know about structure is what a block is and where statements go. I've been reading my text book for the past 12 hours and I still can't figure out how to manage this. That link you posted was somewhat helpful though, thanks.
    Reply With Quote

  7. #7
    HA10 Limited's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    7,800

    Re: C++ Help Part Two

    Could you just not add it into a vector of chars or String and then loop back and compare them?. The map function Kornman linked to would do it well, hopefully the teacher will think "oh, they've actually done some research into it to use a pre-built function" , rather than "oh they are lazy using pre-built function"
    Reply With Quote

  8. #8

    Re: C++ Help Part Two

    Quote Originally Posted by Warsaw View Post
    ^ That's too easy, and the teacher would probably give me a D for that. I'd do it too, but I have to use the same program with at least two different HTML files, which means either an extra long (versus an already long) keyword list. I think the point of this exercise (which is my mid-term) is to get us to use arrays and strings. The only problem is that she didn't teach us jack all about this shit.
    Huh? Why would that be any easier? Your whole code and more would just be transferred into an own class with proper OOP. You would still have to write everything on your own.
    Reply With Quote

  9. #9
    Posts, posts EVERYWHERE! Warsaw's Avatar
    Join Date
    May 2007
    Location
    State of Pandemonium
    Posts
    8,647

    Re: C++ Help Part Two

    Maybe I'm misunderstanding what you mean (very likely), but I took that to mean that I manually copy down all the words once into an array then have the program go back and increment the count of each word in that array as they appear in the file, then list them in descending count order.

    My problem is that I haven't been taught how to code. I'm trying to learn on the fly without actually know how to do much to begin with. I know conceptually what I'm supposed to be doing, but I have no idea how to implement those concepts in code. Yeah, teacher did miss the OO part of OOP. She also managed to miss the P part of OOP.

    tl;dr kids: don't go to community college hoping to learn anything.
    Reply With Quote

  10. #10
    HA10 Limited's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    7,800

    Re: C++ Help Part Two

    You should use a vector, its a like a dynamic array and is really useful as a container for information.

    You'll need to include the vector class though.
    Code:
    #include <vector>
    
    // Defining the vector
    std:: vector <String> strContainer;
    
    To loop through contents 
    String tmpStr;
    for (int i = 0; i < strContainer.size(); i++)
    {
        tmpStr = strContainer.at(i)
    }
    
    //To add words to the vector
    strContainer.push_back(tmpStr);
    To remove values from the container you can use the pop_back which removes last added item first.

    http://www.cplusplus.com/reference/stl/vector/
    Reply With Quote

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •