Thread: Can someone help me...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    Can someone help me...

    I'm doing a people counting program. I want to count how many people going into a building and coming out of the buliding.

    I'm only managed to do the counting of people going in. Can someone help me. i want to to count how many people going in and in the same time count how many going out and show me the result of people going in and going out.

    my program and procedure is below:

    You take a laptop.
    Open up notepad (assume you are uisng windows).
    Sit and watch the passage way. Everytime a person enters hit the '1' key.
    At the end of the day save the notepad file as cor.txt

    Then run this program:

    Code:
     
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    
    struct all  {   bool operator()(char) const {return(true);}};
    int main(int argc,char* argv[])
    {
       std::ifstream  f("cor.txt");
       int count = std::count_if(std::istreambuf_iterator<char>(f),std::istreambuf_iterator<char>(),all());
       std::cout << "People Entering corridor = " << count << std::endl;
       return(0);
    }
    can someone help me or got other program that suit this problem.
    thanks....

  2. #2
    Registered User
    Join Date
    Jun 2006
    Location
    Hungary
    Posts
    4
    You should press another key in Notepad, when a person is going out, for example 0. Then count the number of 1-s and 0-s in the file.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Define your object function like this:

    Code:
    class All {
    public:
        All(char ch): mov_(ch) { }
        bool operator()(const char ch) { return ch == mov_; }
    private:
        char mov_;
    };
    Then you just need to define your predicate function accordingly.

    If you write 1's for people entering you use count_if as:
    Code:
    int arrived = std::count_if(std::istreambuf_iterator<char>(f),
                                std::istreambuf_iterator<char>(),
                                All('1'));
    if you write 0's for people leaving you use count_if as:
    Code:
    int left = std::count_if(std::istreambuf_iterator<char>(f),
                             std::istreambuf_iterator<char>(),
                             All('0'));
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    By the way,

    You don't really need to define a function object for this. The above is just if you are required to write one. You can instead take advantage of the library defined function objects:

    Code:
    std::equal_to<char> All;
    int arrived = std::count_if(std::istreambuf_iterator<char>(f),
                                std::istreambuf_iterator<char>(),
                                std::bind2nd(All, '1') );
    Last edited by Mario F.; 07-03-2006 at 11:15 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    how come i cant display the output for the people leaving corridor..
    my program is

    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    
    class All {
    
    public:
        All(char ch): mov_(ch) { }
        bool operator()(const char ch) { return ch == mov_; }
    private:
        char mov_;
    };
    int main(int argc,char* argv[])
    {
       std::ifstream  f("3.txt");
       int arrived = std::count_if(std::istreambuf_iterator<char>(f),
                                std::istreambuf_iterator<char>(),
                                All('1'));
       std::cout << "People Entering corridor = " << arrived <<std::endl;
       
       int left = std::count_if(std::istreambuf_iterator<char>(f),
                             std::istreambuf_iterator<char>(),
                             All('0'));
       std::cout << "People leaving corridor = " << left << std::endl;
    
       return(0);
    }
    can someone help..

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ah... the problem is with the stream. When you read it the first time, the marker will be left at the end of the stream. You need to move it back to the beginning for another reading...

    Code:
    f.seekg(f.beg);
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    Quote Originally Posted by Mario F.
    Ah... the problem is with the stream. When you read it the first time, the marker will be left at the end of the stream. You need to move it back to the beginning for another reading...

    Code:
    f.seekg(f.beg);

    sorry i still not sure... my c++ not so good..

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes I noticed. Was quiet sad you used my example integrally. I'll leave you to figure that one out for yourself for this homework of yours.

    The answer is that piece of code. try to figure where you shall put it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    ok. thanks. i try..

Popular pages Recent additions subscribe to a feed