C++Primer 中文版 第五版 第六章课后习题答案

//6.1
看概念吧,实在是太复杂了。
//6.2
a:返回类型不对
b:没有定义返回值
c:花括号和两个一样了
d:花括号
//6.3
#include <iostream>


using namespace std;


int fac(int n)
{
    int i;
    int sum=1;
    for(i=1;i<=n;i++)
        {
            sum=sum*i;
        }
    return sum;
}
int main()
{
    cout<<fac(5)<<endl;
}
//6.4
#include <iostream>
using namespace std;
int fac(int n)
{
    int i;
    int sum=1;
    for(i=1;i<=n;i++)
        {
            sum=sum*i;
        }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    cout<<fac(n);
}
//6.5
#include <iostream>
using namespace std;
int juedui(int n)
{
    if(n<0)
        n=-n;
    return n;
}
int main()
{
    int n;
    cin>>n;
    cout<<juedui(n)<<endl;
}


//6.6
形参用完立即销毁。局部变量函数结束时销毁,静态的程序结束后销毁  6.7就是相应的程序
//6.7
#include <iostream>
using namespace std;
int hanshu()
{
    static int i=0;
    return i++;
}
int main()
{
    cout<<hanshu()<<endl;
     cout<<hanshu()<<endl;
}


//6.8
#idndef  charpter6_h
#define chapter9_h
int fact();
#endif chapter6_h
//6.9

//6.10
#include <iostream>
using namespace std;
int swap(int *a,int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}


int main()
{
    int n,m;
    cin>>n>>m;
    swap(n,m);
    cout<<n<<m<<endl;


}


//6.11
#include <iostream>
using namespace std;
void reset(int &i)
{
    i=0;
}
int main()
{
    int n;
    cin>>n;
    reset(n);
    cout<<n<<endl;
}


//6.12
#include <iostream>
using namespace std;
int swap(int &a,int &b)
{
    int t;
    t=a;
    a=b;
    b=t;
}


int main()
{
    int n,m;
    cin>>n>>m;
    swap(n,m)
}


#include <iostream>
using namespace std;
int swap(int &a,int &b)
{
    int t;
    t=a;
    a=b;
    b=t;
}


int main()
{
    int n,m;
    cin>>n>>m;
    swap(n,m);
    cout<<n<<m<<endl;


}
6.13
传至和传引用
//6.14
想改变用引用
不想改变用非引用
//6.15
cosnt可以避免拷贝和被修改参数
occurs需要改变,用引用
//6.16
加上const

//6.17
#include<iostream>
#include<cctype>
#include<string.h>
using namespace std;
bool hanshu1(string a)
{
    int i;
    for(i=0;i<a.size();i++)
      {
          if(isupper(a[i]))
            return true;


      }
    return false;
}
void hanshu2(string &a)
{
    int i;
    for(i=0;i<a.size();i++)
      {
          if(isupper(a[i]))
             a[i]=a[i]+32;


      }




}
int main()
{
    string t;
    cin>>t;
    if(hanshu1(t))
        cout<<"Y"<<endl;
    else
        cout<<"N"<<endl;
    hanshu2(t);
    cout<<t<<endl;
}
//6.18
bool compare(matrix &,matrix &)
vector<int>::iterator change_val(int,vector<int>iterator)
//6.19
a不合法
//6.20
不想改变时,可能出现各种问题
//6.21


 #include<iostream>
#include<cctype>
#include<string.h>
using namespace std;
int hanshu(int a,int *b)
{
    if(a<*b)
      a=*b;
      return a;
}
int main()
{
    int n;
    int m;
    cin>>n;
    cin>>m;
    int *p=&m;
    cout<<hanshu(n,p)<<endl;


}
//6.22
 #include<iostream>
#include<cctype>
#include<string.h>
using namespace std;
void hanshu(int *&a,int *&b)
{
    int *p;
    p=a;
    a=b;
    b=p;
}
int main()
{
    int i,j;
    int *n,*m;
    cin>>i>>j;
    n=&i;
    m=&j;


    hanshu(n,m);
    cout<<*n<<*m;






}
//6.23
#include<iostream>
#include<cctype>
#include<string.h>
using namespace std;
void print1(int a)
{
    cout<<a<<endl;
}
void print2(int a[],int n)
{


    for(int i=0;i<n;i++)
        cout<<a[i]<<endl;
}
int main()
{
    int i=0;
    int j[2]={0,1};
    print1(i);
    print2(j,2);


}


//6.24
#include<iostream>
#include<cctype>
#include<string.h>
using namespace std;
void print1(const int ia[10])
{
    for(size_t i=0;i!=10;++i)
    {
        cout<<ia[i]<<endl;
    }
}
int main()
{
    int a[10]={1,2,3,4,5,6,7,8,9,0};
    print1(a);


}
//6.25
#include<iostream>
#include<string>
int main(int argc,char *argv[])
{
     if(argc>2)
     {
        string str=argv[1];
        str=str+argv[2];
         cout<<str<<endl;
      }
     else
      {
         cout<<"error"<<endl;
     }
}
//6.26
#include<iostream>


#include<string>
using namespace std;
int main(int argc,char *argv[])
{
    for(int i=0;i<argc;i++)
{
cout<<i+1<<":"<<argv[i]<<endl;
}
}
//6.27
#include<iostream>


#include<string>
#include<vector>
using namespace std;
int my_daa(int *beg,int *end)
{   
int sum=0;
while(beg!=end)
{sum=sum+*beg++;
}
return sum;
}
int main()
{
int sumVal;
int a[]={1,2,3,4,5,6,7,8};
sumVal=my_daa(a,a+sizeof(a)/sizeof(*a));
cout<<sumVal;
}
//6.28
string
//6.29
不能,因为const不能加
//6.30
不能,没有返回
//6.31
局部变量的无用
//6.32


#include<iostream>
using namespace std;
int  &get(int *array,int index)
{
    return array[index];
}
int main()
{
    int a[10];
    for(int i=0;i!=10;i++)
        get(a,i)=i;
    for(int i=0;i<10;i++)
        cout<<a[i]<<endl;
}
//6.33
#include<iostream>
#include<vector>
using namespace std;
int t=9;
void hanshu(vector<int>temp)
{
    cout<<temp[t];
    if(t==0)
        return ;
    t--;
    hanshu(temp);


}
int main()
{
    vector<int>a;
    int i;
    for(i=0;i<10;i++)
        a.push_back(i);
    hanshu(a);
}
//6.34
如果是负数,崩溃
//6.35
val--无法递归
//6.36
string(&fun(string s))[10];
//6.37


string(&fun(string s))[10];
typedef string sp[10];
using sp=string[10
string str[10];
decltype(str)&fun(string s);
//3.38

//6.39
a:重复
b:返回值
c:重载


//6.40
第二个错了,往后放!!
//6.41
ac错
b对
c!!!后面提供了默认值,前面就必须提供
//6.42
#include<iostream>


#include<string>
#include<vector>
using namespace std;
string hanshu(int ctr,string &word="success",string &ending="es")
{
  return (ctr>1)?word+ending:word;
}
int main()
{
cout<<hanshu(1)<<endl;
cout<<hanshu(2);
}


//6.43
头头
//6.44
加inline即可
//6.45
看情况吧
//6.46
可以。
cosntexpr const string &shorterString (string &s1,string &s2)
{
    return s1.size()<=s2.size();
}
//6.47
加上#ifndef NDEBUG #endif
//6.48
不可理,因为绝对成立
//6.50
a二义性,,其他成立
//6.51
void fun()
void fun(int i)
void fun(double double)
void fun(int  int)
//6.52
char ->int
double->int
//6.53

//6.54,55,56
#include<iostream>
#include<vector>
using namespace std;
typedef int fun(int,int);
int sum(int a,int b)
{
return a+b;
}
int reduce(int a,int b)
{
if(a>=b)
return a+b;
else
return b-a;
}
int ride(int a,int b)
{
return a*b;
}
int main()
{
int a=20;
int b=10;
vector<fun *>ivc;
ivec.push_back(sum);
ivec.push_back(reduce);
ivec.push_back(ride);
for(auto it=ivec.begin();it!=ivec.end();++it)
cout<<(*it)(a,b)<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值