Chinaunix

标题: c++ 作业题 求解 [打印本页]

作者: zignake    时间: 2015-04-08 10:19
标题: c++ 作业题 求解
本帖最后由 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();
}


作者: hellioncu    时间: 2015-04-08 10:25
data1数据有误吧,根本没有232

BTW:自己一点代码都不写,我估计没人会帮你。

作者: zignake    时间: 2015-04-08 10:51
回复 2# hellioncu


   我知道怎么读取文件,就是不知道怎么筛选,求解惑,该怎么筛选
作者: hellioncu    时间: 2015-04-08 11:00
zignake 发表于 2015-04-08 10:51
回复 2# hellioncu


怎么按字符读呢?应该按行读,然后分析头部的数字,分成不同的list
作者: bruceteen    时间: 2015-04-08 11:13
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.

每一行是 { 消息号,序列号,文字 }
读入vector中,并根据{ 消息号,序列号 }排序
输出
作者: littledick    时间: 2015-04-09 14:37
2级map,连排序算法都可以省掉。
作者: VIP_fuck    时间: 2015-04-09 14:46
LZ这个问题并不困难,好好写写,能锻炼一下基础知识。一般情况下,别人没兴趣帮你写。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2