请教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,直接读第二个字母)
谢谢