use*_*867 3 c++ textpad turbo-c++ operator-keyword
我需要你对错误的看法.
在家里,我用Bloodsheed编写了一个程序并得到了想要的结果.该程序的目的是显示源文件中的行以输出具有特定宽度的文本.源文件无法逐行分析.相反,它应该使用char和string word来读取.
然后我去了uni使用TextPad和Borland提交我的程序:输出是不同的:单词之间的空格和一些行尾字符被忽略.我不明白发生了什么.我花了整整一天的时间没有成功.编译器使用不同的运算符>>来读取字符串吗?看起来在第一种情况下,它在第二种情况下在空格或行尾字符之前停止它丢弃它们.你有关于这个问题的建议吗?
在家里成功的输出是:
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.
Run Code Online (Sandbox Code Playgroud)
在大学:
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.
Run Code Online (Sandbox Code Playgroud)
出错的功能:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是不同换行表示的症状.
在"home",你的换行符是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
Run Code Online (Sandbox Code Playgroud)
C++允许标识符长于单个字符.以下内容更具表现力,无需注释,并且可以更轻松地维护代码.
std::string space, word;
char letter;
int count = 0;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |