From 8e0a3643f3965afb1dcc0abd26bd9a4600826155 Mon Sep 17 00:00:00 2001 From: whoami096 Date: Sat, 4 Nov 2023 18:49:12 +0800 Subject: [PATCH] commit message --- .../string.cpp" | 74 +++++++++++++ README.md | 9 ++ log_in_to_campus_network.py | 63 +++++++++++ ...00\345\215\225\344\275\277\347\224\250.md" | 104 ++++++++++++++++++ ...40\347\247\215\346\226\271\346\263\225.md" | 75 +++++++++++++ ...07\351\222\210\345\210\235\346\216\242.md" | 74 +++++++++++++ ...2\347\216\257\351\230\237\345\210\227.cpp" | 60 ++++++++++ 7 files changed, 459 insertions(+) create mode 100644 "C++\346\225\260\346\215\256\347\273\223\346\236\204\347\232\204\345\217\257\350\203\275\345\256\236\347\216\260/string.cpp" create mode 100644 README.md create mode 100755 log_in_to_campus_network.py create mode 100644 "pbds\347\232\204\347\256\200\345\215\225\344\275\277\347\224\250.md" create mode 100644 "\344\272\244\346\215\242Esc\344\270\216Caps\351\224\256\347\232\204\345\207\240\347\247\215\346\226\271\346\263\225.md" create mode 100644 "\346\214\207\351\222\210\345\210\235\346\216\242.md" create mode 100644 "\351\223\276\345\274\217\345\276\252\347\216\257\351\230\237\345\210\227.cpp" diff --git "a/C++\346\225\260\346\215\256\347\273\223\346\236\204\347\232\204\345\217\257\350\203\275\345\256\236\347\216\260/string.cpp" "b/C++\346\225\260\346\215\256\347\273\223\346\236\204\347\232\204\345\217\257\350\203\275\345\256\236\347\216\260/string.cpp" new file mode 100644 index 0000000..32cbe18 --- /dev/null +++ "b/C++\346\225\260\346\215\256\347\273\223\346\236\204\347\232\204\345\217\257\350\203\275\345\256\236\347\216\260/string.cpp" @@ -0,0 +1,74 @@ +#include +class string { +private: + char* str = nullptr; + size_t len = 0; + size_t max_len = 1; +public: + string(); + string(const string &__str); + string(const char* __str); + ~string(); + string(string &&__str); + + string substr(size_t __pos = 0, size_t __n = 18446744073709551615UL); + void push_back(char __c); + void pop_back(); + size_t size() const; + size_t length() const; + const char* c_str() const; +}; + +string::string() {} + +string::string(const string &__str) { + string(__str.c_str()); +} + +string::string(const char* __str) { + for(size_t i=0;__str[i];i++) this->push_back(__str[i]); +} + +string::string(string &&__str) { + str = __str.str; + len = __str.len; + max_len = __str.max_len; + __str.str = nullptr; + __str.len = 0; + __str.max_len = 1; +} + +string::~string() { + if(str) delete[] str; +} + +const char* string::c_str() const { + return str; +} + +void string::push_back(char __c) { + if(len + 1 == max_len) { + max_len <<= 1; + char *tmp = str; + str = new char[max_len]; + for(size_t i = 0; str[i] = tmp[i]; i++); + } + str[len ++] = __c; + str[len] = '\0'; +} + +void string::pop_back() { + -- len; +} + +size_t string::length() const { + return len; +} + +size_t string::size() const { + return len; +} + +string string::substr(size_t __pos = 0, size_t __n = 18446744073709551615UL) { + // TODO +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2dfd8dc --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +### 目录 + +[指针初探,使用链式存储结构实现循环队列](%E6%8C%87%E9%92%88%E5%88%9D%E6%8E%A2.md) + +[交换Esc与Caps键的几种方法](%E4%BA%A4%E6%8D%A2Esc%E4%B8%8ECaps%E9%94%AE%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95.md) + +[C++数据结构的可能实现](C++%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%9A%84%E5%8F%AF%E8%83%BD%E5%AE%9E%E7%8E%B0) + +[pbds的简单使用](pbds%E7%9A%84%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8.md) \ No newline at end of file diff --git a/log_in_to_campus_network.py b/log_in_to_campus_network.py new file mode 100755 index 0000000..9d7ee31 --- /dev/null +++ b/log_in_to_campus_network.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +'''开机自动登录校园网''' + +import asyncio +import pyppeteer +import requests +import time + + +'''需要修改的变量''' +user_name = '123456' # 学号 +pass_word = '123456' # 密码 +tmp_dir = '/tmp' # 临时文件夹 +chrome_path = '/usr/bin/google-chrome' # chrome浏览器路径 + + +async def loginfunction(loginUrl: str): + start_time = time.time() + while True: + if time.time() - start_time > 60: + return # 60秒内未连接到校园网,退出 + try: + response = requests.get(loginUrl, timeout=3) + if response.status_code == 200: + break # 连接到校园网,准备登录 + except KeyboardInterrupt as e: + return # 用户中断 + except: + pass # 未连接到网络,继续尝试 + + width, height = 1400, 800 # 窗口大小 + # 启动浏览器 + browser = await pyppeteer.launch(headless=False, + userdataDir = tmp_dir, + executablePath = chrome_path, + args=[f'--window-size={width},{height}']) + page = await browser.newPage() # 新建页面 + await page.setViewport({'width': width, 'height': height}) # 设置页面大小 + await page.goto(loginUrl) # 跳转到登录页面 + + if await page.querySelector("body > div.success > p") != None: # 已登录 + await browser.close() + return + + element = await page.querySelector("#username") + await element.type(user_name) # 输入用户名 + element = await page.querySelector("#password") + await element.type(pass_word) # 输入密码 + element = await page.querySelector("#login-account") + await element.click() # 点击登录 + await page.waitForSelector("body > div.success > p", timeout=30000) # 等待登录页面加载完成 + await browser.close() # 关闭浏览器 + + +def main(): + url = 'http://172.22.255.18/srun_portal_pc?ac_id=1&theme=pro' + asyncio.get_event_loop().run_until_complete(loginfunction(url)) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git "a/pbds\347\232\204\347\256\200\345\215\225\344\275\277\347\224\250.md" "b/pbds\347\232\204\347\256\200\345\215\225\344\275\277\347\224\250.md" new file mode 100644 index 0000000..2e6e6e4 --- /dev/null +++ "b/pbds\347\232\204\347\256\200\345\215\225\344\275\277\347\224\250.md" @@ -0,0 +1,104 @@ +# PBDS + +## `__gnu_pbds :: tree` + +```cpp +#include // 因为tree定义在这里 所以需要包含这个头文件 +#include +using namespace __gnu_pbds; +__gnu_pbds ::tree, Tag = rb_tree_tag, + Node_Update = null_tree_node_update, + Allocator = std::allocator > +``` + +## 模板形参 + +- `Key`: 储存的元素类型,如果想要存储多个相同的 `Key` 元素,则需要使用类似于 `std::pair` 和 `struct` 的方法,并配合使用 `lower_bound` 和 `upper_bound` 成员函数进行查找 +- `Mapped`: 映射规则(Mapped-Policy)类型,如果要指示关联容器是 **集合**,类似于存储元素在 `std::set` 中,此处填入 `null_type`,低版本 `g++` 此处为 `null_mapped_type`;如果要指示关联容器是 **带值的集合**,类似于存储元素在 `std::map` 中,此处填入类似于 `std::map` 的 `Value` 类型 +- `Cmp_Fn`: 关键字比较函子,例如 `std::less` +- `Tag`: 选择使用何种底层数据结构类型,默认是 `rb_tree_tag`。`__gnu_pbds` 提供不同的三种平衡树,分别是: + - `rb_tree_tag`:红黑树,一般使用这个,后两者的性能一般不如红黑树 + - `splay_tree_tag`:splay 树 + - `ov_tree_tag`:有序向量树,只是一个由 `vector` 实现的有序结构,类似于排序的 `vector` 来实现平衡树,性能取决于数据想不想卡你 +- `Node_Update`:用于更新节点的策略,默认使用 `null_node_update`,若要使用 `order_of_key` 和 `find_by_order` 方法,需要使用 `tree_order_statistics_node_update` +- `Allocator`:空间分配器类型 + +## 构造方式 + +```c++ +__gnu_pbds::tree, __gnu_pbds::null_type, + std::less >, __gnu_pbds::rb_tree_tag, + __gnu_pbds::tree_order_statistics_node_update> + tr; +``` + +## 成员函数 + +- `insert(x)`:向树中插入一个元素 x,返回 `std::pair`。 +- `erase(x)`:从树中删除一个元素/迭代器 x,返回一个 `bool` 表明是否删除成功。 +- `order_of_key(x)`:返回 x 以 `Cmp_Fn` 比较的排名。 +- `find_by_order(x)`:返回 `Cmp_Fn` 比较的排名所对应元素的迭代器。 +- `lower_bound(x)`:以 `Cmp_Fn` 比较做 `lower_bound`,返回迭代器。 +- `upper_bound(x)`:以 `Cmp_Fn` 比较做 `upper_bound`,返回迭代器。 +- `join(x)`:将 x 树并入当前树,前提是两棵树的类型一样,x 树被删除。 +- `split(x,b)`:以 `Cmp_Fn` 比较,小于等于 x 的属于当前树,其余的属于 b 树。 +- `empty()`:返回是否为空。 +- `size()`:返回大小。 +- `find(x)`:返回指向 x 的迭代器,如果不存在则返回 `tree.end()` + +## 示例 + +```cpp +#include +#include +#include +using namespace std; +using namespace __gnu_pbds; +int main() +{ + tree, rb_tree_tag, tree_order_statistics_node_update> tr; + tr.insert(1); + tr.insert(2); + tr.insert(4); + // 树中元素 1,2,4 + auto it = tr.insert(3); // pair + cout << *it.first << endl; // 3 + cout << it.second << endl; // 1 + + bool flag = tr.erase(3); // bool + cout << flag << endl; // 1 + + // 树中元素 1,2,4 + // order_of_key(x) 返回小于x的元素个数(int) + int cnt = tr.order_of_key(3); + cout << cnt << endl; // 2 + cnt = tr.order_of_key(2); + cout << cnt << endl; // 1 + + // 树中元素 1,2,4 + // find_by_order(k) 返回指向第k小的元素的迭代器 (从0开始) + auto it2 = tr.find_by_order(1); // point_iterator + cout << *it2 << endl; // 2 + + auto it3 = tr.lower_bound(2); // point_iterator + cout << *it3 << endl; // 2 + it3 = tr.upper_bound(2); // point_iterator + cout << *it3 << endl; // 4 + + tree, rb_tree_tag, tree_order_statistics_node_update> tr1; + tr1.insert(8); + tr1.insert(16); + tr1.insert(32); + + // tr.join(tr1) 将tr1合并到tr中,tr1中的元素会被删除 + tr.join(tr1); + for(auto it : tr) + cout << it << endl; // 1 2 4 8 16 32 + + tr.split(4, tr1); // 将tr中小于等于4的元素分离出来,放到tr1中 + for(auto it : tr) + cout << it << endl; // 1, 2, 4 + + return 0; +} +``` diff --git "a/\344\272\244\346\215\242Esc\344\270\216Caps\351\224\256\347\232\204\345\207\240\347\247\215\346\226\271\346\263\225.md" "b/\344\272\244\346\215\242Esc\344\270\216Caps\351\224\256\347\232\204\345\207\240\347\247\215\346\226\271\346\263\225.md" new file mode 100644 index 0000000..10e44a5 --- /dev/null +++ "b/\344\272\244\346\215\242Esc\344\270\216Caps\351\224\256\347\232\204\345\207\240\347\247\215\346\226\271\346\263\225.md" @@ -0,0 +1,75 @@ +# 交换Esc与Caps键的几种方法 + +## Windows + +### PowerToys + +在Microsoft Store 中下载PowerToys,其中有一个`Keyboard Manager`的工具,可以用来交换键位 + +## Linux(Ubuntu) + +### GUI + +无论是`Xorg`还是`Wayland`,该方法都适用 +修改以下文件 +> /usr/share/X11/xkb/symbols/pc + +格式类似如下风格 + +```txt +key { [ Escape ] }; +key { [ Caps_Lock ] }; +``` + +若要交换`Caps_Lock`和`Esc`,则修改为 + +```txt +key { [ Caps_Lock ] }; +key { [ Escape ] }; +``` + +### CLI + +1. 运行以下命令,备份当前键盘映射 + + ```bash + dumpkeys > back.kmap + cp back.kmap new.kmap + ``` + +2. 修改`new.kmap`文件,将`Caps_Lock`和`Esc`的映射交换 + + ```txt + keycode 1 = Escape + ... + keycode 58 = CtrlL_Lock + ... + ``` + + ```txt + keycode 1 = CtrlL_Lock + ... + keycode 58 = Escape + ... + ``` + +3. 运行以下命令,使新的键盘映射生效 + + ```bash + loadkeys new.kmap + ``` + +#### 备注 + +1. 若要恢复默认键盘映射,运行以下命令 + + ```bash + loadkeys back.kmap + ``` + +2. 若要每次开机自动生效,将以下命令添加到`/etc/rc.local`中即可,注意`/etc/rc.local`文件需要有可执行权限,可运行`sudo chmod u+x /etc/rc.local`赋予其可执行权限 + + ```bash + #!/bin/bash + /usr/bin/loadkeys /path/to/new.kmap + ``` diff --git "a/\346\214\207\351\222\210\345\210\235\346\216\242.md" "b/\346\214\207\351\222\210\345\210\235\346\216\242.md" new file mode 100644 index 0000000..f486254 --- /dev/null +++ "b/\346\214\207\351\222\210\345\210\235\346\216\242.md" @@ -0,0 +1,74 @@ +# 使用链式存储结构实现循环队列 + +> 实现方法,仅设置一个指向队列尾节点的指针,尾节点不存储元素,作为特殊节点对待 +> 初始状态下,尾节点的next指针指向自己,表示队列为空 + +> 定义节点结构体 + +```cpp +typedef int Elem; +struct Node +{ + Elem data; + Node* next; +}; +``` + +> 初始化函数,将尾节点的next指针指向自己,此时队列为空 + +```cpp +void init(Node* tail) +{ + tail->next = tail; +} +``` + +> 判断队列是否为空,当尾节点指向自己时,队列为空 + +```cpp +bool empty(Node* tail) +{ + return tail == tail->next; +} +``` + +> 元素入队,将元素赋值给队尾节点,然后新建一个节点作为新的尾节点 + +```cpp +void push(Node*& tail, Elem data) +{ + Node* p = new Node; + tail -> data = data; + p->next = tail->next; + tail->next = p; + tail = p; +} +``` + +> 元素出队,将队尾节点的next指针指向队头元素的下一个节点,然后delete原队头节点 + +```cpp +bool pop(Node* tail, Elem& data) +{ + Node* p = tail->next; + if(!empty(tail)) { + data = p->data; + tail->next = p->next; + delete p; + return true; + } +} +``` + +> 清空队列,队头元素出队,直到队列为空 + +```cpp +void clear(Node*& tail) +{ + Elem data; + while(!empty(tail)) + pop(tail, data); +} +``` + +[完整代码](%E9%93%BE%E5%BC%8F%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97.cpp) diff --git "a/\351\223\276\345\274\217\345\276\252\347\216\257\351\230\237\345\210\227.cpp" "b/\351\223\276\345\274\217\345\276\252\347\216\257\351\230\237\345\210\227.cpp" new file mode 100644 index 0000000..7e587b0 --- /dev/null +++ "b/\351\223\276\345\274\217\345\276\252\347\216\257\351\230\237\345\210\227.cpp" @@ -0,0 +1,60 @@ +#include +using namespace std; +typedef int Elem; +struct Node +{ + Elem data; + Node* next; +}; +void init(Node*& tail) +{ + tail->next = tail; +} +bool empty(Node* tail) +{ + return tail == tail->next; +} +bool pop(Node* tail, Elem& data) +{ + Node* p = tail->next; + if(!empty(tail)) { + data = p->data; + tail->next = p->next; + delete p; + return true; + } + return false; +} +void push(Node*& tail, Elem data) +{ + Node* p = new Node; + tail -> data = data; + p->next = tail->next; + tail->next = p; + tail = p; +} +void clear(Node*& tail) +{ + Elem data; + while(!empty(tail)) + pop(tail, data); +} +int main() +{ + Node* tail = new Node; + init(tail); + for (int i = 0; i < 10; i++) + { + push(tail, i); + } + + int data; + clear(tail); + for (int i = 0; i < 10; i++) + { + push(tail, i); + } + clear(tail); + //modify in windows + return 0; +} \ No newline at end of file