示例#1
0
文件: cwords.c 项目: wijagels/CS220
int main(int argc, char **argv) {

    char inputMsg[MAX_MSG_LENGTH] = "";
    int stri=0;
    char * words[MAX_MSG_LENGTH];
    char * unqWords[MAX_MSG_LENGTH];
    int unqCount[MAX_MSG_LENGTH];
    char in;
    while(1) {
        in=getchar();
        if (feof(stdin)) break;
        inputMsg[stri++]=in;
    }
    inputMsg[stri]=0;
    assert(stri<MAX_MSG_LENGTH);

    int nwords=listWords(inputMsg,words);
    int nunq=uniqueWords(words,nwords,unqWords,unqCount);
    printf("Found a total of %d words %d unique in the input string\n",
            nwords,nunq);

    // int max=maxCount(unqCount,nunq);
    // printf("At least one word is used %d times\n",max);
    //	int i;
    //	for(i=max; i>2; i--) {
    //		printWordsUsed(i,nunq,unqWords,unqCount);
    //}

    return 0;
}
// For each tweet, find a number of unique words.
// As we go through the tweets, update the median using getCurrentMedian function.
void Tweets::getMedians(const char *outputFile) {
  this->validateInputStream(this->inputStream);

  std::cout << "Writing to file " << outputFile << "..." << std::endl;
  this->outputStream2.open(outputFile);
  this->validateOutputStream(this->outputStream2);
  this->outputStream2.setf(std::ios::fixed, std::ios::floatfield);
  this->outputStream2.precision(2);

  float median = 0.f; // current median

  char tweet[TWEET_SIZE];
  char *word;

  while (this->inputStream.getline(tweet, TWEET_SIZE)) {
    // break words into an array
    std::string words[TWEET_SIZE];
    int count = 0;

    word = strtok(tweet, " ");
    while(word != NULL) {
      std::string wordStr(word);
      words[count++] = wordStr;
      word = strtok(NULL, " ");
    }

    // put words into a set to remove duplicates
    std::set<std::string> uniqueWords(words, words+count);
    size_t numUniqueWords = uniqueWords.size();

    // update median
    median = this->getCurrentMedian(numUniqueWords, median);

    // write to file
    this->outputStream2 << median << '\n';
  }

  this->outputStream2.close();
}