예제 #1
0
파일: main.cpp 프로젝트: quartarian/CS3
int main (int argc, char* argv[]) {
  if(argc == 1) {
    cout << "Please select a file." << endl;
  }
  else {
      //Open File
      ifstream fileInput  (argv[1]);
      
      if(!fileInput.is_open()) {
        cout << "Unable to open " << argv[1] << endl;
      }
      else {
        cout << argv[1] << " opened." << endl;
        string word;
        WordList wordList;
        while(fileInput >> word) {
          if( word[word.size()-1] == ',' ||
              word[word.size()-1] == '!' ||
              word[word.size()-1] == '.' ||
              word[word.size()-1] == ';' ||
              word[word.size()-1] == ':' ||
              word[word.size()-1] == '?' ||
              word[word.size()-1] == '-' ||
              word[word.size()-1] == '"' ||
              word[word.size()-1] == '+') 
          {
            word.erase(word.size()-1);
          }
          wordList.addWord(word);
          //cout << word << endl;
            
        }
        
        wordList.sortList();
        wordList.printList();

        // Close File
        fileInput.close();
      }
  }
  
  return 0;
}
예제 #2
0
int main(int argc,const char * argv[])
{

    // insert code here...

    //open file
    
    std::ifstream inputFile;
    inputFile.open(argv[1]);
    
    
    if(!inputFile.is_open())
    {
        cout << argv[1] << " was not open" << endl;
        return 1;
    }
    
    if (inputFile.is_open()) {
        
        cout << argv[1] << " is open!" << endl;
    }
    //declare the strings to use
    string  inputString;
    string buffer;
    
    //declare object needed
    WordList nameList;
    
    //get the stuff from the file
    getline(inputFile, inputString);
    //cout << "done reading in" << endl;
    
    //loop to check for the code being at the end of the file
    while (!inputFile.eof()) {

        //cout << "in the while loop " << end;
        //loops over the whole input string
        for(int i = 0; i < inputString.length(); ++i)
        {
            //checks for letters and characters
            for (int k = i; isalpha(inputString[k]) && inputString[k] != ','; ++k, ++i)
            {
                buffer += inputString[k];
            }
            
            //adds words
            //cout << " adding words " << endl;
            nameList.addWord(buffer);
            buffer.clear();
        }
        
        getline (inputFile, inputString);
    }
    
    //cout<<"out of while loop" << endl;
    
    //sorts the words in decending order
    buffer.clear();
    //cout << "sorting stuff " << endl;
    nameList.wordSort();
    //cout << "done sorting stuff " << endl;
    inputFile.close();
    //cout << "file closed " << endl;
    
    return 0;
}