由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
CS版 - 20个包子,求解c++基础问题
相关主题
Question about author name order请教一道作业题, 感觉老师给的答案有问题
【请教】fscanf 和 fstream 哪一个更好? (转载)About testing of uniform distribution
问一个c++问题Tango.me扩招
请教c++一个程序bug,谢谢前辈们20个包子,求解c++基础问题 (转载)
c++ type conversion 方面的问题急! Python 如何从文件读取数据(整数) ~~在线等
C++牛人能不能现身解答小妹一个问题啊?标题要长长长长长~~~~~~~~~~~~~~~~~~这题你肯定见过,但有准备coding么?
改变 c string 的一个问题C++ Q 99-102
问个概率问题写了一个find kth number in 2 sorted arrays的code 请大牛看
相关话题的讨论汇总
话题: word话题: curword话题: epsilon话题: before话题: after
进入CS版参与讨论
1 (共1页)
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 的大作中提到】
: 又来一次
: #include
: #include
: #include
: using namespace std;
: int main()
: {
: ifstream fin("test.txt");
: string curWord, lastWord = "";
: fin >> curWord;

1 (共1页)
进入CS版参与讨论
相关主题
写了一个find kth number in 2 sorted arrays的code 请大牛看c++ type conversion 方面的问题
问题:从电话号码打出所有单词C++牛人能不能现身解答小妹一个问题啊?标题要长长长长长~~~~~~~~~~~~~~~~~~
背包问题改变 c string 的一个问题
two sigma 的online code test 的问题问个概率问题
Question about author name order请教一道作业题, 感觉老师给的答案有问题
【请教】fscanf 和 fstream 哪一个更好? (转载)About testing of uniform distribution
问一个c++问题Tango.me扩招
请教c++一个程序bug,谢谢前辈们20个包子,求解c++基础问题 (转载)
相关话题的讨论汇总
话题: word话题: curword话题: epsilon话题: before话题: after