| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 359 人关注过本帖
标题:请教c++基础问题
只看楼主 加入收藏
danieljune
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-2-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
请教c++基础问题
刚开始学,第三堂课就遇到困难。。。实在弄不出来。。。


题目是这样的
* Write a program that reads from a file called input.txt until the file
contains the word end.
* For each word you encounter, you will determine if the word is
alphabetically before or after the previous encountered word.
* For a word that is alphabetically after the previous word, write the word
to the screen and then say AFTER. For words that are before the previous,
write the word BEFORE. For the first word, write COMES FIRST. For words that
are the same, write SAME AS.

Sample input:
Alpha Tango Bravo Epsilon Epsilon Delta end

The program should output:

Alpha COMES FIRST
Tango AFTER Alpha
Bravo BEFORE Tango
Epsilon AFTER Bravo
Epsilon SAME AS Epsilon
Delta BEFORE Epsilon

If you want, you may use char instead of string for no penalty. In this case
, use the character 'q' instead of the word "end". Please note that you are
doing this in your readme file.




我选了简单的,只写char

假设input文件里一列字母A B D K L E H H L X Q

我自己乱弄,可是显示的结果还是不对

int main()
{char word;
  ifstream myfile ("input.txt");
  myfile >> word;
  cout<<word<< " COMES FIRST"<<'\n';
  
fstream in;
char c, c1;
c1 = word;
int a, b;
b = word -'0';
in.open("input.txt",ios::in);
while(!in.eof())
{  
   in>>c;
   a = c - '0';
   
   if (c != 'q')
   
   {
   if (a > b)
   {
       cout<<c<< " AFTER "<<c1<<'\n';
   }
   else if (a < b)
   {
       cout<<c<< " BEFORE "<<c1<<'\n';
   }
   else
   {
       cout<<c<< " SAME AS "<<c1<<'\n';
   }
   c1 = c;
   b = c1 - '0';
   }
   
}


cin.get();
   
return 0;

}




显示第一行
A Comes First
A SAME AS A   (这里A字母出现了两次,我不知道怎么跳过A,直接读第二个字母)




谢谢
搜索更多相关主题的帖子: word first determine previous before 
2013-02-03 23:11
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:10 
这个好办,百度下词法分析器,借鉴下它那个处理思路就会了,我会写,可没网手机不好写也不方便…

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-02-04 01:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:10 
没有一丝难度的题目
程序代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream file( "input.txt" );
    for( string word, pre; file>>word; pre=word )
    {
        if( word == "end" )
            break;
        else if( pre.empty() )
            cout << word << " COMES FIRST\n";
        else
        {
            int c = ( pre );
            if( c > 0 )
                cout << word << " AFTER " << pre << '\n';
            else if( c < 0 )
                cout << word << " BEFORE " << pre << '\n';
            else
                cout << word << " SAME AS " << pre << '\n';
        }
    }

    return 0;
}

2013-02-04 09:00
快速回复:请教c++基础问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028919 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved