首页 技术 正文
技术 2022年11月18日
0 收藏 452 点赞 4,017 浏览 12767 个字

<!–
html, body { font-size: 15px; } body { font-family: Helvetica, ‘Hiragino Sans GB’, ‘微软雅黑’, ‘Microsoft YaHei UI’, SimSun, SimHei, arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px 15px; padding: 1.33rem 1rem; } h1, h2, h3, h4, h5, h6 { margin: 20px 0 10px; margin: 1.33rem 0 0.667rem; padding: 0; font-weight: bold; } h1 { font-size: 21px; font-size: 1.4rem; } h2 { font-size: 20px; font-size: 1.33rem; } h3 { font-size: 18px; font-size: 1.2rem; } h4 { font-size: 17px; font-size: 1.13rem; } h5 { font-size: 15px; font-size: 1rem; } h6 { font-size: 15px; font-size: 1rem; color: #777777; margin: 1rem 0; } div, p, ul, ol, dl, li { margin: 0; } blockquote, table, pre, code { margin: 8px 0; } ul, ol { padding-left: 32px; padding-left: 2.13rem; } blockquote { padding: 0 12px; padding: 0 0.8rem; } blockquote >–> :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0; } table { border-collapse: collapse; border: 1px solid #bbbbbb; } td, th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb; } @media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) { html, body { font-size: 17px; } body { line-height: 1.7; padding: 0.75rem 0.9375rem; color: #353c47; } h1 { font-size: 2.125rem; } h2 { font-size: 1.875rem; } h3 { font-size: 1.625rem; } h4 { font-size: 1.375rem; } h5 { font-size: 1.125rem; } h6 { color: inherit; } ul, ol { padding-left: 2.5rem; } blockquote { padding: 0 0.9375rem; } }
–>
<!–
html, body { font-size: 15px;}body { font-family: Helvetica, ‘Hiragino Sans GB’, 微软雅黑, ‘Microsoft YaHei UI’, SimSun, SimHei, arial, sans-serif; line-height: 1.6; color: ; background-color: ; margin: 0; padding: 20px 15px; padding: 1.33rem 1rem;}img{ max-width: 100%;}
–>
<!–
.wiz-img-resize-handle {position: absolute;z-index: 1000;border: 1px solid black;background-color: white;}.wiz-img-resize-handle {width:5px;height:5px;}.wiz-img-resize-handle.lt {cursor: nw-resize;}.wiz-img-resize-handle.tm {cursor: n-resize;}.wiz-img-resize-handle.rt {cursor: ne-resize;}.wiz-img-resize-handle.lm {cursor: w-resize;}.wiz-img-resize-handle.rm {cursor: e-resize;}.wiz-img-resize-handle.lb {cursor: sw-resize;}.wiz-img-resize-handle.bm {cursor: s-resize;}.wiz-img-resize-handle.rb {cursor: se-resize;}.wiz-table-body.wiz-table-moving *, .wiz-table-body.wiz-table-moving *:before, .wiz-table-body.wiz-table-moving *:after {cursor:default !important;}td,th {position:relative;}#wiz-table-range-border {display: none;width: 0;height: 0;position: absolute;top: 0;left: 0; z-index:105}#wiz-table-col-line, #wiz-table-row-line {display: none;background-color: #448aff;position: absolute;z-index:120;}#wiz-table-col-line {width: 1px;cursor:col-resize;}#wiz-table-row-line {height: 1px;cursor:row-resize;}#wiz-table-range-border_start, #wiz-table-range-border_range {display: none;width: 0;height: 0;position: absolute;}#wiz-table-range-border_start_top, #wiz-table-range-border_range_top {height: 2px;background-color: #448aff;position: absolute;top: 0;left: 0;}#wiz-table-range-border_range_top {height: 1px;}#wiz-table-range-border_start_right, #wiz-table-range-border_range_right {width: 2px;background-color: #448aff;position: absolute;top: 0;}#wiz-table-range-border_range_right {width: 1px;}#wiz-table-range-border_start_bottom, #wiz-table-range-border_range_bottom {height: 2px;background-color: #448aff;position: absolute;top: 0;}#wiz-table-range-border_range_bottom {height: 1px;}#wiz-table-range-border_start_left, #wiz-table-range-border_range_left {width: 2px;background-color: #448aff;position: absolute;top: 0;left: 0;}#wiz-table-range-border_range_left {width: 1px;}#wiz-table-range-border_start_dot, #wiz-table-range-border_range_dot {width: 5px;height: 5px;border: 2px solid rgb(255, 255, 255);background-color: #448aff;cursor: crosshair;position: absolute;z-index:110;}.wiz-table-tools {display: block;background-color:#fff;position: absolute;left: 0px;border: 1px solid #ddd;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;z-index:130;}.wiz-table-tools ul {list-style: none;padding: 0;}.wiz-table-tools .wiz-table-menu-item {position: relative;float: left;margin:5px 2px 5px 8px;}.wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button {font-size:15px;width:20px;height:20px;line-height:20px;cursor: pointer;position:relative;}.wiz-table-tools i.editor-icon{font-size: 15px;color: #455a64;}.wiz-table-tools .wiz-table-menu-item .wiz-table-menu-button i#wiz-menu-bg-demo{position: absolute;top:1px;left:0;}.wiz-table-tools .wiz-table-menu-sub {position: absolute;display: none;width: 125px;padding: 5px 0;background: #fff;border-radius: 3px;border: 1px solid #E0E0E0;top:28px;left:-9px;box-shadow: 1px 1px 5px #d0d0d0;}.wiz-table-tools .wiz-table-menu-sub >–> div{font-size:15px;}.wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub {display: block}.wiz-table-tools .wiz-table-menu-sub:before, .wiz-table-tools .wiz-table-menu-sub:after {position: absolute;content: ” “;border-style: solid;border-color: transparent;border-bottom-color: #cccccc;left: 22px;margin-left: -14px;top: -8px;border-width: 0 8px 8px 8px;z-index:10;}.wiz-table-tools .wiz-table-menu-sub:after {border-bottom-color: #ffffff;top: -7px;}.wiz-table-tools .wiz-table-menu-sub-item {padding: 4px 12px;font-size: 14px;}.wiz-table-tools .wiz-table-menu-sub-item.split {border-top: 1px solid #E0E0E0;}.wiz-table-tools .wiz-table-menu-sub-item:hover {background-color: #ececec;}.wiz-table-tools .wiz-table-menu-sub-item.disabled {color: #bbbbbb;cursor: default;}.wiz-table-tools .wiz-table-menu-sub-item.disabled:hover {background-color: transparent;}.wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad {display: block;}.wiz-table-tools .wiz-table-color-pad {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 88px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-color-pad > div{font-size:15px;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo {position: absolute;top:3px;left:0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line{color: #cc0000;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child {margin-right: 0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box {color: #448aff;}.wiz-table-tools .wiz-table-cell-align {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 65px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right:0}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign{position: absolute;top:3px;left:0;color: #d2d2d2;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign {color: #a1c4ff;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align {color: #448aff;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right: 0;}th.wiz-selected-cell, td.wiz-selected-cell {background: rgba(0,102,255,.05);}th:before,td:before,#wiz-table-col-line:before,#wiz-table-range-border_start_right:before,#wiz-table-range-border_range_right:before{content: ” “;position: absolute;top: 0;bottom: 0;right: -5px;width: 9px;cursor: col-resize;background: transparent;z-index:100;}th:after,td:after,#wiz-table-row-line:before,#wiz-table-range-border_start_bottom:before,#wiz-table-range-border_range_bottom:before{content: ” “;position: absolute;left: 0;right: 0;bottom: -5px;height: 9px;cursor: row-resize;background: transparent;z-index:100;}.wiz-table-container {}.wiz-table-body {position:relative;padding:0 0 10px;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch;}.wiz-table-body table {margin:0;outline:none;}td,th {height:28px;word-break:break-all;box-sizing:border-box;outline:none;}
–>

 【1】putchar 格式: putchar( c )参数: c常量、变量或表达式功能:把字符c输出到标准输出上返值:正常,为显示的代码值;也就是字符的ascii  【2】getchar 格式:getchar( void )功能:从标准输入上读一字符返值:正常,返回读取的代码值;出错,返回EOF(-1) 1— 从终端获取一个字符2— 吃掉一个垃圾字符       什么时候吃掉? 

    一般是在这个时候前面是什么无所谓,关键是你在下一个scanf前面按了回车键而你的这个scanf又是接收字符串的。那就废了。原因分析如下,当你按下回车键的时候后面又是接收字符串的scanf或者是getchar他们由于要读取缓冲区里面的内容就会把这个回车都取到,而回车有恰恰是结束都取,所以就会直接结束!而如果你的后面的scanf不是读取字符比如你读取的是%d,那就不存在问题。还有就是如果你的下一次读取是字符串也是没关系的,因为在%s也是会过滤掉前面的空格 回车的。为什么不存在问题?那是因为整形输入会忽略之前的空格、回车,并且以之后的空格、回车作为输入整形结束的标志。还有就是不要有误区:不要认为你printf输出空格会影响scanf这完全是俩回事,因为printf输出的东西是在输出文件流而你在输入缓冲区。还有一种情况就是人家要求你输入整形数据%d,但是你偏偏要输入字符型数据,这时,这个字符型并不会被scanf读取走,下一次读取就会出现问题这个时候缓冲区里面已经是上一次字符的ascii码了,可以读取了。就会出错。 

【3】printf 格式:printf(“格式控制串”,输出表)功能:按指定格式向显示器输出数据返值:正常,返回输出字节数;出错,返回EOF(-1)  比如 120 返回值就是3 ; 123\n 返回值是 4.  1— 数据类型的控制%d  %o   %x   %u%f%c%p打印数据在内存中的地址(使用取地址符:&) 3—字符串%s要求传递一个地址量(字符串常量当作地址量处理)遇到’\0’结束输出;.n对字符串,指定实际输出位数 2— 格式控制m输出数据域宽,数据长度<m,左补空格.n对实数,指定小数点后位数(四舍五入)-输出数据在域内左对齐(缺省右对齐)+指定在有符号数的正数前显示正号(+)0输出数值时指定左面不使用的空位置自动填0#在八进制和十六进制数前显示前导0,0x  【4】scanf 格式: scanf(“格式控制串”,地址表)功能:按指定格式从键盘读入数据,存入地址表指定存储单元中,并按回车键结束返值:正常,返回从标准输入成功获取数据的个数 也就是输入几个数 1— 数据类型的控制%d  %o   %x   %u%f%c%p//打印数据存储的地址 2— 格式间隔符为:空格、换行符、无间隔可通用 3— 字符串char a[10] = {‘a’,’b’,’c’,’d’,’\0′};scanf(“%s”,a);printf(“%s\n”,a); //数组名是数组的首元素的地址//%[^\n]  【5】putsint puts(char * s); 1— char * //要求你传递一个地址量,char型数据的地址量2— 默认带换行,遇‘\0’字符结束输出;4— 返回值:返回输出字节数 注意+1有/0.  【6】getschar * gets(char *) 1— 功能:从键盘输入一以回车结束的字符串放入字符数组中,并自动在末尾加‘\0’2— 遇’\n’代表结束输入3— 返回值:存放字符串的首地址,   【7】sqrt函数:sqrt() 开方数学函数头文件:#include <math.h>链接库操作:gcc 1.c -lm   【8】ifif (表达式) {语句块1}                             else{语句块2} 关于scanf的若干问题 /若干陷阱
 
[cpp] view plaincopy 
其实对于scanf的若干问题就是围绕输出控制符 %d ,%c和%s
他们之间有什么联系和区别且看下面。
当我们在读取一个整形数字的时候我们会用%d来读取。这毫无疑问。而且我们都知道此时的%d是过滤空白符的无论你如何输入都可以过滤掉,你是喜欢输入一个空格一下 还是输入一个回车一下无所谓。都可以完整接收到。但是在%c中似乎问题就没那么简单了,如果你在%d的时候你直接输入回车没关系,它会过滤掉。但是在c%可是不会的,如果你在输入的时候第一次就输入回车,那废了。程序完美结束。究其原因是:因为在读取字符的时候它会把回车也当作字符(还是合理的。回车也是字符)读入后就结束了。这也是我们常常谈的一个问题,要注意吃掉回车字符。什么意思那?就是上一条语句结束的时候我们按了一个回车,下一条语句如果是getchar或者是scanf那这个空格就会被读入。而我们无法输入。解决这个问题的方法是什么?
基本就是用一个getchar吃掉我们上一次的输入,或者是我们用一个特殊的方法就是在前面加一个空格。scanf(“  %d”,&a)或者getchar( a)都是可以的,为什么前面加个空格就可以了?空格也是scnaf的控制符,它的意思就是过滤空格,回车,TAB,Newline等等空白符。明白了吧。但是这也要注意 比如你今天敲代码打了个 scanf(“%d%d%d ”,&a,&b,&c)那废了你要是按回车根本没用。因为后面有过滤。
%s我们来谈谈,%s遇到空格就结束了对字符串的读取。你如果想输入 hellow word 那单纯的%s是不行的。这个一般就用puts  默认带换行。或者在scanf中用%[^\n]也可以实现。
 
下面是我从网上摘录的一小段
1. 
2. #include <stdio.h>  
3. 
4. int main (void)   
5. {  
6.     int i ;  
7.     char ch ;  
8.       
9.     scanf(“%d”,&i) ;  
10.     scanf(” %c”,&ch) ;  //这行的%C前有个空格  
11.     printf(“i = %d\nch = %c\n”, i, ch) ;   
12.       
13.     /*  输入100 y 后的输出结果: 
14.         i = 100 
15.         ch = y */  
16.       
17.     return 0 ;  
18. }  
       有些朋友或许会有疑问,为什么 scanf(” %c”,&ch) 中的%c 前面要加一个空格,空格的作用到底是什么…在这里可以告诉大家,%c前面的空格就是用来屏蔽空白符的,但想要弄明白这个问题,必须要知道C语言 scanf() 函数的特性,以及scanf()与缓冲区、标准输入流之间的联系。 但在此之前,我们先介绍两个专业术语。
1、缓冲区
   缓冲区是一块存放临时输入数据的内存,我们在键盘上输入数据之所以不会马上显示,就是因为数据存放在了缓冲区,在上面例子中,当我们按下回车键后,缓冲区的数据会传送到标准输入流。
2、标准输入流
   标准输入流(stdin)是文件指针,scanf()就是从标准输入流获取数据。 在以后的文章中,会接触到fscanf函数的介绍中详细解释。
   以上面代码为例,当执行到scanf()时,开始等待用户输入,当我们输入数据,在按下回车键前会把数据储存在缓冲区,按下回车键后,会把数据以及换行符(\n)一起传送到标准输入流(stdin)中,scanf()就是从标准输入流中或许数据,上面第一个scanf()要读入一个整数,它从标准输入流中查找,跳过空白符(空格、换行符、制表符),如果遇到数字,则开始读入,直到遇到非数字的字符结束,最后计算读取到的数值,储存到相关变量中。
   如果能理解上面一段话,那么你应该知道%c前空格的作用了,如果不能,请看看%c前不加空格程序的输出结果。
[cpp] view plaincopy1. #include <stdio.h>  
2.   
3. int main (void)   
4. {  
5.     int i ;  
6.     char ch ;  
7.       
8.     scanf(“%d”,&i) ;  
9.     scanf(“%c”,&ch) ;  
10.     printf(“i = %d\nch = %c\n”, i, ch) ;   
11.       
12.     /*  输入100 : 
13.         i = 100 
14.         ch = */  
15.       
16.     return 0 ;  
17. }  
   请看上面的程序,我们输入100后,咦,怎么程序中止了,马上输入结果了? 但真的是终止了吗?别忘了,标准输入流中除了100外,还有一个换行符’\n’。 第一次scanf()读取了100后,把’\n’留在了标准输入流,第二个scanf()就从输入流中获取换行符了,由于scanf()函数的特性,空白符直接忽略,因此程序便结束了,第二个scanf()函数看似失效,实际上是执行完毕了。
 
 所以对于scanf()而言,%c是个较为特殊的说明符。 %c前没空格,scanf()将读取标准输入流中的第一个字符,%c前有空格,scanf()则读取标准输入流中第一个非空白字符。读到这,大家应该理解了吧?
 
原因何在,在%前面加一个空格就这么有用,答案是肯定的,就是%前面的空格在起作用,读者看看此文章的前面部分,在scanf的使用过程中应注意的问题中已经指出:“scanf()的格式控制串可以使用空白字符或其它非空白字符,使用空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符。”   所以在%前面加上了空格(空格属于空白字符,此外还有像制表符等也属于空白字符),在输入过程中,将略去输入中的一个或多个空白字符,所以我们输入的0 1 2 3 4 5 6 7 8 9这些字符中的空白字符就被略去了,字符9也就正确的打印出来了,这样子解释,相信大家都看明白勒吧
 
 
其实对于scanf的若干问题就是围绕输出控制符 %d ,%c和%s
他们之间有什么联系和区别且看下面。
当我们在读取一个整形数字的时候我们会用%d来读取。这毫无疑问。而且我们都知道此时的%d是过滤空白符的无论你如何输入都可以过滤掉,你是喜欢输入一个空格一下 还是输入一个回车一下无所谓。都可以完整接收到。
但是在%c中似乎问题就没那么简单了,如果你在%d的时候你直接输入回车没关系,它会过滤掉。但是在c%可是不会的,如果你在输入的时候第一次就输入回车,那废了。程序完美结束。究其原因是:因为在读取字符的时候它会把回车也当作字符(还是合理的。回车也是字符)读入后就结束了。这也是我们常常谈的一个问题,要注意吃掉回车字符。什么意思那?
就是上一条语句结束的时候我们按了一个回车,下一条语句如果是getchar或者是scanf那这个空格就会被读入。而我们无法输入。解决这个问题的方法是什么?
基本就是用一个getchar吃掉我们上一次的输入,或者是我们用一个特殊的方法就是在前面加一个空格。scanf(“  %d”,&a)或者getchar( a)都是可以的,为什么前面加个空格就可以了?空格也是scnaf的控制符,它的意思就是过滤空格,回车,TAB,Newline等等空白符。
明白了吧。但是这也要注意 比如你今天敲代码打了个 scanf(“%d%d%d ”,&a,&b,&c)那废了
你要是按回车根本没用。因为后面有过滤。
%s我们来谈谈,%s遇到空格就结束了对字符串的读取。你如果想输入 hellow word 那单纯的%s是不行的。

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774