Cod sursă (job #403284)

Utilizator avatar andra_alazaroaie Andra Alazaroaie andra_alazaroaie IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 0,82 kb
Rundă Arhiva de probleme Status evaluat
Dată 18 nov. 2018 18:55:41 Scor 100
#include <stdio.h>
#include <ctype.h>
#define MAX 100000

/*
E -> nrL+
L -> '(' E (,E*)')'
*/

FILE *fin,*fout;
int vnr[MAX],poz;
char ch;

char nextc(){
  ch=fgetc(fin);
  while(ch==' ' || ch=='\n')
    ch=fgetc(fin);
  return ch;
}

int num(){
  int nr=0;
  while(isdigit(ch)){
    nr=nr*10+ch-'0';
    ch=nextc();
  }
  return nr;
}

int e();

int l(int n){
  int len=0;
  ch=nextc();
  vnr[poz++]=e();
  len++;
  while(ch==','){
    ch=nextc();
    vnr[poz++]=e();
    len++;
  }
  ch=nextc();
  n=(n-1)%len;
  poz-=len;
  return vnr[poz+n];
}

int e(){
  int n=num();
  while(ch=='(')
    n=l(n);
  return n;
}

int main(){
  fin=fopen("bizar.in","r");
  fout=fopen("bizar.out","w");

  ch=nextc();
  fprintf(fout,"%d\n",e());
  fclose(fin);
  fclose(fout);
  return 0;
}