c++试题3

16.In the main() function, after ModifyString(text) is called, what’s the value of ‘text’?

int FindSubString( char* pch )
{
    int   count  = 0;
    char  * p1   = pch;
    while ( *p1 != '\0' )
    {    
        if ( *p1 == p1[1] - 1 )
        {
            p1++;
            count++;
        }else  {
            break;
        }
    }
    int count2 = count;
    while ( *p1 != '\0' )
    {
        if ( *p1 == p1[1] + 1 )
        {
            p1++;
            count2--;
        }else  {
            break;
        }
    }
    if ( count2 == 0 )
        return(count);
    return(0);
}
void ModifyString( char* pText )
{
    char  * p1   = pText;
    char  * p2   = p1;
    while ( *p1 != '\0' )
    {
        int count = FindSubString( p1 );
        if ( count > 0 )
        {
            *p2++ = *p1;
            sprintf( p2, "%i", count );
            while ( *p2 != '\0' )
            {
                p2++;
            }
            p1 += count + count + 1;
        }else  {
            *p2++ = *p1++;
        }
    }
}
void main( void )
{
    char text[32] = "XYBCDCBABABA";
    ModifyString( text );
    printf( text );
}  

XYBCDCBABABA
XYBCBCDA1BAA
XYBCDCBA1BAA
XYBCDDBA1BAB

正确答案:C(XYBCDCBA1BAA)
链接:https://www.nowcoder.com/questionTerminal/b9a49e1702c4422c9b5ef0b11509bb62
来源:牛客网

解析:
C 库函数 int sprintf(char *str, const char *format, …) 发送格式化输出到 str 所指向的字符串。

p2++ = p1++
1.先计算
p1:对指针p1取间接引用;
2.再计算p1++(将指针p1向后移动1个自身长度的偏移量);
3.再计算
p2:对指针p2取间接引用;
4.再计算p2++(将指针p2向后移动1个自身长度的偏移量);
5.最后将第1步所得结果赋到第3步中的内存,即*p2=*p1;

//count 0 p2 XYBCDCBABABA p1 XYBCDCBABABA p2 YBCDCBABABA p1 YBCDCBABABAXYBCDCBABABA
//count 0 p2 YBCDCBABABA p1 YBCDCBABABA p2 BCDCBABABA p1 BCDCBABABAXYBCDCBABABA
//count 0 p2 BCDCBABABA p1 BCDCBABABA p2 CDCBABABA p1 CDCBABABAXYBCDCBABABA
//count 0 p2 CDCBABABA p1 CDCBABABA p2 DCBABABA p1 DCBABABAXYBCDCBABABA
//count 0 p2 DCBABABA p1 DCBABABA p2 CBABABA p1 CBABABAXYBCDCBABABA
//count 0 p2 CBABABA p1 CBABABA p2 BABABA p1 BABABAXYBCDCBABABA
//count 0 p2 BABABA p1 BABABA p2 ABABA p1 ABABAXYBCDCBABABA
//count 1 XYBCDCBA1
//count 0 p2 p1 BA p2 BA p1 AXYBCDCBA1BBA
//count 0 p2 BA p1 A p2 A p1 XYBCDCBA1BAA
此题不懂,以后再分析。

15。写出下面程序的输出结果

class A
{
public:
 void FuncA()
 {
     printf( "FuncA called\n" );
 }
 virtual void FuncB()
 {
     printf( "FuncB called\n" );
 }
};
class B : public A
{
public:
 void FuncA()
 {
     A::FuncA();
     printf( "FuncAB called\n" );
 }
 virtual void FuncB()
 {
     printf( "FuncBB called\n" );
 }
};
void main( void )
{
 B	b;
 A	*pa;
 pa = &b;
 A *pa2 = new A;
 pa->FuncA();3) 
 pa->FuncB();4)
 pa2->FuncA();5)
 pa2->FuncB();
 delete pa2;
}

正确答案: B
FuncA called
FuncB called
FuncA called
FuncB called

FuncA called
FuncBB called
FuncA called
FuncB called

FuncA called
FuncBB called
FuncAB called
FuncBB called

FuncAB called
FuncBB called
FuncA called
FuncB called

解析:
此处易错是 pa->FuncA(); ( 3) FuncA未重写,A *pa;pa是父类,指向子类。FuncA调用父类(A)FuncA.;
pa->FuncB(); ( 4) 是重写函数,*pa;pa是父类,指向子类。FuncB调用子类(B)FuncB.;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值