用C++来实现,本来想了很多,后来越写越烂,而且结果总是不尽人意,干脆这样子好了:

1 int main() {
2     int judge = system("cls");
3     if(judge == 0)     cout << "Windows!" << endl;
4     else    cout << "Linux!" << endl;
5     return 0;
6 }

  只是 linux 下会多了一行东西,如果运行时在背后加个 2>> error 也是可以的,不过好像用户体验还是不好,为了去掉这个,我又想了如何囧办法:

(感觉好像有点小题大做了,不过确实实现了功能,就不管了,或许以后能用上)

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 const char *password = "123";
 5 
 6 int main(int argc, char *argv[]) {
 7 
 8     if(argc != 2 || strcmp(argv[1], password) != 0) {
 9         puts("Permission denied!");
10         return 2;
11     }
12     freopen("err", "w", stderr);
13     FILE *fp = fopen("out", "w");
14     int judge = system("cls");            // 0 为 Windows,>0 为 Linux
15     fprintf(fp, "%d\n", judge == 0 ? 0 : 1);
16     fclose(fp);
17     return 0; 
18 }

create.cpp