3

何が悪いのかについてあなたの意見が必要です。

自宅からBloodsheedを使用してプログラムを作成し、希望する結果を得ました。プログラムの目的は、ソースファイルからの行を表示して、特定の幅のテキストを出力することです。ソースファイルを1行ずつ分析することはできません。代わりに、charとstringwordを使用して読み取る必要があります。

次に、uniに行き、TextPadとBorlandを使用してプログラムを送信しました。出力は異なります。単語間のスペースと行末文字の一部は無視されます。何が起こっているのかわかりません。私はその事件に一日中費やしたが失敗した。コンパイラは、文字列を読み取るために演算子>>を異なる方法で使用しますか?最初のケースでは、スペースまたは行末文字の前で停止し、2番目のケースではそれらを破棄するように見えます。問題についての提案はありますか?

自宅での成功した出力は次のとおりです。

Max line length: 40

___Inglis_(1994)_describes_it_thus:

"For_output_of___floating-point_numbers,
the_format_strings_used_by_printf_may
include_%f_or_%e_(or_%g,_which_we_will
ignore).__These_have_much_in_common_with
%i:

____"A_minus_sign_indicates_left
justification,_a_plus_sign_indicates
that_the_converted_value_will_start_with
a_plus_sign_if_it_is_positive,_and_a
minimum_field_width_and/or_a_precision
may_be_specified.

大学で:

Max line length: 40

___Inglis(1994)describesitthus:

"Foroutputof__floating-pointnumbers,the
formatstringsusedbyprintfmayinclude%for
%e(or%g,whichwewillignore)._Thesehave
muchincommonwith%i:
____"Aminussignindicatesleft
justification,aplussignindicatesthatthe
convertedvaluewillstartwithaplussignifit
ispositive,andaminimumfieldwidthand/ora
precisionmaybespecified.

うまくいかない関数:

void Text::display(ofstream & out)
{ ifstream from(infileName.c_str());
  if (from.fail())
  { cerr<<infileName<<" not open\n";
    exit(1);
  }
  out<<"Max line length: "<<lineLength<<endl<<endl;
  string s, w;   //s stands for space, w for word
  char l;        //l stands for letter
  int c=0;       //c syands for count
  while(true)
  { if(static_cast<int>(w.length())>0)
    {  if(lineLength<w.length())
       { cerr <<"The line length "<<lineLength
             <<" is not long enough.\n"<<"The longuest word is "
             <<w<<" and has "<<w.length()
             <<" letters.\n";
         exit(1);
       }
       c+=w.length();
       out<<w;
       w.erase();
    }
    from.get(l);
    if (from.fail())
    {  out<<endl;
       break;
    }
    if (l=='\n')
    {  out<<l;
       s.erase();
       c=0;
       continue;
    }
    while (l==' ')
    {  s.push_back('_');
       c++;
       from.get(l);
    }
    if (l=='\n')
    {  out<<l;
       s.erase();
       c=0;
       continue;
    }
    from.putback(l);
    from>>w;
    c+=w.length();
    if (lineLength<c)
    {  out<<endl;
       s.erase();
       c=0;
    }
    else if(w.length()>0)
    {  out<<s<<w;
       w.erase();
       s.erase();
    }
  }
}
4

1 に答える 1

7

これは、異なる改行表現の兆候です。

「ホーム」では、改行は LF ('\n'または0x0A) です。

「uni」では、改行は CR+LF ('\r\n'または0x0D0A) です。

コードでは LF 改行のみが許可されます。


余談として...

string s, w;   //s stands for space, w for word
char l;        //l stands for letter
int c=0;       //c syands for count

C++ では、1 文字より長い識別子を使用できます。以下はより表現力があり、コメントの必要がなくなり、コードの保守がはるかに簡単になります。

std::string space, word;
char letter;
int count = 0;
于 2011-12-16T23:36:31.330 に答える