Cod sursa(job #2790447)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 28 octombrie 2021 23:57:19
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <ctype.h>

using namespace std;

FILE *fin;
char ch;
int numere;

char nextChar() {
    char ch;

    ch = fgetc( fin );
    while ( isspace( ch ) && ch != '\n' )
        ch = fgetc( fin );

    return ch;
}

int numar() {
    int a, semn;

    numere++;

    semn = 1;
    if ( ch == '-' ) {
        semn = -1;
        ch = nextChar();
    }

    a = 0;
    while ( isdigit( ch ) ) {
        a = a * 10 + ch - '0';
        ch = nextChar();
    }

    return a * semn;
}
int parantezaRotunda();
int parantezaPatrata();

int factor() {
    if ( ch == '(' ) {
        ch = nextChar();
        return parantezaRotunda();
    } else if ( ch == '[' ) {
        ch = nextChar();
        return parantezaPatrata();
    } else
        return numar();
}

int parantezaRotunda() {
    int maxim, suma;

    maxim = suma = 0;
    while ( ch != ')' ) {
        suma += factor();
        if ( suma > maxim )
            maxim = suma;
        if ( suma < 0 )
            suma = 0;
        if ( ch == ',' )
            ch = nextChar();
    }
    ch = nextChar();

    return maxim;
}

int parantezaPatrata() {
    vector <int> v;

    while ( ch != ']' ) {
        v.push_back( factor() );
        if ( ch == ',' )
            ch = nextChar();
    }
    ch = nextChar();

    nth_element( v.begin(), v.begin() + (v.size() - 1) / 2, v.end() );

    return v[(v.size() - 1) / 2];
}

int expresie() {
    int suma;

    suma = 0;
    while ( ch != '\n' ) {
        suma += factor();
        if ( ch == ',' )
            ch = nextChar();
    }

    return suma;
}

int main() {
    FILE *fout;
    int rez;

    numere = 0;
    fin = fopen( "expresie2.in", "r" );
    ch = nextChar();
    rez = expresie();
    fclose( fin );

    fout = fopen( "expresie2.out", "w" );
    fprintf( fout, "%d\n%d\n", numere, rez );
    fclose( fout );

    return 0;
}