The Shortest.. Solved Again [Problem of the Week]

it’s time to show you the solutions submitted to this problem..

I have heard about this problem once from Abdalla Gamal, The acm Guru that taught us every thing we know about acming.. then i found it later mentioned in a book

i recommend that you copy all the code in a cpp, comment it, then un-comment and test each one alone.

so .. first there were solutions by Mohamed Samy, Mohamed Adbelghani, Hatem Adbelghani and Tasniem they used Printf

//m samy
#include <stdio.h>
int main(){char q='"',nl='\n',bs='\\',p='%';char *s="#include<stdio.h>%cint main(){char q='%c',nl='%cn',bs='%c%c',p='%c';char *s=%c%s%c;printf(s,nl,q,bs,bs,bs,p,q,s,q);}";printf(s,nl,q,bs,bs,bs,p,q,s,q);}

//m adbelghani
#include<stdio.h>
char* printedCode="#include<stdio.h>%cchar* printedCode= %c%s%c;%cvoid main()%c{%cprintf(printedCode,10,34,printedCode,34,10,10,10,10);%c}";
void main()
{
    printf(printedCode,10,34,printedCode,34,10,10,10,10);
}

//hatem
#include<stdio.h>
char prog[]="void main(){printf(_char prog[]=%c%s%c;_,34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf(_%s_,prog);}";void main(){printf("char prog[]=%c%s%c;",34,prog,34);for(char*s=prog;*s;s++)*s=(*s==95?34:*s);printf("%s",prog);}

//tasniem
#include <stdio.h>
char *myProg = "#include<stdio.h>%cchar *myProg=%c%s%c;%cvoid main(){printf(myProg,10,34,myProg,34,10,10);}%c";
void main(){printf(myProg,10,34,myProg,34,10,10);}

Then there was Jaqoup and Tecno who both used Macros

//jaqoup
#include <iostream>
using namespace std;
#define ENIUQ(TEMPLATE) cout << TEMPLATE << "(" << #TEMPLATE << ");\n}";

void main()
{
    ENIUQ("#include <iostream>\nusing namespace std; \n#define ENIUQ(TEMPLATE) cout << TEMPLATE <<  \"(\"  << #TEMPLATE << \");\\n}; \n \nvoid main() \n{\n   ENIUQ");
}

//tecno
#include <iostream>
using namespace std;

#define STR(str)\
    cout <<"int main(){\nSTR(" << #str<<")\n}";\
    str
int main()
{
    STR(
        cout << "this is test program";
    return 0; )
}

Tecno’s Solution although not 100% accurate.. is very elegant.

Then a solution by Mohamed Abd El-Mon’em (Harry Potter), he used cout with no Macros which is why i like his solution alot.

//harry potter
char *str="#include <iostream> using namespace std; int main(){cout<<char(99)<<char(104)<<char(97)<<char(114)<<char(32)<<char(42)<<char(115)<<char(116)<<char(114)<<char(61)<<char(34)<<str<<char(34)<<char(59)<<str;}";
#include <iostream>
using namespace std;

int main()
{
    cout<<char(99)<<char(104)<<char(97)<<char(114)<<char(32)<<char(42)<<char(115)<<char(116)<<char(114)<<char(61)<<char(34)<<str<<char(34)<<char(59)<<str;
}

Then i want you to take a look at another two solutions by Alaa Shaker and Tasniem..

//AlaaShaker
#include <iostream>
using namespace std;

int main()
{
    // Write ANYTHING .. ANYWHERE!
    FILE *file = fopen(__FILE__, "r");

    if (!file) return 1;

    for(int ch = fgetc(file); ch != EOF; ch = fgetc(file))
        printf ("%c", ch);
    printf("\n");

    fclose(file);

    // Thanks to Abdallah Gamal .. LOL
    return 0;
}


//Tasniem
#include<iostream>
using namespace std;
#include<fstream>

void main ()
{
    ifstream fin(__FILE__);
    char a;
    while( !fin.eof())
    {
        fin.get( a );
        cout<<a;
    }
}

.. shocking .. hah ?! 😀

.. both though totally outside the box.. simple, straight forward .. and even prints comments as they said 😀

Thanks to every one.. 😀 .. i hope you liked that problem

Advertisements

2 thoughts on “The Shortest.. Solved Again [Problem of the Week]

  1. Hmm .. but solving the problem using __FILE__ is valid under the assumption that the program will run on the same machine on which it was compiled and that the code file will remain in the same location forever.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s