Y********6 发帖数: 28 | 1 刚开始学,第三堂课就遇到困难。。。实在弄不出来。。。
题目是这样的
* 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<
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<
}
else if (a < b)
{
cout<
}
else
{
cout<
}
c1 = c;
b = c1 - '0';
}
}
cin.get();
return 0;
}
显示第一行
A Comes First
A SAME AS A (这里A字母出现了两次,我不知道怎么跳过A,直接读第二个字母)
谢谢 | l********a 发帖数: 1154 | 2 又来一次
#include
#include
#include
using namespace std;
int main()
{
ifstream fin("test.txt");
string curWord, lastWord = "";
fin >> curWord;
while (curWord.compare("end")!=0)
{
if (lastWord.empty())
{
cout << curWord << " COMES FIRST" << endl;
}
else if (curWord>lastWord)
{
cout << curWord << " AFTER " << lastWord << endl;
}
else if (curWord
{
cout << curWord << " BEFORE " << lastWord << endl;
}
else
{
cout << curWord << " SAME AS " << lastWord << endl;
}
lastWord = curWord;
fin >> curWord;
}
fin.close();
return 0;
} | K****n 发帖数: 5970 | 3 牛,赞“lastWord”,呵呵
【在 l********a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 又来一次 : #include : #include : #include : using namespace std; : int main() : { : ifstream fin("test.txt"); : string curWord, lastWord = ""; : fin >> curWord;
|
|