Pagini recente » Cod sursa (job #585260) | Cod sursa (job #3030083) | Monitorul de evaluare | Cod sursa (job #2064752) | Cod sursa (job #2790447)
#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;
}