.net开发:为程式码加上行号的方法详解

所属分类: 网络编程 / ASP.NET 阅读数: 1304
收藏 0 赞 0 分享

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

Introduction

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

一段C++的小程式,可以帮程式码加上行号后输出。
以下为引用的内容:
map_code_line.cpp / C++

复制代码 代码如下:

         /*
         (C) OOMusou 2008

         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();
         }

执行结果
以下为引用的内容:

复制代码 代码如下:


         /*
         (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;
          }
        };

配合for_each()的functor,22行的cout可以拿掉,只是方面在萤幕显示而已。

Conclusion

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

更多精彩内容其他人还在看

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享

ASP.NET入门随想之检票的老太太

ASP.NET入门随想之检票的老太太
收藏 0 赞 0 分享
查看更多