西安交大C++考前练习1Word格式.docx
《西安交大C++考前练习1Word格式.docx》由会员分享,可在线阅读,更多相关《西安交大C++考前练习1Word格式.docx(14页珍藏版)》请在冰点文库上搜索。
for(inti=100;
i<
1000;
i++)
{
a=i/100;
b=i%100/10;
c=i%10;
if(a*a*a+b*b*b+c*c*c==1099)
{sum=sum+i;
cout<
+"
k++;
}
}
\b"
)/"
k<
="
doubleaverage=sum/k;
average<
endl;
return0;
}
2、2:
(20分)
定义5×
5二维数组,找出第m行中元素的最大值,并输出最大值元素以及所在的列号。
找出第n列中元素的最小值,并输出最小值元素以及所在的行号。
(m和n均在1~5之间)
(1)数组定义及产生正确(5分)
(2)求最大值及列号功能正确(5分)
(3)求最小值及行号功能正确(5分)
(4)输出格式正确,格式如下:
(5分)
5×
5数组为:
4587323454
5456767866
4678872343
1232445678
5566778899
输入m=2(即第3行),则程序输出:
第3行的最大值:
87,所在列号:
3
输入n=3(即第4列),则程序输出:
第4列的最小值:
23,所在行号:
程序源代码
inta[5][5];
for(inti=0;
5;
for(intj=0;
j<
j++)
cin>
>
a[i][j];
a[i][j]<
\t"
intm,n;
cin>
m;
intmax1,m1;
max1=a[m][0];
m1=0;
if(a[m][i]>
max1)
max1=a[m][i];
m1=i;
}
cout<
第"
m+1<
行最大元素:
max1<
所在列号:
m1+1<
n;
intmin,n1;
min=a[0][n];
n1=0;
for(intj=0;
if(a[j][n]<
min)
min=a[j][n];
n1=j;
n+1<
列最大元素:
min<
所在行号:
n1+1<
return0;
3、(必做题)第5章第18题(增加重载字符型)。
3:
编写一个函数,将某个字符串中所有非字母和数字字符均用*替代。
例如:
字符串“Iamagentlman_18!
”,形成字符串为“I*am*a*gentlman*18*”其原型为:
char*mycode(char*string);
其中参数string为字符串,返回值为指向string的指针。
并编出主函数进行验证。
(1)子函数头设计正确;
(2)子函数功能正确;
(3)主函数调用的方法正确;
(4)输出结果正确;
#include<
char*mycode(char*string)
while(*string!
=0)
if(*string>
='
a'
&
*string<
z'
)
*string=*string;
elseif(*string>
0'
9'
*string=*string;
else
*string='
*'
string++;
returnstring;
charstr[50];
请输入一段字符串:
cin.getline(str,50);
char*p;
p=str;
*mycode(p);
转换后得字符串:
p<
4、定义长方体类cuboid,应有两个构造函数(即cuboid()和cuboid(int,int,int))、计算体积函数、计算长方体总面积函数、运算符+重载函数(注意:
对应长宽高相加,仍然形成长方体),运算符==重载函数。
(1)给出满足要求的完整的长方体类的定义及其成员函数;
(2)+和==运算符重载函数编写正确;
(3)计算体积和总面积函数正确;
(4)编写主函数,测试输出正确(5分)
classcuboid
intlength,width,height;
public:
cuboid()
length=width=height=1;
cuboid(inta,intb,intc)
length=a;
width=b;
height=c;
intarea()
{return2*(length*width+length*height+width*height);
intv()
{returnlength*width*height;
cuboidoperator+(cuboid&
c)
length=length+c.length;
width=width+c.width;
height=height+c.height;
return*this;
intoperator==(cuboid&
a)
if(length==a.length&
width==a.width&
height==a.height)
return1;
return0;
voidshow()
************************************************"
长、宽、高依次为:
length<
width<
height<
};
cuboida(1,3,5);
a的信息"
a.show();
cuboidb(2,5,7);
cuboidc(1,3,5);
输出判断结果:
if(a==b)
a==b"
else
a!
=b"
if(a==c)
a==c"
=c"
输出计算长方体的面积:
"
a.area()<
输出计算长方体的体积:
a.v()<
a+b后的信息:
a+b;
5、5:
(20分)
编写一个程序,可以读入一个数据文件s_data中以N×
N二维数组排列的数据(N不小于3),将二维数组置逆,然后写入另一个文件t_data中。
假设s_data文件中数据如下:
45873234
54567678
46788723
12324456
则t_data文件中的数据如下:
45544612
87567832
32768744
34782356
(20分)
(1)程序开始执行时提示:
“Pleaseinputfilename:
”,能正确输入文件名;
(5分)
(2)文件打开和关闭正确;
(3)能正确读取文件数据;
(4)能正确产生输出文件。
fstream>
iomanip>
charname[15];
ifstreamfin("
F:
\\s-data.txt"
);
ofstreamfout;
cin>
name;
fout.open(name,ios:
:
app|ios:
out);
if(!
fin||!
fout)
读取失败"
return1;
intn;
inti=0;
inta[50];
while(fin)
fin>
if(fin)
a[i]=n;
i++;
intj=sqrt(i);
intb[10][10];
for(intk=0;
i;
k++)
b[k/j][k%j]=a[k];
for(k=0;
j;
for(intm=0;
m<
m++)
fout<
b[m][k]<
fin.close();
fout.close();