Advertisement
Underdogs

Untitled

Mar 8th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n,id;
  5.  
  6. int hashIt(string ss)
  7. {
  8.  
  9. int ret = 0;
  10.  
  11. for(int i = 0; i < ss.size(); i++){
  12. ret = (ret+i*i)%n;
  13.  
  14. }
  15.  
  16. return ret;
  17. }
  18.  
  19. class symbolInfo{
  20. private:
  21. string Name,Type;
  22. symbolInfo *nxt;
  23. public:
  24. symbolInfo() {}
  25. symbolInfo(string str1,string str2){
  26. Name = str1;
  27. Type = str2;
  28. }
  29.  
  30. void setNxt(symbolInfo *p){
  31. nxt = p;
  32. }
  33.  
  34. symbolInfo *getNxt(){
  35. return nxt;
  36. }
  37.  
  38. string getName(){
  39. return Name;
  40. }
  41. string getType(){
  42. return Type;
  43. }
  44.  
  45. };
  46.  
  47.  
  48. class scopeTable{
  49. public:
  50. int cnt;
  51. scopeTable *scopetable;
  52. symbolInfo *head;
  53. //scopeTable *nxt;
  54. //scopeTable *parentScope;
  55. scopeTable(){
  56.  
  57. for(int i = 0; i < n; i++) {scopetable[i].head = NULL;cout << "no" << endl;}
  58.  
  59. }
  60. ~scopeTable(){
  61. for(int i = 0; i < n; i++){
  62. symbolInfo *cur;
  63. cur = scopetable[i].head;
  64. while(cur != NULL){
  65. symbolInfo *tmp = cur;
  66. cur = cur->getNxt();
  67. delete tmp;
  68. }
  69. }
  70. delete scopetable;
  71. }
  72. bool Insert(int hashInd,string name,string type){
  73. cout << "yes yes" << endl;
  74. symbolInfo *newInfo = new symbolInfo(name,type);
  75. if(scopetable[hashInd].head == NULL){
  76. cout << "yes" << endl;
  77. scopetable[hashInd].head = newInfo;
  78. scopetable[hashInd].head->setNxt(NULL);
  79. scopetable[hashInd].cnt = 1;
  80. }
  81. else{
  82. cout << "no" << endl;
  83. newInfo->setNxt(scopetable[hashInd].head);
  84. scopetable[hashInd].head = newInfo;
  85. scopetable[hashInd].cnt++;
  86. }
  87. return 1;
  88. }
  89.  
  90. /*bool Remove(string name){
  91. int Ind = hashIt[name];
  92. int hashInd = Ind % n;
  93. if(scopetable[hashInd].head == NULL){
  94. return 0;
  95. }
  96. symbolInfo *cur = scopetable[hashInd].head;
  97. symbolInfo *pre = scopetable[hashInd].head;
  98.  
  99. while(cur != NULL){
  100. if(cur->getName() == name){
  101. if(cur == scopetable[hashInd].head){
  102. scopetable[hashInd].head = cur->getNxt();
  103. }
  104. else{
  105. pre->setNxt(cur->getNxt());
  106. }
  107. scopetable[hashInd].cnt--;
  108. delete cur;
  109. return 1;
  110. }
  111. pre = cur;
  112. cur = cur->getNxt();
  113. }
  114.  
  115. return 0;
  116. }
  117.  
  118. symbolInfo *LookUp(string name){
  119. int Ind = hashIt[name];
  120. int hashInd = Ind % n;
  121. if(scopetable[hashInd].head == NULL){
  122. return NULL;
  123. }
  124. symbolInfo *cur = scopetable[hashInd].head;
  125. while(cur != NULL){
  126. if(cur->getName() == name){
  127. return cur;
  128. }
  129. cur = cur->getNxt();
  130. }
  131. return NULL;
  132. }*/
  133.  
  134. void Print(){
  135. symbolInfo *cur;
  136. for(int i = 0; i < n; i++){
  137. cout << i << "-->";
  138. if(scopetable[i].head == NULL){
  139. //cout << "naaaa " << endl;
  140. cout << endl;
  141. continue;
  142. }
  143. //cout << " bah ";
  144. cur = scopetable[i].head;
  145. while(cur != NULL){
  146. cout << " < " << cur->getName() << " " << cur->getType() << " > ";
  147. cur = cur->getNxt();
  148. }
  149. cout << endl;
  150. }
  151. }
  152.  
  153. };
  154.  
  155. /*class symbolTable{
  156.  
  157. public:
  158. scopeTable *cur;
  159.  
  160. symbolTable() {
  161. cout << "ok1" << endl;
  162. cur->parentScope = NULL;
  163. }
  164. void EnterScope(){
  165.  
  166. scopeTable *newScope = new scopeTable;
  167. cout << "ok2" << endl;
  168. if(cur->parentScope == NULL){
  169. cout << "ok2" << endl;
  170. cur->parentScope = newScope;
  171. (cur->parentScope)->nxt = NULL;
  172. }
  173. else{
  174. newScope->nxt = cur->parentScope;
  175. cur->parentScope = newScope;
  176. }
  177.  
  178.  
  179. }
  180. scopeTable *ExitTable(scopeTable *now){
  181. scopeTable *tmp = now;
  182. now = now -> pre;
  183. cur = now;
  184. delete tmp;
  185. now->nxt = NULL;
  186. if(cur == NULL) return NULL;
  187. else return cur;
  188.  
  189. }
  190.  
  191. bool InsertIt(string s1,string s2){
  192. int key = hashFunc(s1);
  193. bool is = (curr->matha)->Insert(key,s1,s2);
  194. if(!is) return 0;
  195. else return 1;
  196. }
  197.  
  198. void CurrentScopeTable(){
  199. (curr->matha)->Print();
  200. }
  201.  
  202. };*/
  203.  
  204.  
  205. int main()
  206. {
  207.  
  208. cin >> n;
  209.  
  210. scopeTable *sc = new scopeTable();
  211. int cases,tt = 0;
  212. cin >> cases;
  213. while(++tt <= cases){
  214. int ya;
  215. cin >> ya;
  216. if(ya == 1){
  217. string aa,bb;
  218. cin >> aa >> bb;
  219. int val = hashIt(aa);
  220. sc->Insert(val,aa,bb);
  221.  
  222. }
  223. else{
  224. sc->Print();
  225. }
  226. }
  227.  
  228. delete sc;
  229.  
  230. return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement