鱼C论坛

 找回密码
 立即注册

C++Primer Plus 第六版编程练习8.8-4

已有 365 次阅读2015-7-6 21:03 |个人分类:c++Primer Plus 第6版编程练习笔记| Plus

#include <iostream>

using namespace std;
#include <cstring>
struct stringy
{
    char * str ;
    int ct ;
};

void show(const char * , int n =1);

void show(const stringy & , int n=1) ;

void set(stringy & , char *) ;

int main()
{
    stringy beany ;
    char testing[] = "Reality isn't what it used to be." ;
    set(beany ,testing) ;
    show(beany) ;
    show(beany,2) ;
    testing[0] = 'D' ;
    testing[1] = 'u' ;
    show(testing) ;
    show(testing ,3) ;
    show("Done!") ;

    delete [] beany.str ;  //用new创建的char*千万别忘了这个delete
    return 0;
}

void set(stringy & stry , char *p)
{
    int n = strlen(p);
    stry.str = new char [n+1] ;
    
    strcpy(stry.str ,p) ;
    stry.ct = n ;
    //delete [] stry.ct  ;
}

void show(const char * p , int n )
{
    if(n==0)
    {
        return ;
    }
    else
    {
        cout<<"testing show:"<<endl;
        for(int i=0 ;i<n ;i++)
        {
            cout<<p<<endl ;
        }
    }
    
}

void show(const stringy & stry , int n)
{
    if(n==0)
    {
        return ;
    }
    else
    {
        cout<<"Stringy show:"<<endl;  
        for(int i=0 ;i<n ;i++)
        {
            cout<<stry.str<<" "<<stry.ct<<endl ;
        }
    }
    
}


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-14 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部