免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: tyguaike
打印 上一主题 下一主题

[算法] c语言怎么实现分隔 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2014-08-25 21:40 |显示全部楼层
大家都很无聊啊。

论坛徽章:
0
2 [报告]
发表于 2014-08-25 21:55 |显示全部楼层
我也贴个C++的。
  1. #ifdef _MSC_VER
  2. #define _SCL_SECURE_NO_WARNINGS 1
  3. #endif

  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <boost/foreach.hpp>
  9. #include <boost/lexical_cast.hpp>
  10. #include <boost/algorithm/string.hpp>

  11. // 对应一行的数据
  12. typedef std::vector<int> ResultLineType;
  13. // 所有的行
  14. typedef std::vector<ResultLineType> ResultVectorType;

  15. // 打印结果
  16. void print_result(const ResultVectorType &result_vec)
  17. {
  18.     std::cout << "total line: " << result_vec.size() << std::endl;
  19.     // 一行一行的输出
  20.     for (size_t m = 0; m < result_vec.size(); ++m)
  21.     {
  22.         const ResultLineType &line_vec = result_vec[m];
  23.         // 输出行号,空行也输出行号
  24.         std::cout << "line" << m + 1 << ":\t";
  25.         if (line_vec.empty())
  26.         {
  27.             std::cout << std::endl;
  28.             continue;
  29.         }

  30.         // 遍历一行中的各个数值
  31.         for (size_t n = 0; n < line_vec.size(); ++n)
  32.         {
  33.             std::cout << line_vec[n];
  34.             if (n == line_vec.size() - 1)
  35.                 std::cout << std::endl;
  36.             else
  37.                 std::cout << " ";
  38.         }
  39.     }
  40. }

  41. // ifs 输入
  42. // result_vec 结果
  43. int parse_stream(std::ifstream &ifs, ResultVectorType &result_vec)
  44. {
  45.     // 行数,从1开始
  46.     int line_count = 0;

  47.     // 读到的一行字符串
  48.     std::string line;
  49.     // 字符串去除逗号和空白后的字符串结果集合
  50.     std::vector<std::string> split_vec;
  51.     // 从split_vec转换成的整数结果集合
  52.     ResultLineType result_line;

  53.     // 每次处理一行
  54.     for (;;)
  55.     {
  56.         std::getline(ifs, line);
  57.         if (!ifs.good())
  58.             break;

  59.         ++line_count;
  60.         split_vec.clear();
  61.         result_line.clear();

  62.         // 跳过空行
  63.         if (line.empty())
  64.         {
  65.             result_vec.push_back(result_line);
  66.             continue;
  67.         }

  68.         // 分割字符串
  69.         boost::split(split_vec, line, boost::is_any_of(" ,\r\n"), boost::token_compress_on);
  70.         try
  71.         {
  72.             // 转换成整数
  73.             BOOST_FOREACH (std::string &str, split_vec)
  74.             {
  75.                 int num = boost::lexical_cast<int>(str);
  76.                 result_line.push_back(num);
  77.             }
  78.             // 存入
  79.             result_vec.push_back(result_line);
  80.         }
  81.         catch (const boost::bad_lexical_cast &)
  82.         {
  83.             // 格式错误则中断处理
  84.             std::cerr << "error: file format error, line: " << line_count << std::endl;
  85.             return 3;
  86.         }
  87.     }

  88.     return 0;
  89. }

  90. int main(int argc, char **argv)
  91. {
  92.     if (argc != 2)
  93.     {
  94.         std::cerr << "usage: fl <filepath>" << std::endl;
  95.         return 1;
  96.     }

  97.     // 只读方式打开文件
  98.     std::ifstream ifs(argv[1], std::ios_base::in);
  99.     if (!ifs.good())
  100.     {
  101.         std::cerr << "error: can not read file." << std::endl;
  102.         return 2;
  103.     }

  104.     ResultVectorType result_vec;
  105.     // 处理
  106.     int ret = parse_stream(ifs, result_vec);
  107.     if (ret == 0)
  108.     {
  109.         // 打印结果
  110.         print_result(result_vec);
  111.     }
  112.     ifs.close();

  113.     return ret;
  114. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP