#include <iostream> #include <string> #include <ctime> #include <csignal> #include "Automaton.h" #ifdef _WIN32 #include <windows.h> #else #include <unistd.h> #endif bool isRunning = true; Automaton* automaton; void wait(int ms) { #ifdef _WIN32 Sleep(ms); #else usleep(ms * 1000); #endif } void handleCtrlC(int); int main() { srand(time(NULL)); int lines = -1, columns = -1; std::cout << "Lines?" << std::endl; std::cin >> lines; std::cout << "Columns?" << std::endl; std::cin >> columns; std::cout << "Generate random patterns? [y/N]" << std::endl; std::string yn; std::cin >> yn; automaton = new Automaton(lines, columns); if (yn == "y" || yn == "Y") automaton->init(true); else automaton->init(); std::string s; std::getline(std::cin, s); signal(SIGINT, handleCtrlC);while (true) { if (isRunning) { std::cout << "[RUNNING]" << std::endl; automaton->run(); automaton->display(); } wait(1000); } } void handleCtrlC(int) { isRunning = false; std::cout << "[PAUSED]" << std::endl; while (true) { automaton->display(); std::cout << "Press Enter to continue, S to save, L to load, C to change cell state, R to revert to previous generation, or Q to quit." << std::endl; std::string input; std::getline(std::cin, input); if (input.empty()) { std::cout << "[CONTINUE]" << std::endl; break; } else if (input == "S" || input == "s") { std::cout << "Enter file name: "; std::getline(std::cin, input); automaton->saveToFile(input); } else if (input == "L" || input == "l") { std::cout << "Enter file name: "; std::getline(std::cin, input); automaton->loadFromFile(input); } else if (input == "C" || input == "c") { std::cout << "Enter cell coordinates (x,y): "; std::getline(std::cin, input); int x = std::stoi(input.substr(0, input.find(','))); int y = std::stoi(input.substr(input.find(',') + 1)); automaton->changeCellState(x, y); } else if (input == "R" || input == "r") { automaton->revertToPreviousGeneration(); } else if (input == "Q" || input == "q") { std::cout << "[EXIT]" << std::endl; exit(0); } else { std::cout << "Invalid input." << std::endl; } } isRunning = true; signal(SIGINT, handleCtrlC); } 介绍这段代码思路
时间: 2024-01-19 19:02:17 浏览: 31
这段代码实现了一个元胞自动机,它可以模拟生命游戏等各种自动化过程。它通过在二维平面上的单元格之间应用简单的规则来模拟各种动态系统。用户可以指定行和列的数量,并选择是否使用随机模式。然后,程序将在一个死循环中运行,每秒钟更新一次,并显示当前状态。当用户按下 CTRL-C 时,程序会暂停,并等待用户输入命令。用户可以选择继续运行、保存状态、加载状态、更改单元格状态、回退到上一个状态或退出程序。一旦用户完成输入,程序将继续运行,并等待下一个 CTRL-C 信号。这个程序使用了一些 C++ 标准库函数和系统库函数,如 std::cout, std::cin, std::getline, signal 等。同时,它也使用了自定义的 Automaton 类,用来实现元胞自动机的各种操作。
相关问题
#include<iostream> #include<stack> #include<queue>
这是C++的头文件引用,分别包含了输入输出流、栈和队列的类。其中iostream头文件定义了输入输出流的类,stack头文件定义了栈的类,queue头文件定义了队列的类。以下是它们的简单使用示例:
1. iostream头文件的使用:
```c++
#include<iostream>
using namespace std;
int main() {
int num;
cin >> num; // 从控制台读取一个整数
cout << "You entered " << num << endl; // 输出读取到的整数
return 0;
}
```
2. stack头文件的使用:
```c++
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int> s; // 定义一个int类型的栈
s.push(1); // 压入元素1
s.push(2); // 压入元素2
s.push(3); // 压入元素3
while (!s.empty()) { // 遍历栈中元素
cout << s.top() << " "; // 输出栈顶元素
s.pop(); // 弹出栈顶元素
}
return 0;
}
```
3. queue头文件的使用:
```c++
#include<iostream>
#include<queue>
using namespace std;
int main() {
queue<int> q; // 定义一个int类型的队列
q.push(1); // 入队1
q.push(2); // 入队2
q.push(3); // 入队3
while (!q.empty()) { // 遍历队列中元素
cout << q.front() << " "; // 输出队首元素
q.pop(); // 弹出队首元素
}
return 0;
}
```
#include <iostream>#include <string>#include <queue>
这是 C++ 代码中的头文件引用,分别对应了输入输出流、字符串操作和队列等数据结构的实现。这些头文件是 C++ 程序编写中常用的标准库头文件,可以提供诸如 cout、cin、string、queue 等对象和方法供程序调用使用。
下面是一个简单的使用样例,演示了如何创建一个队列并向其中添加元素:
```c++
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue; // 定义一个整型队列
myQueue.push(1); // 向队列中添加元素
myQueue.push(2);
myQueue.push(3);
while (!myQueue.empty()) { // 输出队列中的所有元素
std::cout << myQueue.front() << std::endl;
myQueue.pop();
}
return 0;
}
```
以上代码定义了一个整型队列 myQueue,并向其中添加了三个元素。在 while 循环中,我们通过 front() 方法访问队列的首个元素,并通过 pop() 方法将其移出队列。当队列为空时,程序退出循环。运行程序后,输出结果为:
```
1
2
3
```