20250305-Rain

1. 链表

#include <iostream>

using namespace std;

class Node
{
public:
  int val;    // 当前节点存储的值
  Node *next; // 下一个节点

  typedef Node *List; // List表示头结点, List->next是第一个存储数据的节点

public:
  /*
  List createList()
  {
  }
  */
  // 构造函数
  Node() : val(0), next(nullptr) {};

  // 添加到头部
  void addAtHead(int val)
  {
    Node *newNode = new Node();
    newNode->val = val;
    newNode->next = this->next;
    this->next = newNode;
  }

  // 添加到尾部
  void addAtTail(int val)
  {
    Node *newNode = new Node();
    newNode->val = val;
    Node *q = this;
    while (q->next)
    {
      q = q->next;
    }
    q->next = newNode;
  }

  // 指定索引插入
  void addAtIndex(int index, int val)
  {
    Node *p = this;
    for (int i = 0; i < index; i++)
    {
      // idx exceed socope
      if (!p->next)
      {
        return;
      }
      p = p->next;
    }
    Node *newNode = new Node();
    newNode->val = val;
    newNode->next = p->next;
    p->next = newNode;
  }

  // 指定索引删除
  void deleteAtIndex(int index)
  {
    Node *p = this;
    for (int i = 0; i < index; i++)
    {
      // idx exceed socope
      if (!p->next)
      {
        return;
      }
      p = p->next;
    }
    if (p->next)
    {
      // 删除 temp 节点
      Node *temp = p->next;
      p->next = temp->next;
      delete temp;
    }
  }

  // 打印结点
  void SListPrint()
  {
    Node *q = this->next;
    while (q)
    {
      std::cout << q->val << ' ';
      q = q->next;
    }
    cout << endl;
  }
};

int main()
{
  Node list;
  list.addAtHead(1);
  list.addAtTail(2);
  list.addAtTail(3);
  list.addAtTail(4);
  list.addAtIndex(3, 5);
  list.addAtHead(6);
  list.SListPrint();
  list.deleteAtIndex(3);
  list.SListPrint();
  // 样例输出 :
  // 6 1 2 5 3 4
  // 6 1 5 3 4
  return 0;
}

2. 控制台

#include <iostream>
#include <string>
#include <vector>
#include <limits>

using namespace std;

// 清屏函数
void clearScreen()
{
  system("cls");
}

// 高亮显示函数
string highlight(const string &text)
{
  // 使用ANSI转义序列高亮(仅在支持ANSI的终端有效)
  return "\033[1;32m" + text + "\033[0m"; // 绿色高亮
}

// 游戏设置类
class GameSettings
{
private:
  vector<string> options = {"Difficulty: Normal", "Music: On", "Volume: 50", "Save/Exit"};
  int selectedIndex = 0; // 当前选中的选项索引

public:
  void run()
  {
    while (true)
    {
      clearScreen();
      displaySettings();
      char input = getCharInput();

      switch (input)
      {
      case 'w': // 向上移动
        selectedIndex = max(0, selectedIndex - 1);
        break;
      case 's': // 向下移动
        selectedIndex = min((int)options.size() - 1, selectedIndex + 1);
        break;
      case 'a':                                  // 修改选项
        if (selectedIndex == options.size() - 1) // 保存
        {
          cout << "Settings saved!" << endl;
          cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空输入缓冲区
          cin.get();                                           // 按任意键继续
        }
        else
        {
          modifyOption(selectedIndex, false);
        }
        break;
      case 'd':                                  // 修改选项
        if (selectedIndex == options.size() - 1) // 退出
        {
          cout << "Exiting settings..." << endl;
          return;
        }
        else
        {
          modifyOption(selectedIndex, true);
        }
        break;
      case '\n':                                 // 回车键
        if (selectedIndex == options.size() - 1) // 退出
        {
          cout << "Exiting settings..." << endl;
          return;
        }
        break;
      default:
        break;
      }
    }
  }

  void displaySettings()
  {
    for (int i = 0; i < options.size(); ++i)
    {
      if (i == selectedIndex)
      {
        cout << highlight(options[i]) << endl;
      }
      else
      {
        cout << options[i] << endl;
      }
    }
  }

  void modifyOption(int index, bool increment)
  {
    if (index == 0) // 修改难度
    {
      string difficulty = options[index].substr(12); // 提取当前难度值
      if (increment)
      {
        if (difficulty == "Normal")
          options[index] = "Difficulty: Hard";
        else if (difficulty == "Hard")
          options[index] = "Difficulty: Easy";
        else
          options[index] = "Difficulty: Normal";
      }
      else
      {
        if (difficulty == "Normal")
          options[index] = "Difficulty: Easy";
        else if (difficulty == "Hard")
          options[index] = "Difficulty: Normal";
        else
          options[index] = "Difficulty: Hard";
      }
    }
    else if (index == 1) // 修改音乐开关
    {
      string music = options[index].substr(7); // 提取当前音乐状态
      if (increment)
      {
        options[index] = "Music: " + std::string(music == "On" ? "Off" : "On");
      }
      else
      {
        options[index] = "Music: " + std::string(music == "On" ? "Off" : "On");
      }
    }
    else if (index == 2) // 修改音量
    {
      int volume = stoi(options[index].substr(8)); // 提取当前音量值
      if (increment)
      {
        options[index] = "Volume: " + to_string(min(100, volume + 10));
      }
      else
      {
        options[index] = "Volume: " + to_string(max(0, volume - 10));
      }
    }
  }

  char getCharInput()
  {
    char input;
    cin >> input;
    return input;
  }
};

int main()
{
  GameSettings settings;
  settings.run();
  return 0;
}

3. EasyX

#include <graphics.h>
#include <windows.h> // 包含 Windows API 的头文件
#include <array>     // 包含 std::array

#define M_PINK 0x500A8B // 定义粉红色
#define PINK RGB(255, 182, 193)

int brushSize = 5;  // 初始笔刷大小
int colorIndex = 0; // 初始颜色索引
const int colors[4] = { RED, PINK, BLACK, WHITE }; // 定义四种颜色

// 点
struct Point {
    int x, y;
};

// 线工具
struct LineTool {
    int size;        // 大小
    COLORREF color;  // 颜色
    struct Point beginPos;  // 起点
    bool isDown;     // 鼠标操作:按下
};

struct LineTool* createLineTool(int size, COLORREF color) {
    struct LineTool* pLine = new struct LineTool;
    pLine->size = size;
    pLine->color = color;
    pLine->beginPos.x = 0;
    pLine->beginPos.y = 0;
    pLine->isDown = false;
    return pLine;
}

void drawLine(struct LineTool* pTool, MOUSEMSG m) {
    // 鼠标左键按下: 记录起点,标记按下
    if (m.uMsg == WM_LBUTTONDOWN) {
        pTool->beginPos.x = m.x;
        pTool->beginPos.y = m.y;
        pTool->isDown = true;
    }

    // 鼠标左键弹起
    if (m.uMsg == WM_LBUTTONUP) {
        pTool->isDown = false;
    }

    // 鼠标移动 + 左键按下
    if (m.uMsg == WM_MOUSEMOVE && pTool->isDown) {
        setlinestyle(PS_ENDCAP_ROUND, pTool->size);
        setcolor(pTool->color);
        line(pTool->beginPos.x, pTool->beginPos.y, m.x, m.y);
    }
    pTool->beginPos.x = m.x;
    pTool->beginPos.y = m.y;
}

int main() {
    initgraph(800, 600);
    setbkcolor(WHITE);
    cleardevice();

    struct LineTool* pLine = createLineTool(brushSize, colors[colorIndex]);

    // 上一次按键状态
    bool lastKeyState[256] = { false }; // 用于记录每个按键的上一次状态

    // 使用 std::array 明确范围类型
    std::array<int, 5> keys = { 'A', 'D', 'Q', 'E', VK_ESCAPE };

    while (true) {
        // 处理鼠标事件
        while (MouseHit()) {
            MOUSEMSG m = GetMouseMsg();
            drawLine(pLine, m);
        }

        // 处理键盘事件
        for (int key : keys) {
            bool currentKeyState = GetAsyncKeyState(key) & 0x8000;
            if (currentKeyState && !lastKeyState[key]) { // 按键按下且上一次未按下
                switch (key) {
                case 'A': // 切换到上一个颜色
                    colorIndex = (colorIndex - 1 + 4) % 4;
                    pLine->color = colors[colorIndex];
                    break;
                case 'D': // 切换到下一个颜色
                    colorIndex = (colorIndex + 1) % 4;
                    pLine->color = colors[colorIndex];
                    break;
                case 'Q': // 减小笔刷大小
                    if (brushSize > 1) {
                        brushSize--;
                        pLine->size = brushSize;
                    }
                    break;
                case 'E': // 增大笔刷大小
                    if (brushSize < 25) {
                        brushSize++;
                        pLine->size = brushSize;
                    }
                    break;
                case VK_ESCAPE: // 按下 ESC 键退出
                    closegraph();
                    return 0;
                }
            }
            lastKeyState[key] = currentKeyState; // 更新按键状态
        }

        // 防止 CPU 占用过高
        Sleep(20);
    }

    return 0;
}

Last updated