- 论坛徽章:
- 0
|
本帖最后由 zignake 于 2015-04-08 10:35 编辑
C++ 的作业题,做不出来了,请高手解答,谢谢了
作业定义:需要从一个文件里读出文字来,用linked list, 然后再筛选
1 txt文件 需要从一个文件里读出字符来
例 data1.txt
101:021:"Greetings, earthlings, I have come to rule you!"
232:013:Hello, Mother, I can't talk right now,
101:017:Here is a message from an important visitor:
232:015:I am being harangued by a little green thingy.
END
打印出的结果
Message 101
Here is a message from an important visitor:
"Greetings, earthlings, I have come to rule you!"
Message 232
Hello, Mother, I can't talk right now,
I am being harangued by a little green thingy.
代码写了一点:
#include <iostream>
#include <fstream>
#include <list>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
void printList(const list<char> &myList);
void fillList(list<char> &myList, const char *file);
int main(int argc, char *argv[])
{
if (argc != 2) {
cout << "Syntax : euro file\n";
return 0;
}
list<char> myList;
fillList(myList, argv[1]);
printList(myList);
return 0;
}
void printList(const list<char> &myList)
{
list<char>::const_iterator itr;
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
cout << *itr;
}
cout << '\n' << '\n';
}
void fillList(list<char> &myList, const char *file)
{
ifstream fin;
char c;
fin.open(file);
if (!fin) {
cout << "ERROR - unable to read " << file << "\n";
exit(0);
}
while (!fin.eof()) {
c = fin.get();
if(fin.good()) myList.push_back(c);
}
fin.close();
}
|
|