C语言输入输专项训练chen.docx_第1页
C语言输入输专项训练chen.docx_第2页
C语言输入输专项训练chen.docx_第3页
C语言输入输专项训练chen.docx_第4页
C语言输入输专项训练chen.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言实训教程 -输入输出专项练习一、 实验目的1. 能够熟练并正确定义、输入、输出并使用常用数据类型:整型、实型、字 符型2. 能够使用scanf(),printf(),getchar(),putchar(),gets(),puts()进行各种数据正确格式的输入输 出二、 实验内容及实验步骤(一) 验证性试验,验证以下实验,并分析实验结果1. 用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=A,c2=a,问在键盘上如何输入数据?#includeint main()int a,b;float x,y;char c1,c2;scanf(“a=%d b=%d”,&a,&b);scanf(“%f%e”,&x,&y);scanf(“%c%c”,&c1,&c2);printf(a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%cn,a,b,x,y,c1,c2);return 0;运行时分别按以下方式输入数据,观察输出结果,分析原因。 a=3,b=7,x=8.5,y=71.82,A,a a=3,b=-858993460,x=-107374176.000000,y=-107374176.000000,c1=,c2=bPress any key to continue a=3 b=7 x=8.5 y=71.82 A a a=3 b=7 8.5 71.82 A a a=3 b=7 8.5 71.82Aa 3 7 8.5 71.82Aa a=3 b=78.5 71.82Aa a=3 b=78.5 71.82Aa a=3 b=78.5 71.82Aa原因:“ ,”号、enter键都会被当做值给输入进去。2. 字符输入#include main() int a; char b; float c; printf(Please input an integer:); scanf(%d, &a); printf(integer: %dn, a); printf(Please input a character:); scanf(%c, &b); printf(character: %cn, b); printf(Please input a float number:); scanf(%f, &c); printf(float: %fn, c); 如果把scanf(%c, &b);改为scanf(%1s, &b);观察运行结果原因:发 3. 验证格式输入#include void main() int a, b; printf(Please input a and b:); scanf(%2d%*2d%2d, &a, &b); printf(a=%d, b=%d, a+b=%dn,a,b,a+b);(1)输入123456,验证上述实验结果(2)输入12345a, 验证上述实验结果4. 格式输入与输出#include main() int a=-1; printf (%d,%o, %x,a,a, a); printf(%8o, %12x,a, a); 验证程序分析实验结果5. 无符号数据的输出 #includevoid main()unsigned int a=65535;int b=-2;printf(“a=%d,%o,%x,%un”,a,a,a,a);printf(“b=%d,%o,%x,%un”,b,b,b,b);验证程序分析实验结果6. 字符串的输出#includevoid main()printf(“%3s,%7.2s,%.4s,%-5.3sn”, “CHINA”, “CHINA”, “CHINA”, “CHINA”);验证程序分析实验结果%3s,格式输出字符串,右对齐,超出3个长度的,就全部输出;%7.2s,输出字符串占7个位置,有对齐,左补空格,输出2个字符;%.4s,仅输出4个字符,占位4个位置,右对齐;%-5.3s:输出3个字符,占位5个位置,左对齐右补空格.7. 输出实数时的有效位数#include void main() float x,y;3x=111111.111;y=222222.222;prinft(“%f”,x+y); 验证程序分析实验结果8. 输出双精度数时的有效位数#include void main()double x,y;x=1111111111111.111111111; y=2222222222222.222222222;printf(“%f”,x+y);验证程序分析实验结果9. 输出实数时指定小数位数#include void main()float f=123.456;printf(“%f%10f%10.2f%.2f%-10.2fn”,f,f,f,f,f);验证程序分析实验结果10. 字符输出#include int main(void) int c; for ( ; ; ) c = getchar();if (c = EOF) break;if (c = a) & (c = a) & (c = z); c += A - a;这两句代码,应该如何修改?(5) 利用ctype函数修改上面代码,验证程序结果#include #include int main(void) int c; for ( ; ; ) c = getchar(); if (c = EOF) break; if (islower(c) c = toupper(c); putchar(c); return 0;11. 转义符输出#include void main ( ) printf(ab ct derftgn);printf(“htibbjkn”);验证程序分析实验结果12. 字符串输入输出,连续输入三个单词,每个单词以空格分隔#include void main ( ) char str15,str25,str35;scanf(”%s%s%s”,str1,str2,str3);printf(%s %s %s, str1,str2,str3);验证程序分析实验结果(二)编程题1. 编写printf函数调用下列格式来显示float型变量x:a) 指数表示形式: 最小为8的字段宽度内左对齐; 小数点后保留1位数字.b) 指数表示形式: 最小为10的字段宽度内右对齐; 小数点后保留6位数字c) 定点十进制表示形式: 最小为8的字段宽度内左对齐; 小数点后保留3位数字d) 定点十进制表示形式: 最小为6的字段宽度内右对齐; 小数点后无数字.#include stdafx.hmain()float x;x=0.00001;printf(%-8.1en,x,x,x);/*最小为8的字段宽度内左对齐; 小数点后保留1位数字./printf(%10.6en,x);/*最小为10的字段宽度内右对齐; 小数点后保留6位数字/printf(%-8.3dn,x);/*最小为8的字段宽度内左对齐; 小数点后保留3位数字/printf(%6un,x);/* 最小为6的字段宽度内右对齐; 小数点后无数字./return 0;2. 设计程序使得用户可以以任意字符(回车、空格、制表符、逗号、其它)作为分隔符进行数据的输入#include stdafx.hmain() int a,b;printf(please input a date:);scanf(%d%*c%d,&a,&b);printf(%dt%d,a,b);return 0;3. 编写一个程序, 接收用户录入的日期信息并且将其显示出来. 其中, 输入日期的形式为月/日/年(mm/dd/yy), 输出日期的形式为年月日(yymmdd)#include stdafx.hmain() int year,month,day;printf(please input the date:month,day,yearn);scanf(%d%d%d,&month,&day,&year);printf(%d/%d/%dn,year,month,day);return 0;4. 有3个字符串,要求找出其中最大者#include stdafx.h#includemain() char str120,str220;int a;printf(please input the str1 and str2:n);gets(str1);gets(str2);if(strcmp(str1,str2)0) printf(str1str2);elseprintf(str1str2); return a; #include stdafx.h#include#includemain() char str120,str220,str320;int a;printf(please input the str1,str2 and str3:n);gets(str1);gets(str2);gets(str3); a=strcmp(str1,str2);if(a0) if(strcmp(str1,str3)0)printf(str1str2,str1str3nstr1 is max stringn);else printf(str1str2,str10) printf(str21,str2str3,nstr2 is max stringn);else printf(str2str1,str3str2n str3 is max stringn);return a; 5. 编写一个程序, 对用户录入的产品信息进行格式化, 程序运行后需要有以下会话:Enter item number: 583Enter unit price: 13.5Enter purchase date(mm/dd/yy): 10/24/95Item Unit Price Purchase Date583 $ 13.50 10/24/95其中, 数字项和日期项左对齐, 单位价格右对齐, 美元数量最大取值为9999.99#include stdafx.h#include main()int num_ber;int mm,dd,yy;float Unitprice;printf(请输入itemnum_ber:number=583t);scanf(%d,&num_ber);printf(请输入Unitprice:Unit price=13.5t);scanf(%f,&Unitprice);printf(请输入日期mm/dd/yy=10/24/95);scanf(%d/%d/%dt,&mm,&dd,&yy);printf(ItemnumbertUnit PricetPurchase Date );printf(%4dt%ft%3d/%3d/%3d,num_ber,UnitPrice,mm,dd,yy );return 0;6. 计算若干整数的值,要求输入有若干行,每行第一个整数n,代表后面还有n个数据,如果n=0代表输入结束。输出:要求对于每一行都要在相应的行输出和。#include stdafx.h#include main()int m,n,i,j,sum=0;int a55;scanf(%d,&m);if(m=0) printf(n);else for(i=0;im;i+)for(j=0;jm;j+)scanf(%d,&aij);a00=m;for(i=0;im;i+)for(j=0;jm;j+)printf(%dt,aij);printf(n);for(i=0;im;i+)for(j=0;jm;j+)sum=sum+aij;printf(sum=%dn,sum);sum=0;7. 编程实现如下程序,输入Bb a=7b=6 96.37并输出,每个数据以逗号分隔,每个数据宽度为10,浮点型数据小数后保留2位。8. 有如下结构体,编程实现n(n=10)名同学的数据输入与输出,n为通过键盘输入,成绩输出格式为左对齐,总长度为6,保留小数点后2位,要求当所有数据输入后再输出。struct studentchar name20;char sex;long num;float score3;9. 通过键盘不断输入字符,当输入到?号时停止输入将输入的字符转换

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论