2010-11江科大vc阶段测试7 - 10章

2026/1/14 18:25:17

10、执行下列程序后,程序的第一行输出为3,0,10,3,0,11,程序的第二行输出为2,3,10,2,3,15。

#include class B{ int a,b; public:

B(int x,int y=0) { a=x; b=y; } void fun(int=10); void fun(int,int);

};

void B::fun(int x){cout<

{ B b1(3); b1.fun(); b1.fun(5,6);

B b2(2,3); b2.fun(); b2.fun(7,8); }

11、执行下列程序后,程序的第二行输出为1 3 5,程序的第三行输出为

1 3 3,程序的最后一行输出为4 8 5。 [程序]

#include void main(void) {

int b[2][3],(*p1)[3],*p2; p1=b;

for(int i=0;i<2;i++){

for(int j=0;j<3;j++){

p1[i][j]=j+i*(i+j);

void B:: fun(int x,int y){cout<

cout<<*(*(p1+i)+j)<<'\\t'; }

cout<<'\\n';

}

for(p2=*b,i=0;p2<*b+2*3;p2++) { if(p2!=(int*)b+2*3-1) *p2+=*(p2+1);

cout<<*p2<<'\\t'; i++;

if(i%3==0) {cout<<'\\n';}

共8页,第5页

}

cout<<'\\n'; }

?

完善程序题

12、以下程序找出二维数组a中每列的最大值,并按一一对应的顺序放入一维数组b中。即第0列的最大值放入b[0],第1行的最大值放入b[1]…然后输出数组a和每列的最大值。请完善程序。

[程序]

#include

void fun(int(*p)[5],int n,int a[ ]) {

for(int i=0; i<5 ;i++){

a[i]= * (*p+i)或p[0][i] ; for(int j=0; j< n ;j++) if(p[j][i]>a[i])a[i]=p[j][i]; }

}

void main()

{int b[5] ,a[3][5]={23,4,45,65,5,65,8,87,3,26,7,68,4,97,243};

fun( a,3,b );

for(int i=0;i<3;i++){

for(int j=0;j<5;j++) cout<

cout<<\该列最大值:\for(i=0;i<5;i++) cout<< b[i]<<'\\t'; cout<<'\\n'; }

13、创建一条结点数据结构为struct NODE{ int x;NODE *next;};的有序链表(结点按x值的升序排序),参数n为结点个数,函数返回该链表的首指针。算法思想:每产生一个新生的结点,插入到链表的恰当位置,使链表仍然有序。最后输出链表。请完善程序。

[程序]

#include #define NULL 0

NODE* Create(int n)

共8页,第6页

{

NODE *p,*p1,*p2,*h=NULL; int i=0;

if(n<1)return NULL; while(i

p=new NODE ;

cin>>p->x; p->next=NULL; if(h==NULL) h= p ; else{

p1=p2=h;

while(p2&&p->x>=p2->x){ p1=p2; p2=p1->next ; }

if(p2==h){p->next=p2;h=p;} else{p->next=p2;p1->next=p;}

} i++; } return h;

}

14、定义一个函数int num(char *),删除一个字符串中的所有非数字字符,并将剩下的数字字符串逆序转换为一个整数。如字符串“a a 2hf34##5 6ga”经函数处理后字符串变为“23456”,函数的返回值为65432。在主函数中输出处理后的字符串及函数的返回值。

#include

#include

char *del(char *str){ int flag=0; for(char *p1=str;*p1;p1++){

if(*p1<'0'||*p1>'9') //处理非数字字符

}

for(char *p2=p1;*p2;p2++) {*p2=*(p2+1); flag=1;} if(flag==1) { p1--; flag=0;}

return str; } int value(char *str){

int s=0;

for(char *p=str+strlen(str)-1;p>=str;p--) s= s*10+*p-'0'; //转换数字字符串为整数 return s; }

共8页,第7页

void main(){

char s[]=\

cout<

cout<< del(s)<

三、编程题(2 0分)

建立一个类WORD,统计一个英文字符串中的英文单词个数。字符串中各英文单词以一个或多个空格分隔。如字符串“I am very happy today”

中的英文单词个数为5。 具体要求如下:

(1) 私有数据成员:

?

char str[80]:存放字符串str;

? int c: 存放字符串str中的英文单词的个数。

(2) 共有成员函数

? ?

WORD(char *s):构造函数,用参数s初始化str,同时初始化单词个数c为0;

void process(): 统计字符串中的英文单词个数;

? void print(): 输出字符串及单词个数。

在主函数中对该类进行测试。定义一个WORD类的对象w,并用字符串“I am very happy today”初始化对象w。通过w调用成员函数,统计并输出字符串中英文单词的个数。

#include #include class WORD{ char str[80]; int c; public:

WORD(char *s); void process(); void print(); };

WORD::WORD(char *s){ strcpy(str,s); c=0; }

共8页,第8页

void WORD::process(){ char *p=str;

while(*p){ while(*p!='\\0'&&*p==' ')p++; } }

void WORD::print(){ cout<

cout<<\}

void main(){

WORD w(\ w.process(); w.print(); }

if(*p!='\\0'&&*p!=' ')c++; while(*p!='\\0'&&*p!=' ')p++;

共8页,第9页


2010-11江科大vc阶段测试7 - 10章.doc 将本文的Word文档下载到电脑
搜索更多关于: 2010-11江科大vc阶段测试7 - 10章 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

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

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