fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct symbolInfo{
  6. string name;
  7. string type;
  8. };
  9.  
  10. class symbolTable{
  11. vector<symbolInfo> vec[10];
  12. public:
  13. int hashfunction(string name)
  14. {
  15. int sum=0;
  16. int len= name.length();
  17. for(int i=0; i<len; i++)
  18. {
  19. sum+=name[i];
  20. }
  21. return sum%10;
  22. }
  23.  
  24. void insertt(string name, string type)
  25. {
  26. symbolInfo *ob= new symbolInfo(); ///not mandatory
  27. ob->name=name; ///not mandatory
  28. ob->type=type; ///not mandatory
  29. int hashval=hashfunction(name);
  30. vector<symbolInfo>v= vec[hashval];
  31. int flag=0;
  32.  
  33. for(auto findd=v.begin(); findd!=v.end(); findd++)
  34. {
  35. if(findd->name==name && findd->type==type)
  36. {
  37. flag=1;
  38. break;
  39. }
  40. }
  41. if(flag==0)
  42. {
  43. vec[hashval].push_back(*ob);
  44. cout << "Symbol inserted successfully" << endl;
  45. }
  46. else{
  47. cout << "Symbol already inserted" << endl;
  48. }
  49. }
  50.  
  51. void lookup(string name, string type)
  52. {
  53. symbolInfo *ob= new symbolInfo(); ///not mandatory
  54. ob->name=name; ///not mandatory
  55. ob->type=type; ///not mandatory
  56. int hashval=hashfunction(name);
  57. vector<symbolInfo>v= vec[hashval];
  58. int flag=0;
  59.  
  60. for(auto findd=v.begin(); findd!=v.end(); findd++)
  61. {
  62. if(findd->name==name && findd->type==type)
  63. {
  64. flag=1;
  65. break;
  66. }
  67. }
  68. if(flag==0)
  69. {
  70. cout << " Not Found" << endl;
  71. }
  72. else{
  73. cout << "Found" << endl;
  74. }
  75. }
  76.  
  77.  
  78. void deletee(string name,string type)
  79. {
  80. symbolInfo *ob= new symbolInfo(); ///not mandatory
  81. ob->name=name; ///not mandatory
  82. ob->type=type; ///not mandatory
  83. int hashval=hashfunction(name);
  84. vector<symbolInfo>v= vec[hashval];
  85. int flag=0;
  86. int cnt=0;
  87. for(auto findd=v.begin(); findd!=v.end(); findd++)
  88. { cnt++;
  89. if(findd->name==name && findd->type==type)
  90. {
  91. flag=1;
  92. break;
  93. }
  94. }
  95. if(flag==0)
  96. {
  97. cout << "symbol doesn't exist" << endl;
  98. }
  99. else{
  100. vec[hashval].erase(v.begin()+ cnt);
  101. cout << "Deleted" << endl;
  102. }
  103. }
  104.  
  105. void print()
  106. {
  107. vector<symbolInfo>::iterator it;
  108. for(int i=0; i<10; i++)
  109. {
  110. cout << i << "--->";
  111. for(auto it:vec[i])
  112. {
  113. cout <<"<" << it.name << "," << it.type << ">" ;
  114. }
  115. cout << endl;
  116. }
  117. }
  118. };
  119. int main()
  120. {
  121. symbolTable s;
  122. s.insertt("int","id");
  123. s.lookup("int","id");
  124. s.deletee("int","id");
  125. s.lookup("int","id");
  126. }
  127.  
  128.  
  129. /*
  130. 0->
  131. 1->int->main->=
  132. 2->4->*->/
  133. 3->div
  134. 4->mul->float
  135. 5->
  136. 6->
  137. 7->a->9
  138. 8->b
  139. 9->c->1
  140. */
  141.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Symbol inserted successfully
Found
Deleted
 Not Found