⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 letters.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>#include <fstream>          // for ifstream#include <cstdlib>          // for exit()#include <cctype>           // for tolower()#include <climits>          // for CHAR_MAX#include <string>#include <iomanip>using namespace std;#include "prompt.h"#include "tvector.h"// count # occurrences of all characters in a file// written: 8/5/94, Owen Astrachan, modified 5/1/99void Print(const tvector<int> & counts, int total);void Count(istream & input, tvector<int> & counts, int & total);int main(){    int totalAlph = 0;        string filename = PromptString("enter name of input file: ");	ifstream input(filename.c_str());    if (input.fail() )    {   cout << "could not open file " << filename << endl;        exit(1);    }    tvector<int> charCounts(CHAR_MAX+1,0);    // all initialized to 0     Count(input,charCounts,totalAlph);    Print(charCounts,totalAlph);        return 0;}void Count(istream & input, tvector<int> & counts, int & total)// precondition: input open for reading//               counts[k] == 0, 0 <= k < CHAR_MAX// postcondition: counts[k] = # occurrences of character k//                total = # alphabetic characters     {    char ch;    while (input.get(ch))               // read a character    {   if (isalpha(ch))                // is alphabetic (a-z)?        {   total++;        }        ch = tolower(ch);               // convert to lower case        counts[ch]++;                   // count all characters    }    }void Print(const tvector<int> & counts, int total)// precondition: total = total of all entries in counts['a']..counts['z']// postcondition: all values of counts from 'a' to 'z' printed     {     const int MIDALPH = 13;    cout.setf(ios::fixed);    // print 1 decimal place     cout.precision(1);        char k;        for(k = 'a'; k <= 'm'; k++)    {   cout << k << setw(7) << counts[k] << " ";        cout << setw(4) << 100 * double(counts[k])/total << "% \t\t";        cout << char(k+MIDALPH) << setw(7) << counts[k+MIDALPH] << " ";        cout << setw(4) << 100 * double(counts[k+MIDALPH])/total << "%" << endl;    }}

⌨️ 快捷键说明

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