Advertisement
Guest User

Assignment_1

a guest
Feb 29th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #define MOD 101
  7. #define SIZE 101
  8. using namespace std;
  9.  
  10. class Symbolinfo
  11. {
  12. public:
  13. string identifier;
  14. string name;
  15. Symbolinfo(string identifier, string name)
  16. {
  17. this->identifier = identifier;
  18. this->name = name;
  19. }
  20.  
  21. };
  22. class SymbolTable
  23. {
  24. public:
  25. vector<Symbolinfo> table[SIZE];
  26. };
  27. int hashFunction(string identifier)
  28. {
  29. int r = 4 + 3;
  30. int len = identifier.length();
  31. if(len%2 == 0){
  32. char c1, c2;
  33. c1 = identifier[0];
  34. c2 = identifier[1];
  35. int sub = abs(c1 - c2);
  36. int ascii = 0;
  37. int j = 0;
  38. for(int i = 0; i < sub; i++, j += 2){
  39. ascii += identifier[j];
  40. if(j >= len){
  41. j = 0;
  42. }
  43. }
  44. ascii = ascii * 2 * 2 * 2 * 2 * 2 * 2 * 2;
  45. ascii = ascii % MOD;
  46. return ascii;
  47.  
  48. }
  49. else {
  50. char c1, c2;
  51. c1 = identifier[len - 1];
  52. c2 = identifier[len - 2];
  53. int sub = abs(c1 - c2);
  54. int ascii = 0;
  55. int j = 1;
  56. for(int i = 0; i < sub; i++, j += 2){
  57. ascii += identifier[j];
  58. if(j >= len){
  59. j = 1;
  60. }
  61. }
  62. ascii = ascii * 2 * 2 * 2 * 2 * 2 * 2 * 2;
  63. ascii = ascii % MOD;
  64. return ascii;
  65. }
  66. }
  67. int main()
  68. {
  69. fstream f;
  70. SymbolTable Table;
  71. f.open("code.txt");
  72. while(f){
  73. string cmd, id, na;
  74. f>>cmd;
  75.  
  76. if(cmd == "I"){
  77.  
  78. f>>id>>na;
  79. Symbolinfo sym = Symbolinfo(id, na);
  80.  
  81. int hValue = hashFunction(id);
  82. bool flag = true;
  83. for(int i = 0; i < Table.table[hValue].size(); i++){
  84. if(sym.identifier == Table.table[hValue][i].identifier){
  85. cout<<"Identifier exists"<<endl;
  86. flag = false;
  87. }
  88. }
  89. if(flag == true){
  90. Table.table[hValue].push_back(sym);
  91. cout<< "<" << na << "," << id << "> Inserted at position(" << hValue << "," << Table.table[hValue].size() << ")" << endl;
  92. }
  93. }
  94.  
  95. else if(cmd == "P")
  96. {
  97. int i = 0;
  98. while(i < SIZE)
  99. {
  100. cout<<i<<" ->";
  101. for(int j = 0; j < Table.table[i].size(); j++){
  102. cout<<"<"<<Table.table[i][j].identifier<<","<<Table.table[i][j].name<<">";
  103. }
  104. cout<<endl;
  105. i++;
  106.  
  107. }
  108. }
  109. else if(cmd == "D")
  110. {
  111. string l;
  112. f>>l;
  113. int hValue = hashFunction(l);
  114.  
  115.  
  116. for(int j =0; j < Table.table[hValue].size(); j++){
  117. if(Table.table[hValue][j].identifier == l){
  118. Table.table[hValue].erase(Table.table[hValue].begin()+j);
  119. }
  120. cout<<"Deleted"<<endl;
  121. }
  122.  
  123. }
  124. else if(cmd == "L")
  125. {
  126. string l;
  127. f>>l;
  128. int hValue = hashFunction(l);
  129. for(int j =0; j < Table.table[hValue].size(); j++){
  130. if(Table.table[hValue][j].identifier == l){
  131. cout<<"Found at "<<hValue<<","<<j<<endl;
  132. }
  133. }
  134.  
  135. }
  136.  
  137. }
  138. f.close();
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement