Advertisement
a53

gcl

a53
Jan 7th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. //Emanuela Cerchez -100 puncte
  2. #include <fstream>
  3. #define LGMAX 104
  4. #define NRMAX 54
  5.  
  6. using namespace std;
  7. ifstream fin("gcl.in");
  8. ofstream fout("gcl.out");
  9. char s[LGMAX];
  10. char p[NRMAX][LGMAX];
  11. int v[26];
  12.  
  13. void unu();
  14. void doi();
  15. void spatii(char *s);
  16. int expresie (char *);
  17.  
  18. int main()
  19. {char c;
  20. int cerinta;
  21. fin>>cerinta; fin.get(c);
  22. if (cerinta==1) unu();
  23. else doi();
  24. fout.close();
  25. return 0;
  26. }
  27.  
  28. void unu()
  29. {
  30. int total=0, nr=1, i;
  31. fin.getline(s,LGMAX);//Incepe
  32. while (fin.getline(s,LGMAX))
  33. {spatii(s);
  34. if (s[1]=='C') //scrie
  35. total+=nr;
  36. else
  37. if (s[1]=='T') //stop
  38. nr=1;
  39. else
  40. if (s[1]=='E') //repeta
  41. {nr=0; i=6;
  42. while (s[i]>='0' && s[i]<='9')
  43. {nr=nr*10+s[i]-'0'; i++; }
  44. }
  45. else //atribuire
  46. v[s[0]-'a']=expresie(s+2);
  47. }
  48. fout<<total<<'\n';
  49. }
  50.  
  51. void spatii(char * s)
  52. {int nrs=0, i;
  53. for (i=0; s[i]; i++)
  54. if (s[i]==' ') nrs++;
  55. else s[i-nrs]=s[i];
  56. s[i-nrs]=0;
  57. }
  58.  
  59. int expresie (char * s)
  60. {int i, valoare=0, t1, semn, n;
  61. //primul termen
  62. i=0; semn=1;
  63. while (1)
  64. {if (s[i]>='a' && s[0]<='z') {t1=v[s[i]-'a'];i++;}
  65. else //constanta
  66. {n=0;
  67. while (s[i]>='0' && s[i]<='9') {n=n*10+s[i]-'0'; i++;}
  68. if (s[i]>='a' && s[0]<='z')
  69. {t1=n*v[s[i]-'a'];i++;}
  70. else t1=n;
  71. }
  72. valoare+=semn*t1;
  73. if (s[i]=='+') {semn=1; i++;}
  74. else
  75. if (s[i]=='-') {semn=-1; i++;}
  76. else break;
  77. }
  78. return valoare;
  79. }
  80.  
  81. void doi()
  82. {int i=0, unde, lg, nr=1, j;
  83. while (fin.getline(p[i],LGMAX))
  84. {spatii(p[i]); i++; }
  85. lg=i; i=1;
  86. while (1)
  87. {
  88. if (p[i][1]=='C') //scrie
  89. {
  90. fout<<p[i][5]<<" = "<<v[p[i][5]-'a']<<'\n';
  91. i++;
  92. }
  93. else
  94. if (p[i][1]=='T') //stop
  95. {if (nr==1) {i++; if (i==lg) break;}
  96. else {nr--; i=unde;}
  97. }
  98. else
  99. if (p[i][1]=='E') //repeta
  100. {
  101. nr=0; j=6;
  102. while (p[i][j]>='0' && p[i][j]<='9')
  103. {
  104. nr=nr*10+p[i][j]-'0';
  105. j++;
  106. }
  107. i++;
  108. if (nr>0)
  109. unde=i;
  110. else
  111. {
  112. while (p[i][1]!='T') i++;
  113. i++;
  114. }
  115. }
  116. else //atribuire
  117. {
  118. v[p[i][0]-'a']=expresie(p[i]+2);
  119. i++;
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement