All pastes #948618 Raw Edit

asdf

public text v1 · immutable
·published 2008-03-19 09:26 UTC
rendered paste body
/**********************************************
***********************************************
*******    Comp125 - Assignment 1       *******  
*******   Student Number:               *******
*******     Name:                       ******* 
***********************************************
**********************************************/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;

/* - global variables - */
const int DICT_SIZE = 250000;
const int ALPHABET_SIZE = 26;
/* - - - - - - - - - - - */

/* - function prototypes - */
int readDict(string readin[]);
void parseDict(string a[], int n[], int m);
int menu();
int checkString(string m);
string toLowerCase(string n);
/* - - - - - - - - - - - - */

int main () {
    /* - variable initialisation - */
    string target;
    string input;
    int method;
    string dictionary[DICT_SIZE];
    int FirstLetterChange[ALPHABET_SIZE] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    int size;
    int location;
    /* - - - - - - - - - - - - - */
    
    size = readDict(dictionary);
    if (size == -1) {
       cout << "Read Failed: Check location for dict.txt" << endl;
       system("pause");
       return EXIT_FAILURE;
    }
    parseDict(dictionary, FirstLetterChange, size);
    
    /* - enable looped menu - */       
    while(true) {
             switch(menu()) {
                            case 1:  
                                 cout << "Enter Word" << endl;
                                 cin >> input;
                                 if (checkString(input) == -1) {
                                                         cout << "Inappropriate Entry" << endl;
                                 }
                                 else {
                                      target = toLowerCase(input);  
                                      cout << "target word is " << target << endl;                              
                                      }
                                 
                                 break;
                            case 2: 
                                 cout << "Enter Word" << endl;
                                 cin >> input;                                 
                                 if (checkString(input) == -1) {
                                                         cout << "Inappropriate Entry" << endl;
                                 }
                                 else {
                                      target = toLowerCase(input);
                                      cout << "target word is " << target << endl;   
                                      }
                                 
                                 break;
                            case 3: 
                                 system("pause");
                                 return EXIT_SUCCESS;
                            default: 
                                     cout << "Invalid Choice" << endl;
                            }
    }
    /* - - - - - - - - - - */
}    

/* - functions - */
int readDict(string readin[]) {
//precondition - none
//postcondition - reads dict.txt into an array 
    int loop = 0;
    string line; 
    ifstream myfile ("dict.txt"); 
    if (myfile.is_open()) 
    {
        while (! myfile.eof() ) 
        {
            getline (myfile,line); 
            readin[loop] = line;
            loop++;
        }
        myfile.close(); 
        return loop;
    }
    else {
         return -1;
    }
}

void parseDict(string a[], int n[], int m) {
//precondition - dictionary array
//postcondition - ascertains where each letter starts in dictionary array
    char temp = 'a';
    int num1 = 1;
    string str;
    int i = 0;
    while (i < m) {
        str = a[i];
        if (str.at(0) != temp) {
           n[num1] = i;
           num1++;
           temp++;
        }
        i++;
    }
}

int menu() {
//precondition - none
//postcondition - users entry point
     int choice;
     cout << "\tCrossword Helper " << endl << endl;
     cout << " 1 : Check if word is in dictionary " << endl;
     cout << " 2 : Find words in dictionary which match pattern " << endl;
     cout << " 3 : Quit " << endl;
     cin >> choice;
     cin.clear();
     return choice;
}

int checkString(string m) {
//precondition - none
//postcondition - checks to see whether string m contains any incorrect chars
    for (int i = 0; i < m.length(); i++) { 
        if (isalpha(m.at(i))) {
        }
        else if (m.at(i) != '?') {
             return -1;
        }
    }
    return 0;
}

string toLowerCase(string n) {
//precondition - none
//postcondition - switches string n into lower case
       for (int l = 0; l < n.length(); l++) {
           if (isupper(n.at(l))) {
                                 n.at(l) = tolower(n.at(l));
           }
       }
       return n;
}
/* - - - - - - - */
                     
/* - END OF FILE - */