C++ Primer Plus 第六版第8章习题答案

最近再学习c++服务器端编程,需要用到c++这门语言,就顺带把c++ Primerplus 后面的习题做一下,如果有不对的请指正;


第一题:

// 20171114_One.cpp : 定义控制台应用程序的入口点。
//
//第一题 扯淡题
#include "stdafx.h"
#include<iostream>
using namespace std;
void     PrintArray(const char * p1,int i=0);//建立一个原型 第二个参数默认为0(其他数也可以,不影响)
int _tmain(int argc, _TCHAR* argv[])
{//下面函数调用涉及到重载
    char a[]="强";
    PrintArray(a);
    cout<<endl;
    PrintArray(a,123);
    cout<<endl;
    PrintArray(a,123);
    cout<<endl;
    PrintArray(a,123);
    cout<<endl;
    PrintArray(a,123);
    cout<<endl;
    cin.get();
    system("pause");
    return 0;
}
void     PrintArray(const char *p1,int i)
{
    static int sum=0;//全局变量  用于累加函数执行次数
    ++sum;
    if(!i)
    {
        cout<<p1;//如果输入的参数 int为空  则执行此步骤
    }
    else
    {
        for(int k=0;k<sum;k++)
        {
            cout<<p1;//全局变量来for() 求函数执行次数的输出
        }
    }

};

结果:这里写图片描述


第二题:

// 20171115_two.cpp : 定义控制台应用程序的入口点。
//函数的传参与参数的处理

#include "stdafx.h"
#include<iostream>
using namespace std;
const int NameMax=40;//姓名的常量
struct CandyBar
{
    char                 c_name[NameMax];//名称
    double               c_weight;//重量
    int                  c_heat;//热量
};
void   SetFunc(CandyBar &candybar , const char * name="Millennium Munch",const double weight =2.85,const int heat=350);//后三个都有默认值
void   ShowAllInfo(const CandyBar &candybar );//输出函数
int _tmain(int argc, _TCHAR* argv[])
{
    CandyBar  k;
    SetFunc(k);
    ShowAllInfo(k);
    CandyBar  s;
    char a[NameMax]="李明飞";
    strncpy(s.c_name,a,NameMax);
    s.c_weight=178.23;
    s.c_heat=23;
    SetFunc(s,a,s.c_weight,s.c_heat);//再次输入参数 如果不输入也可以运行 因为已经设置过s的参数了
    ShowAllInfo(s);
    cin.get();
    system("pause");
    return 0;
}

void   SetFunc(CandyBar &candybar , const char * name,const double weight ,const int heat)//函数的定义不用再写默认了
{
    strncpy(candybar.c_name,name,NameMax);//字符串的复制函数
    candybar.c_weight=weight;
    candybar.c_heat=heat;
};

void   ShowAllInfo(const CandyBar &candybar )
{
    cout<<candybar.c_name<<endl;
    cout<<candybar.c_weight<<endl;
    cout<<candybar.c_heat<<endl;
};

结果:这里写图片描述


第三题

// 20171115_Three.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<string>
#include<cctype>
#include<iostream>
using namespace std;
const  int a=20;
void   SetToupper(string  &s);//有String引用的函数
int _tmain(int argc, _TCHAR* argv[])
{
    string k;
    while(getline(cin,k))
    {
        if(k=="q")//退出判断
        {
            break;
        }
        SetToupper(k);
    }
    cin.get();
    system("pause");
    return 0;
}
void   SetToupper(string  &s)
{
    for(int i=0;i<s.size();i++)
    {
        //s[i]-=32;//非函数方法改大小写
        s[i]=toupper(s[i]);//函数方法编写大小写
    }
    cout<<s<<endl;
};



结果:
这里写图片描述


第四题:

// 20171115_Four.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<cstring>
#include<iostream>
using namespace std;

struct  stringy
{
    char * str;
    int ct;
};
void set( stringy &s,const char*s1);
void show(const stringy &s,int i=1);
void show(const  char* s1,int i=1);
int _tmain(int argc, _TCHAR* argv[])
{
    stringy beany;
    char testing[]="Reality isn't what it used to be .";
    set(beany,testing);
    show(beany);
    cout<<endl;
    show(beany,2);
    cout<<endl;
    testing[0]='D';
    testing[2]='u';
    show(testing);
    cout<<endl;
    show(testing,3);
    cout<<endl;
    show("Done!");
    cin.get();
    system("pause");
    return 0;
}
void set( stringy &s,const char*s1)
{
    s.ct=strlen(s1);
    s.str=new  char[s.ct+1];
    strcpy(s.str,s1);
};
void show(const stringy &s,int i)
{
    for(int k=0;k<i;k++)
    {
        cout<<s.str<<endl;
    }
};
void show(const  char* s1,int i)
{
    for(int k=0;k<i;k++)
    {
        for(int j=0;j<sizeof(s1);j++)
        {
            cout<<s1[j];
        }
    }
};

结果:这里写图片描述


第五题

// 20171115_Five.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;
const int MaxT=5;
template<typename  T>
T    ReturnT(T   a[MaxT]);
int _tmain(int argc, _TCHAR* argv[])
{
    int w[MaxT]={1,2,3,4,5};
    double e[MaxT]={1,2,3,4,5};
    int Max=0;
    Max=ReturnT(w);
    cout<<Max<<endl;
    double Max1=0;
    Max1=ReturnT(e);
    cout<<Max1<<endl;
    cin.get();
    system("pause");
    return 0;
}
template<typename  T>
T    ReturnT(T   a[MaxT])
{
    T  temp;
    for(int i=0;i<(MaxT-1);i++)
    {
        if(a[i]<a[i+1])
        {
            temp=a[i+1];
        }
    }
    return temp;
};

结果:这里写图片描述


第六题

这里写代码片
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值