《C++语言程序设计》综合练习与期末复习题(4)

2025-04-28

};

bool operator = =(Worker &r1, Worker &r2){ if(strcmp(r1.name,r2.name)= =0) return true; else return false; }

void main()

{ Worker a[4]={{“abc”,25,420},{“def”,58,638}, {“ghi”,49,560},{“jkl”,36,375}};

Worker x={“ghi”}; for(i=0;i<4;i++)

if(a[i]= =x) break;

cout<

(18) #include

void main()

{int a[8]={3,5,7,9,11,13,15,17}; int *p=a;

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

{ cout<

if ((i+1)%4= =0) cout<

} }

(19) #include \

void main() {int s=0;

for(i=1;i<=6;i++) s=+i*i;

cout<<”s=”<

(20) #include

void LG(int*&a,int &m){ a=new int[m];

int* p=a;

for(int i=0;i

void main() { int *b,n=5;

16

?<

for(int i=0;i

cout<

四、写出下列每个函数的功能 (1) double SF(double x,int n){ //n为大于等于0的整数 double p=1,s=1;

for(int i=1;i<=n;i++){ p*=x;

s+=p/(i+1); } return s; }

(2) float FH(){

float x,y=0,n=0; cin>>x;

while(x!=-1){n++;y+=x;cin>>x;} if(n= =0)return y;

else return y/n; }

(3) #include {

for(int i=0;i

for(int j=i+1;j int x=a[i];a[i]=a[k];a[k]=x; } }

(4) #include #include void JB(char *fname){ //称为fname文件

//假定文件中保存着一批字符串,每个字符串长度均小于20

ifstream fin(fname); char a[20]; int i=0;

while(fin>>a){

cout<

cout<<”i=”<

(5) #include

17

void SA(int a,int b){

if(a>b) return 1;

else if(a= =b)return 0; else return –1;

}

(6)float FI(int n){ / /n>=1

float x,y=0; do{

cin>>x;n--;y+=x*x; }while(n>0); return y; }

(7)template

void WE(Type a[],Type b[],int n){ for(int i=0;i

(8)struct StrNode{

char name[15]; StrNode *next; };

void QB(StrNode *&f,int n){

if(n= =0){f=NULL;return;} f=new StrNode; cin>>f->name; StrNode *p=f; while(--n){

p=p->next=new StrNode; cin>>p->name;

}

p->next=NULL; }

(9) struct StrNode{

char name[15]; StrNode *next; };

void QC(StrNode *f){

while(f) {cout<name<next;} }

(10)int SC(int a,int b,int c){

if(a>=b && a>=c)return a; if(b>=a && b>=c)return b;

18

if(c>=a && c>=b)return c;

}

(11)bool WB(int a[],int n,int x){

for(int i=0;i

if (a[i]= =x)return true; return false; }

(12)#include int *LJ(int a[],int n){

int k=0;

for(int i=1;ia[k]) k=i; return &a[k];

}

(13)int SE(int n){

// n>=1

int x; cin>>x; if(n= =1) return x; int m=x;

while(--n){cin>>x;m+=x;} return m;

}

(14)int WC(int a[],int n,int k){

int c=0;

for(int i=0;i=k) c++; return c; }

(15) #include

#include #include

int JB(char *fname,char *key)

{//假定文件中保存着一批字符串,每个字符串长度均小于20 ifstream fin(fname);//称为fname文件 char a[20]; int i=0;

while(fin>>a)

if(strcmp(a,key)= =0) i++; fin.close(); return i; } (16) struct Worker{

char name[16]; int age;

19

float pay; };

istream &operator>>(istream &istr,Worker &x){ cout<<”请输入一个职工记录:”<>x.name>>x.age>>x.pay;

return iatr; }

(17)#include

void LI(int n){

int *a=new int[n];

for(int i=0;i>a[i];

for(i=n-1;i>=0;i--) cout<<*(a+i)<

}

(18) #include

#include #include int JC(char *fname) {//称为fname文件

ofstream fout(fname); char a[20]; cin>>a;

while(strcmp(a,”end”)! =0) { fout<>a; }

fout.close(); }

五、根据要求直接编写一个程序、一个函数

(1)编写函数void selectsort(int a[],int n),使数组元素升序排列(用选择法)。

(2) 编制一个函数:此函数能对一维数组内存放的10个学生成绩求平均成绩。(形参用数组)

(3) 编制一个函数:此函数能对一维数组内存放的10个学生成绩求平均成绩。(形参用指针)。 (4)编写函数int FF(int a[],int n),求出数组a的n个元素中大于等于平均值的元素个数并返回。

(5)有如下两个函数实例:

double Max(double x,double y){return (x>y)?x:y;} int Max(int x,int y){return (x>y)?x:y;} 请写出函数摸板。

(6)将下列程序分别用do?while和for 循环结构进行改写。 main()

20


《C++语言程序设计》综合练习与期末复习题(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:一轮辩论资料

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219