textchek.cpp

来自「Thinking_in_C___2.rar」· C++ 代码 · 共 33 行

CPP
33
字号
//: C03:Textchek.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Counts words and lines in a text file.
// Ensures no line is wider than maxwidth.
#include <iostream>
#include <cstring> // strlen() & strtok()
using namespace std;

int main() {
  // const means "you can't change it":
  const int maxwidth = 64;
  int linecount = 0;
  int wordcount = 0;
  char buf[100], c, trash;
  while(cin.get(buf,100)) {
    cin.get(trash); // Discard terminator
    linecount++; // We just read a whole line
    if(strtok(buf," \t")) {
      wordcount++;  // Count the first word
      while(strtok(0," \t"))
        wordcount++; // Count the rest
    }
    if(strlen(buf) > maxwidth)
      cout << "line " << linecount 
        << "is too long." << endl;
  }
  cout << "Lines: " << linecount << endl;
  cout << "Words: " << wordcount << endl;
} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?