[返回电脑前线首页]·[所有跟帖]·[ 回复本帖 ] ·[热门原创] ·[繁體閱讀]·[版主管理]
better code
送交者: mmax[☆一毛神帝☆] 于 2013-02-05 18:42 已读 14 次  

mmax的个人频道

回答: The completed c++ 由 mmax 于 2013-02-05 18:27

#include 
#include
#include www.6park.com

using namespace std; www.6park.com

int main()
{
string line = "";
string first = "";
string current = "";
string next = ""; www.6park.com

ifstream inputFile("input.txt"); www.6park.com

// Open and Read File www.6park.com

if ( inputFile.is_open() )
{
while (inputFile.good() )
{
getline (inputFile,line); www.6park.com

if ( line.length() > 0 )
{
cout << endl << "The current line from the file is => " << line << endl; www.6park.com

// For each line, we print out BEFORE or AFTER for each world www.6park.com

first = ""; www.6park.com

for ( int i=0; i < line.length(); i=i+1 )
{
// Building the first word www.6park.com

if ( first == "" && (int)toupper(line[i]) != 32 )
{
current = current + line[i];
}
else
if ( next == "" && (int)toupper(line[i]) == 32 )
{
// Print out the first word
first = line[0];
cout << current << " COMES FIRST";
} www.6park.com

// Building the second word
if ( first != "" && (int)toupper(line[i]) != 32 )
{
next = next + line[i];
} www.6park.com

if ( next != "" && (int)toupper(line[i]) == 32 )
{
if ( (int)toupper(next[0]) > (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " AFTER " << current;
} www.6park.com

if ( (int)toupper(next[0]) < (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " BEFORE " << current;
} www.6park.com

if ( (int)toupper(next[0]) == (int)toupper(current[0]) && next != "end" )
{
cout << endl << next << " SAME AS " << current;
} www.6park.com

current = next;
next = "";
} www.6park.com

} www.6park.com

if ( current == "end" || next == "end" )
{
first = "";
current = "";
next = ""; www.6park.com

cout << endl << endl;
} www.6park.com

}
} www.6park.com

cout << endl;
inputFile.close(); www.6park.com

}
else
{
cout << "Unable to open file";
} www.6park.com

return 0; www.6park.com

} www.6park.com

=== www.6park.com

> cp cpp01.cpp cpp01.cc
> g++ cpp01.cc
> ./a.out www.6park.com

The current line from the file is => Alpha Tango Bravo Epsilon Epsilon Delta end
Alpha COMES FIRST
Tango AFTER Alpha
Bravo BEFORE Tango
Epsilon AFTER Bravo
Epsilon SAME AS Epsilon
Delta BEFORE Epsilon www.6park.com


The current line from the file is => Alpha CTango Fravo Epsilon Epsilon Delta end
Alpha COMES FIRST
CTango AFTER Alpha
Fravo AFTER CTango
Epsilon BEFORE Fravo
Epsilon SAME AS Epsilon
Delta BEFORE Epsilon www.6park.com

www.6park.com

喜欢mmax朋友的这个贴子的话, 请点这里投票,“赞”助支持!
[举报反馈]·[ mmax的个人频道 ]·[-->>参与评论回复]·[用户前期主贴]·[手机扫描浏览分享]·[返回电脑前线首页]
帖子内容是网友自行贴上分享,如果您认为其中内容违规或者侵犯了您的权益,请与我们联系,我们核实后会第一时间删除。

所有跟帖:        ( 主贴楼主有权删除不文明回复,拉黑不受欢迎的用户 )


    用户名:密码:[--注册ID--]

    标 题:

    粗体 斜体 下划线 居中 插入图片插入图片 插入Flash插入Flash动画


         图片上传  Youtube代码器  预览辅助

    手机扫描进入,浏览分享更畅快!

    楼主本栏目热帖推荐:

    >>>>查看更多楼主社区动态...






    [ 留园条例 ] [ 广告服务 ] [ 联系我们 ] [ 个人帐户 ] [ 版主申请 ] [ Contact us ]