#002 MiniPy
2026-10-29 19:16:57
发布于:广东
来来来,这是一个长期作品,此为最旧版本v0.1,后续均在此帖发布更新,谢谢配合
// MiniPy v0.2 —— 单文件、C++17、标准 stdin/stdout
// 只保留:x = 5 | print(x) | print(5) ;空行和 # 注释忽略。
// 默认:逐行读、回车执行,EOF 退出。无自动演示、无算术、无复杂报错。
//
// Mac: g++ -std=c++17 minipy.cpp -o minipy && ./minipy (Ctrl+D 退出)
// Windows: g++ -std=c++17 minipy.cpp -o minipy.exe && minipy (Ctrl+Z 回车退出)
// ACGO: 直接提交/运行;把多行输入预填到 stdin。若没有输入区,读到 EOF 即结束。
// 可选: g++ -std=c++17 -DMINIPY_AUTOEXIT=1 minipy.cpp -o minipy_batch
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
// 裁剪两端空白;不改动中间内容,避免引入表达式规则。
static string trim(const string& s) {
size_t a = 0, b = s.size();
while (a < b && isspace(static_cast<unsigned char>(s[a]))) ++a;
while (b > a && isspace(static_cast<unsigned char>(s[b - 1]))) --b;
return s.substr(a, b - a);
}
// 只认十进制整数;不识别 0x、八进制、浮点和算式。
// 不合规则时返回“不可解析”哨兵,调用处会静默忽略该行。
static int parse_int(const string& s) {
if (s.empty()) return 0x7fffffff; // 哨兵:不是合法整数
size_t i = 0;
bool neg = false;
if (s[i] == '+' || s[i] == '-') { neg = (s[i] == '-'); ++i; }
if (i == s.size()) return 0x7fffffff; // 只剩符号
long long n = 0;
for (; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') return 0x7fffffff;
n = n * 10 + (s[i] - '0');
}
return neg ? -static_cast<int>(n) : static_cast<int>(n);
}
static const string* match(const string& s, const string& pat,
size_t* a, size_t* b) {
// 极简匹配器:pat 中 'V' 表示“变量名”,'I' 表示“整数”,其余字符必须逐字相等。
// 成功时 *a/*b 给出被标记段在 s 中的闭开区间;不依赖 <regex>,避免方言/头文件杂音。
size_t j = 0;
*a = *b = 0;
for (size_t i = 0; i < pat.size(); ) {
char p = pat[i];
if (p == 'V') {
*a = j;
while (j < s.size() && (isalnum(static_cast<unsigned char>(s[j])) || s[j] == '_')) ++j;
*b = j;
if (*b == *a || !(isalpha(static_cast<unsigned char>(s[*a])) || s[*a] == '_'))
return nullptr; // 空名或数字开头,不认
++i;
} else if (p == 'I') {
*a = j;
while (j < s.size() && s[j] >= '0' && s[j] <= '9') ++j;
*b = j;
if (*b == *a) return nullptr; // 至少要有一位数字
++i;
} else {
if (j >= s.size() || s[j] != p) return nullptr;
++j; ++i;
}
}
return (j == s.size()) ? &s : nullptr; // 整行必须耗尽
}
int main() {
unordered_map<string, int> vars;
string line;
while (getline(cin, line)) { // EOF 时自动结束,tty 与文件同一条路径
string s = trim(line);
if (s.empty() || s[0] == '#') continue;
size_t a = 0, b = 0;
const string* hit = nullptr;
// x = 5
if ((hit = match(s, "V = I", &a, &b))) {
string name = s.substr(a, b - a);
int value = parse_int(s.substr(a, b - a));
if (value != 0x7fffffff) vars[name] = value;
}
// print(5) 或 print(x)
else if ((hit = match(s, "print(I)", &a, &b))) {
int value = parse_int(s.substr(a, b - a));
if (value != 0x7fffffff) cout << value << '\n';
} else if ((hit = match(s, "print(V)", &a, &b))) {
string name = s.substr(a, b - a);
auto it = vars.find(name);
cout << (it != vars.end() ? it->second : 0) << '\n';
}
// 其他输入:静默忽略,按用户要求不报复杂错误。
cout.flush(); // 网页/交互式评测也能及时看到输出
#ifdef MINIPY_AUTOEXIT
// 批处理形态:读完一行就结束,适合“运行按钮只跑一条”的保守网页体验。
break;
#endif
}
return 0;
}
全部评论 4
- 置顶
v0.2版本来啦!
#include <iostream> #include <fstream> #include <string> #include <map> #include <sstream> #include <stack> #include <cctype> using namespace std; map<string, int> vars; string trim(string s) { while (!s.empty() && s[0] == ' ') s.erase(0, 1); while (!s.empty() && s.back() == ' ') s.pop_back(); return s; } string strip_comment(string s) { size_t h = s.find('#'); if (h != string::npos) s = s.substr(0, h); return trim(s); } int get_val(string s) { s = trim(s); if (vars.count(s)) return vars[s]; return stoi(s); } int precedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; } int apply_op(int a, int b, char op) { if (op == '+') return a + b; if (op == '-') return a - b; if (op == '*') return a * b; if (op == '/') { if (b == 0) { cout << "MiniPy: 除零!" << endl; return 0; } return a / b; } return 0; } int calc_expr(string expr) { stack<int> vals; stack<char> ops; for (size_t i = 0; i < expr.length(); i++) { if (expr[i] == ' ') continue; if (expr[i] == '(') { ops.push(expr[i]); } else if (expr[i] == ')') { while (!ops.empty() && ops.top() != '(') { int b = vals.top(); vals.pop(); int a = vals.top(); vals.pop(); char op = ops.top(); ops.pop(); vals.push(apply_op(a, b, op)); } if (!ops.empty()) ops.pop(); } else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') { while (!ops.empty() && precedence(ops.top()) >= precedence(expr[i])) { int b = vals.top(); vals.pop(); int a = vals.top(); vals.pop(); char op = ops.top(); ops.pop(); vals.push(apply_op(a, b, op)); } ops.push(expr[i]); } else { string token;5天前 来自 广东
0 提示:v0.2和v0.2.5均为不完整版
4天前 来自 广东
0v0.2.5版本来啦!本版要求仅能在Windows/Mac终端(Windows Powershell和iTerm)运行,acgo无法体验本版本的全部功能,请见谅。
本版本兼容标准库,标准库内容可看该评论的附件。#include <iostream> #include <string> #include <vector> #include <map> #include <sstream> #include <fstream> #include <cctype> using namespace std; // ═══════════════════════════════════════════ // 全局环境 // ═══════════════════════════════════════════ map<string, string> vars; bool loaded_module[3] = {false, false, false}; // [0]=base [1]=math [2]=io // ═══════════════════════════════════════════ // 工具函数 // ═══════════════════════════════════════════ string trim(const string& s) { size_t a = 0, b = s.size(); while (a < b && isspace(s[a])) a++; while (b > a && isspace(s[b-1])) b--; return s.substr(a, b - a); } bool isNumber(const string& s) { if (s.empty()) return false; size_t i = 0; if (s[0] == '-') i++; bool dot = false; for (; i < s.size(); i++) { if (s[i] == '.') { if (dot) return false; dot = true; } else if (!isdigit(s[i])) return false; } return true; } double toNum(const string& s) { return stod(s); } bool isIdChar(char c) { return isalnum(c) || c == '_'; } pair<string, int> nextToken(const string& s, int pos) { while (pos < (int)s.size() && isspace(s[pos])) pos++; if (pos >= (int)s.size()) return {"", 0}; char c = s[pos]; if (c == '=' && pos+1 < (int)s.size() && s[pos+1] == '=') return {s.substr(pos,2), 2}; if (c == '!' && pos+1 < (int)s.size() && s[pos+1] == '=') return {s.substr(pos,2), 2}; if (c == '>' && pos+1 < (int)s.size() && s[pos+1] == '=') return {s.substr(pos,2), 2}; if (c == '<' && pos+1 < (int)s.size() && s[pos+1] == '=') return {s.substr(pos,2), 2}; if (c == 'a' && s.substr(pos,3) == "and") { if (pos+3 >= (int)s.size() || !isIdChar(s[pos+3])) return {"and", 3}; } if (c == 'o' && s.substr(pos,2) == "or") { if (pos+2 >= (int)s.size() || !isIdChar(s[pos+2])) return {"or", 2}; }5天前 来自 广东
0dev能搞吗
1周前 来自 广东
0运行后啥也没有
1周前 来自 广东
0不知道,也许吧
要真不行就下个g++(你要是Windows的话就下微软的IDE)1周前 来自 广东
0就是弹窗后要输入啥吗
1周前 来自 广东
0





















有帮助,赞一个