全部评论 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天前 来自 广东

    0
  • v0.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天前 来自 广东

    0
  • dev能搞吗

    1周前 来自 广东

    0

热门讨论