Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Node {
  7. public:
  8. Node* next;
  9. string from;
  10. string to;
  11. int amount;
  12. };
  13.  
  14. class list {
  15. public:
  16. int len;
  17. Node* head;
  18.  
  19. list();
  20. void add(string from, string to, int amount);
  21. Node* find(string from, string to);
  22. };
  23.  
  24. list::list() {
  25. head = NULL;
  26. len = 0;
  27. }
  28.  
  29. void list::add(string from, string to, int amount) {
  30. Node* same = find(from, to);
  31. if (same == NULL) {
  32. Node* node = new Node();
  33. node->from = from;
  34. node->to = to;
  35. node->amount = amount;
  36. node->next = this->head;
  37. this->head = node;
  38. }
  39. else
  40. same->amount += amount;
  41. }
  42.  
  43. Node* list::find(string from, string to) {
  44. if (head == NULL) return NULL;
  45. Node* node = head;
  46. while (node->from != from || node->to != to) {
  47. node = node->next;
  48. if (node == NULL)
  49. break;
  50. }
  51. return node;
  52. }
  53.  
  54. int hashh(string name) {
  55. int hashed = 1;
  56. for (int i = 0; i < name.length(); i+=2) {
  57. if (i + 1 < name.length()) {
  58. hashed += ((int)name[i] - (int)('_')) * ((int)name[i + 1] - (int)('_'));
  59. }
  60. }
  61. return hashed;
  62. }
  63.  
  64. int main()
  65. {
  66. char chr;
  67. string from, to;
  68. int amount, ind;
  69. list* tab[15000];
  70. for (int i = 0; i < 15000; i++) {
  71. tab[i] = new list();
  72. }
  73. while (cin >> chr) {
  74. if (chr == '+') {
  75. cin >> from >> to >> amount;
  76. ind = hashh(from);
  77. tab[ind]->add(from, to, amount);
  78. }
  79. else if (chr == '?') {
  80. cin >> from >> to;
  81. ind = hashh(from);
  82. Node* found = tab[ind]->find(from, to);
  83. if (found == NULL) cout << "NULL poszedł" << endl;
  84. else cout << found->amount << endl;
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement