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

前言:自己做的,如果有错误,要提出来哟...

//5.1

空语句只包含一个;

//5.2

块是有花括号括起来的部分

很多地方都用,while,for等等

//5.3

While(int sum=0,val=1;val<=10;sum+=val,++val)

//5.4

(a)      Iter没有初值

(b)      Word没有初值

//5.5

//5.6

这俩一般都会吧..简单的语句

//5.7

a:少了一个分号

b.少了花括号

c.ival应该在之前定义

d.少了一个=

//5.8

存在多个else时,与那个if匹配的问题

//5.9

#include <iostream>

 

using namespace std;

 

int main()

{

   char t;

   int aa=0,ee=0,ii=0,oo=0,uu=0;

   while(cin>>t)

    {

       if(t=='a')

           aa++;

       if(t=='e')

           ee++;

           if(t=='i')

           ii++;

           if(t=='o')

           oo++;

           if(t=='u')

           uu++;

 

 

    }

   cout<<"a:"<<aa<<"e"<<ee<<"i"<<ii<<"o"<<oo<<"u"<<uu<<endl;

}

//5.10

#include <iostream>

 

using namespace std;

 

int main()

{

   char t;

   int aa=0,ee=0,ii=0,oo=0,uu=0;

   while(cin>>t)

    {

        if(t=='a'||t=='A')

           aa++;

       if(t=='e'||t=='E')

           ee++;

           if(t=='i'||t=='I')

           ii++;

           if(t=='o'||t=='O')

           oo++;

           if(t=='u'||t=='U')

           uu++;

 

 

    }

   cout<<"a:"<<aa<<"e"<<ee<<"i"<<ii<<"o"<<oo<<"u"<<uu<<endl;

}

//5.11

#include <iostream>

 

using namespace std;

 

int main()

{

   char t;

   int aa=0,ee=0,ii=0,oo=0,uu=0,kong=0,zhibiao=0,huanhang=0;

   while(cin>>t)

    {

       if(t=='a'||t=='A')

           aa++;

       if(t=='e'||t=='E')

           ee++;

           if(t=='i'||t=='I')

           ii++;

           if(t=='o'||t=='O')

           oo++;

           if(t=='u'||t=='U')

           uu++;

           if(t==' ')

           kong++;

           if(t=='\t')

           zhibiao++;

           if(t=='\n')

           huanhang++;

 

 

    }

   cout<<"a:"<<aa<<"e"<<ee<<"i"<<ii<<"o"<<oo<<"u"<<uu<<endl;

   cout<<"kong"<<kong<<"zhibiao"<<zhibiao<<"huanhang"<<huanhang<<endl;

}

//5.12

#include <iostream>

 

using namespace std;

 

int main()

{

   char t='\0';

   char q;

   int aa=0,ee=0,ii=0,oo=0,uu=0,kong=0,zhibiao=0,huanhang=0;

   int ff=0,f1=0,fi=0;

   while(cin>>q)

    {

       if(t=='a'||t=='A')

           aa++;

       if(t=='e'||t=='E')

           ee++;

       if(t=='i'||t=='I')

           ii++;

       if(t=='o'||t=='O')

           oo++;

       if(t=='u'||t=='U')

           uu++;

       if(t==' ')

           kong++;

       if(t=='\t')

           zhibiao++;

       if(t=='\n')

           huanhang++;

       if(t=='f')

       {

           if(q=='f')

                ff++;

           if(q=='1')

                f1++;

           if(q=='i')

                fi++;

       }

       t=q;

 

 

    }

   cout<<"a:"<<aa<<"e"<<ee<<"i"<<ii<<"o"<<oo<<"u"<<uu<<endl;

   cout<<"kong"<<kong<<"zhibiao"<<zhibiao<<"huanhang"<<huanhang<<endl;

}

//5.13

a:少break

b:int ix位置错了

c:case后面不能连着写

d:case后面加常量表达式,而不是变量

//5.14

#include <iostream>

#include<string>

using namespace std;

 

int main()

{

   string before,now,result;

   int cnt=0,MaxCnt=1;

   while(cin>>now)

    {

       if(now==before)

           ++cnt;

       else

       {

           if(cnt>MaxCnt)

           {

                MaxCnt=cnt;

                result=before;

           }

           cnt=1;

       }

       before=now;

    }

   if(MaxCnt!=1)

    {

       cout<<result<<" "<<MaxCnt<<endl;

    }

}

//5.15

a:逻辑错误

b:少了一个;

c:死循环

//5.16

各有用处,没有最好

//5.17

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

voidhanshu(vector<int>a,vector<int>b);

int main()

{

   vector<int>a;

   vector<int>b;

   int t;

   int tt;

   while(cin>>t)

    {

        a.push_back(t);

    }

   while(cin>>tt)

    {

       b.push_back(tt);

    }

   if(a.size()>b.size())

    {

       hanshu(a,b);

    }

   else

    {

       hanshu(b,a);

    }

}

voidhanshu(vector<int>a,vector<int>b)

{

   int flag=0;

   int i;

   for(i=0;i<b.size();i++)

       if(a[i]!=b[i])

    {

        flag=1;

        break;

    }

    if(flag)

           cout<<"不是"<<endl;

    else

       cout<<"是"<<endl;

 

}

 

//5.18
a:没加花括号

b:在条件部分不能定义变量

c:正确

//5.19

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

voidhanshu(vector<int>a,vector<int>b);

int main()

{

   string str1,str2;

   do

   {

       cout<<"请输入两个字符串"<<endl;

      cin>>str1>>str2;

      if(str1>str2)

       cout<<str2<<endl;

      else

       cout<<str1<<endl;

 

  }while(cin);//注意,这里吸收回车键

}

//5.20

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

void hanshu(vector<int>a,vector<int>b);

int main()

{

   string str1,str2;

   while(cin>>str2)

    {

       if(str1==str2)

           break;

       else

           str1=str2;

    }

   if(!str1.empty()&&str1==str2)

       cout<<str2<<endl;

   else

       cout<<"无"<<endl;

 

}

//5.21

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

voidhanshu(vector<int>a,vector<int>b);

int main()

{

   string str1,str2;

   while(cin>>str2)

    {

       if(str1==str2)

       {

           if(str2[0]>='A'&&str2[0]<='Z')

                break;

           else

                continue;

       }

       else

           str1=str2;

    }

   if(!str1.empty()&&str1==str2)

       cout<<str2<<endl;

   else

       cout<<"无"<<endl;

 

}

//5.22

do

{

Int sz=get_size();

}while(sz<0)

//5.23

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

int main()

{

   int a,b;

   cin>>a>>b;

   cout<<a/b<<endl;

}

//5.24

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

int main()

{

   int a,b;

   cin>>a>>b;

   if(b==0)

       throw runtime_error("第二个为0了");

   cout<<a/b<<endl;

}

//5.25

#include <iostream>

#include<string>

#include<vector>

using namespace std;

 

int main()

{

   int a,b;

   while(cin>>a>>b)

    {

       try

       {

           if(b==0)

                throw runtime_error("第二个数为0");

           cout<<a+b<<endl;

       }

       catch(runtime_error err)

       {

           cout<<err.what()<<"请在输入一次"<<endl;

           char c;

           cin>>c;

           if(!cin||c=='n')

                break;

       }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值