免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1552 | 回复: 6
打印 上一主题 下一主题

[C++] c++ 作业题 求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-08 10:19 |只看该作者 |倒序浏览
本帖最后由 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();
}

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2015-04-08 10:25 |只看该作者
data1数据有误吧,根本没有232

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

论坛徽章:
0
3 [报告]
发表于 2015-04-08 10:51 |只看该作者
回复 2# hellioncu


   我知道怎么读取文件,就是不知道怎么筛选,求解惑,该怎么筛选

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2015-04-08 11:00 |只看该作者
zignake 发表于 2015-04-08 10:51
回复 2# hellioncu


怎么按字符读呢?应该按行读,然后分析头部的数字,分成不同的list

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
5 [报告]
发表于 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中,并根据{ 消息号,序列号 }排序
输出

论坛徽章:
6
技术图书徽章
日期:2013-11-13 11:11:27子鼠
日期:2014-02-20 17:54:13处女座
日期:2014-06-16 17:43:33午马
日期:2014-08-08 09:11:17未羊
日期:2014-08-10 11:57:072015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2015-04-09 14:37 |只看该作者
2级map,连排序算法都可以省掉。

论坛徽章:
12
2015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之同曦
日期:2017-03-17 19:13:162016科比退役纪念章
日期:2016-11-07 08:28:12luobin
日期:2016-06-17 17:46:36wusuopu
日期:2016-06-17 17:43:4515-16赛季CBA联赛之福建
日期:2016-01-14 12:49:22程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:002015年亚洲杯之科威特
日期:2015-03-24 14:21:272015年迎新春徽章
日期:2015-03-04 09:57:092016科比退役纪念章
日期:2018-04-10 16:20:18
7 [报告]
发表于 2015-04-09 14:46 |只看该作者
LZ这个问题并不困难,好好写写,能锻炼一下基础知识。一般情况下,别人没兴趣帮你写。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP