Fun C++ Problem - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 5 FirstFirst 12345 LastLast
Results 16 to 30 of 69

Thread: Fun C++ Problem

  1. #16
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Fun C++ Problem

    I have 3 solutions. Took me a few minutes. Didn't actually time it.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Fun C++ Problem

    I have all three.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Fun C++ Problem

    So I ask dcjr84, do we bring the solutions in open or do you want some more time?

    I can't hold any longer... I will put them all in my next post here ...

  4. #19
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Fun C++ Problem

    Still can't get it

    Is replacing the same character twice valid?
    If there is no love sun won't shine

  5. #20
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Fun C++ Problem

    me neither, I only get 2!

  6. #21
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Fun C++ Problem

    Quote Originally Posted by laitinen
    me neither, I only get 2!
    Try to think outside the box.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #22
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Fun C++ Problem

    A hint to the third solution, if it's the one I think some here are having a problem with, is "do the math".

    Regards,

    Paul McKenzie

  8. #23
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: Fun C++ Problem

    Now, I think I have three. Not sure unless someone posts the solutions hehe.....
    If there is no love sun won't shine

  9. #24
    Join Date
    Oct 2003
    Location
    Merate-North Italy
    Posts
    230

    Re: Fun C++ Problem

    got 2.......
    ++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
    God does not play dice with the universe.(A.Einstein)

  10. #25
    Join Date
    Oct 2003
    Location
    Merate-North Italy
    Posts
    230

    Re: Fun C++ Problem

    got 3 ! !
    ++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
    God does not play dice with the universe.(A.Einstein)

  11. #26
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    347

    Re: Fun C++ Problem

    Got all three!!! Took me a few seconds to get the first one. Then, my mind went completely blank (started thinking too hard for some difficult trick) and it took me almost 20 minutes to get the second. But then, following that, I almost immediately got the third.

  12. #27
    Join Date
    Oct 2003
    Location
    Merate-North Italy
    Posts
    230

    Re: Fun C++ Problem

    Got a 4th solution......
    but it's a cheat based on solution 1.........
    and works wrong too
    ++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
    God does not play dice with the universe.(A.Einstein)

  13. #28
    Join Date
    Oct 2006
    Posts
    1

    Angry Re: Fun C++ Problem

    Got two in a few minutes. But the third one took me 30 minutes!!!

  14. #29
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Fun C++ Problem

    Code:
    #include <windows.h>
    #include <limits>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    bool exists(const char* fname)
    {
        ifstream fin(fname);
        return fin.is_open();
    }
    
    bool check_out(const char* fname)
    {
        ifstream out(fname);
        for(int i=0;i<20;i++)
        {
            int c = out.get();
            if(c!='x')
                return false;
            c = out.get();
            if(c!='\n')
                return false;
        }
        int end = out.get();
        if(!out.eof()) return false;
        return true;
    }
    
    void run_with_timeout(char* cmdline)
    {
        PROCESS_INFORMATION info;
        ZeroMemory(&info,sizeof(info));
        STARTUPINFO si;
        ZeroMemory(&si,sizeof(si));
        if(!CreateProcess(0,cmdline,0,0,0,DETACHED_PROCESS,0,0,&si,&info))
            return;
        WaitForSingleObject(info.hProcess,1000);
        TerminateProcess(info.hProcess, 1);
    }
    
    bool test_code(const char* code)
    {
        const char* src_name = "test_code.cpp";
        ofstream src(src_name);
        src<<"#include <fstream>\n";
        src<<"#include <iostream>\n";
        src<<"using std::endl;\n";
        src<<"int main() { \n";
        src<<"std::ofstream cout(\"out.txt\");\n";
        src<<code;
        src<<"}\n";
        src.close();
        if(exists("a.exe"))
            system("del a.exe");
        string cmd = string() + "g++ " + src_name + " 2> log.txt";
        system(cmd.c_str());
        if(!exists("a.exe"))
            return false;
        
        run_with_timeout("a.exe");
        bool res = check_out("out.txt");
        if(exists("out.txt"))
            system("del out.txt");
        return res;
    }
    
    void log_success(ostream& str, const char* code, int i, char c)
    {
        str<<endl;
        str<<c<<" at "<<i<<":"<<endl;
        str<<code;
        str<<endl;
    }
    
    int main()
    {
        char code[] = 
            "int i, n = 20;\n"
            "for (i=0; i<n; i--)\n"
            "{\n"
            "cout << \"x\" << endl;\n"
            "}\n";
        ofstream success_log("success_log.txt");
        for(int i=0, n=strlen(code);i<n;i++)
        {
            cout<<i<<" of "<<n<<"..."<<endl;
            for(char c = 32;c<127;c++) 
            {
                cout<<c;
                char old = code[i];
                code[i] = c;
                if(test_code(code)) 
                {
                    log_success(success_log, code, i, c);
                    log_success(cout, code, i, c);
                }
                code[i] = old;
            }
            cout<<endl;
        }
        return 0;
    }


    Edit: v0.02 <- bugfix
    Last edited by RoboTact; October 19th, 2006 at 11:45 AM. Reason: bugfix
    "Programs must be written for people to read, and only incidentally for machines to execute."

  15. #30
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Page 2 of 5 FirstFirst 12345 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center