由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - 20个包子,求解c++基础问题 (转载)
相关主题
如何用c++程序读入文本文件中数据,求高手指教一下三道 Amazon Onsite Coding 题 (转载)
求助:C++里的fstream究竟该怎么用? (转载)[转载] 面试问题!
请问编译时怎么把自己的C/C++代码链接到一个库20个包子,求解c++基础问题
How to read binary(data) file generated by Fortran in C/C++?急! Python 如何从文件读取数据(整数) ~~在线等
再次求教用qsub和直接用./file的区别这题你肯定见过,但有准备coding么?
问个C++读入文件的问题C++ Q 99-102
C++里用Blas/Lapack的问题写了一个find kth number in 2 sorted arrays的code 请大牛看
c++, sort() 为啥显示 结果不对问题:从电话号码打出所有单词
相关话题的讨论汇总
话题: word话题: curword话题: epsilon话题: before话题: after
进入Computation版参与讨论
1 (共1页)
Y********6
发帖数: 28
1
【 以下文字转载自 CS 讨论区 】
发信人: Yolanda886 (Yolanda886), 信区: CS
标 题: 20个包子,求解c++基础问题
发信站: BBS 未名空间站 (Sun Feb 3 10:06:51 2013, 美东)
刚开始学,第三堂课就遇到困难。。。实在弄不出来。。。
题目是这样的
* 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
是c++不是c吧,那用string吧,别用char了
#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;
}
1 (共1页)
进入Computation版参与讨论
相关主题
问题:从电话号码打出所有单词再次求教用qsub和直接用./file的区别
背包问题问个C++读入文件的问题
two sigma 的online code test 的问题C++里用Blas/Lapack的问题
读取数据求教c++, sort() 为啥显示 结果不对
如何用c++程序读入文本文件中数据,求高手指教一下三道 Amazon Onsite Coding 题 (转载)
求助:C++里的fstream究竟该怎么用? (转载)[转载] 面试问题!
请问编译时怎么把自己的C/C++代码链接到一个库20个包子,求解c++基础问题
How to read binary(data) file generated by Fortran in C/C++?急! Python 如何从文件读取数据(整数) ~~在线等
相关话题的讨论汇总
话题: word话题: curword话题: epsilon话题: before话题: after