第二章 基本概念 [c和指针]
2011-12-26 21:11
239 查看
代码风格和规范有多么重要?看看本章中所列入的例程就明白了。
例程2-1
程序输出
不管你是否相信,这竟然是歌曲The twelve Days of Christmas的歌词,而这段代码是Cambridge Consultants Ltd 的 Ian Phillipps编写,用于参加国际C混乱代码大赛。
课后练习2-2
这里面我添加了while()退出条件,即if(n > 10) break;书中参考例子中没有这个退出条件,也就是无法退出循环,但因为连回车也寄存在缓冲区了,这里的n将所有输入字符也计算在内了,所以没有达到仅记录花括号的目的。再次阅读getchar函数的实现后发现,这里循环条件的判断和循环体内的计数器并不能同步实现,而是先将输入的字符存在缓存中,等待输入回车后一起返回并循环执行循环体内的语句。
注:
函数名: getchar
功 能: 从stdin流中读字符
用 法: int getchar(void);
注解:
getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键
例程2-1
#include <stdio.h> main(t,_,a) char *a; {return!0<t?t<3?main(-79,-13,a+main(-87,1-_, main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a )&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_, t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\ ,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\ +k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\ l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\ n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \ ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\ #'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\ +1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \ i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}
程序输出
On the first day of Christmas my true love gave to me a partridge in a pear tree. On the second day of Christmas my true love gave to me two turtle doves and a partridge in a pear tree. On the third day of Christmas my true love gave to me three french hens, two turtle doves and a partridge in a pear tree. On the fourth day of Christmas my true love gave to me four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the fifth day of Christmas my true love gave to me five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the sixth day of Christmas my true love gave to me six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the seventh day of Christmas my true love gave to me seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the eighth day of Christmas my true love gave to me eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the ninth day of Christmas my true love gave to me nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the tenth day of Christmas my true love gave to me ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the eleventh day of Christmas my true love gave to me eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree. On the twelfth day of Christmas my true love gave to me twelve drummers drumming, eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three french hens, two turtle doves and a partridge in a pear tree.
不管你是否相信,这竟然是歌曲The twelve Days of Christmas的歌词,而这段代码是Cambridge Consultants Ltd 的 Ian Phillipps编写,用于参加国际C混乱代码大赛。
课后练习2-2
/*练习2-2 检验输入的花括号是否成对并且合理,以}开始的花括号对是不合理的表现; 要求不合理时,提示错误;{多时,打印多出的个数 */ #include <stdio.h> #include <stdlib.h> int main() { int num; int n = 0; char ch; num = 0; while((ch = getchar()) != NULL){ if(ch == '{') num += 1; if(ch == '}'){ if(num == 0) printf("Error,duo yu de da kuo hu!\n"); else num -= 1; } /*if more than 10 times input,stop checking*/ n++; if(n>10) break; } if(num > 0) printf(" duo yu de kuo hu shu: %d \n",num); return EXIT_SUCCESS; }
这里面我添加了while()退出条件,即if(n > 10) break;书中参考例子中没有这个退出条件,也就是无法退出循环,但因为连回车也寄存在缓冲区了,这里的n将所有输入字符也计算在内了,所以没有达到仅记录花括号的目的。再次阅读getchar函数的实现后发现,这里循环条件的判断和循环体内的计数器并不能同步实现,而是先将输入的字符存在缓存中,等待输入回车后一起返回并循环执行循环体内的语句。
注:
函数名: getchar
功 能: 从stdin流中读字符
用 法: int getchar(void);
注解:
getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键
相关文章推荐
- C和指针----->第二章(基本概念)->C执行文件的产生和代码规范
- C++一些基本概念总结【面向过程篇】——指针
- 日拱一卒之C++基础一 指针与引用基本概念
- 关于指针的四个方面的基本概念
- 数据库设计和ER模型-------之ER模型的基本概念(第二章)
- 源码-PL/SQL从入门到精通-第二章-PL/SQL基本概念-Part 3(完)
- javascript高级程序设计(第三版)-第二章 基本概念-学习笔记
- C/C++基础知识:函数指针和指针函数的基本概念
- C++基本概念复习之一:枚举、引用、指针、类、多态
- staruml之 第二章 基本概念
- C++指针基本概念(一)
- c++ primer 概念整理第二章:变量与基本类型
- 《C现代方法》第二章:C语言基本概念 (2)
- 指针的基本概念
- 深入理解指针,数组的基本概念
- C++基本概念总结——引用和指针
- 深入浅出MFC学习笔记:(第一章:win32基本概念,第二章:C++的重要性质)
- 面试宝典5,6,7--程序设计基本概念;预处理、const与sizeof;指针与引用
- C++ 指针基本概念
- C语言基本概念之空指针