外包网www.waibao.com.cn
欢迎 游客 , 注册 | 登录 | 会员 | 界面 | 简洁版本 | 在线 | 帮助
外包网论坛(威客,威客网,悬赏任务)

如何成为一名群主?   

发表新主题 回复该主题
本主题被查看82次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第   上一主题   下一主题
标题: .net开发:如何为程式码加上行号
版主
UID: 16051
来自:
精华: 0
积分: 6673
帖子: 6669
注册: 2008-6-10 11:56:00
状态: 离线
包子皮: 4.00
包子馅: 1670.50 元
只看楼主 2008-11-20 17:47
.net开发:如何为程式码加上行号
我的身份:公司
认证信息:  未实名认证
基本诚信:0
项目诚信:0
经验积分:26
我的服务范围:
尚未填写
    
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();
        }
执行结果
以下为引用的内容:          /*
        (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;这麽漂亮的写法。

                    
#1  
发表新主题 回复该主题
本主题被查看82次, 共1个帖子, 1页, 当前为第1页     选择页数: 1      跳转到第







现在的时间是 2009-01-09 14:34:18

版权所有 外包网  
         Powered by Discuz!NT 1.0.2656    Copyright © 2001-2009 Comsenz Inc.
Processed in 0.112 seconds