一個用來計算TXT檔案中各種型別數目的小小小程式。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int SIZE = 100;
int main(int argc, char** argv) {
cout << "Enter file for analysis.";
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
char filename[SIZE];
cin.getline(filename,SIZE);
ifstream inFile;
inFile.open(filename);
if(!inFile.is_open())
cout << "Error404!"<<endl ;
inFile >> ch;
while(inFile.good()) // test for sentinel
{
if(isalpha(ch)) // is it an alphabetic character?
chars++;
else if(isspace(ch)) // is it a whitespace character?
whitespace++;
else if(isdigit(ch)) // is it a digit?
digits++;
else if(ispunct(ch)) // is it punctuation?
punct++;
else
others++;
inFile >> ch; // get next character
}
if(inFile.eof())
cout << "Read over!"<<endl;
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digits << " digits, "
<< punct << " punctuations, "
<< others << " others.\n";
inFile.close();
return 0;
}
uj5u.com熱心網友回復:
有可能被跳過了,流運算子有跳過空白字符的功能的uj5u.com熱心網友回復:
蟹蟹蟹蟹解決了!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98861.html
標籤:C++ 語言
上一篇:c語言鏈表插入問題
下一篇:按空格和換行符讀取檔案
