logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> Asp/Asp.Net教程 -> .net开发:如何为程式码加上行号
回复
标题:.net开发:如何为程式码加上行号收藏 编辑 删除 楼主 | 上一篇 下一篇
点点头
头像
等级:社区游侠
权限:普通用户
积分:47
金钱:3369
声望:91
经验:91
发帖数:814
注册:2007年1月16日
资料 短消息2008-8-11 11:36:43
Abstract

        若需要将程式码放进word交报告或做文件时,或许我们会想将程式码加上行号方便讲解,如同博客园显示程式码那样,我们该如何做呢?

        Introduction

        使用环境:Visual C++ 9.0 / Visual Studio 2008

        一段C++的小程式,可以帮程式码加上行号后输出。

        map_code_line.cpp / C++
         /*
         (C) OOMusou 2008 http://oomusou.cnblogs.com

         Filename    : map_code_line.cpp
         Compiler    : Visual C++ 9.0 / Visual Studio 2008
         Description : Demo how to add line number for code
         Release     : 07/18/2008 1.0
         */
         #include <iostream>
         #include <fstream>
         #include <string>
         #include <map>
         #include <algorithm>
         using namespace std;
         ifstream infile("map_code_line.cpp");
         ofstream outfile("map_code_line_r.cpp");
         struct print_map {
           void operator() (pair<int, string> p) {
             cout    << p.first << " " << p.second << endl;
             outfile << p.first << " " << p.second << endl;
           }
         };
         int main() {
           map<int, string> lines;
           string line;
           int line_num = 1;
           while(getline(infile, line))
           lines[line_num++] = line;
           infile.close();
           for_each(lines.begin(), lines.end(), print_map());
           outfile.close();
         }

执行结果

签名

2008-8-11 11:36:43 顶部
点点头
头像
等级:社区游侠
权限:普通用户
积分:47
金钱:3369
声望:91
经验:91
发帖数:814
注册:2007年1月16日
  资料  消息 短消息编辑 删除 引用 第2楼

         /*
         (C) OOMusou 2008 http://oomusou.cnblogs.com

         Filename    : map_code_line.cpp
         Compiler    : Visual C++ 9.0 / Visual Studio 2008
         Description : Demo how to add line number for code
         Release     : 07/18/2008 1.0
         */
         #include <iostream>
         #include <fstream>
         #include <string>
         #include <map>
         #include <algorithm>
         using namespace std;
         ifstream infile("map_code_line.cpp");
         ofstream outfile("map_code_line_r.cpp");
         struct print_map {
           void operator() (pair<int, string> p) {
             cout    << p.first << " " << p.second << endl;
             outfile << p.first << " " << p.second << endl;
           }
         };
         int main() {
           map<int, string> lines;
           string line;
           int line_num = 1;
           while(getline(infile, line))
           lines[line_num++] = line;
           infile.close();
           for_each(lines.begin(), lines.end(), print_map());
           outfile.close();
         }

        32行

         while(getline(infile, line))
          lines[line_num++] = line;

        是整个程式的关键:使用map,key存放行号,value存放每一行的程式码。而且随着每一行程式码的读入,自动增加行号。

        37行

         for_each(lines.begin(), lines.end(), print_map());

        将map内容印出,因为map无法配合copy(),只好退而求其次使用for_each()与functor。

        20行

         struct print_map {
          void operator() (pair<int, string> p) {
            cout    << p.first << " " << p.second << endl;
            outfile << p.first << " " << p.second << endl;
          }
        };

签名
2008-8-11 11:36:56 顶部
点点头
头像
等级:社区游侠
权限:普通用户
积分:47
金钱:3369
声望:91
经验:91
发帖数:814
注册:2007年1月16日
  资料  消息 短消息编辑 删除 引用 第3楼
配合for_each()的functor,22行的cout可以拿掉,只是方面在萤幕显示而已。

        Conclusion

        STL的map是很好用的容器,尤其substring写法,若index下没有元素,会自动新增,所以才会有lines[line_number++] = line;这麽漂亮的写法。

签名
2008-8-11 11:37:10 顶部
第1页 共1页 共2个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com