Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include "pch.h"
  2. #include <string>
  3. #include <iostream>
  4. struct key
  5. {
  6. const char *word;
  7. int count;
  8. } keytab[] =
  9. {
  10. "auto", 0,
  11. "break", 0,
  12. "void", 0,
  13. "volatile", 0,
  14. "while", 0
  15. };
  16. int binsearch(char *, struct key *, int);
  17. using namespace std;
  18.  
  19. int main()
  20. {
  21. int n;
  22. int NKEYS = sizeof(keytab) / sizeof(key);
  23. char str[1000];
  24. char *tokens;
  25. const char *separ = " !?.,;/*-+\n\t";
  26. setlocale(LC_ALL, "rus");
  27. cout << "Введите что-нибудь\n";
  28. cin.getline(str, 1000);
  29. char* word = strtok_s(str, separ, &tokens); // начинаем выделять слова
  30. while (word != NULL)
  31. {
  32. if ((n = binsearch(word, keytab, NKEYS)) >= 0)
  33. keytab[n].count++;
  34. word = strtok_s(NULL, separ, &tokens);
  35. }
  36. for (n = 0; n < NKEYS; n++)
  37. if (keytab[n].count > 0)
  38. cout << keytab[n].count << " " << keytab[n].word << "\n";
  39. }
  40. int binsearch(char *word, struct key keytab[], int n)
  41. {
  42. int cond;
  43. int low, high, mid;
  44. low = 0;
  45. high = n - 1;
  46. while (low <= high)
  47. {
  48. mid = (low + high) / 2;
  49. if ((cond = strcmp(word, keytab[mid].word)) < 0)
  50. high = mid - 1;
  51. else if (cond > 0)
  52. low = mid + 1;
  53. else
  54. return mid;
  55. }
  56. return -1;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement