volcanol的工控博客
Email : lilinly225@126.com 索要资料加QQ 点击进入 或 点击左侧的资料分享专用帖

volcanol ---- View OF Linux Can Appreciate Nature OF Linux

天行健,君子以自强不息

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  在园子里面贴代码的时候,经常有人贴代码就像下面这样前面带有行号,复制下来后,粘贴到代码编辑器里面后,前面很多的数字

编译前还要全部的删除这些数字才能编译,几十行的代码删除也就算了,如果代码行成千上万行后就比较麻烦了。

 Exp:带前导行号的C源代码

1 #include <stdio.h>
2
3 int main(int argc,char **argv)
4 {
5 printf("hello world!");
6
7 return 0;
8 }

  如果能有一个简单的方法来去除这些代码前面的行号,将有利于提高学习效率;既然大家都是懂得编程的人,就会想到用编程的方法来

实现这个功能;比方说利用C语言编写一个读写文件的小程序来实现这个功能;但是这样又比较麻烦,那有么有更简单的方法呢?

      答案是:有。

  利用命令行提供的功能就能实现。基于上面的需求我用命令行实现的一个简单的去除C语言代码中前面的行号批处理。下面给出批处理

文件的源代码:

@echo off

title C语言代码去除前导数字批处理
echo.
echo.
echo ***************************************************************
echo.
echo.
echo 欢迎使用——C语言代码去除前导数字批处理——工具
echo.
echo.
echo Author: volcanol
echo Version: v0.0
echo QQ: 164498180
echo Email: lilinly225
echo Blog: http://www.cnblogs.com/volcanol/
echo.
echo.
echo ****************************************************************
echo.
echo.
echo 按任意键继续.........
echo.
echo.
pause >nul

::接受用户输入文件名
cls
echo.
echo 请输入完整路径及文件名(文件在当前文件夹可仅输入文件名):
echo.
set /p file=

::判断文件是否存在
if exist %file% goto goon

::不存在要编辑的文件,提示后退出
echo.
echo.
cls
echo ******************************************************************
echo.
echo.
echo 您要编辑的文件不存在,请确认路径及文件名是否正确!
echo.
echo 感谢使用——C语言代码去除前导数字批处理——工具
echo.
echo 请按任意键退出................
echo.
echo.
echo ******************************************************************
pause >nul
exit

::存在文件就执行下面的语句
:goon
echo. >de_pre_num.c
for /f "tokens=2-30" %%i in ( %file% ) do echo %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z>>de_pre_num.c
cls
echo ******************************************************************
echo.
echo.
echo 已经成功处理文件,并将处理后的文件保存在:de_pre_num.c
echo.
echo 感谢使用——C语言代码去除前导数字批处理——工具
echo.
echo 请按任意键退出.........
echo.
echo.
echo ******************************************************************
pause >nul

Exp:下面是带前导数字C语言代码。

 1 /*                 
2 StrToInt: 将字符串转换为整型数
3 */
4 #include <stdio.h>
5 unsigned long int str_to_int(const char *source);
6 int main(int argc char *argv[])
7 {
8 char str[5]="1234";
9 unsigned long int test;
10 test=str_to_int(str);
11 printf("%d\n" test);
12 return 0;
13 }
14 /*
15 函数功能:
16 将一个以0-9字符组成的字符串转换为整型数据
17 函数原型:
18 unsigned long int str_to_int(const char *source)
19 函数参数:
20 char *source: 存储字符串的首地址
21 返回值:
22 转换后的Int数值
23 异常:
24 传递空指针或者时函数无法返回正确值
25 */
26 unsigned long int str_to_int(const char *source)
27 {
28 unsigned long int result=0;
29 while('\0'!= *source )
30 {
31 result =(*source + result - '0')*10;
32 ++source;
33 }
34 return result/10;
35 }

Exp:下面是用批处理处理后的C语言代码:

/*                 
StrToInt: 将字符串转换为整型数
*/
#include <stdio.h>
unsigned long int str_to_int(const char *source);
int main(int argc char *argv[])
{
char str[5]="1234";
unsigned long int test;
test=str_to_int(str);
printf("%d\n" test);
return 0;
}
/*
函数功能:
将一个以0-9字符组成的字符串转换为整型数据
函数原型:
unsigned long int str_to_int(const char *source)
函数参数:
char *source: 存储字符串的首地址
返回值:
转换后的Int数值
异常:
传递空指针或者时函数无法返回正确值
*/
unsigned long int str_to_int(const char *source)
{
unsigned long int result=0;
while('\0'!= *source )
{
result =(*source + result - '0')*10;
++source;
}
return result/10;
}

     在学习linux的时候,知道有正则表达式这么个玩意,在VB Script或JScript里面好像也有,都可以用来处理文本文件;以前没有深入了解命令行这个东西,现在

发现Win下的批处理还是不错的,可以实现很多功能。

 

posted on 2012-04-07 15:27  volcanol  阅读(1640)  评论(10编辑  收藏  举报
volcanol ----View OF Linux Can Appreciate Nature OF Linux。