A C++ program that prints itself

#include <iostream>
using namespace std;

int main() {
  string s = "  cout << \"#include <iostream>\\nusing namespace std;\\n\\nint main() {\\n  string s = \\\"\";\n  for (char i : s)\n    if (i == '\\n')\n      cout << \"\\\\n\";\n    else if (i == '\"')\n      cout << \"\\\\\\\"\";\n    else if (i == '\\\\')\n      cout << \"\\\\\\\\\";\n    else cout << i;\n  cout << \"\\\";\\n\" << s;\n}\n";
  cout << "#include <iostream>\nusing namespace std;\n\nint main() {\n  string s = \"";
  for (char i : s)
    if (i == '\n')
      cout << "\\n";
    else if (i == '"')
      cout << "\\\"";
    else if (i == '\\')
      cout << "\\\\";
    else cout << i;
  cout << "\";\n" << s;
}
posted @ 2023-04-07 12:39  王子睿  阅读(7)  评论(0编辑  收藏  举报