Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Emanuela Cerchez -100 puncte
- #include <fstream>
- #define LGMAX 104
- #define NRMAX 54
- using namespace std;
- ifstream fin("gcl.in");
- ofstream fout("gcl.out");
- char s[LGMAX];
- char p[NRMAX][LGMAX];
- int v[26];
- void unu();
- void doi();
- void spatii(char *s);
- int expresie (char *);
- int main()
- {char c;
- int cerinta;
- fin>>cerinta; fin.get(c);
- if (cerinta==1) unu();
- else doi();
- fout.close();
- return 0;
- }
- void unu()
- {
- int total=0, nr=1, i;
- fin.getline(s,LGMAX);//Incepe
- while (fin.getline(s,LGMAX))
- {spatii(s);
- if (s[1]=='C') //scrie
- total+=nr;
- else
- if (s[1]=='T') //stop
- nr=1;
- else
- if (s[1]=='E') //repeta
- {nr=0; i=6;
- while (s[i]>='0' && s[i]<='9')
- {nr=nr*10+s[i]-'0'; i++; }
- }
- else //atribuire
- v[s[0]-'a']=expresie(s+2);
- }
- fout<<total<<'\n';
- }
- void spatii(char * s)
- {int nrs=0, i;
- for (i=0; s[i]; i++)
- if (s[i]==' ') nrs++;
- else s[i-nrs]=s[i];
- s[i-nrs]=0;
- }
- int expresie (char * s)
- {int i, valoare=0, t1, semn, n;
- //primul termen
- i=0; semn=1;
- while (1)
- {if (s[i]>='a' && s[0]<='z') {t1=v[s[i]-'a'];i++;}
- else //constanta
- {n=0;
- while (s[i]>='0' && s[i]<='9') {n=n*10+s[i]-'0'; i++;}
- if (s[i]>='a' && s[0]<='z')
- {t1=n*v[s[i]-'a'];i++;}
- else t1=n;
- }
- valoare+=semn*t1;
- if (s[i]=='+') {semn=1; i++;}
- else
- if (s[i]=='-') {semn=-1; i++;}
- else break;
- }
- return valoare;
- }
- void doi()
- {int i=0, unde, lg, nr=1, j;
- while (fin.getline(p[i],LGMAX))
- {spatii(p[i]); i++; }
- lg=i; i=1;
- while (1)
- {
- if (p[i][1]=='C') //scrie
- {
- fout<<p[i][5]<<" = "<<v[p[i][5]-'a']<<'\n';
- i++;
- }
- else
- if (p[i][1]=='T') //stop
- {if (nr==1) {i++; if (i==lg) break;}
- else {nr--; i=unde;}
- }
- else
- if (p[i][1]=='E') //repeta
- {
- nr=0; j=6;
- while (p[i][j]>='0' && p[i][j]<='9')
- {
- nr=nr*10+p[i][j]-'0';
- j++;
- }
- i++;
- if (nr>0)
- unde=i;
- else
- {
- while (p[i][1]!='T') i++;
- i++;
- }
- }
- else //atribuire
- {
- v[p[i][0]-'a']=expresie(p[i]+2);
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement