C程序设计ch输入与输出操作管理学习资料

上传人:yulij****0329 文档编号:137341635 上传时间:2020-07-07 格式:PPT 页数:19 大小:394KB
返回 下载 相关 举报
C程序设计ch输入与输出操作管理学习资料_第1页
第1页 / 共19页
C程序设计ch输入与输出操作管理学习资料_第2页
第2页 / 共19页
C程序设计ch输入与输出操作管理学习资料_第3页
第3页 / 共19页
C程序设计ch输入与输出操作管理学习资料_第4页
第4页 / 共19页
C程序设计ch输入与输出操作管理学习资料_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《C程序设计ch输入与输出操作管理学习资料》由会员分享,可在线阅读,更多相关《C程序设计ch输入与输出操作管理学习资料(19页珍藏版)》请在金锄头文库上搜索。

1、第三章 输入与输出操作管理,C语言无I/O语句,I/O操作由函数实现 #include ,格式:getchar( ) 功能:从键盘读一字符 返值:正常,返回读取的代码值;出错,返回EOF(-1),3.1 输入字符 p81 字符输入函数,例,/*use of getchar function*/ #include main() int c; printf(Enter a character:); c=getchar(); printf(%c-hex%xn,c,c); ,运行结果: Enter a character:A A-hex41,格式: putchar( c ) 参数: c为字符常量、变量

2、或表达式 功能:把字符c输出到显示器上 返值:正常,为显示的代码值;出错,为EOF(-1),3.2 字符输出 (p84) 字符输出函数,/*ch3.1-1.cpp*/ #include main() int c; char a; c=65; a=B; putchar(c); putchar(n); putchar(a); ,运行结果:A B,例,3.3 格式输入 (p85) 格式输入函数:,格式: scanf(“格式控制串”,地址表) 功能:按指定格式从键盘读入数据,存入地址表指定的 存储单元中,并按回车键结束 返值:正常,返回输入数据个数,地址表:变量的地址,常用取地址运算符 char ch

3、; scanf(“%d”, 执行:123 输出:x=123,ch=10,例 /3.1-3.cpp int x; char ch; scanf(“%d”, 执行:123 输出:x=123,ch=10,解决方法: (1)用getchar()清除 (2)用函数fflush(stdin)清除全部剩余内容 例3.1-5.cpp (3) 用格式串中空格或“%*c”来“吃掉”,例 int x; char ch; scanf(“%d”,格式:printf(“格式控制串”,输出表) 功能:按指定格式向显示器输出数据 返值:正常,返回输出字节数;出错,返回EOF(-1),3.4 格式输出 (p94) 格式输出函数

4、,输出表:要输出的数据(可以没有,多个时以“,”分隔) 格式控制串:包含两种信息 格式说明: %修饰符格式字符 ,用于指定输出格式 普通字符或转义序列:原样输出 格式字符,int a=567;printf ( “%d”,a);,int a=255;printf(“%x”,a);,int a=65;printf(“%o”,a);,int a=567;printf(“%u”,a);,char a=65;printf(“%c”,a);,printf(“%s”,“ABC”);,float a=567.789;printf(“%e”,a);,float a=567.789;printf(“%f”,a);

5、,float a=567.789;printf(“%g”,a);,printf(“%”);,567,ff,101,567,A,ABC,5.677890e+02,567.789000,567.789,%,说明 格式字符要用小写 格式字符与输出项个数应相同,按先后顺序一一对应 输出转换:格式字符与输出项类型不一致,自动按指定格式输出,例 main() unsigned int u=65535; printf(”u=%dn,u); 输出结果:u=-1,例 int a=3,b=4; printf(“%d %dn”,a,b); printf(“a=%d , b=%dn”,a,b);,例 int a=3,

6、b=4; printf(“%d %dn”,a,b); printf(“a=%d , b=%dn”,a,b); 输出结果: 3 4 a=3, b=4,附加格式说明符(修饰符),例 int a=1234; float f=123.456; char ch=a; printf(“%8d,%2dn”,a,a); printf(“%f,%8f,%8.1f,%.2f,%.2en”,f,f,f,f,f); printf(“%3cn”,ch);,运行 1234,1234 结果: 123.456000,123.456000, 123.5,123.46,1.23e+02 a,例 static char a=“He

7、llo,world!” printf(“%sn%15sn%10.5sn%2.5sn%.3sn”,a,a,a,a,a);,运行结果:Hello,world! Hello,world! Hello Hello Hel,例 m.n,例 int a=1234; float f=123.456; static char c=“Hello,world!”; printf(“%8d,%-8dn”,a,a); printf(“%10.2f,%-10.1fn”,f,f); printf(“%10.5s,%-10.3sn”,c,c);,运行结果:1234,1234 123.46,123.5 Hello,Hel,例

8、 -,例 int a=1234; float f=123.456; printf(“%08dn”,a); printf(“%010.2fn”,f); printf(“%0+8dn”,a); printf(“0+10.2fn”,f);,例 0 、+,例 int a=123;float f=123.456; printf(“%o,%#o,%X,%#Xn”,a,a,a,a); printf(“%#fn”,f);,例 #o、#x、#X,例 long a=65536; printf(“%d,%8ldn”,a, a);,例 l,/00001234,/0000123.46,/+0001234,/+00012

9、3.56,/173,0173,7B,0X7B,/0, 65536,3.3 程序举例,/*Calculate Area*/ #include #include main() float a,b,c,s,area; scanf(%f,%f,%f, ,例 输入三角形边长,求面积,输入:3,4,6 输出:a= 3.00, b= 4.00, c= 6.00 s= 6.50 area= 5.33,例 从键盘输入大写字母,用小写字母输出,/*Upper letter to lower letter*/ #include stdio.h main() char c1,c2; c1=getchar(); pri

10、ntf(%c,%dn,c1,c1); c2=c1+32; printf(%c,%dn,c2,c2); ,输入:A 输出:A,65 a,97,Code Quantity Rate Value - - - - - - - - - - - - - - - - Total Value -,例pp.103 The ABC Electric Company manufactures four consumer products.Their inventory position on a particular day is given below:,It is required to prepare the

11、 inventory report table in the following format: INVENTORY REPORT,例pp.104 Draw a graph to determine the reliability of an electronic component at various operating times, from 0 to 3000 hours. The reliability of an electronic component is given by Reliability(r) = e (-t) :the component failure rate per hour.The failure rate (lambda) is 0.001. t: the time of operation in hours.,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号