-2

わかりましたので、100 の選択された単語を含むテキスト ファイルを読み、次に別の「テキスト」ファイルを読み込んで、それらの 100 の選択された単語のうちのいくつが「テキスト」ファイルに表示され、どれが最も長いかを比較する宿題をしています。最短の単語と、それらの選択された単語がテキスト ファイルに含まれる回数。私は完全な助けが必要です。これは私がこれまでに得たものです。ちなみに、選択したファイルをまだ入力していないので、ifsステートメントが不完全です

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;


int main ()
{
int fileNumber;
string Words [100];
string wordCounts [100];
string Frequencies [100];
string shortest = "abcdefghijklmnopqrstuvwxyz";
string longest = "";
int totalWordCount = 0;
ifstream inData;
int I=0;

inData.open("SelectWords.txt"); 

while ( !inData.eof () ) {
    inData >> Words [I];

}
int irow;
while ( irow=0, irow<=100, irow++) {
cout<<Words[I]<<endl;
}
cout<<"Choose a file you want to open"<<endl;
cout<<"1= A Modest Proposal"<<endl;
cout<<"2=Apology" <<endl;
cout<<"3=The Call of the Wild"<<endl;
cout<<"4=The Adventures of Tom Sawyer"<<endl;
 cin>> fileNumber;
 while ((fileNumber<1) || (fileNumber>4))

    {
cout<<"Error please try again"<<endl;
cout<<"Choose a file you want to open"<<endl;
cout<<"1 - A Modest Proposal"<<endl;
cout<<"2 - Apology" <<endl;
cout<<"3 - The Call of the Wild"<<endl;
cout<<"4 - The Adventures of Tom Sawyer"<<endl;
cin>> fileNumber;
 }

  if (fileNumber==1)
 {
 }
 if (fileNumber==2)
 {
 }
 if (fileNumber==3)
 {
 }
 if (fileNumber==4)
 {
 }



system ("pause");
return 0;
}
4

1 に答える 1

2

私はこのようなことをします:

std::string filename;
switch (filenumber)
{
  case 1:
    filename = "modest_proposal.txt";
    break;
  case 2:
    filename = "apology.txt";
    break;
  //...
}

std::ifstream target_file(filename.c_str());
if (!target_file)
{
  std::cerr << "Error opening file " << filename << endl;
  return EXIT_FAILURE;
}

std::string word;
while (target_file >> word)
{
  // Search word list
}

switchファイル名を決定するためだけに を使用していることに注目してください。アルゴリズムの残りの部分は、ファイル名に関係なく同じです。

編集 1: 提案

  1. std::map<word, count>2 つの別個の配列の代わりに 使用します。
  2. コードを複製する代わりに、メニュー用のテキスト変数を 1 つ作成し、その変数を 2 回出力します。
于 2013-07-14T18:51:29.943 に答える