Cod sursa(job #3367979)

Utilizator VladGtjGatej Vlad VladGtj Data 12 octombrie 2026 16:22:23
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <stdio.h>
#include <string.h>

#define MAX_S 100000

char s[MAX_S];
int n;
int sIndex;

char operators[MAX_S];
int operatorsSize;

int factors[MAX_S];
int factorsSize;

int priority(char op) {
    if(op=='m'||op=='M'){
        return 1;
    }
    return 0;
}
void pushOperator(char op) {
  if (op=='m'||op=='M'||op=='('||op==')')
    operators[operatorsSize++] = op;
}
char popOperator() {
  return operatorsSize ? operators[--operatorsSize] : '\0';
}
char topOperator() {
  return operatorsSize ? operators[operatorsSize - 1] : '\0';
}
bool hasOperators() {
  return operatorsSize>0;
}

void pushFactor(int factor) {
  factors[factorsSize++]=factor;
}
int popFactor() {
  return factorsSize ? factors[--factorsSize] : 0;
}

int compute(int a, int b, char op) {
  int result;
  result = 0;
  if(op=='m'){
      if(a<b){
          result=a;
      }else{
          result=b;
      }
  }else if(op=='M'){
        if(a>b){
            result=a;
        }else{
            result=b;
        }
    }
  return result;
}

void computeTop() {
  int a,b;

  b=popFactor();
  a=popFactor();
  pushFactor(compute(a,b,popOperator()));
}

int factor() {
  int number;

  number=0;
  while(s[sIndex]>='0' && s[sIndex]<='9') {
    number=number*10+s[sIndex]-'0';
    sIndex++;
  }

  return number;
}

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

  fgets(s,MAX_S,fin);
  n=strlen(s);

  sIndex=0;
  while(sIndex<n) {
    if(s[sIndex]>='0' && s[sIndex]<='9')
      pushFactor(factor());
    else if(s[sIndex]=='(')
      pushOperator(s[sIndex++]);
    else if(s[sIndex] ==')') {
      while(topOperator()!='(')
        computeTop();
      popOperator();
      sIndex++;
    } else if(priority(s[sIndex])) {
      while(priority(topOperator()) && priority(topOperator())>=priority(s[sIndex]))
        computeTop();
      pushOperator(s[sIndex++]);
    } else
      sIndex++;
  }

  while(hasOperators())
    computeTop();

  fprintf(fout,"%d\n",popFactor());

  fclose(fin);
  fclose(fout);
  return 0;
}