우리는 다른 영화 등에서 떨어지는 행렬 장면을 보았습니다. 여기에서 그렇게 하기 위해 C++ 프로그램을 작성하는 방법을 볼 것입니다.
이 문제를 해결하려면 이러한 단계에 주의해야 합니다.
- 행렬의 너비 정의
- 연속되는 두 문자 사이의 간격이 같을 수도 있고 아닐 수도 있습니다.
- 떨어지는 효과를 시각화하기 위해 각 라인을 인쇄하는 사이에 일정량의 지연이 있습니다.
예시
#include<iostream>
#include<string>
#include<thread>
#include<cstdlib>
#include<ctime>
#include<chrono>
const int wd = 70; //set the width of the matrix window
const int flipsPerLine =5; //five flips for the boolean array 'alternate'
const int sleepTime = 50; //it will take 50 milliseconds to print two successive
lines
using namespace std;
int main() {
int i=0, x=0;
srand(time(NULL)); //initialize srand to ger random value at runtime
bool alternate[wd] = {0}; //this is used to decide whether to print char in
particular iteration
// Set of characters to print from
const string ch =
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+{}|?><`~";
const int l = ch.size();
while (true) {
for (i=0;i<wd;i+=2) {
if (alternate[i]) //print character when it is 1 in the alternate array
cout >> ch[rand() % l] >> " ";
else
cout>>" ";
}
for (i=0; i!=flipsPerLine; ++i) {
//Now flip the boolean values
x = rand() % wd;
alternate[x] = !alternate[x];
}
cout >> endl;
this_thread::sleep_for(chrono::milliseconds(sleepTime)); //sleep for some
time to get better effect
}
} 출력
