IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 2021-07-23 -> 正文阅读

[C++知识库]2021-07-23

C++ Primer答案
第六章 函数

6.4 编写一个与用户交互的函数,要求输入一个数字,计算生成该数字的阶乘

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int multify(int )
{
    int x;
    while (cin>>x)
    {
    int ret=1;
    while (x>1)
    {
        ret *= x--;
    }
    return ret;
    }
        
}
int main()
{
 cout<<multify(3);//这里实参随便输入一个int型
}

6.5 编写函数输出其实参的绝对值

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

float absolute(float x)
{
    if (x>=0)
        return x;
    else 
        return -x;
  
}
int main()
{
cout<<absolute(-3.14);
}

6.10

#include <iostream>
using std::cin;
using std::cout;
using std::endl;


void exc(int *a,int *b)
{
    int c=*a;
    *a=*b;
    *b=c;
}
int main()
{
    int m=3,n=4;
    int *a=&m;
    int *b=&n;
    exc(a,b);
    cout<<*a<<" "<<*b;
    
}

6.11

void exc(int &a,int &b)
{
    int c=a;
    a=b;
    b=c;
}
int main()
{
    int m=3,n=4;
    exc(m,n);
    cout<<m<<" "<<n;
}

6.16

bool have_up(const string s)
{   
    bool a=false;
    for (auto &c:s)
        {
            if (isupper(c))
                a = true;
        }
    return a;
}
string up2low(string s)
{
    for (auto &c:s)
    {
        if (isupper(c))
            c=tolower(c);
    }
    return s;
}
int main()
{
    cout<<have_up("hello")<<endl;
    cout<<up2low("HEllO");
}
}

6.21

int comp(int a,int *b)
{
    if (a>*b)
        return a;
    else
        return *b;
}

int main()
{
    int b=5;
    cout<<comp(4,&b);
}

6.22

void exc(int *&a,int *&b) //接受的形参是对指针的引用
{
    int *c=a;
    a=b;
    b=c;
}
int main()
{
    int a=4,b=5;
    int *m=&a;
    int *n=&b;
    exc(m,n);
    cout<<*m<<" "<<*n;
}

6.25

int main(int argc,char **argv)
{
    string str;
    for (int i;i!=argc;++i)
    {
        str+=argv[i];
        str+=" ";
    }
    cout<<str<<endl;
    return 0;
}

6.27

int sum(initializer_list<int> v)
{
    int i=0;
    for (const auto c:v)
        i+=c;
    return i;
}

int main()
{
    initializer_list<int> v={1,2,3,4,5,6};
    cout<<sum(v);
}

6.33

void output(vector<int> v,int ix)
{
    if (ix!=v.size())
    {
        cout<<v[ix++]<<endl;
        output(v,ix);
    }
    
}
int main()
{
    vector<int> v={5,4,3,2,1};
    output(v,0);
}

6.38

int odd[]={1,3,5,7,9};
int even[]={0,2,4,6,8};
decltype(odd) &arrPtr(int i)
{
    return (i%2) ? odd :even;
}
int main()
{
    for (auto c:arrPtr(2))
        cout<<c<<" ";
}

6.42

string make_plural(size_t ctr,const string &word,const string &ending="s")
{
    return (ctr>1)?word+ending:word;
}
int main()
{
    cout<<make_plural(1,"success")<<endl;
    cout<<make_plural(2,"failure")<<endl;
}

6.47

void output(vector<int> v,int ix)
{
    if (ix!=v.size())
    {
        #ifndef NDEBUG
        cout<<"第"<<ix+1<<"个数"<<endl;
        #endif
        cout<<v[ix++]<<endl;
        output(v,ix);
    }
    
}
int main()
{
    vector<int> v={1,2,3,4,5};
    output(v,0);
}

6.55 6.56

int sum(int a,int b)
{
    return a+b;
}
int subtract(int a,int b)
{
    return a-b;
}
int multify(int a,int b)
{
    return a*b;
}
int divide(int a,int b)
{
    return (b!=0) ? a/b :0;
}
int main()
{
    typedef int(*p) (int a,int b);
    vector<p> v{sum,subtract,multify,divide};
    for (auto c:v)
        cout<<c(1,0)<<endl;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-27 16:01:53  更:2021-07-27 16:02:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 1:16:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码