文件练习题.docx
《文件练习题.docx》由会员分享,可在线阅读,更多相关《文件练习题.docx(17页珍藏版)》请在冰点文库上搜索。
![文件练习题.docx](https://file1.bingdoc.com/fileroot1/2023-4/29/da04385b-ae71-4468-818f-08e5d54693f2/da04385b-ae71-4468-818f-08e5d54693f21.gif)
文件练习题
1.设有一文本文件f1.txt的内容如下:
NameaddressTel
Zhangxian82346581
LiBeijing83246581
Wanghangzhou42357424
…..
该文件自己创建。
文件的特点是:
(1)实际文件会有更多行,行数不定。
(2)NameAddressTel三列由于名字、地址的长度不等,没有对齐。
请编写程序,读取f1.txt文件,在屏幕上显示,并同时写入另一个文件f2.txt,要求屏幕显示和新的文本文件的列左对齐,每项数据的宽度为20字符。
参考答案:
#include
#include
#include
usingnamespacestd;
intmain()
{
ifstreamin("f1.txt");
ofstreamout("f2.txt");
if(!
in||!
out)
{cout<<"文件未打开,结束。
"<return1;
}
chars1[20],s2[20],s3[20];
while(in)
{
in>>s1>>s2>>s3;
if(in)
{
cout<cout<cout<cout<cout<}
}
in.close();
out.close();
return0;
}2.
编写程序,将字符串中的数字找出,转变成整数后,存放在整型数组中。
例如:
字符串“jdiL56lih123Lk99dd”,输出结果应为:
5612399。
算法分析:
(1)需要从前向后进行字符的ASCII比较,若发现数字字符,就将其取出。
(2)数字字符变成整数的方法是将其ASCII值减去48(数字0的ASCII值)。
(3)将连续的数字字符变成整数,可以利用循环,将数乘以10后加每次得到的数个位
数即可。
参考答案:
#include
usingnamespacestd;
intmain()
{
chars[200],*p,*q;;
intk[50],num=0,idx=0,n;
cout<<"请输入字符串:
"<cin.getline(s,200);
p=s;
while(*p!
='\0')
{
while((*p<'0'||*p>'9')&&*p!
='\0')
p++;
q=p;
while(*p>='0'&&*p<='9'&&*p!
='\0')
{
n=*p-'0';
num=num*10+n;
p++;
}
if(p>q)
{
k[idx]=num;
idx++;
}
num=0;
}
cout<<"计算结果是:
"<for(inti=0;icout<cout<return0;
}
3.
编写一个程序,首先读入用户从键盘输入的一个6×6整数矩阵,而后实现下列要求:
①求6×6整数矩阵每列元素之和并输出;
②求6×6矩阵每列元素之和的最大数并输出。
4.
现有一存储二维矩阵的文本文件,text.txt(请考生自己创建),其内容如下:
10102235200
5173310560
70206897153
请编写程序,读取二维矩阵数据,再将矩阵内容作中心对称变换(即垂直方向镜像变换+水平方向镜像变换),然后将结果写入文件new.txt,同时将新文件内容显示在屏幕上。
new.txt的内容及屏幕显示结果均为:
15397682070
6010533175
20023510201
参考答案:
#include
#include
usingnamespacestd;
constintROW=3;
constintCOL=5;
intmain()
{
intm[ROW][COL];
ifstreamin("text.txt");
if(!
in){
cout<<"Cannotopenfile.\n";
return1;
}
ofstreamout;
out.open("new.txt");
if(!
out){
cout<<"Cannotopenfile.";
return1;
}
for(inti=0;ifor(intj=0;j
{
in>>m[i][j];
}
intt;
for(intj=0;j
{
for(intk=0;k{
t=m[k][j];m[k][j]=m[k][COL-j-1];m[k][COL-j-1]=t;
}
}
for(i=0;i{
for(intj=0;j
{
t=m[i][j];m[i][j]=m[ROW-i-1][j];m[ROW-i-1][j]=t;
}
}
cout<<"\n结果:
\n"<for(i=0;i{
for(intj=0;j
{
out<cout<}
out<cout<}
in.close();
return0;
}
5.
编程实现下面的过程:
用户输入由若干英文单词组成的字符串(单词间有且仅有一个空格,单词个数<20),每个单词长度<20,调用自编的函数split分离出所有单词。
函数split的原型为:
voidsplit(char*str)
编写主函数验证其正确性。
运行样例看下面说明。
6.
现有文本文件,text.txt(请考生自己创建),其内容如下:
11000101
00101000
01011010
11111101
请编写程序,读取文件内容,将二进制转化为十六进制表示形式后,写入新文件new.txt,同时将new.txt内容在屏幕上输出。
new.txt的内容及屏幕输出结果如下:
C5
28
5A
FD
提示:
每4位二进制数转换为1位十六进制数。
参考答案:
#include
#include
usingnamespacestd;
intmain()
{
charc1,c2,c3,c4;
ifstreamin("text.txt");
if(!
in){
cout<<"Cannotopenfile.\n";
return1;
}
ofstreamout;
out.open("new.txt");
if(!
out){
cout<<"Cannotopenfile.";
return1;
}
intr,l=0;
while(in)
{
in>>c1>>c2>>c3>>c4;
r=(c1-'0')*8+(c2-'0')*4+(c3-'0')*2+c4-'0';
if(in)
{
if(r<10)
{out<else
{
out<}
l++;
if(l%2==0){out<}
}
in.close();
out.close();
return0;
}
7.
有一个字符数组A[1024],用两个指针变量p和q分别指向A中的两个位置,请使用指针查找p和q之间(包含这两个位置所指字符)所包括的字符(ASCII值是从48到122)的个数。
算法设计:
(1)A的内容、p和q所指的位置通过键盘输入来确定,用户输入的位置是从0开始的两个序号,程序中的p,q为指针类型;
(2)查找字符时,不允许使用A数组本身以及下标变量,只能使用p或q指针来进行;
(3)统计结果保存到另一个int数组B中,并将初值定义为0,具体定义如下:
intB[123]={0};
运行结果:
请输入A的内容、p和q的位置:
abcdegassagh1331313188
330
计算结果如下:
0:
0,1:
4,2:
0,3:
4,4:
0,5:
0,6:
0,7:
0,8:
2,9:
0,:
:
0,;:
0,<:
0,=:
0,>:
0,?
:
0,@:
0,A:
0,B:
0,C:
0,
D:
0,E:
0,F:
0,G:
0,H:
0,I:
0,J:
0,K:
0,L:
0,M:
0,N:
0,O:
0,P:
0,Q:
0,R:
0,S:
0,T:
0,U:
0,V:
0,W:
0,
X:
0,Y:
0,Z:
0,[:
0,\:
0,]:
0,^:
0,_:
0,`:
0,a:
2,b:
0,c:
0,d:
1,e:
1,f:
0,g:
2,h:
1,i:
0,j:
0,k:
0,
l:
0,m:
0,n:
0,o:
0,p:
0,q:
0,r:
0,s:
2,t:
0,u:
0,v:
0,w:
0,x:
0,y:
0,z:
0,
注意:
输出结果中每项由字符和数量组成,中间用冒号“:
”隔开,项间用逗号“,”。
输出不分行。
参考答案:
#include
usingnamespacestd;
intmain()
{
cout<<"请输入A的内容、p和q的位置:
"<charA[1024];
cin.getline(A,1023);
inti=0;
cin>>i;
char*p=&A[i];
cin>>i;
char*q=&A[i];
intB[128]={0};
while(p<=q){
B[*p]++;
p++;
}
cout<<"计算结果如下:
"<for(i=48;i<123;i++){
cout<<(char)i<<':
'<
}
cout<return0;
}
8.
现有文本文件text.txt(请考生自己创建),文件各行前均有行号,只是行序已乱,文件内容如下:
2aaa
1one
6sixsix
4fff
3three
5f
请编写程序,读取文件内容,将各行按行号从小到大排列后写入新文件new.txt,同时将新文件内容在屏幕上显示,new.txt及屏幕显示内容如下:
1one
2aaa
3three
4fff
5f
6sixsix
参考答案:
第一种写法,针对仅有1位行号的情况
#include
#include
intmain()
{
charstr[10][80];
charln[10];
ifstreamin("text.txt");
if(!
in){
cout<<"Cannotopenfile.\n";
return1;
}
ofstreamout;
out.open("new.txt");
if(!
out){
cout<<"Cannotopenfile.";
return1;
}
inti=1;
while(in)
{
in.getline(str[i],80);
ln[i]=str[i][0];
i++;
}
for(intj=1;j<=i;j++)
{
for(intk=1;k<=i;k++)
{
if(ln[k]-'0'==j)
{
out<cout<}
}
}
in.close();
out.close();
return0;
}
第二种编写方法(使用数组):
#include
#include
usingnamespacestd;
intmain()
{
charstr[10][80];
intln[10];
ifstreamin("test81.txt");
if(!
in){
cout<<"Cannotopenfile.\n";
return1;
}
ofstreamout;
out.open("new.txt");
if(!
out){
cout<<"Cannotopenfile.";
return1;
}
inti=0;
in>>ln[i];
in.getline(str[i],80);
while(in)
{i++;
in>>ln[i];
in.getline(str[i],80);
}
//排序
for(intj=1;j
{
for(intk=i-1;k>=j;k--)
{
chart[80];
intm;
if(ln[k]{m=ln[k];
ln[k]=ln[k-1];
ln[k-1]=m;
strcpy(t,str[k]);
strcpy(str[k],str[k-1]);
strcpy(str[k-1],t);
}
}
}
for(intk=0;k
{
out<cout<}
in.close();
out.close();
return0;
}
第三种编写方法:
//使用结构类型
#include
#include
usingnamespacestd;
structstrline
{intln;
charstr[80];
};
intmain()
{
strlinestr[10];
ifstreamin("test81.txt");
if(!
in){
cout<<"Cannotopenfile.\n";
return1;
}
ofstreamout;
out.open("new.txt");
if(!
out){
cout<<"Cannotopenfile.";
return1;
}
inti=0;
in>>str[i].ln;
in.getline(str[i].str,80);
while(in)
{i++;
in>>str[i].ln;
in.getline(str[i].str,80);
}
//排序
for(intj=1;j
{
for(intk=i-1;k>=j;k--)
{
chart[80];
intm;
if(str[k].ln{m=str[k].ln;
str[k].ln=str[k-1].ln;
str[k-1].ln=m;
strcpy(t,str[k].str);
strcpy(str[k].str,str[k-1].str);
strcpy(str[k-1].str,t);
}
}
}
for(intk=0;k
{
out<cout<}
in.close();
out.close();
return0;
}
|
|
|
|