Ïðîãðàììà, ïå÷àòàþùàÿ ñâîé êîä

Êëàññè÷åñêîå óïðàæíåíèå ïî ïðîãðàììèðîâàíèþ.

Ïðåæäå ÷åì ñìîòðåòü ðåøåíèå, ïîïðîáóéòå íàïèñàòü ñàìè, ïðîâåðüòå ñâîé óðîâåíü.

Ìíå ïîíàäîáèëîñü 20 ìèíóò, ÷òîáû íàáðîñàòü êîñòÿê è åùå 10 ìèíóò, ÷òîáû äîáèòüñÿ òî÷íîãî ñîîòâåòñòâèÿ.

Ðåçóëüòàò ñ èñõîäíûì êîäîì ñðàâíèâàë ñ ïîìîùüþ ñðàâíåíèÿ ôàéëîâ. Òåêñòû äåéñòâèòåëüíî èäåíòè÷íû.

Ìîæíî, êîíå÷íî, ïîïðîáîâàòü âàðèàíò ñ îïåðàòîðîì «Âûïîëíèòü», íî è òàê íåïëîõî.

Ñóòü ðåøåíèÿ ïîíÿòíà.

Êîä = "

|Äëÿ Èíä = 1 Ïî 2 Öèêë

|             Åñëè Èíä = 2 Òîãäà

|                             Ñîîáùèòü(Êîä);

|             Èíà÷å

|                             Ð = ""Êîä = """""";

|                             Äëÿ ÈíäÑ = 1 Ïî ÑòðÄëèíà(Êîä) Öèêë

|                                             Ñèìâîë = Ñðåä(Êîä, ÈíäÑ, 1);

|                                             Åñëè Ñèìâîë = Ñèìâîëû.ÏÑ Òîãäà

|                                                            Ð = Ð + Ñèìâîëû.ÏÑ + ""|"";

|                                             Èíà÷åÅñëè Ñèìâîë = """""""" Òîãäà

|                                                            Ð = Ð + """""""""""";

|                                             Èíà÷å

|                                                            Ð = Ð + Ñèìâîë;

|                                             ÊîíåöÅñëè;

|                                            

|                             ÊîíåöÖèêëà;

|                             Ð = Ð + """""";"";

|                             Ñîîáùèòü(Ð);

|             ÊîíåöÅñëè;

|ÊîíåöÖèêëà;";

 

Äëÿ Èíä = 1 Ïî 2 Öèêë

                Åñëè Èíä = 2 Òîãäà

                               Ñîîáùèòü(Êîä);

                Èíà÷å

                               Ð = "Êîä = """;

                               Äëÿ ÈíäÑ = 1 Ïî ÑòðÄëèíà(Êîä) Öèêë

                                               Ñèìâîë = Ñðåä(Êîä, ÈíäÑ, 1);

                                               Åñëè Ñèìâîë = Ñèìâîëû.ÏÑ Òîãäà

                                                               Ð = Ð + Ñèìâîëû.ÏÑ + "|";

                                               Èíà÷åÅñëè Ñèìâîë = """" Òîãäà

                                                               Ð = Ð + """""";

                                               Èíà÷å

                                                               Ð = Ð + Ñèìâîë;

                                               ÊîíåöÅñëè;

                                                

                               ÊîíåöÖèêëà;

                               Ð = Ð + """;";

                               Ñîîáùèòü(Ð);

                ÊîíåöÅñëè;

ÊîíåöÖèêëà;

Èäåÿ íàïèñàòü òàêóþ ïðîãðàììó âîçíèêëà ó ìåíÿ, êîãäà ÿ ïðî÷èòàë ïðî ñáîðíèê ïðîãðàìì "Hello World" íà ðàçíûõ ÿçûêàõ ïðîãðàììèðîâàíèÿ, â òîì ÷èñëå è íà 1ñ.

À âû çà ñêîëüêî íàïèñàëè òàêóþ ïðîãðàììó?

Ìîæåò åñòü âàðèàíòû ïîêîðî÷å (êðîìå Âûïîëíèòü - íåñïîðòèâíî), õîòÿ äëÿ êîëëåêöèè ìîæåòå íàïèñàòü è ñ Âûïîëíèòü - âêëþ÷ó â ñòàòüþ.

Ïî çäðàâîìó ðàçìûøëåíèþ ÿ ïîäóìàë, ÷òî ïåðâûé öèêë â ìîåì êîäå ëèøíèé. À âòîðîé öèêë ìîæíî çàìåíèòü íà ôóíêöèþ "ÑòðÇàìåíèòü".

Ïóòåì îïòèìèçàöèè ó ìåíÿ ïîëó÷èëàñü âîò òàêàÿ ïðîãðàììêà â äâå ñòðî÷êè:

Êîä = "Ñîîáùèòü(""Êîä = """""" + ÑòðÇàìåíèòü(Êîä, """""""", """""""""""") + """""";""); Ñîîáùèòü(Êîä);";

Ñîîáùèòü("Êîä = """ + ÑòðÇàìåíèòü(Êîä, """", """""") + """;"); Ñîîáùèòü(Êîä);