Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;,我正在制作一个程序,计算一个字母在文件中出现的次数_C++_File - Fatal编程技术网

C++ c++;,我正在制作一个程序,计算一个字母在文件中出现的次数

C++ c++;,我正在制作一个程序,计算一个字母在文件中出现的次数,c++,file,C++,File,应该说明的是,我们也在检查涉及该字母的最长单词以及最短单词 应该说明的是,我是一名学生,我的代码有一些错误 警告:缺乏评论也应该指出,由于老师给出的模糊指示,我不知道自己在做什么 我的代码;word.cpp #include<iostream> #include<fstream> #include<string> #include<cstdlib> #include<iomanip> #include"myStrCharFunc.h"

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

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

警告:缺乏评论也应该指出,由于老师给出的模糊指示,我不知道自己在做什么 我的代码;word.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;
     }

}
myToUpper(字符)“[-fpPermissive]字符myToUpper(字符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

我不知道你是否能够在你的项目/任务中使用STL结构(如果不能,我很抱歉有一个糟糕的指导老师),因此如果不能,那么这将有利于任何想用一种/理智的/方法来解决这个问题的访问者,因为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';
    }
}
如果我们阅读的是《标准》中的内容,就会简单得多。这个/可以/可以适应用于文件,但是对于特别大的文件,它可能比我上面使用的方法慢

#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';
    }
}
#包括
#包括
#包括
#包括
int main(){
std::map char\u map;
std::字符串行;
while(std::getline(std::cin,line))
for(字符c:行)
如果(c!='')
++字符映射[char(std::toupper(c))];
用于(自动常量和对:字符映射){

std::cout假设您还没有覆盖C中的容器++

因此,您需要计算您读取的文件中字母的频率

现在唯一需要的是一个数组,其中可以表示所有字母

int freq[MAXCHARS] = {0};  // e.g. 26
假设我们使用的是ASCII字符,它们从ASCII值65开始,即“A”

在C++数组中,p>从索引0开始,因此需要一个方法将字符映射到数组“FRQ”

通过从字符中减去'A'来计算索引,然后将得到'A'的索引0,'B'的索引1等

文本可能是混合大小写的,因此使用函数
toupper()

现在,您需要做的是从文件中一次读取一个字符,并确定它是否是一个字母 如果是,则更新阵列

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

const int MAXCHARS = 26;

int main(/*int argc, char* argv[]*/)
{
  using namespace std;

  int freq[MAXCHARS] = {0};

  std::ifstream f("yourfile");
  do
  {
    auto ch = f.get();
    if (std::isalpha(ch))
    {
      ch = std::toupper(ch);
      auto index = ch - 'A';
      freq[index]++;
    }
  }
  while (!f.eof());

  for (int i = 0; i < MAXCHARS; ++i)
  {
    std::cout << char(65+i) << ":" << freq[i] << '\n';
  }
  std::cout << std::endl;    

  return 0;
}
#包括
#包括
#包括
#包括
常量int MAXCHARS=26;
int main(/*int argc,char*argv[]*/)
{
使用名称空间std;
int freq[MAXCHARS]={0};
std::ifstream f(“您的文件”);
做
{
auto ch=f.get();
if(标准:艾萨帕(ch))
{
ch=std::toupper(ch);
自动索引=ch-‘A’;
频率[指数]+;
}
}
而(!f.eof());
对于(int i=0;istd::请试着问一个专门的问题,而不是转储所有不属于头文件的函数。不确定
myToUpper
函数应该做什么,将整个字符串转换为大写,或者只转换为现在的一个字符,但然后是哪个字符?你永远不会将字符传递给它。我在111类中,函数是pu正如我之前所说的,我不知道我在做什么,但我知道它只应该转换每个单词的第一个字符。“对于任何其他文本编码都不安全”?您正在对cout使用控制台输出编码,对toupper使用std locale,对文件进行编码,对文本使用编译执行字符集编码,但没有一个可能是ASCII编码/字符集。如果它们都兼容相关数据,则可以使用。我想您可以说它需要单字节编码,因为正在一次分析一个字节的文件。@TomBlodget我已经更新了帖子,使其在这方面更加准确。
#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';
    }
}
int freq[MAXCHARS] = {0};  // e.g. 26
ch = std::toupper(ch);  // make sure it is upper case
auto index = ch - 'A';  // calculate the index in the array
freq[index]++;          // add one for the character found
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

const int MAXCHARS = 26;

int main(/*int argc, char* argv[]*/)
{
  using namespace std;

  int freq[MAXCHARS] = {0};

  std::ifstream f("yourfile");
  do
  {
    auto ch = f.get();
    if (std::isalpha(ch))
    {
      ch = std::toupper(ch);
      auto index = ch - 'A';
      freq[index]++;
    }
  }
  while (!f.eof());

  for (int i = 0; i < MAXCHARS; ++i)
  {
    std::cout << char(65+i) << ":" << freq[i] << '\n';
  }
  std::cout << std::endl;    

  return 0;
}