📄 xt4.htm
字号:
include <iostream.h><BR>void fun(float (*p)[6],int m)
//形参p为指向一维数组的指针变量<BR>{ float
sum=0,max=(*p)[0],min=(*p)[0],ave;<BR> for (int
i=0;i<m;i++)<BR> { for (int j=0;j<6;j++)
<BR> {<BR> sum+=(*p)[j]; //(*p)[j]表示a[i][j]的元素值<BR> if
((*p)[j]>max) max=(*p)[j];<BR> if ((*p)[j]<min)
min=(*p)[j];<BR> }<BR> p++;
//p++后,p将指向下一行<BR> }<BR> ave=sum/36;<BR> cout<<"min="<<min<<"max="<<max<<"ave="<<ave<<endl;<BR>}<BR>void
main(void)<BR>{ float a[6][6];<BR> int i,j;<BR> for
(i=0;i<6;i++)<BR> for(j=0;j<6;j++)<BR> cin>>a[i][j];<BR> fun(&a[0],6);
//实参为行指针,<BR> //形参为指向一维数组的指针<BR>}<BR>程序执行后输入1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36<BR>程序执行后输出为:<BR>min=1 max=36 ave=18.5</P>
<P class=xt12>4.36 用指针与数组作为函数参数,按如下四种情况用擂台法对一维实型数组进行降序排序。
<BR>(1)函数的实参为数组名,形参为数组。<BR>(2)函数的实参为数组名,形参为指针变量,。<BR>(3)函数的实参为指针变量,形参为数组。<BR>(4)函数的实参为指针变量,形参为指针变量。<BR>解:<BR>#include
<iostream.h><BR>void sort1( float a[ ],int n)
//形参为数组名<BR>{ int i,j,temp;<BR> for (i=0;i<n-1;i++)<BR> for
(j=i+1;j<n;j++)<BR> if (a[i]<a[j]) <BR> { temp=a[i];
a[i]=a[j]; a[j]=temp; }<BR>}<BR>void sort2( float *p,int n)
//形参为指针<BR>{ int i,j,temp;<BR> for (i=0;i<n-1;i++)<BR> for
(j=i+1;j<n;j++)<BR> if (*(p+i)<*(p+j)) <BR> {
temp=*(p+i); *(p+i)=*(p+j); *(p+j)=temp; }<BR>}<BR>void sort3(
float a[ ],int n) //形参为数组名<BR>{ int i,j,temp;<BR> for
(i=0;i<n-1;i++)<BR> for (j=i+1;j<n;j++)<BR> if
(*(a+i)<*(a+j)) <BR> { temp=*(a+i); *(a+i)=*(a+j);
*(a+j)=temp; }<BR>}<BR>void sort4( float *p,int n) //
形参为指针<BR>{ int i,j,temp;<BR> for (i=0;i<n-1;i++)<BR> for
(j=i+1;j<n;j++)<BR> if (p[i]<p[j]) <BR> { temp=p[i];
p[i]=p[j]; p[j]=temp; }<BR>}<BR>void main( void)<BR>{ float
a1[6]={1,3,2,5,4,6},*pi,i;<BR> float
a2[6]={1,3,2,5,4,6};<BR> float a3[6]={1,3,2,5,4,6};<BR> float
a4[6]={1,3,2,5,4,6};<BR> sort1(a1,6);
//实参为数组名,形参为数组<BR> sort2(a2,6);
//实参为数组名,形参为指针变量<BR> pi=a3;<BR> sort3(pi,6) ;
//实参为指针变量,形参为数组<BR> pi=a4;<BR> sort4(pi,6) ;
//实参为指针变量,形参指针变量<BR> for (i=0;i<6;i++)
cout<<a1[i]<<'\t';<BR> cout<<endl;<BR> for
(i=0;i<6;i++)
cout<<a2[i]<<'\t';<BR> cout<<endl;<BR> for
(i=0;i<6;i++)
cout<<a3[i]<<'\t';<BR> cout<<endl;<BR> for
(i=0;i<6;i++)
cout<<a4[i]<<'\t';<BR> cout<<endl;<BR>}<BR>程序执行后输出结果:<BR>6
5 4 3 2 1 <BR>6 5 4 3 2 1 <BR>6 5 4 3 2 1 <BR>6 5 4 3 2 1 </P>
<P class=xt12>4.37设计返回指针值的函数,将输入的一个字符串按逆向输出。<BR>解:<BR>#
include <iostream.h><BR># include
<string.h><BR>char *fun(char * p1)<BR>{
<BR> while(*p1++); //指针p1移到s1的串尾 <BR> p1--;<BR> return p1;
//返回指向目标串尾地址的指针p1 <BR>}<BR>void main(void)<BR>{ char
s1[100],*q,*p1;<BR> cout<<"输入一个字符串:";<BR> cin.getline(
s1,100);<BR> cout<<"逆向输出的字符串:";<BR> q=s1;<BR> p1=fun(s1);<BR>do<BR>{<BR> cout<<*p1;
//字符串按逆向输出<BR> p1--;
<BR> }while(q<=p1);<BR>}<BR>程序执行后输入abcdef<BR>程序执行后输出为:<BR>fedcba</P>
<P
class=xt12>4.38将字符串存入字符数组s,然后将s中的每一个字符按例7.20中程序加密,设计对应的解密程序,解密函数为:<BR>s[i]
+16 ; 当32≤s[i]≤41 ;<BR>s[i] = s[i] +23 ; 当 42≤s[i]≤67;<BR>s[i]
-5 ; 当102≤s[i]≤127
;<BR>其中s[i]为字符串中第i个字符的ASCII码。输入的字符串只能由字符或数字组成。<BR>解:<BR>#
include <iostream.h><BR># include
<string.h><BR>char *encrypt(char *pstr)
//定义加密函数为返回指针值的函数<BR>{ char *p=pstr;
//保存字符串首地址到指针变量p<BR> while (*pstr)<BR> { if (*pstr>=32
&& *pstr<=41) *pstr+=16;
//用指针变量*pstr表示s[i],<BR> else if (*pstr>=42 &&
*pstr<=67) *pstr+=23; //进行加密处理<BR> else if (*pstr>=102
&& *pstr<=127) *pstr-=5;<BR> pstr++;
//指针变量加1,指向下个单元 <BR> }<BR> return p; //返回字符串指针<BR>}<BR>void
main(void)<BR>{ char s[80];<BR> cin.getline(s,80);
//输入字符串到数组s<BR> cout<< encrypt (s)<<endl; //
将字符串加密后输出 <BR>}<BR>程序执行时输入:<BR>123ABC <BR>加密后输出:<BR>HIJXYZ</P>
<P><SPAN class=xt12>4.39
设计程序,用函数指针变量完成两个操作数的加、减、乘、除四则运算。<BR>解:<BR># include
<iostream.h><BR># include <stdlib.h><BR>float
add(float a,float b) //定义求和函数<BR>{ <BR> return
a+b;<BR>}<BR>float sub(float a,float b) //定义求减函数<BR>{
<BR> return a-b;<BR>}<BR>float cheng(float a,float b)
//定义求和函数<BR>{ <BR> return a*b;<BR>}<BR>float chu(float a,float
b) //定义求和函数<BR>{ <BR> return a/b;<BR>}<BR>void main(void)<BR>{
float x,y;<BR> float (*p1)(float,float);
//定义函数指针变量<BR> cin>>x>>y;<BR> p1=add;
//将函数add的入口地址赋给p1
<BR> cout<<"和="<<p1(x,y)<<endl;
//对p1()的调用就是对add()的调用<BR> p1=sub; //将函数sub的入口地址赋给p1
<BR> cout<<"减="<<p1(x,y)<<endl;
//对p1()的调用就是对sub()的调用<BR> p1=cheng; //将函数cheng的入口地址赋给p1
<BR> cout<<"乘积="<<p1(x,y)<<endl;
//对p1()的调用就是对cheng()的调用<BR> p1=chu; //将函数chu的入口地址赋给p1
<BR> cout<<"除="<<p1(x,y)<<endl;
//对p1()的调用就是对chu()的调用<BR>}</SPAN><BR><SPAN
class=xt12>程序执行时输入:<BR>3 4
<BR>程序执行后输出:<BR>和=7<BR>减=-1<BR>乘积=12<BR>除=0.75</SPAN></P>
<P class=xt12>4.40
定义一个指向字符串的指针数组,用一个函数完成n个不等长字符串的输入,根据实际输入的字符串长度用new运算符分配存储空间,依次使指针数组中的元素指向每一个输入的字符串。设计一个完成n个字符串按升序排序的函数(在排序过程中,要求只交换指向字符串的指针值,不交换字符串)。在主函数中实现将排序后的字符串输出。<BR>解:<BR>#
include <iostream.h><BR># include
<string.h><BR>const int N=5;<BR>void input(char *p[],int
n)<BR>{ char s[50];<BR> int i;<BR> for (i=0;i<n;i++)<BR> {
cout<<"Input
String["<<i<<"]:";<BR> cin>>s;<BR> p[i]=new
char[strlen(s)+1];<BR> strcpy(p[i],s);<BR> }<BR>}<BR>void
sort(char *p[],int n)<BR>{ int i,j,k;<BR> char *pt;<BR> for
(i=0;i<n-1;i++)<BR> {
k=i;<BR> for(j=i+1;j<n;j++)<BR> if
(strcmp(p[k],p[j])>0) k=j;<BR> if (i!=k)<BR> {
pt=p[k];p[k]=p[i];p[i]=pt;}<BR> }<BR>}<BR>void main(void)<BR>{
char *p[N];<BR> int i;<BR> input(p,N);<BR> sort(p,N);<BR> for
(i=0;i<N;i++)<BR> {
cout<<p[i]<<endl;<BR> delete
[]p[i];<BR> }<BR>}</P>
<P class=xt12>4.41
编写求一维数组平均值的函数,用引用类型变量作为函数参数返回平均值。在主函数中输入数组元素值,并输出平均值。<BR>#
include <iostream.h><BR>void average(float &rave,int
a[ ],int m) //将形参rave定义为整形引用变量。<BR>{ int sum=0
,i;<BR> for(i=0;i<5;i++)<BR> sum=sum+a[i];<BR> rave=sum/5;<BR>}<BR>void
main (void)<BR>{ int a[5],i;<BR> float
ave;<BR> for(i=0;i<5;i++)<BR> cin>>a[i];
<BR> average(ave,a,5);
<BR> cout<<"ave="<<ave<<endl;<BR>}<BR>程序执行时输入:<BR>1
2 3 4 5 <BR>程序执行后输出:<BR>ave=3</P>
<P class=xt12>4.42
用引用类型变量作为函数参数,编写对三个变量进行按升序排序的函数,在主函数中输入三个变量的值,输出排序后三个变量的值。<BR>解:<BR>#
include <iostream.h><BR>void sort(int &rx,int
&ry,int &rz) //将形参rx、ry、rz定义为整形引用变量。<BR>{ int
temp;<BR> if (rx>ry)<BR> {
temp=rx;rx=ry;ry=temp;}<BR> if(rx>rz)<BR> {temp=rx;rx=rz;rz=temp;}<BR> if(ry>rz)<BR> {temp=ry;ry=rz;rz=temp;}<BR>}<BR>void
main (void)<BR>{ int x,y,z;<BR> cout<<"输入X
,Y与Z:"<<endl;<BR> cin>>x>>y>>z;<BR> sort(x,y,z);
<BR> cout<<"x="<<x<<'\t'<<"y="<<y<<'\t'<<"z="<<z<<endl;<BR>}<BR>程序执行时输入:<BR>3
2 1 <BR>程序执行后输出:<BR>x=1 y=2 z=3<BR></P></TD></TR></TBODY></TABLE><!-- InstanceEndEditable --></TD></TR></TBODY></TABLE></TD></TR>
<TR vAlign=center bgColor=#eeeeee>
<TD colSpan=3 height=28>
<DIV align=center><A
onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.nuist.edu.cn')"
href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt4.htm#"><SPAN
class=mb14>设为首页</SPAN></A><STRONG>|</STRONG><A class=mb14
href="javascript:addme()">加入收藏</A><STRONG>|</STRONG><A
href="http://www.nuist.edu.cn/"><SPAN
class=mb14>学校首页</SPAN></A><STRONG>|</STRONG><A
href="http://www.nuist.edu.cn/jsj"><SPAN
class=mb14>计科系首页</SPAN></A></DIV></TD></TR>
<TR>
<TD class=mb14 vAlign=top bgColor=#176bab colSpan=2 height=88>
<DIV align=center><BR><FONT color=#ffffff>Copyright© 2004-2005 Depart of
Computer Science & Technology Nuist</FONT></DIV></TD>
<TD width=15 bgColor=#176bab> </TD></TR>
<TR>
<TD height=1></TD>
<TD width=581></TD>
<TD></TD></TR></TBODY></TABLE><MAP name=Map><AREA shape=RECT coords=9,9,75,25
href="http://www.nuist.edu.cn/JSJ/syjx/index.htm"><AREA shape=RECT
coords=86,9,150,26 href="http://www.nuist.edu.cn/JSJ/syjx/jxdg.htm"><AREA
shape=RECT coords=157,9,220,27
href="http://www.nuist.edu.cn/JSJ/syjx/skja.htm"><AREA shape=RECT
coords=230,9,310,26 href="http://www.nuist.edu.cn/JSJ/syjx/dmt.htm"><AREA
onclick="MM_goToURL('parent','../xt.htm');return document.MM_returnValue"
shape=RECT coords=320,10,397,26
href="http://www.nuist.edu.cn/JSJ/syjx/xt.htm"><AREA
onclick="MM_showHideLayers('Layer1','','hide','Layer2','','show')" shape=RECT
coords=405,10,469,25 href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt4.htm#"><AREA
onclick="MM_showHideLayers('Layer1','','show','Layer2','','hide')" shape=RECT
coords=476,10,538,26 href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt4.htm#"><AREA
shape=RECT coords=549,10,612,26
href="http://www.nuist.edu.cn/JSJ/syjx/ckwx.htm"><AREA shape=RECT
coords=620,9,684,26
href="http://www.nuist.edu.cn/JSJ/syjx/zlxz.htm"></MAP></CENTER><!-- InstanceEnd --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -