C语言输入输专项训练chen.docx
《C语言输入输专项训练chen.docx》由会员分享,可在线阅读,更多相关《C语言输入输专项训练chen.docx(11页珍藏版)》请在冰豆网上搜索。
![C语言输入输专项训练chen.docx](https://file1.bdocx.com/fileroot1/2022-10/11/0de4bd3d-a1b8-4e77-bd27-2ccb591cdb4f/0de4bd3d-a1b8-4e77-bd27-2ccb591cdb4f1.gif)
C语言输入输专项训练chen
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ˊ,问在键盘上如何输入数据?
#include
intmain()
{
inta,b;
floatx,y;
charc1,c2;
scanf(“a=%db=%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=%c\n",a,b,x,y,c1,c2);
return0;
}
运行时分别按以下方式输入数据,观察输出结果,分析原因。
1a=3,b=7,x=8.5,y=71.82,A,a↙
a=3,b=-858993460,x=-107374176.000000,y=-107374176.000000,c1=,,c2=b
Pressanykeytocontinue
2a=3b=7x=8.5y=71.82Aa↙
3a=3b=78.571.82Aa↙
4a=3b=78.571.82Aa↙
5378.571.82Aa↙
6a=3b=7↙
8.571.82↙
A↙
a↙
⑦a=3b=7↙
8.571.82↙
Aa↙
⑧a=3b=7↙
8.571.82Aa↙
原因:
“,”号、enter键都会被当做值给输入进去。
2.字符输入
#include
main()
{
inta;
charb;
floatc;
printf("Pleaseinputaninteger:
");
scanf("%d",&a);
printf("integer:
%d\n",a);
printf("Pleaseinputacharacter:
");
scanf("%c",&b);
printf("character:
%c\n",b);
printf("Pleaseinputafloatnumber:
");
scanf("%f",&c);
printf("float:
%f\n",c);
}
如果把scanf("%c",&b);改为scanf("%1s",&b);观察运行结果
原因:
发
3.验证格式输入
#include
voidmain()
{
inta,b;
printf("Pleaseinputaandb:
");
scanf("%2d%*2d%2d",&a,&b);
printf("a=%d,b=%d,a+b=%d\n",a,b,a+b);
}
(1)输入123456,验证上述实验结果
(2)输入12345a,验证上述实验结果
4.格式输入与输出
#include
main()
{
inta=-1;
printf("%d,%o,%x",a,a,a);
printf("%8o,%12x",a,a);
}
验证程序分析实验结果
5.无符号数据的输出
#include
voidmain()
{
unsignedinta=65535;intb=-2;
printf(“a=%d,%o,%x,%u\n”,a,a,a,a);
printf(“b=%d,%o,%x,%u\n”,b,b,b,b);
}
验证程序分析实验结果
6.字符串的输出
#include
voidmain()
{
printf(“%3s,%7.2s,%.4s,%-5.3s\n”,“CHINA”,“CHINA”,“CHINA”,“CHINA”);
}
验证程序分析实验结果
%3s,格式输出字符串,右对齐,超出3个长度的,就全部输出;
%7.2s,输出字符串占7个位置,有对齐,左补空格,输出2个字符;
%.4s,仅输出4个字符,占位4个位置,右对齐;
%-5.3s:
输出3个字符,占位5个位置,左对齐右补空格.
7.输出实数时的有效位数
#include
voidmain()
{floatx,y;
3 x=111111.111;
y=222222.222;
prinft(“%f”,x+y);
}
验证程序分析实验结果
8.输出双精度数时的有效位数
#include
voidmain()
{doublex,y;
x=1111111111111.111111111;
y=2222222222222.222222222;
printf(“%f”,x+y);
}
验证程序分析实验结果
9.输出实数时指定小数位数
#include
voidmain()
{
floatf=123.456;
printf(“%f%10f%10.2f%.2f%-10.2f\n”,f,f,f,f,f);
}
验证程序分析实验结果
10.字符输出
#include
intmain(void){
intc;
for(;;){
c=getchar();
if(c==EOF)
break;
if((c>=’a’)&&(c<=’z’))
c+=’A’-’a’;
putchar(c);
}
return0;
}
(1)对比下列五种代码,将4-7、10行代码替换如下,程序应该如何修改才能保持输出结果相同?
for(c=getchar();c!
=EOF;c=getchar())
putchar(c);
(2)将4-7、10行代码替换如下,程序应该如何修改才能保持输出结果相同?
while((c=getchar())!
=EOF)putchar(c);
(3)将4-7、10行代码替换如下,程序应该如何修改才能保持输出结果相同?
c=getchar();
while(c!
=EOF)
{putchar(c);c=getchar();}
(4)如果用ASCII码修改if((c>=’a’)&&(c<=’z’));c+=’A’-’a’;这两句代码,应该如何修改?
(5)利用ctype函数修改上面代码,验证程序结果
#include
#include
intmain(void){
intc;
for(;;){
c=getchar();
if(c==EOF)break;
if(islower(c))
c=toupper(c);
putchar(c);
}
return0;
}
11.转义符输出
#include
voidmain()
{
printf("abc\tde\rf\tg\n");
printf(“h\ti\b\bjk\n”);
}
验证程序分析实验结果
12.字符串输入输出,连续输入三个单词,每个单词以空格分隔
#include
voidmain()
{
charstr1[5],str2[5],str3[5];
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.h"
main()
{floatx;
x=0.00001;
printf("%-8.1e\n",x,x,x);//*最小为8的字段宽度内左对齐;小数点后保留1位数字.//
printf("%10.6e\n",x);//*最小为10的字段宽度内右对齐;小数点后保留6位数字//
printf("%-8.3d\n",x);//*最小为8的字段宽度内左对齐;小数点后保留3位数字//
printf("%6u\n",x);//*最小为6的字段宽度内右对齐;小数点后无数字.//
return0;
}
2.设计程序使得用户可以以任意字符(回车、空格、制表符、逗号、其它)作为分隔符进行数据的输入
#include"stdafx.h"
main()
{inta,b;
printf("pleaseinputadate:
");
scanf("%d%*c%d",&a,&b);
printf("%d\t%d",a,b);
return0;
}
3.编写一个程序,接收用户录入的日期信息并且将其显示出来.其中,输入日期的形式为月/日/年(mm/dd/yy),输出日期的形式为年月日(yymmdd)
#include"stdafx.h"
main()
{intyear,month,day;
printf("pleaseinputthedate:
month,day,year\n");
scanf("%d%d%d",&month,&day,&year);
printf("%d/%d/%d\n",year,month,day);
return0;
}
4.有3个字符串,要求找出其中最大者
#include"stdafx.h"
#include
main()
{charstr1[20],str2[20];
inta;
printf("pleaseinputthestr1andstr2:
\n");
gets(str1);
gets(str2);
if(strcmp(str1,str2)>0)
printf("str1>str2");
elseprintf("str1returna;
}
#include"stdafx.h"
#include
#include
main()
{charstr1[20],str2[20],str3[20];
inta;
printf("pleaseinputthestr1,str2andstr3:
\n");
gets(str1);
gets(str2);
gets(str3);
a=strcmp