by freeuniverser
程序语言现在是越来越多,但是好多都是在现有语言的基础上改了改,本质上并没有给程序员带来什么,这就提醒广大程序员不要被某种语言限制住了,不要过分迷恋某种语言,因为现在还没有完美语言(也许人类追求的完美本来就是个美丽的错误)。这个月的TIOBE上,C语言又回到了首位,但是也不必太在意,仅供参考嘛。
C语言的一些小程序举例:
Count blanks,tabs and newlines
/*First Version*/ #include <stdio.h> int main(void) { int blanks; int tabs; int newlines; int c; blanks = 0; tabs = 0; newlines = 0; while((c = getchar()) != EOF) { if(c == ' ') blanks++; if(c == '\t') tabs++; if(c == '\n') newlines++; } printf("\nBlanks: %d\nTab: %d\nNewline: %d\n", blanks,tabs,newlines); return 0; }
写程序时的注释非常重要,以前初写的时候乱写,而且命名单字母一大堆,其实吧,好的命名与注释同等重要,甚至见名知意比注释更加有力。程序小的话还可以,但是涉及大点的项目的话,如果注释和命名不注意,那么很容易产生问题,后期的维护会异常困难,没人愿意再深究你的变量究竟代表什么,甚至你自己都不知道当初的意思是什么。
1 /*Second Version*/ 2 #include <stdio.h> 3 4 int mian(void) 5 { 6 int blanks; 7 int tabs; 8 int newlines; 9 int c; 10 int done; 11 int lastchar; 12 13 blanks = 0; 14 tabs = 0; 15 newlines = 0; 16 done = 0; 17 lastchar = 0; 18 19 while(done == 0) 20 { 21 c = getchat(); 22 if(c == ' ') 23 blanks = blanks + 1; 24 if(c == '\t') 25 tabs = tabs + 1; 26 if(c == '\n') 27 newlines = newlines + 1; 28 if(c == EOF) 29 { 30 if(lastchar == '\n') 31 { 32 newlines = newlines + 1; 33 } 34 done = 1; 35 } 36 lastchar = c; 37 } 38 printf("\nBlanks: %d\nTabs: %d\nNewlines: %d\n", 39 blanks,tabs,newlines); 40 return 0; 41 }
自加自减运算符看似方便,但是滥用其实很不好,容易绕进去。其实好多语言在设计上都是在玩儿技巧,这样可以少敲点字,可以少占点内存,可以……现在内存不小了,多敲几个字母也没什么,实际上是在节省时间,如果后期维护的话,那将会少很多力气,这不挺好的吗?
Copy input to output
1 /*First Version*/ 2 #include <stdio.h> 3 #define NONBLANK 'c' 4 int main(void) 5 { 6 int c; 7 int lastchar; 8 lastchar = NONBLANK; 9 10 while((c == getchar()) != EOF) 11 { 12 if(c != ' ') 13 putchar(c); 14 if(c == ' ' && lastchar != ' ') 15 putchar(c); 16 lastchar = c; 17 } 18 }
用getchar()和putchar()搭配干活不累,有好多这样的函数可以拿来用,当然你也可以自己去写自己的,不过他山之石何不用呢?如果不是特别需要的地方,那么不妨就“拿来主义”下吧
1 /******Second Version*********************/ 2 int c; 3 int inspace; 4 inspace = 0; 5 while((c = getchar()) != EOF) 6 { 7 if(c == ' ') 8 { 9 if(inspace ==0) 10 { 11 inspace = 1; 12 putchar(c); 13 } 14 } 15 if(c != ' ') 16 { 17 inspace = 0; 18 putchar(c); 19 } 20 }
Copy input to output,replace tab by \t,backspace by \b and backslash by \\
这个程序的\b是个问题,可能会有点错误,或者不是很理想,有兴趣自己可以写写。
1 //*****First Version*****************************/ 2 3 int c; 4 while((c = getchar()) != EOF) 5 { 6 if(c == '\t') 7 printf("\\t"); 8 if(c == '\b') 9 printf("\\b"); 10 if(c == '\\') 11 printf("\\\\"); 12 if(c != '\t' &&c != '\b' && c != '\\') 13 putchar(c); 14 }
简单的用if判断条件,虽然显得有点单调,但是确实非常常用的。这很符合人的逻辑,如果怎么……你看其实程序语言里面用的单词其实没有太难的,对比好多语言你就会发现,像while,if,for等等,好多都是重复的,就行写文章一样,把不同的字做一个合乎逻辑的排列组合即可,程序也是如此,只不过是换种表达方式罢了。
1 /******Second Version***************************/ 2 int c,t; 3 while((c = getchar()) != EOF) 4 { 5 t = 0; 6 if(c == '\\') 7 { 8 putchar('\\'); 9 putchar('\\'); 10 t = 1; 11 } 12 if(c == '\t') 13 { 14 putchar('\\'); 15 putchar('t'); 16 t = 1; 17 } 18 if(c == '\b') 19 { 20 putchar('\\'); 21 putchar('b'); 22 t = 1; 23 } 24 if(t == 0) 25 putchar(c); 26 }
putchar()双剑合璧,显得更清晰。这就像篱笆,你可以两个捆一起,还可以三个、四个……最后连起来就可以了,分开写可能有时候会显得更加清晰、有条理,挤一块当然也可以,注意分隔,没人规定篱笆不能是一长条弯起来吧。
1 /******Third Version***********************/ 2 #define DOUBLELINE '\\' 3 int c; 4 while((c = getchar()) != EOF) 5 { 6 switch(c) 7 { 8 case DOUBLELINE: 9 putchar(DOUBLELINE); 10 putchar(DOUBLELINE); 11 break; 12 case '\t': 13 putchar(DOUBLELINE); 14 putchar('t'); 15 break; 16 case '\b': 17 putchar(DOUBLELINE); 18 putchar('b'); 19 break; 20 default: 21 putchar(c); 22 break; 23 } 24 }
switch-case显得更为清晰而有条理,但是好多时候不及if有效。其实,好多时候,一种语言提供了多种方式来表达同样的意思,只不过要看场合和自己的兴趣了,不要太拘束,怎样顺怎么来表达,做到简洁清晰就行了。
prints its input one word per line
1 int c; 2 int inspace; 3 inspace = 0; 4 while((c = getchar()) != EOF) 5 { 6 if(c == ' ' || c == '\t' || c == '\n') 7 { 8 if(inspace == 0) 9 { 10 inspace = 1; 11 putchar('\n'); 12 } 13 } 14 else 15 { 16 inspace = 0; 17 putchar(c); 18 } 19 }
inspace在上面的作用已经很明显,和上面的done一样,作为控制作用,有点相当于开关。毕竟程序语言借自然的智慧诞生于人类的头脑,其结果也必然是来源于现实社会,又反映现实社会。
好多语言发展到现在都遇到了瓶颈,而且好多都是在炒剩饭,然后换件外衣,新瓶装老酒而已。其实也没有哪种语言可以一统程序界江湖,不必有宗教情节,期待简洁清晰方便的语言出现。