查看: 7751|回复: 1
打印 上一主题 下一主题

.net开发:如何为程式码加上行号

[复制链接]
跳转到指定楼层
1#
发表于 2008-10-31 15:06:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
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();
         }


执行结果
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2008-10-31 15:07:12 | 只看该作者
台州网址导航

.net开发:如何为程式码加上行号

以下为引用的内容:
         /*
         (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;这麽漂亮的写法。
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表