Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <list>
  4. #include <string.h>
  5. #include <fstream>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9. #define MOD 101
  10. #define SIZE 101
  11.  
  12. int hash_func(string s)
  13. {
  14. int n = s.length();
  15.  
  16. // declaring character array
  17. char name[n+1];
  18.  
  19. // copying the contents of the
  20. // string to char array
  21. strcpy(name, s.c_str());
  22. int n1=2,n2=8;
  23. double sum=0;
  24. for(int i=0;i<strlen(name);i+=2)
  25. {
  26. sum=sum+(int)name[i]/pow(2,n1);
  27. }
  28. sum=sum*pow(2,n2);
  29. int d=int(sum)%MOD;
  30. return d;
  31.  
  32. }
  33.  
  34. class symbolInfo
  35. {
  36. public:
  37. string name;
  38. string type;
  39. };
  40.  
  41. class symbolTable
  42. {
  43. public:
  44. list <symbolInfo> stable[SIZE];
  45. };
  46.  
  47. int main()
  48. {
  49. // define as per your requirement
  50.  
  51. ifstream thisfile("input.txt");
  52. char c;
  53. string name, type;
  54. symbolTable symtab;
  55. symbolInfo syminf;
  56. while(!thisfile.eof()){
  57. thisfile>>c;
  58. if(c=='I')
  59. {
  60.  
  61. thisfile>>name>>type;
  62. syminf.name=name;
  63. syminf.type=type;
  64. cout<<c<<" "<<name<<" "<<type<<endl;
  65. int f=hash_func(syminf.name);
  66. cout<<f<<endl;
  67.  
  68. symtab.stable[f].push_back(syminf);
  69.  
  70.  
  71.  
  72. cout<<"<"<<" "<<name<<","<<type<<"> inserted at position ("<<f<<")"<<endl;
  73. }
  74. if(c=='L')
  75. {
  76. thisfile>>name;
  77. cout<<c<<" "<<name<<endl;
  78. }
  79. if(c=='P')
  80. {
  81. cout<<c<<endl;
  82. list <symbolInfo> :: iterator it;
  83. for(int i=0;i<101;i++)
  84. {
  85. cout<<i<<" ------>";
  86. for(it = symtab.stable[i].begin(); it != symtab.stable[i].end(); ++it)
  87. cout <<" <"<<(*it).name<<","<<(*it).type<<">";
  88. cout << '\n';
  89.  
  90. }
  91.  
  92. }
  93. if(c=='D')
  94. {
  95. thisfile>>name;
  96. int f=hash_func(name);
  97. //symtab.stable[f].remove();
  98. cout<<c<<" "<<name<<" "<<endl;
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement