Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <string>
- #include <iostream>
- struct key
- {
- const char *word;
- int count;
- } keytab[] =
- {
- "auto", 0,
- "break", 0,
- "void", 0,
- "volatile", 0,
- "while", 0
- };
- int binsearch(char *, struct key *, int);
- using namespace std;
- int main()
- {
- int n;
- int NKEYS = sizeof(keytab) / sizeof(key);
- char str[1000];
- char *tokens;
- const char *separ = " !?.,;/*-+\n\t";
- setlocale(LC_ALL, "rus");
- cout << "Введите что-нибудь\n";
- cin.getline(str, 1000);
- char* word = strtok_s(str, separ, &tokens); // начинаем выделять слова
- while (word != NULL)
- {
- if ((n = binsearch(word, keytab, NKEYS)) >= 0)
- keytab[n].count++;
- word = strtok_s(NULL, separ, &tokens);
- }
- for (n = 0; n < NKEYS; n++)
- if (keytab[n].count > 0)
- cout << keytab[n].count << " " << keytab[n].word << "\n";
- }
- int binsearch(char *word, struct key keytab[], int n)
- {
- int cond;
- int low, high, mid;
- low = 0;
- high = n - 1;
- while (low <= high)
- {
- mid = (low + high) / 2;
- if ((cond = strcmp(word, keytab[mid].word)) < 0)
- high = mid - 1;
- else if (cond > 0)
- low = mid + 1;
- else
- return mid;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement