资源描述
·34·
Error! Reference source not found.
习 题 5
一、选择题
1. 下列叙述错误的是____A_____。
A. 主函数中定义的变量在整个程序中都是有效的
B. 复合语句中定义的变量只在该复合语句中有效
C. 其它函数中定义的变量在主函数中不能使用
D. 形式参数是局部变量
2. 若函数的形参为一维数组,则下列说法中错误的是____B、才_____。
A. 形参数组可以不指定大小
B. 函数调用时对应的实参只能是数组名
C. 函数调用时,系统会为形参数组分配存储单元
D. 函数中对形参的修改将会影响对应的实参值
3. 若函数的类型和return语句中的表达式的类型不一致,则____D_____。
A. 编译时出错
B. 运行时出现不确定结果
C. 不会出错,且返回值的类型以return语句中表达式的类型为准
D. 不会出错,且返回值的类型以函数类型为准
4. 下面的函数定义正确的是_____D____。
A. float f(float x;float y) B. float f(float x,y)
{return x*y;} {return x*y;}
C. float f(x,y) D. float f( int x, int y)
{int x,y ; return x*y;} {return x*y;}
5. 下面函数头的定义格式正确的是____C_____。
A. void sort(int a[n],int n) B. void sort(int a[ ][ ],int n)
C. void sort(int a[ ],int n) D. void sort(int a[ ],n)
(2). #include "iostream.h"
void cube(int &x)
{ x=x*x*x; }
void main()
{
int x=5; cube(x);
cout<<x;
}
6. 下面4个程序中输出结果是125的有____C_____。
(1). #include "iostream.h"
void cube(int x)
{ x=x*x*x; }
void main()
{
int x=5;cube(x);
cout<<x;
(4). #include "iostream.h"
int x=5;
void cube()
{ x=x*x*x;}
void main()
{ cube(); cout<<x; }
}
(3). #include "iostream.h"
int cube(int x)
{ x=x*x*x; return(x); }
void main()
{
int x=cube(5);
cout<<x;
}
A. 1 B. 2 C. 3 D. 4
7. 设函数m()的说明形式为void m(int,int *); 利用函数m()对数5和整数j作函数m()定义的计算,正确的调用形式为_____C_____。
A. m(&5,&j) B. m(5,j) C. m(5,&j) D. m(&5,j)
8. 设函数的说明为: void fun(int a[],int m); ,若有定义:int a[10],n,x; 则下面调用该函数正确的是____A_____。
A. fun( a, n); B. x=fun( a, n); C. fun( a[10], 10); D. x=fun( a[], n);
9. 下面函数说明正确的是____C_____。
A. void f1(int a=3, int b, int c); B. void f2 int a, int b=3, int c);
C. void f3(int a, int b, int c=3); D. void f4(int a, int b, int 3);
10. 有两个函数分别为: int f(int);和int f(int,int =100);,则下面说法正确的是_____B_____。
A. 不能在同一个程序中定义
B. 可以在同一个程序中定义,但不可以重载
C. 可以在同一个程序中定义并可重载
D. 以上说法均错误
11. 以下几种函数模板的定义正确的是____A_____。
B. template <class T1,T2 >
void fun1(T1 a,T1 b,T2 c)
{…… }
A. template <class T>
T fun1(T a,int b)
{…… }
D. template <class T1,class T2 >
T2 fun1(T1 a,T1 b)
{ …… }
C. template <class T>
void fun1(int a,int b)
{T i; …… }
12. 下面程序的输出结果是____B_____。
#include "iostream.h"
int m=10;
void f(int m,int &n)
{ m=m+2; n=n+2;}
void main()
{
int n=5;
f(m,n);
cout<<"m="<<m<<" n="<<n<<endl;
}
A. m=10 n=5 B. m=10 n=7 C. m=12 n=7 D. m=12 n=5
二、阅读程序,写出运行结果
1. yes 3 not 4
2. 4
3. 12
25 16 9 2 1
4. 21234
5. m=5n=3
6. 911
三、程序填空
1. 该程序功能:对x=1,2,...,10,求f(x)=x*x-5*x+sin(x)的最大值。
#include "iostream.h"
#include "math.h"
float f(int x)
{
float y;
y=x*x-5*x+sin(x);
______return y _______;
}
void main()
{
int x; float max;
____max=f(1)___;
for(x=2;x<=10;x++)
___if(f(x)>max)max=f(x)__ ;
cout<<max<<endl;
}
2. 函数backmove()是把字符指针x所指的字符串平移m个字符,即将最后m个字符移到串首。如“abcdefghij”, 平移3个字符,成“hijabcdefg”。
#include "stdio.h"
#include "string.h"
void backmove(char *x,int m)
{
int i,j,n;char w;
n=strlen(x);
for(j=0;j<m;j++)
{ w=_____*(x+n-1)_____;
for(i=0;i<n-1;i++) *(x+n-1-i)=_____ *(x+n-2-i)_____;
____*x____=w;
}
}
void main()
{
char s[20];
gets(s);
______backmove(s,3)______; //假设平移3个字符
puts(s);
}
3. 函数index()为查找字符串sub是否是字符串st的子串。若是,返回sub在st中首次出现的下标,否则返回-1。字符串sub和st非空。如sub: " cd ",st: "abcdefcd ",返回2。
#include "iostream.h"
#include "stdio.h"
void main()
{
char s1[80],s2[80];
______ int index(char [],char []);______;
gets(s1);gets(s2);
if(_______index(s1,s2)________)
cout<<"子串在字符串中首次出现的下标:"<<index(s1,s2);
else
cout<<"找不到";
}
int index(char st[],char sub[])
{
int i,j,k;
for(i=0;st[i]!='\0';i++)
{
for(j=i,k=0;sub[k]!= '\0'&&st[j]==sub[k] ;____ k++,j++_____);
if(sub[k]== '\0')_____ return(i)______;
}
return 0;
}
4. 函数root为用二分法求方程f(x)=0在[x1,x2]的实根,精度为eps。二分法求根的基本思想为 f(x)在区间[a,b]上连续,f(a) 与 f(b)异号,区间中点 c=(a+b)/2 的 f(c) 符号和 f(a) 符号确定 c 代替 a 或 b,使根所在区间每次减半,直到|a- b|<eps或|f(c)|<eps,c 即为所求的根,其中eps为精度。下面程序为求方程x3-5x2+16x-80=0在[1,7]区间的实根,精度取10-6。
#include "math.h"
#include "iostream.h"
double root(double x1, double x2, double eps=1e-6)
{
_______double f(double x);_______ ;
double x,y,y1;
y1=f(x1);
do{
x=0.5*(x1+x2);
y=f(x);
if(y1*y>0.0)_____ x1=x_____;
else if (y1*y<0.0) _____x2=x_____;
}while(fabs(y)>=eps___&&___fabs(x2-x1)>=eps);
return(x);
}
double f(double x)
{return x*x*x-5*x*x+16*x-80;}
void main()
{cout<<_____root(1,7)_____<<endl;}
5.随机生成10个1~100之间的数放在一维数组中,求其平均值及最大的元素值。
#include "iostream.h"
#include "stdlib.h"
const int N=10;
void fun(float *p,float *p1,float *p2)
{
float sum,max1;
_______max1=*p________;
for(int i=1;i<N;i++)
{
if (max1<*p) max1=*p;
sum=sum+*p;
p++;
}
_______*p1=max1_________;
_______*p2=sum/N_________;
}
void main()
{
float a[10],aver,max,x;
for(int i=0;i<10;i++)
{
x=rand() % 100+1; a[i]=x;
}
for(i = 0;i<10;i++) cout<<a[i]<<" "; cout<<endl;
_______fun(a,max,aver)______;
cout<<"平均值:"<<aver<<" 最大值:"<<max<<endl;
}
6. 函数convert的功能是将一个十进制整数转换为二到十六进制的字符串。
#include "iostream.h"
void convert(int m,int h,char ch[])
{
char b[17]="0123456789ABCDEF";
int c[10],i=0,k=0;
do
c[ ___i++__ ]=m%h;
while(____(m=m/h)!=0_____);
for(--i;i>=0;--i)
{ ch[k++]=b[ ___c[i]___ ]; }
______ch[k]='\0'_______;
}
void main()
{
char ch[10];int m,h;
cin>>m>>h;
convert(m,h,ch);
cout<<ch<<endl;
}
四、编写程序
1. 编写函数,功能为将字符串s中的字符c1用字符c2替换,并加以调用。函数形式为:
void match(char s[], char c1,char c2);
#include <iostream.h>
void replace(char s[],char c1,char c2)
{char *p=s;
while(*p!='\0')
{
if(*p==c1)
*p=c2;
p++;
}
}
void main()
{char s[80],c1,c2;
cin>>s;
cin>>c1>>c2;
replace(s,c1,c2);
cout<<s<<endl;
}
2. 编写函数,功能为求圆的周长和面积。函数分别定义为如下形式:
double area(double r, double * girth ,double pi=3.14159);
void fun(double r, double &girth ,double &area,double pi=3.14159);
分别编二个程序实现,半径从键盘输入。
方法一、
#include <iostream.h>
double area(double r,double *girth,double pi=3.14159)
{
*girth=2*pi*r;
return(pi*r*r);
}
void main()
{double r,len,s;
cin>>r;
s=area(r,&len);
cout<<"len="<<len<<",s="<<s<<endl;
}
方法二
#include <iostream.h>
void fun(double r,double &girth,double &area,double pi=3.14159)
{
girth=2*pi*r;
area=pi*r*r;
}
void main()
{double r,len,s;
cin>>r;
fun(r,len,s);
cout<<"len="<<len<<",s="<<s<<endl;
}
3. 编写函数,功能是求二维数组中最大元素所在的行号和列号,再编写主函数调用之。
#define SIZE1 3
#define SIZE2 4
#include "iostream.h"
#include "stdlib.h"
float max_value(float x[][4],int &ii,int &jj)
{
float max=x[0][0];
for(int i=0;i<SIZE1;i++)
for(int j=0;j<SIZE2;j++)
if(x[i][j]>max)
{max=x[i][j]; ii=i; jj=j; }
return(max);
}
void main()
{
int i,j,t1,t2;float a[SIZE1][SIZE2];
cout<<"enter the array:\n";
for(i=0;i<SIZE1;i++)
{ for(j=0;j<SIZE2;j++)
{ a[i][j]=rand()%101;
cout<<a[i][j]<<" ";}
cout<<endl;
}
cout<<"max value is" << max_value(a,t1,t2);
cout<<" line="<<t1<<" row="<<t2<<endl;
}
4. 编写函数,将两个字符串s和t的前n个字符拼接成新的字符串,结果存放在s中。如果s或t中字符串的长度不足n,按实际长度处理。例如,如果有"ABCDEFGH"和"abcdefghijk",n为3,则新的字符串为”ABCabc”,并加以调用。函数形式为:
void mystrcat(char s[],char t[],int n);
#include <iostream.h>
#include <string.h>
void mystrcat(char s[],char t[],int n)
{
int l1=strlen(s),l2=strlen(t);
int k1=l1>n?n:l1,k2=l2>n?n:l2;
for(int i=0;i<k2;i++)
s[k1++]=t[i];
s[k1]='\0';
}
void main()
{
char s[30],t[30];
int n;
cin>>s>>t;
cin>>n;
mystrcat(s,t,n);
cout<<s<<endl;
}
5. 编写函数,其功能是逐字符比较两个字符串s1和s2,并将s1中第一个与s2不相同字符的地址返回给主函数。再编写主函数调用该函数,并在主函数中输出s1从这个位置开始的子串。函数形式为:
char *dif(char s1[],char s2[]);
#include <iostream.h>
#include "stdio.h"
char *dif(char s1[],char s2[])
{
int i=0;
while(s1[i]==s2[i]&&s1[i]!='\0')
i++;
if(s1[i]!='\0')
return(&s1[i]);
else return NULL;
}
void main()
{
char s[30],t[30];
cin>>s>>t;
char *p;
if((p=dif(s,t))!=NULL)
cout<<p<<endl;
else
cout<<"s包含于t中\n";
}
6. 用递归方法求正整数m,n的最大公约数。
#include <iostream.h>
int gcd(int m,int n)
{
int r=m%n;
if(r!=0)
return gcd(n,r);
else
return(n);
}
void main()
{
int m,n;
cin>>m>>n;
cout<<gcd(m,n)<<endl;
}
7. 编写四个同名函数max,分别求两个整数、三个整数,两个双精度数、三个双精度数的最大值。
#include <iostream.h>
int max(int a,int b)
{
return(a>b?a:b);
}
int max(int a,int b,int c)
{
int t=max(a,b);
return(max(t,c));
}
double max(double a,double b)
{
return(a>b?a:b);
}
double max(double a,double b,double c)
{
double t=max(a,b);
return(max(t,c));
}
void main()
{double x,y,z;
int a,b,c;
cin>>a>>b>>c;
cin>>x>>y>>z;
cout<<max(a,b)<<endl<<max(a,b,c)<<endl;
cout<<max(x,y)<<endl<<max(x,y,z)<<endl;
}
第四章习题
一、
1.C 2.A 3.C 4.D 5.C 6.B 7.A 8.B
二、
1. 6789054321 2. 4 3. 14
三、
1.
(1){0,1} (3)x[i]=x[i-1]+x[i-2] (4)setw(5)<<x[i]
2.
(1)rand()%101 (2)j<a[i]/2 (3)"A("<<i<<")="
3.
(1)b[m]=0 (2)n<m+4 (3)b[m]=b[m]/4
4.(该题目需要加上头文件#include "stdio.h")
(1)gets(s1) (2)gets(s2) (3)*s1==*s2 (4) r=0
5.
(1)j=k=l=0 (2)j<M&&k<N (3)c[l]=a[j] (4)c[l++]=b[k++] (5)k<N (6)j<M
四、
1.(1)
#include "stdlib.h"
#include "iostream.h"
void main()
{int a[4][4],b[4][4],c[4][4],i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
b[i][j]=rand()%35+101;
cout<<"矩阵A的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
cout<<"矩阵B的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<b[i][j]<<' ';
cout<<endl;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"矩阵C的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<c[i][j]<<' ';
cout<<endl;
}
}
(2)
#include "stdlib.h"
#include "iostream.h"
void main()
{int a[4][4],b[4][4],c[4][4],i,j,max,imax,jmax;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
b[i][j]=rand()%35+101;
cout<<"矩阵A的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
cout<<"矩阵B的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<b[i][j]<<' ';
cout<<endl;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"矩阵C的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<c[i][j]<<' ';
cout<<endl;
}
max=c[0][0];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(c[i][j]>max)
{max=c[i][j];
imax=i;
jmax=j;
}
cout<<"max=c["<<imax<<"]["<<jmax<<"]="<<max<<endl;
}
(3)
#include "stdlib.h"
#include "iostream.h"
void main()
{int a[4][4],b[4][4],i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
b[i][j]=rand()%35+101;
cout<<"矩阵A的主对角线以下元素内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<=i;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
cout<<"矩阵B的主对角线以上元素内容如下:\n";
for(i=0;i<4;i++)
{for(j=i;j<4;j++)
cout<<b[i][j]<<' ';
cout<<endl;
}
}
(4)
#include "stdlib.h"
#include "iostream.h"
void main()
{int a[4][4],i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
cout<<"矩阵A的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
for(j=0;j<4;j++)
{int t=a[0][j]; a[0][j]=a[2][j]; a[2][j]=t;}
cout<<"改变后矩阵A的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
}
(5)
#include "stdlib.h"
#include "iostream.h"
void main()
{int a[4][4],i,j,s=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%41+30;
cout<<"矩阵A的内容如下:\n";
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j||i+j==3)
s=s+a[i][j];
cout<<"对角线元素之和为:"<<s<<endl;
}
2.
用字符数组实现
#include "stdio.h"
void main()
{int i;char s1[50],s2[20];
gets(s1);
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
puts(s2);
}
用字符指针实现
#include "stdio.h"
void main()
{int i;char s1[50],*p1=s1,*p2;
p2=new char[20];
gets(p2);
while(*p2!='\0')
*p1++=*p2++;
*p1='\0';
puts(s1);
}
3.
#include "stdio.h"
void main()
{ char s[80],ch;
int i,len=0;
gets(s);
while(s[len]!='\0')len++;
for(i=0;i<len/2;i++)
{ch=s[i];
s[i]=s[len-i-1];
s[len-i-1]=ch;
}
puts(s);
}
4.
#include "stdio.h"
#include "iostream.h"
void main()
{char s[80],ch;
int i,j;
gets(s);
cin>>ch;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=ch)
{s[j]=s[i];j++;}
s[j]='\0';
puts(s);
}
第六章习题
一、选择题
1、A(在C语言中int占2个字节) 2、D 3、B C 4、D 5、a[1][0].ch
6、A 7、C 8、B 9、A 10、A
二、程序填空
1、stu[i].score k=j p[k]=p[i] p[i]=temp
2、p!=NULL p->data p->next
3、struct node * s->data=ch r=s NULL
三、编程题
1、
#include "iostream.h"
struct staff
{char num[6];
char name[8];
float salary[3]; //分项工资
float gs; //实得工资
}s[100];
void main( )
{
int i,j,n;
cin>>n; /*输入职工人数*/
for(i=0;i<n;i++)
{
cin>>s[i].num>>s[i].name;
for(j=0;j<3;j++)
cin>>s[i].salary[j];
}
cout<<"NO. salary\n";
for(i=0;i<n;i++)
{
s[i].gs= s[i].salary[0]+ s[i].salary[1]- s[i].salary[2] ;
cout<<s[i].num<<'\t'<<s[i].gs<<endl;
}
}
2、
#include "iostream.h"
#include "stdio.h"
struct str
{
char ch;
struct str *next;
};
void main()
{
char s[100];
int i=0,num=0;
struct str *insert,*head=NULL,*p;
gets(s);
while(s[i]!='\0')
{
insert=new str;
insert->ch=s[i];
if(head==NULL)
{
head=insert ;
head->next=NULL;
}
else
{
insert->next=head ;
head=insert;
}
i++;
}
p=head;
while(p!=NULL)
{
if(p->ch>='A'&&p->ch<='Z')
num++;
cout<<p->ch;
p=p->next;
}
cout<<endl<<"num="<<num<<endl;
}
3、
#include "iostream.h"
#include "stdio.h"
struct node
{
char ch;
int count;
struct node *next;
};
void main( )
{
struct node *head,*p1,*p2,*insert;
char c;
head=NULL;
while((c=getchar())!='\n')
{
p1=head;
while(p1!=NULL&&c>p1->ch)
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)
{
insert=new node;
insert->ch=c;
insert->count=1;
if(head==NULL)
{
insert->next=head;
head=insert;
}
else
{
insert->next=NULL;
p2->next=insert;
}
}
else if(c==p1->ch)
p1->count++;
else
{
insert=new node;
insert->ch=c;
insert->count=1;
if(p1==head)
{
insert->next=head;
head=insert;
}
else
{
insert->next=p1;
p2->next=insert;
}
}
}
p
展开阅读全文