首页 > 解决方案 > c++,我正在制作一个程序来计算一个字母在文件中出现的次数

问题描述

应该说明的是,我们也在检查涉及该字母的最长单词以及最短单词。

应该说我是学生,我的代码有一些错误

警告:缺乏评论还应该指出,由于老师对我的代码给出的模糊指示,我不知道自己在做什么;单词.cpp

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<iomanip>
#include"myStrCharFunc.h"
using namespace std;

const int SIZE=26; //Size of the area, one for each letter
const int MAX=30; //Size of the c string that will store word from the input file

typedef char cstr[MAX];

struct let
 {
   int count;//nummber of words that start with thr letter
   int shortest;
   int longest;
 };

void initializeArray(let ar[]);
void readData(let ar[]);
void processWord(cstr word, let ar[]);

int main()
 {
   //cstr s="Hi";
   let ar[SIZE];

   return 0;
 }

void initializeArray(let ar[])
 {
   for(int i=0;i<SIZE; i++)
    {
      ar[i].count=0;
      ar[i].shortest=9999;
      ar[i].longest=0;
    }
 }

void readData(let ar[])
 {
  ifstream fin;
  fin.open("project2.dat");
  if(!fin)
   {
     cout<<"Your input file doesn't exist"<<endl;
   }
  else
   {

     //let temp;
     //fin>>temp.count;
     //fin>>temp.shortest;
     //fin>>temp.longest;
     cstr word=" ";
     fin>>word;
     while(fin)
      {
        processWord(word, ar);
        fin>>word;
      }
   }
 fin.close();
}

void processWord(cstr word, let ar[])
 {

  for(int i=0; i < SIZE; i++)
   {
    ar[i].count++;
    myToUpper(word);
    int fev = myStrLen(word);
    if(ar[i].longest < fev)
      {
        ar[i].longest = fev;
      }
    if(ar[i].shortest > fev)
     }
       ar[i].shortest=fev;
     }

}

与此相关的另一个程序;myStrCharFunc.h

 //myToUpper('a')-->'A'
//myToUpper('A')-->'A'
char myToUpper(char b)
 {

   if('a'<= b && b <= 'z')
    {
      b-=('a'-'A');
    }
   return b;
 }

int myStrLen(const char cstr[])
 {
  int i=0;
  for(i; cstr[i] != '\0'; i++)
    ;
  return i;

 }

myToUpper 假设接受一个字符或 C-String 并使用 ASCII 表将其设为大写字母

同时 myStrLen 应该记录它所接受的每个单词的长度。

我写的代码的错误是:

word.cpp:在函数'void processWord(char *,let *)'中:word.cpp:77:21:错误:从'char *'到'char'的无效转换[-fpermissive]

   myToUpper(word);
                 ^ In file included from word.cpp:6:0:  myStrCharFunc.h:3:6: error:   initializing argument 1 of ‘char

myToUpper(char)' [-fpermissive] char myToUpper(char b)

/需要说明的是,我在这里写的代码在按下 CTRL+K 并剪切粘贴到给定区域后都没有正确格式化,所以都是手工完成的/

还有我们正在使用的文件;project2.dat 仅包含以下内容:

Economists attributed the jump in tourism to a strengthening
economy high consumer confidence and pent up demand among
Americans who put off travel during the recession Also
a growing middle class in China is pushing visits from that
country experts said

The state persistent drought although weighing heavily on
residents does not appear to bother travelers interested in
sunshine shopping and sightseeing

Visitors to Los Angeles are not going to care if it gets a
little brown around the edges quipped Dean Runyan a
consultant who studied the tourism economy for Visit
California report released Tuesday

Still Runyan cautioned the drought could affect tourism
in rural counties where fishing and boating are popular pastimes

Some experts worry that a bigger issue is the US dollar
strength compared to other currencies

标签: c++file

解决方案


我不知道您是否可以将 STL 构造用于您的项目/作业(如果没有,我很抱歉有一个糟糕的讲师),所以如果没有,那么这将有利于任何想要的访问者/sane/ 方法来解决这个问题,因为少量的 STL 使得这非常简单:

#include<fstream>
#include<iostream>
#include<map>
#include<string>
#include<cctype>

int main() {
    //Container for the frequency data
    std::map<char, size_t> char_map;

    //Read the whole file into memory, for efficient reads
    std::string file_contents;
    std::ifstream file("project2.dat");
    size_t length;
    file.seekg(0, std::ios::end);
    length = file.tellg();
    file.seekg(0, std::ios::beg);
    file_contents.resize(length);
    file.read(file_contents.data(), length);
    //If you don't have a C++17 compiler, use this instead:
    //file.read(&file_contents.front(), length);

    //Iterate over the file, which we've stored in memory.
    for(char c : file_contents)
        //Omit if we're including spaces
        if(c != ' ')
            ++char_map[char(std::toupper(c))];

    //Display the results
    for(auto const& pair : char_map) {
        std::cout << "Occurrences of \'" << pair.first << "\': " << pair.second << '\n';
    }
}

注意:这种方法只对单字节文本编码是安全的。任何多字节文本编码都不安全。

该程序的输出将是文件中每个字符的频率,按字典顺序排列,不区分大小写。

//Input:
I watched as nobody attended my fourth grade birthday party.

//Output:
Occurrences of '.': 1
Occurrences of 'A': 6
Occurrences of 'B': 2
Occurrences of 'C': 1
Occurrences of 'D': 6
Occurrences of 'E': 4
Occurrences of 'F': 1
Occurrences of 'G': 1
Occurrences of 'H': 3
Occurrences of 'I': 2
Occurrences of 'M': 1
Occurrences of 'N': 2
Occurrences of 'O': 3
Occurrences of 'P': 1
Occurrences of 'R': 4
Occurrences of 'S': 1
Occurrences of 'T': 6
Occurrences of 'U': 1
Occurrences of 'W': 1
Occurrences of 'Y': 4

如果我们从标准输入中读取它会简单得多。这个 /can/ 适合与文件一起使用,但对于特别大的文件,它可能会比我上面使用的方法慢。

#include<iostream>
#include<map>
#include<string>
#include<cctype>

int main() {
    std::map<char, size_t> char_map;
    std::string line;

    while(std::getline(std::cin, line))
        for(char c : line)
            if(c != ' ')
                ++char_map[char(std::toupper(c))];

    for(auto const& pair : char_map) {
        std::cout << "Occurrences of \'" << pair.first << "\': " << pair.second << '\n';
    }
}

推荐阅读