activity栈管理类ScreenManager

public class ScreenManager {private static Stack activityStack;private static ScreenManager instance;private ScreenManager() {}public static ScreenManager getScreenManager() {if (instance == null) {instance = new ScreenManager();}return instance;}public void popActivity() {Activity activity = activityStack.lastElement();if (activity != null) {activity.finish();activity = null;}}public void popActivity(Activity activity) {if (activity != null) {activity.finish();activityStack.remove(activity);activity = null;}}public Activity currentActivity() {Activity activity = activityStack.lastElement();return activity;}public void pushActivity(Activity activity) {if (activityStack == null) {activityStack = new Stack();}activityStack.add(activity);}//除了当前activity,其他activity全被清除出栈public void popAllActivityExceptOne(Class <?>cls) {while (true) {Activity activity = currentActivity();if (activity == null) {break;}if (activity.getClass().equals(cls)) {break;}popActivity(activity);}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现该功能的代码: ```cpp #include <iostream> #include <vector> #include "shape.h" using namespace std; class ScreenManager { public: ScreenManager(int width, int height) : width(width), height(height) { screen.resize(height, vector<char>(width, ' ')); // 初始化屏幕矩阵 } void addShape(Shape* shape) { shapes.push_back(shape); // 将新图形添加到数组中 redraw(); // 重新绘制整个屏幕图像 } void redraw() { clearScreen(); // 清空屏幕 for (auto shape : shapes) { // 遍历每个图形 shape->draw(screen); // 绘制该图形 } show(); // 显示屏幕 } private: int width, height; vector<vector<char>> screen; // 屏幕矩阵 vector<Shape*> shapes; // 图形数组 void clearScreen() { for (auto& row : screen) { // 将屏幕矩阵中所有元素设为空格 fill(row.begin(), row.end(), ' '); } } void show() { for (auto& row : screen) { // 输出屏幕矩阵 for (auto ch : row) { cout << ch; } cout << endl; } } }; int main() { ScreenManager screen(80, 24); // 创建屏幕管理器 char fillChar; while (true) { cout << "Enter shape (square, rectangle, triangle, circle): "; string shapeType; cin >> shapeType; Shape* shape = nullptr; if (shapeType == "square") { int sideLength; cout << "Enter side length: "; cin >> sideLength; cout << "Enter fill character: "; cin >> fillChar; shape = new Square(sideLength, fillChar); } else if (shapeType == "rectangle") { int width, height; cout << "Enter width: "; cin >> width; cout << "Enter height: "; cin >> height; cout << "Enter fill character: "; cin >> fillChar; shape = new Rectangle(width, height, fillChar); } else if (shapeType == "triangle") { int base, height; cout << "Enter base: "; cin >> base; cout << "Enter height: "; cin >> height; cout << "Enter fill character: "; cin >> fillChar; shape = new Triangle(base, height, fillChar); } else if (shapeType == "circle") { int radius; cout << "Enter radius: "; cin >> radius; cout << "Enter fill character: "; cin >> fillChar; shape = new Circle(radius, fillChar); } else { cout << "Invalid shape type." << endl; } if (shape != nullptr) { screen.addShape(shape); // 将新图形添加到屏幕管理器中 } } return 0; } ``` 该程序会不断循环,每次提示用户输入一个图形型,然后根据输入创建相应的图形对象,并让用户指定位置、大小和填充字符。每当用户添加一个新的图形,程序就会重新绘制整个屏幕图像,并将所有图形都显示在屏幕上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值