自从计算机诞生以来,好莱坞就已经在很大程度上展示了一个黑客或程序员坐在计算机上,在计算机上输入随机键,最终编译成一个下落矩阵式的模拟。在这里,我们将尝试使用C++实现控制台上类似的降矩阵仿真。
null
这里的想法是在定义的宽度上打印随机字符,其中两个连续字符可能有或可能没有随机定义的一定数量的间隙。为了产生“下降效应”,必须在连续打印行之间实施一定程度的延迟。
// C++ program for implementation of falling matrix. #include<iostream> #include<string> #include<thread> #include<cstdlib> #include<ctime> #include<chrono> // Width of the matrix line const int width = 70; // Defines the number of flips in Boolean Array 'switches' const int flipsPerLine =5; // Delay between two successive line print const int sleepTime = 100; using namespace std; int main() { int i=0, x=0; // srand initialized with time function // to get distinct rand values at runtime srand ( time (NULL)); // Used to decide whether to print // the character in that particular iteration bool switches[width] = {0}; // Set of characters to print from const string ch = "1234567890qwertyuiopasdfghjkl" "zxcvbnm,./';[]!@#$%^&*()-=_+" ; const int l = ch.size(); // Green font over black console, duh! system ( "Color 0A" ); // Indefinite Loop while ( true ) { // Loop over the width // Increment by 2 gives better effect for (i=0;i<width;i+=2) { // Print character if switches[i] is 1 // Else print a blank character if (switches[i]) cout << ch[ rand () % l] << " " ; else cout<< " " ; } // Flip the defined amount of Boolean values // after each line for (i=0; i!=flipsPerLine; ++i) { x = rand () % width; switches[x] = !switches[x]; } // New Line cout << endl; // Using sleep_for function to delay, // chrono milliseconds function to convert to milliseconds this_thread::sleep_for(chrono::milliseconds(sleepTime)); } return 0; } |
这将在控制台上打印惊人的下落矩阵模拟。
注:
- 由于系统已禁用,此程序无法使用“在IDE上运行”按钮运行。
- 如果编译此程序时出现编译器错误。在GCC上使用下面的命令编译它。
$ g++ -std=c++11 abc.cpp -o falling.o $ falling.o
本文由 拉加夫·贾乔迪亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END