Cod sursă (job #284977)

Utilizator avatar o_mic bianca o_mic IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,43 kb
Rundă Arhiva de probleme Status evaluat
Dată 24 feb. 2017 23:23:48 Scor 100
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define DN 105
#define DL 5005
#define x first
#define y second
#define LL long long
  
using namespace std;
 
ifstream fin("bizar.in");
ofstream fout("bizar.out");
 
// ifstream fin("input.txt");
// ofstream fout("output.txt");

int get_number(string &e, int &ind) {
    while (e[ind] == ' ') ++ind;
    int res = 0;
    while (e[ind] >= '0' && e[ind] <= '9')
        res = res * 10 + e[ind++] - '0';
    return res;
}

void trim_commas(string &e, int &ind) {
    while (e[ind] == ' ' || e[ind] == ',') ++ind;
}

int get_expression(string &e, int &ind) {
    int nr = get_number(e, ind), is_open = 0;
    vector<int> arg;
    while (ind < e.size()) {
        if (e[ind] == '(') {
            is_open = 1;
            arg.clear();
        } else if (e[ind] == ')') {
            if (!is_open)
                break;
            is_open = 0;
            nr = arg[(nr + arg.size() - 1) % arg.size()];
        } else if (e[ind] == ',') {
            break;
        } else if (e[ind] != ' ') {
            arg.push_back(get_expression(e, ind));
            trim_commas(e, ind);
            --ind;
        }
        ++ind;
    }
    return nr;
}

int main() {
    string expr;
    int ind = 0, res;

    getline(fin, expr);
    res = get_expression(expr, ind);
    fout << res;
    return 0;
}