| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 359 人关注过本帖
标题:请教c++基础问题
取消只看楼主 加入收藏
danieljune
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-2-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
请教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
快速回复:请教c++基础问题
数据加载中...
 
   



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

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