Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <cstdlib>
- #define MOD 101
- #define SIZE 101
- using namespace std;
- class Symbolinfo
- {
- public:
- string identifier;
- string name;
- Symbolinfo(string identifier, string name)
- {
- this->identifier = identifier;
- this->name = name;
- }
- };
- class SymbolTable
- {
- public:
- vector<Symbolinfo> table[SIZE];
- };
- int hashFunction(string identifier)
- {
- int r = 4 + 3;
- int len = identifier.length();
- if(len%2 == 0){
- char c1, c2;
- c1 = identifier[0];
- c2 = identifier[1];
- int sub = abs(c1 - c2);
- int ascii = 0;
- int j = 0;
- for(int i = 0; i < sub; i++, j += 2){
- ascii += identifier[j];
- if(j >= len){
- j = 0;
- }
- }
- ascii = ascii * 2 * 2 * 2 * 2 * 2 * 2 * 2;
- ascii = ascii % MOD;
- return ascii;
- }
- else {
- char c1, c2;
- c1 = identifier[len - 1];
- c2 = identifier[len - 2];
- int sub = abs(c1 - c2);
- int ascii = 0;
- int j = 1;
- for(int i = 0; i < sub; i++, j += 2){
- ascii += identifier[j];
- if(j >= len){
- j = 1;
- }
- }
- ascii = ascii * 2 * 2 * 2 * 2 * 2 * 2 * 2;
- ascii = ascii % MOD;
- return ascii;
- }
- }
- int main()
- {
- fstream f;
- SymbolTable Table;
- f.open("code.txt");
- while(f){
- string cmd, id, na;
- f>>cmd;
- if(cmd == "I"){
- f>>id>>na;
- Symbolinfo sym = Symbolinfo(id, na);
- int hValue = hashFunction(id);
- bool flag = true;
- for(int i = 0; i < Table.table[hValue].size(); i++){
- if(sym.identifier == Table.table[hValue][i].identifier){
- cout<<"Identifier exists"<<endl;
- flag = false;
- }
- }
- if(flag == true){
- Table.table[hValue].push_back(sym);
- cout<< "<" << na << "," << id << "> Inserted at position(" << hValue << "," << Table.table[hValue].size() << ")" << endl;
- }
- }
- else if(cmd == "P")
- {
- int i = 0;
- while(i < SIZE)
- {
- cout<<i<<" ->";
- for(int j = 0; j < Table.table[i].size(); j++){
- cout<<"<"<<Table.table[i][j].identifier<<","<<Table.table[i][j].name<<">";
- }
- cout<<endl;
- i++;
- }
- }
- else if(cmd == "D")
- {
- string l;
- f>>l;
- int hValue = hashFunction(l);
- for(int j =0; j < Table.table[hValue].size(); j++){
- if(Table.table[hValue][j].identifier == l){
- Table.table[hValue].erase(Table.table[hValue].begin()+j);
- }
- cout<<"Deleted"<<endl;
- }
- }
- else if(cmd == "L")
- {
- string l;
- f>>l;
- int hValue = hashFunction(l);
- for(int j =0; j < Table.table[hValue].size(); j++){
- if(Table.table[hValue][j].identifier == l){
- cout<<"Found at "<<hValue<<","<<j<<endl;
- }
- }
- }
- }
- f.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement