栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Primer第五版第六章6.1、6.2部分课后习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Primer第五版第六章6.1、6.2部分课后习题

6.4

//编写一个与用户交互的函数,要求用户输入一个数字,计算该数字的阶乘
#include
using namespace std;
int fact(void){  
    int val;
    cout<<"请输入一个数字"<>val;
    int ret=1;
    while(val>1){
        ret*=val;
        --val;
    }
    return ret;
}


int main(){ 
    int j=fact();
    cout< 

6.6

#include
#include
using namespace std;

vector test(int a,int b){ //形参
    int res=0;//局部变量
    res=a+b;
    vector v;
    v.push_back(res);
    static size_t call_cnt=0;
    ++call_cnt;
    v.push_back(call_cnt);
    return v;
}


int main(){
    int a,b;//4 2
    cout<<"请输入两个整数"<>a>>b;
    vector ans;
    for(size_t i=0;i!=10;++i){
        ans=test(a,b);//res是局部变量,每次执行结果都为6;call_cnt第一次调用结束后为1,第二次调用后为2,依次递增。
    }
    for(const auto &i:ans){
        cout< 

6.10

#include
using namespace std;

void swap(int *p1,int *p2){
    int temp=*p1;
    *p1=*p2;
    *p2=temp;    
}


int main(){
    int a=4,b=2;
    cout<<"交换前:"< 

6.12

#include
using namespace std;

void swap(int &p1,int &p2){
    int temp=p1;
    p1=p2;
    p2=temp;    
}


int main(){
    int a=4,b=2;
    cout<<"交换前:"< 

6.15



#include
#include
using namespace std;

//返回s中字符c第一次出现的位置索引
//引用形参occurs负责统计c出现的总次数
string::size_type find_char(const string &s,char c,string::size_type &occurs){
    auto ret=s.size();//若s中没有c 返回s的长度
    occurs=0;
    for(decltype(ret) i=0;i!=s.size();++i){
        if(s[i]==c){
            if(ret==s.size()){
                ret=i; //记录c第一次出现的位置
            }
            ++occurs;
        }
    }
    return ret; //出现次数通过occurs隐式地返回
}



int main(){
    string s="hhhhheeellohhafsdfsag";
    char c='h';
    string::size_type str;
    auto index=find_char(s,c,str);
    cout<<"字母:"< 

6.17

#include
#include
using namespace std;

bool isHaveUp(const string &s){//形参为常量引用
    for(string::size_type i=0;i!=s.size();++i){
        if(isupper(s[i])){
            return true;
        }
    }
    return false;
}

void toLower(string &s){ //形参为普通引用,需要修改对象s的值
    for(string::size_type i=0;i!=s.size();++i){
        s[i]=tolower(s[i]);
    }
}

int main(){
    bool flag=isHaveUp("heLLo world");
    cout<
        cout< 

6.21

#include
using namespace std;

int bigInt(const int a,const int *p){ 
    if(a>=*p){
        return a;
    }else{
        return *p;
    }
}

int main(){
    int a=1,b=2,ans;
    ans=bigInt(a,&b);
    cout< 

6.22

#include
using namespace std;

void swap(int *&p1,int *&p2){//指针的引用,这样会交换两个指针本身的值即地址
    int *temp=p1;
    p1=p2;
    p2=temp;
}


int main(){
    int a=1,b=2;
    int *p1=&a,*p2=&b;
    //交换前
    cout<<*p1<<*p2< 

6.25

#include
#include
using namespace std;
//命令行传参 第一个参数是可执行文件的名字 第二个参数是you 第三个参数i
//hrh@hrh-ubuntu:~/Code$ ./6_25.out you i he
int main(int argc,char *argv[]){//C语言风格  指针char **argv
    string s;
    for(auto i=1;i!=argc;++i){
        s+=argv[i];
    }
    cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号