Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cctype> /* isalpha() */
- #include<cstdio>
- using namespace std;
- char alphabet[256] = "abcdefghijklmnopqrstuvwxyz";
- int countLetter(const char letter, FILE *f) {
- cout << "In countLetter..." << endl;
- /* Assumes it's a alpha letter and that there is a database */
- int pos = 1;
- cout << "Letter: " << letter << endl;
- for (int i = 0; i < strlen(alphabet); ++i) {
- if (alphabet[i] == letter) {
- pos += i;
- }
- }
- /* Store in Database */
- fprintf(f, "%c %d\n", letter, pos);
- return pos;
- }
- int findInDatabase(const char letter, FILE *f) {
- cout << "findInDatabase reached..." << endl;
- /* Assumes that there is a database */
- char line[256];
- char currentLetter;
- int currentValue = 0;
- int letterValue = 0;
- int numberOfLines = 0;
- while (!feof(f)) {
- //fseek(f, numberOfLines, SEEK_SET);
- /*
- printf("fgets(): \n");
- fgets(line, 256, f);
- cout << "The line:" << line << endl;
- */
- printf("fscanf(): ");
- fscanf(f, "%c %d\n", ¤tLetter, ¤tValue);
- cout << "L:" << currentLetter << " V:" << currentValue << endl;
- if (currentLetter == letter) {
- letterValue = currentValue;
- break;
- }
- numberOfLines++;
- }
- return letterValue;
- }
- int countWord(const char *w) {
- cout << "In countWord..." << endl;
- int total = 0;
- FILE *f = fopen("worddb.txt", "a+");
- int databaseResult = 0;
- cout << "After opening file..." << endl;
- for (int i = 0; i < strlen(w); ++i) {
- rewind(f);
- if (isalnum(w[i]) && f != NULL) {
- databaseResult = findInDatabase(w[i], f);
- if (databaseResult) {
- total += databaseResult;
- cout << "Total: " << total << endl;
- } else {
- total += countLetter(w[i], f);
- }
- }
- }
- fclose(f);
- return total;
- }
- int main() {
- char word[256];
- while (1) {
- cout << "Enter a word: ";
- cin >> word;
- if (strcmp(word, "quit") == 0) {
- break;
- }
- cout << word << " has a total of " << countWord(word) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement