华中科技大学c++程序设计上机作业4.docx

上传人:b****4 文档编号:24923299 上传时间:2023-06-02 格式:DOCX 页数:9 大小:22.16KB
下载 相关 举报
华中科技大学c++程序设计上机作业4.docx_第1页
第1页 / 共9页
华中科技大学c++程序设计上机作业4.docx_第2页
第2页 / 共9页
华中科技大学c++程序设计上机作业4.docx_第3页
第3页 / 共9页
华中科技大学c++程序设计上机作业4.docx_第4页
第4页 / 共9页
华中科技大学c++程序设计上机作业4.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

华中科技大学c++程序设计上机作业4.docx

《华中科技大学c++程序设计上机作业4.docx》由会员分享,可在线阅读,更多相关《华中科技大学c++程序设计上机作业4.docx(9页珍藏版)》请在冰豆网上搜索。

华中科技大学c++程序设计上机作业4.docx

华中科技大学c++程序设计上机作业4

第4次上机

通知:

考试系统练习已经放出,请练习

考试题型与范围见网站通知http:

//115.156.150.83/tzgg/8047.htm

1.编写程序,根据用户输入的宽和高的数目,显示一个由"*"号组成的空心矩形

函数voidrectangle(intw,inth)用于打印该矩形,w为矩形的宽,h为矩形的高

运行结果参考:

2.使用指针编写一个对整型数组进行冒泡排序的函数voidbubbleup(int*ptr,intcount),ptr为指向数组的指针,count为数组实际长度。

并在主函数调用该函数完成对数组的冒泡排序

3.http:

//202.114.18.8:

8080/cshiyan/cpp/exp13.html

第3题

voidtrim(chars[]){

inti=0;

while(s[i]!

='\0')i++;

i--;

while(s[i]=='')s[i--]='\0';}

voidleftstring(chars1[],chars2[],intn){

inti;

for(i=0;i

s2[i]='\0';}

intindex(chars1[],chars2[]){

boolb=0;

inti,j,n=-1,n1=strlen(s1),n2=strlen(s2);

for(i=0;i

if(s1[i]==s2[0]){

b=1;

for(j=1;j

if(s1[i+j]!

=s2[j]){

b=0;

break;

}}}}if(b==1){n=i;break;}}returnn;

 

//1.编写程序,根据用户输入的宽和高的数目,显示一个由"*"号组成的空心矩形

//函数voidrectangle(intw,inth)用于打印该矩形,w为矩形的宽,h为矩形的高

//运行结果参考:

/*#include

#include

usingnamespacestd;

voidrectangle(intw,inth)

{

inti,j,d;

for(i=1;i<=w;i++)

{cout<<'*';}

cout<

for(j=1;j<=(h-2);j++)

{cout<<'*'<

for(d=1;d<=w;d++)

{cout<<'*';}

cout<

}

intmain(){

inta,b;

cout<<"请输入矩形的长和高(要求使用正整数,中间使用空格隔开)";

cin>>a>>b;

rectangle(a,b);

}*/

 

//2.使用指针编写一个对整型数组进行冒泡排序的函数

//voidbubbleup(int*ptr,intcount)

//,ptr为指向数组的指针,count为数组实际长度。

//并在主函数调用该函数完成对数组的冒泡排序

/*#include

usingnamespacestd;

voidbubbleup(int*ptr,intcount)

{

for(inti=0;i

for(intj=count;j>i;j--)

if(*(ptr+j-1)<*(ptr+j))

{

inta=*(ptr+j-1);

*(ptr+j-1)=*(ptr+j);

*(ptr+j)=a;

}

}

intmain()

{

inta,n;

cin>>n;

intq[200];

for(intx=0;x

cin>>q[x];

bubbleup(q,n);

for(ints=0;s

cout<

cout<

return0;

}*/

//1.范例:

求一个3×4矩阵中的最大元素,将求矩阵中的最大元素的过程定义为一个函数。

函数的第一个参数是矩阵本身

//,第二个参数是第一维的大小。

//这种方法的优点是使函数具有通用性,即无论一个矩阵的第一维是多大,只要该矩阵的第二维是4个元素,都可用该函数求最大元素;

//也可用该函数求一个矩阵开始几行中的最大元素。

 

//【程序】

/*#include

usingnamespacestd;

intmax_value(intarry[][4],intn);

intmain()

{

inta[3][4]={{1,3,6,7},{2,4,6,8},{15,17,34,12}};

cout<<"最大元素为"<

return0;

}

intmax_value(intarry[][4],intn)

{

inti,j,max=arry[0][0];

for(i=0;i

{for(j=0;j<4;j++)

{if(arry[i][j]>max)

max=arry[i][j];}}

cout<<"最大元素所在的行为"<

returnmax;

}

【要求】

修改上述程序使其不仅求矩阵中的最大元素而且还能求最大元素的行列值。

2.打印杨辉三角形(10行)。

使用二维数组并利用每个系数等于其上两系数之和。

 

#include

#include

usingnamespacestd;

constintn=11;

intmain()

{

inta[n][n],i,j;

for(i=0;i

{

a[i][0]=1;a[i][i]=1;

}

for(i=2;i

for(j=1;j

a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i

{

for(j=0;j<=i;j++)

cout<

cout<

}

return0;

}

 

*///3.分别编写实现下列字符数组处理的函数,用数组作为参数:

//

(1)voidtrim(chars[])//删除字符串s的尾部空格。

/*#include

usingnamespacestd;

voidtrim(chars[]);

intmain()

{

chara[100];

inti=0,j=0,e,g;

cout<<"请输入字符串:

";

cin.getline(a,100);

char*p=a;

intn=0;

while(*p++)

{n++;}//统计输入字符的长度。

trim(chara[n]);

for(inte=0;e

cout<

}

inttrim(chars[]);

for(i=0;i

{if(a[n-i]=='')

{

b[j]=a[i];

j++;

}

if(a[i]=='')

{

continue;

}

}

b[j]='\0';*/

 

#include

#include

usingnamespacestd;

voidtrim(chars[]);

voidleftstring(chars1[],chars2[],intn);

intindex(chars1[],chars2[]);

intmain()

{

charstr1[]="I'mstudent.",str2[]="student",str3[4];

intn;

cout<<"包含尾部空格的串str1:

"<

"<

trim(str1);//1

cout<<"无尾部空格的串str1:

"<

"<

//leftstring(str1,str3,3);//2

cout<<"串str3:

"<

"<

cout<<"串str2为:

"<

//n=index(str1,str2);//3

if(n!

=-1)cout<<"串str1包含子串str2,从第"<

"<

elsecout<<"串str1不包含字串str2"<

return0;}

voidtrim(chars[]){

inti=0;

while(s[i]!

='\0')i++;

//i--;

while(s[i]=='')s[i--]='\0';}

 

 

//

(2)voidlefstring(chars1[],chars2[],intn)//得到指定字符串s1中前n个字符的子串s2.

//(3)intindex(chars1[],chars2[])

//检查字符串s2是否为字符串s1的子串,根据查找结果返回s2在s1中的开始位置,如果s2不是s1的子串,则返回-1

//主程序如下,请编写相应函数。

//4.编程:

编写函数itoa0(intn,chars[],intb),将十进制整数n转换为以十六进制为基数的数字字符数组。

//*/

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1