AllenNewOk的博客
AllenNewOk · 共 26 篇文章
批量重命名的一个shell脚本,当前目录及其所有子目录------依然繁琐
这是在上一篇脚本之上的改进,可以支持目录及其所有子目录中的文本文件重命名,亦为练习之作。Ubuntu 8.04.4上测试通过。 #!/bin/bash # Rename_AllChildrenDirectories.sh # 问题描述: # 某目录及其子目录中出现一些以“;1”结尾的文本文件名,编写shell脚本, # 以去除这个目录及其子目录中所有该类文件名中结尾部分的“;1”。 # COUNT用于计数,错误输入的情况下,默认为允许3次尝试。 COUNT=0 while [ $COUNT -lt 3 ] do echo "请输入要重命名的文件所在父目录的路径:" read PATH if …
2010-12-13 17:04 · Shell · 评论(4)
批量重命名的一个shell脚本,仅限当前目录------写得有些繁琐
实现某类文本文件的重命名,作为练习。Ubuntu 8.04.4上测试通过。 #!/bin/bash # Rename_OnlyOneDirectory.sh # 问题描述: # 某目录中出现一些以“;1”结尾的文本文件名,编写shell脚本, # 以去除仅当前这个目录中该类文件名中结尾部分的“;1”。 # COUNT用于计数,错误输入的情况下,默认为允许3次尝试。 COUNT=0 while [ $COUNT -lt 3 ] do echo "请输入要重命名的文件所在目录的路径:" read PATH if [ -d $PATH ] then cd $PATH # NUM代表符合条件的、可重…
2010-12-13 13:57 · Shell · 评论(2)
Shell编程之目录判断及文件拷贝------------关于一道考题的设计及解决
一道考题,回忆了一下,完善了一下。 #!/bin/bash # 问题描述:输入一个路径,如果该路径是指目录,则将该目录中的所有文件 # (包括文件夹)全部拷贝到另一个已经存在的用户test_user根目录下tmp # 目录中的一个自定义的文件夹里,并更改其用户为test_user。 # (假设对所涉及到的相关目录及文件具有相应的读、写或可执行权限) echo "请输入路径:" read PATH if [ -d $PATH ] then echo "该路径为目录,将执行拷贝。" echo ".................." /bin/mkdir -p ~test_user/tmp/de…
2010-12-12 21:42 · Shell · 评论(2)
C和C++的简单比较一:内存分配与释放
简单比较C与C++在内存分配与释放中指针及其所指向的变量的变化,使用gcc 、g++编译。 示例一_C: Example_One: #include<stdio.h> #include<stdlib.h> int main() { int *p; printf("%p\n",p); printf("%d\n\n",*p); p=(int*)malloc(sizeof(int)); printf("%p\n",p); printf("%d\n\n",*p); *p=6; printf("%p\n",p); printf("%d\n",*p); free(p); return 0; } Resu…
2010-11-12 16:52 · C/C++ · 评论(2)
atoi_字符串数字转换为整型
实现字符串中的数字转换为整型,具体规则见源码注释。gcc编译通过。 第一种代码,过程有些繁琐: #include<stdio.h> /* 求幂 * 此处求幂也可以调用math.h中的函数pow(), * 编译时添加“-lm”参数。 */ double mypow(double x,double y) { int tmp=1; while(y-->0) { tmp*=x; } return tmp; } /* 转化字符串中的数字字符到整数。规则如下: * 1、如果字符串开始是一个字母,则返回零; * 2、否则,把数字字符转换成整型数,直到遇到字母字符为止。 */ int myatoi(cons…
2010-10-28 12:54 · C/C++ · 评论(0)
strcmp函数的两种实现
strcmp函数的两种实现,gcc测试通过。 一种实现: #include<stdio.h> int strcmp(const char *str1,const char *str2) { /* 注释以下的五行(while循环)可以简写为: * for(;(*str1==*str2)&&*str1!='\0';str1++,str2++); * */ while((*str1==*str2)&&*str1!='\0') { str1++; str2++; } if(*str1=='\0'&&*str2=='\0') return 1; else return -1; } int main()…
2010-10-21 13:12 · C/C++ · 评论(0)
C语法的几个小测试
C语法的几个小测试,十分无聊…… #include<stdio.h> int main() { int a=2,b=1; /* 验证表达式正确的运算方式,gcc对a+++b是以(a++)+b来处理的,或与规范有关,但确实“贪心” */ printf("a+++b = %d,\ta = %d\n",a+++b,a); /* 验证括号中逗号表达式的作用,取最后一个逗号后的值,但括号内所有运算照旧 */ printf("(a++,0,a-b)+b = %d, a = %d\n",(a++,0,a-b)+b,a); /* 证实:先进行“+”运算,然后再处理相关变量右边的“++”问题 */ b+=a+…
2010-10-11 21:32 · C/C++ · 评论(0)
for循环与续行符“\”测试
for循环与续行符“\”小测 #include<stdio.h> /* 该程序主要验证for循环的含义,关注其中变量i的变化 */ int main() { int i; /* 续行符“\”的右边紧邻位置不能出现空格,在语句的自然停顿处也可以直接换行而不使用续行符 */ for(i=0,printf("First = %d",\ /* 在分号位置可以直接换行 */ i); printf("\tSecond = %d",i),i<10;\ i++,printf("\tThird = %d\n",i)) { printf("\tFourth = %d",i); } printf("\n"); r…
2010-10-11 20:53 · C/C++ · 评论(0)
Review_singlelinkedlist
时间长了,不写手生,复习一下单链表。 #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct _node { int mydata; struct _node *next; }node; node *Create() { int i,len; node *h,*p,*q; printf("定制链表长度:"); scanf("%d",&len); if(len<0) { printf("输入有误!\n"); exit(1); } h=(node*)malloc(sizeof(node)); if(h!=NUL…
2010-10-11 17:02 · C/C++ · 评论(0)
获取整型数组的第二大的数_纯粹地用数组实现
获取整型数组的第二大的数,完全采用数组来实现(当然也可以借助单链表来做)。 大体思想就是:先由小到大排序,然后可以获得最大的数,接着可以得到第二大的数,这里考虑到了数组中可能会出现重复数据的情况。gcc调试通过。 #include<stdio.h> #include<stdlib.h> #define N 12 void GetSecondNumber(int array[N]) { int i,j,tmp,array_backup[N]; printf("原数组为:\n"); for(i=0;i<N;i++) { printf("%5d",array[i]); } printf("\n")…
2010-09-19 14:08 · C/C++ · 评论(0)
多项式的规范化_数据结构_单链表_C语言实现
多项式的规范化,采用单链表,使用C语言实现,gcc调试通过。 //该程序是为了将无序的、不规范的多项式进行规范化而写的。 #include<stdio.h> #include<stdlib.h> #define N 8 //指明多项式数据项的数目 int GetLength(); //获得单链表的长度 void Print(); //打印出单链表的节点数据 typedef struct multinomialnode //定义存储多项式数据项的节点的结构体 { int coefficient,power; //定义系数和幂 struct multinomialnode *next; }nod…
2010-09-17 10:41 · C/C++ · 评论(0)
单链表的创建、计数打印、删除节点、增加节点、逆序操作与查找中间节点
单链表的创建、计数打印、删除节点、增加节点、逆序操作与查找中间节点,是上上一篇的补充,gcc调试通过。 #include<stdio.h> #include<stdlib.h> /*使用到其中的malloc和exit函数*/ #define times 4 /*用于循环次数的控制*/ static int N=4; /*静态全局变量,用于控制单链表长度*/ typedef struct _person { char name[12]; int age; struct _person *next; }stud; stud *Create(int num) /*创建单链表的函数,num为单链表的…
2010-09-13 23:12 · C/C++ · 评论(0)
从一字符串数组中取出指定字符并将其组成新的字符串数组
从一字符串数组中取出指定字符并将其组成新的字符串数组,gcc编译通过。 #include<stdio.h> #include<string.h> int main() { char ch1[]="askdaskaskdaskg",str[]=""; int i=0; while(ch1[i]!='\0') { if(('d'==ch1[i])||('g'==ch1[i])) { char ch2[2]={ch1[i],'\0'}; printf("%c\t%d\n",ch1[i],i+1); strcat(str,ch2); } i++; } printf("%s\n%d\n%d\n",st…
2010-09-13 15:54 · C/C++ · 评论(0)
单链表的创建、计数打印、删除节点、增加节点和逆序操作
单链表的创建、计数打印、删除节点、增加节点和逆序操作,是在上一篇的基础上完善了逆序操作,gcc编译通过。 #include<stdio.h> #include<stdlib.h> /*使用到其中的malloc和exit函数*/ #define times 4 /*用于循环次数的控制*/ static int N=4; /*静态全局变量,用于控制单链表长度*/ typedef struct _person { char name[12]; int age; struct _person *next; }stud; stud *Create(int num) /*创建单链表的函数,num为单链表…
2010-09-12 16:15 · C/C++ · 评论(0)
素数的打印问题_GCC_VS2005
打印指定数字范围内的素数,程序如下: 1 #include<stdio.h> 2 void find_primes(int m) 3 { 4 int i,j,n=0; // n用于控制换行 5 for(i=1;i<m;i++) 6 { 7 for(j=2;j<i;j++) 8 { 9 if(i%j==0) break; 10 } 11 if(j==i) 12 { 13 printf("%6d",i); 14 n++; 15 if(n%7==0) printf("\n"); 16 } 17 } 18 printf("\n"); 19 } 20 int main() 21 { 22 int i;…
2010-09-03 19:29 · C/C++ · 评论(0)