Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters
Register

Reply
 
LinkBack Thread Tools
Old 06.03.2002, 21:31   #1
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Downloads: 4
Uploads: 0
Reputation: 146 | 4
Post problem to solve :)

ok, по просьбам трудящихся (см. топик про виртуальное наследование) предлагаю интересную задачу. напишите программу, выводящую в стандартный output свой исходник. если знаете решение с использованием printf, то решите через cout <img src="smooch-1.gif" border="0">
Reply With Quote
Old 07.03.2002, 00:23   #2
the mochinger
 
Hans Andersen's Avatar
 
Join Date: 02 2002
Location: Paranoid Android, @10:50
Age: 38
Posts: 1,894
Downloads: 2
Uploads: 0
Reputation: 204 | 4
Post

#include <iostream.h>
#include <string.h>

const char *p = "\n\nconst char *f(const char *s)\n{\n\tstatic char buf[2000];\n\tint l = strlen(s), j = 0;\n\tfor(int i = 0; i < l; i++)\n\t{\n\t\tchar c = s[i];\n\t\tswitch (c)\n\t\t{\n\t\tcase 9:\n\t\t\tbuf[j++] = 92;\n\t\t\tbuf[j] = 't';\n\t\t\tbreak;\n\t\tcase 10:\n\t\t\tbuf[j++] = 92;\n\t\t\tbuf[j] = 'n';\n\t\t\tbreak;\n\t\tcase 34:\n\t\t\tbuf[j++] = 92;\n\t\t\tbuf[j] = '\"';\n\t\t\tbreak;\n\t\tcase 92:\n\t\t\tbuf[j++] = 92;\n\t\t\tbuf[j] = 92;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbuf[j] = c;\n\t\t}\n\t\tj++;\n\t}\n\treturn buf;\n}\n\nint main()\n{\n\tcout << \"#include <iostream.h>\\n#include <string.h>\\n\\nconst char *p = \\\"\" << f(p) << '\"' << ';' << p;\n\treturn 0;\n}";

const char *f(const char *s)
{
static char buf[2000];
int l = strlen(s), j = 0;
for(int i = 0; i < l; i++)
{
char c = s[i];
switch (c)
{
case 9:
buf[j++] = 92;
buf[j] = 't';
break;
case 10:
buf[j++] = 92;
buf[j] = 'n';
break;
case 34:
buf[j++] = 92;
buf[j] = '"';
break;
case 92:
buf[j++] = 92;
buf[j] = 92;
break;
default:
buf[j] = c;
}
j++;
}
return buf;
}

int main()
{
cout << "#include <iostream.h>\n#include <string.h>\n\nconst char *p = \"" << f(p) << '"' << ';' << p;
return 0;
}
__________________
The flower that blooms in adversity is the most rare and beautiful of all.
Reply With Quote
Old 07.03.2002, 00:30   #3
the mochinger
 
Hans Andersen's Avatar
 
Join Date: 02 2002
Location: Paranoid Android, @10:50
Age: 38
Posts: 1,894
Downloads: 2
Uploads: 0
Reputation: 204 | 4
Post

vo dela,
chto za formatirovka v UBB?
a kuda tabuljazii delis'?

vobshem run areq, inch vor tpi, ed isxodnikn el klini Arami uzazy <img src="smooch-1.gif" border="0">

ujas, kashmai <img src="smooch-1.gif" border="0">
Reply With Quote
Old 07.03.2002, 01:46   #4
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Downloads: 102
Uploads: 34
Blog Entries: 16
Reputation: 561 | 6
Post

2 Hans
pishi isxodniki v tag-e [CODE].
Reply With Quote
Old 07.03.2002, 05:10   #5
the mochinger
 
Hans Andersen's Avatar
 
Join Date: 02 2002
Location: Paranoid Android, @10:50
Age: 38
Posts: 1,894
Downloads: 2
Uploads: 0
Reputation: 204 | 4
Red face

<img src="conf.gif" border="0">
acid jan,

mi hat orinak ber eli, pls
10x
<img src="conf.gif" border="0">
Reply With Quote
Old 07.03.2002, 05:22   #6
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Downloads: 102
Uploads: 34
Blog Entries: 16
Reputation: 561 | 6
Post

<blockquote><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><hr><pre>#include "win31.h"
#include "win95.h"
#include "win98.h"
#include "workst~1.h"
#include "evenmore.h"
#include "oldstuff.h"
#include "billrulz.h"
#include "monopoly.h"
#define INSTALL = HARD
char make_prog_look_big[1600000];
void main()
{
while(!CRASHED)
{
display_copyright_message();
display_bill_rules_message();
do_nothing_loop();
if (first_time_installation)
{
make_50_megabyte_swapfile();
do_nothing_loop();
totally_screw_up_HPFS_file_system();
search_and_destroy_the_rest_of_OS/2();
make_futile_attempt_to_damage_Linux();
disable_Netscape();
disable_RealPlayer();
disable_Lotus_Products();
hang_system();
}
write_something(anything);
display_copyright_message();
do_nothing_loop();
do_some_stuff();
if (still_not_crashed)
{
display_copyright_message();
do_nothing_loop();
basically_run_windows_3.1();
do_nothing_loop();
do_nothing_loop();
}
if (detect_cache())
disable_cache();
if (fast_cpu())
{
set_wait_states(lots);
set_mouse(speed, very_slow);
set_mouse(action, jumpy);
set_mouse(reaction, sometimes); }
/* printf("Welcome to Windows 3.1"); */
/* printf("Welcome to Windows 3.11"); */
/* printf("Welcome to Windows 95"); */
/* printf("Welcome to Windows NT 3.0"); */
/* printf("Welcome to Windows 98"); */
/* printf("Welcome to Windows NT 4.0"); */
printf("Welcome to Windows 2000");
if (system_ok())
crash(to_dos_prompt);
else
system_memory = void(0); .swp",O_CREATE);
while(something)
{
sleep(5);
get_user_input();
sleep(5);
act_on_user_input();
sleep(5);
}
create_general_protection_fault();
}
}</pre><hr></blockquote>
Reply With Quote
Old 07.03.2002, 15:28   #7
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Downloads: 4
Uploads: 0
Reputation: 146 | 4
Post

<blockquote><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by Hans Andersen:

const char *p = "\n\nconst char *f(const char *s)\n{\n\tstatic char buf[2000];\n\tint l = strlen(s), j = 0;\n\tfor(int i = 0; i < l; i++)\n\t{\n\t\tchar c = s[i];\n
<hr></blockquote>

а вот мое решение (я правда не загружал текст форматированием, т.к. это не принципиально. в конце концов, можно текст набрать неформатированно! ) также я опустил для краткости - сестры таланта - некоторые пункты в cout << ...)

#include <iostream>
using namespace std;

int main()
{
char q = '"';
char* a = "'";

char *s[] = {"char q = ",
"; char* a = ",
"; char *s[] = {",
",",
"};",
"cout << s[0] << ... << s[5];"};

cout << s[0] << a << q << a // char q='"'
<< s[1] << q << a << q // ; char* a="'"

<< s[2] << // ; char *s[] = {
q << s[0] << q << s[3] << // "char q = ",
q << s[1] << q << s[3] << // "; char a = ",
q << s[2] << q << s[3] << // "; char *s[] = {",
q << s[3] << q << s[3] << // ",",
q << s[4] << q << s[3] << // "};",
q << s[5] << q << s[4] << // "cout << ..."};
s[5]; // cout << ...;

return 0;
}


а с printf:

#include <stdio.h>

void main()
{
char q = '"', *a = "'";
char* s = "char q = %s%c%s, *a = %c%s%c; char* s = %c%s%c; printf(s, a, q, a, q, a, q, q, s, q);";
printf(s, a, q, a, q, a, q, q, s, q);
}
Reply With Quote
Old 07.03.2002, 15:45   #8
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Downloads: 4
Uploads: 0
Reputation: 146 | 4
Post

кстати, я считаю, что здесь за правильный ответ надо принять также следующий трюк:

файл 1.h:

void f() { printf("#include \"1.h\"\nint main() {f(); return 0;}"); }

файл main.c:

#include "1.h"
int main() {f(); return 0;}

ведь даже с printf и cout часть кода, отвечающая за вывод, находится вне наших файлов, и этот код мы не выводим )
Reply With Quote
Sponsored Links
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 05:42.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.