Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct elem
  6. {
  7. int value;
  8. unsigned int k;
  9. int balance;
  10. struct elem * parent;
  11. struct elem * left;
  12. struct elem * right;
  13. };
  14.  
  15. struct elem * Descend(struct elem * tree, unsigned int k)
  16. {
  17. struct elem * x =tree;
  18. while(x!= NULL && x->k != k)
  19. {
  20. if(k < x->k)
  21. {
  22. x = x->left;
  23. }
  24. else
  25. {
  26. x = x->right;
  27. }
  28. }
  29. return x;
  30.  
  31. }
  32.  
  33. int Lookup(struct elem * tree, unsigned int k)
  34. {
  35. struct elem * x;
  36. x = Descend(tree, k);
  37. if(x == NULL || k != x->k)
  38. {
  39. return -1;
  40. }
  41. else
  42. {
  43. return x->value;
  44. }
  45. }
  46.  
  47. struct elem * ReplaceNode(struct elem* tree, struct elem* x, struct elem* y)
  48. {
  49. if(tree == x)
  50. {
  51. tree = y;
  52. if(y != NULL)
  53. {
  54. y->parent = NULL;
  55. }
  56. }
  57. else
  58. {
  59. struct elem * p = x->parent;
  60. if(y != NULL)
  61. {
  62. y->parent = p;
  63. }
  64. if(p->left == x)
  65. {
  66. p->left = y;
  67. }
  68. else
  69. {
  70. p->right = y;
  71. }
  72. }
  73. return tree;
  74. }
  75.  
  76. struct elem * RotateLeft(struct elem * tree, struct elem * x)
  77. {
  78. struct elem * y = x->right;
  79. if(y == NULL)
  80. {
  81. printf("ERROR IN ROTATE LEFT");
  82. return tree;
  83. }
  84. tree = ReplaceNode(tree, x, y);
  85. struct elem * b = y->left;
  86. if(b != NULL)
  87. {
  88. b->parent = x;
  89. }
  90. x->right = b;
  91. x->parent = y;
  92. y->left = x;
  93. x->balance--;
  94. if(y->balance > 0)
  95. {
  96. x->balance -= y->balance;
  97. }
  98. y->balance--;
  99. if(x->balance < 0)
  100. {
  101. y->balance += x->balance;
  102. }
  103. return tree;
  104. }
  105.  
  106. struct elem * RotateRight(struct elem * tree, struct elem * x)
  107. {
  108. struct elem * y = x->left;
  109. if(y == NULL)
  110. {
  111. printf("ERROR IN ROTATE RIGHT");
  112. return tree;
  113. }
  114. tree = ReplaceNode(tree, x, y);
  115. struct elem * b = y->right;
  116. if(b != NULL)
  117. {
  118. b->parent = x;
  119. }
  120. x->left = b;
  121. x->parent = y;
  122. y->right = x;
  123. x->balance++;
  124. if(y->balance < 0)
  125. {
  126. x->balance -= y->balance;
  127. }
  128. y->balance++;
  129. if(x->balance > 0)
  130. {
  131. y->balance += x->balance;
  132. }
  133. return tree;
  134. }
  135.  
  136.  
  137. struct elem * Insert(struct elem ** tree, unsigned int k, unsigned int v)
  138. {
  139. struct elem * y = (struct elem *)malloc(sizeof(struct elem));
  140. struct elem * x;
  141. y->k = k;
  142. y->value = v;
  143. y->parent = NULL;
  144. y->left = NULL;
  145. y->right = NULL;
  146. if((*tree) == NULL)
  147. {
  148. (*tree) = y;
  149. }
  150. else
  151. {
  152. x = (*tree);
  153. while(1)
  154. {
  155. if(k < x->k)
  156. {
  157. if(x->left == NULL)
  158. {
  159. x->left = y;
  160. y->parent = x;
  161. break;
  162. }
  163. x = x->left;
  164. }
  165. else
  166. {
  167. if(x->right == NULL)
  168. {
  169. x->right = y;
  170. y->parent = x;
  171. break;
  172. }
  173. x = x->right;
  174. }
  175.  
  176. }
  177. }
  178. return y;
  179. }
  180.  
  181. struct elem * InsertAVL(struct elem * tree, unsigned int k, unsigned int v)
  182. {
  183. struct elem * a = Insert(&tree, k, v);
  184. struct elem * x;
  185. a->balance = 0;
  186. while(1)
  187. {
  188. x = a->parent;
  189. if(x == NULL)
  190. {
  191. break;
  192. }
  193. if(a == x->left)
  194. {
  195. x->balance--;
  196. if(x->balance == 0)
  197. {
  198. break;
  199. }
  200. if(x->balance == -2)
  201. {
  202. if(a->balance == 1)
  203. {
  204. tree = RotateLeft(tree, a);
  205. }
  206. tree = RotateRight(tree, x);
  207. break;
  208. }
  209. }
  210. else
  211. {
  212. x->balance++;
  213. if(x->balance == 0)
  214. {
  215. break;
  216. }
  217. if(x->balance == 2)
  218. {
  219. if(a->balance == -1)
  220. {
  221. tree = RotateRight(tree, a);
  222. }
  223. tree = RotateLeft(tree, x);
  224. break;
  225. }
  226. }
  227. a = x;
  228. }
  229. return tree;
  230. }
  231.  
  232. void DeleteAll(struct elem* tree)
  233. {
  234. struct elem *temp;
  235. if (tree != NULL)
  236. {
  237. DeleteAll(tree->left);
  238. temp = tree->right;
  239. free(tree);
  240. DeleteAll(temp);
  241. }
  242. }
  243.  
  244. unsigned int HashLy(const char * str)
  245. {
  246.  
  247. unsigned int hash = 0;
  248.  
  249. for(; *str; str++)
  250. {
  251. hash = (hash * 1664525) + (unsigned char)(*str) + 1013904223;
  252. }
  253.  
  254. return hash;
  255.  
  256. }
  257.  
  258. int SpecTag(char a)
  259. {
  260. switch (a)
  261. {
  262. case '+':
  263. {
  264. return 0;
  265. }
  266. case '-':
  267. {
  268. return 1;
  269. }
  270. case '*':
  271. {
  272. return 2;
  273. }
  274. case '/':
  275. {
  276. return 3;
  277. }
  278. case '(':
  279. {
  280. return 4;
  281. }
  282. case ')':
  283. {
  284. return 5;
  285. }
  286. default:
  287. {
  288. return -1;
  289. }
  290. }
  291. }
  292.  
  293.  
  294. int main(void)
  295. {
  296. int n;
  297. scanf("%d ", &n);
  298. char * arr = (char *)malloc((n+1)*sizeof(char));
  299. struct elem * tree = NULL;
  300. int i = 0;
  301. int j = 0;
  302. int tempSPEC = -1;
  303. unsigned int resultIDENT = -1;
  304. int number = 0;
  305. unsigned int k = 0;
  306. char * word = malloc(15*sizeof(char));
  307. memset(word, '\0', 15);
  308. int countIDENT = 0;
  309. gets(arr);
  310. int len = strlen(arr);
  311. for(i = 0; i < len;i++)
  312. {
  313. if(arr[i] != ' ')
  314. {
  315. tempSPEC = SpecTag(arr[i]);
  316. if(tempSPEC != -1)
  317. {
  318. if(word[0] != '\0')
  319. {
  320. if(((int)word[0]) < 48 || ((int)word[0]) > 57)
  321. {
  322. k = HashLy(word);
  323. resultIDENT = Lookup(tree, k);
  324. if(resultIDENT != -1)
  325. {
  326. printf("IDENT %d \n", resultIDENT);
  327. }
  328. else
  329. {
  330. printf("IDENT %d \n", countIDENT);
  331. tree = InsertAVL(tree, k, countIDENT);
  332. countIDENT++;
  333. }
  334.  
  335. }
  336. else
  337. {
  338. number = atoi(word);
  339. printf("CONST %d \n", number);
  340. }
  341. memset(word, '\0', 15);
  342. j = 0;
  343. }
  344. printf("SPEC %d \n", tempSPEC);
  345. }
  346. else
  347. {
  348. word[j] = arr[i];
  349. j++;
  350. }
  351. tempSPEC = -1;
  352. }
  353. else
  354. {
  355. if(word[0] != '\0')
  356. {
  357. if(((int)word[0]) < 48 || ((int)word[0]) > 57)
  358. {
  359. k = HashLy(word);
  360. resultIDENT = Lookup(tree, k);
  361. if(resultIDENT != -1)
  362. {
  363. printf("IDENT %d \n", resultIDENT);
  364. }
  365. else
  366. {
  367. printf("IDENT %d \n", countIDENT);
  368. tree = InsertAVL(tree, k, countIDENT);
  369. countIDENT++;
  370. }
  371.  
  372. }
  373. else
  374. {
  375. number = atoi(word);
  376. printf("CONST %d \n", number);
  377. }
  378. memset(word, '\0', 15);
  379. j = 0;
  380. }
  381. }
  382. }
  383. if(word[0] != '\0')
  384. {
  385. if(((int)word[0]) < 48 || ((int)word[0]) > 57)
  386. {
  387. k = HashLy(word);
  388. resultIDENT = Lookup(tree, k);
  389. if(resultIDENT != -1)
  390. {
  391. printf("IDENT %d \n", resultIDENT);
  392. }
  393. else
  394. {
  395. printf("IDENT %d \n", countIDENT);
  396. tree = InsertAVL(tree, k, countIDENT);
  397. countIDENT++;
  398. }
  399.  
  400. }
  401. else
  402. {
  403. number = atoi(word);
  404. printf("CONST %d \n", number);
  405. }
  406. }
  407. free(arr);
  408. free(word);
  409. DeleteAll(tree);
  410. return 0;
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement