《C++ primer》第五版习题答案整理——第五章 语句

P155

练习5.1

空语句是只含一个分号的语句,表示当前什么也不做。

在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。

练习5.2

块就是用花括号括起来的(可能为空的)语句和声明的序列,一个块就是一个作用域。

在程序的某些地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句。

练习5.3

会降低代码的可读性

while(a > 1)

     sum += a ,a--;

P156

练习5.4

其他代码也需要访问控制变量时,变量需要定义在语句的外部

(a):iter变量未初始化,且需要定义在语句的外部

(b):if部分无意义,在while循环中已经完成了判断

P159

练习5.5

#include "stdafx.h"

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

       int i;

       cin >> i;

       if (i > 90)

       {

              cout << "成绩为:A" << endl;

       }

       else if(i > 60)

       {

              cout << "成绩为:B" << endl;

       }

       else

       {

              cout << "成绩为:C" << endl;

       }

       system("pause");

}

练习5.6

#include "stdafx.h"

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

       int i;

       cin >> i;

       char s = 'A';

       s = i < 60 ? 'C' : (i < 90 ? 'B' : 'A');

       cout << "成绩为" << s << endl;



       system("pause");

}

练习5.7

  1. ival1 = ival2 ; //缺少一个分号
  2. 使用了两条语句,需要使用花括号括起来
  3. 下面的if需要换成else if
  4. =替换为==

练习5.8

悬垂else:C++规定,else与其最近的尚未匹配的if相匹配

P164

练习5.9

#include <iostream>

#include <string>

#include <vector>

using namespace std;

void main()

{     

       char cval;

       int sum_a = 0,sum_e = 0,sum_i = 0,sum_o = 0,sum_u = 0;

       while (cin >> cval)

       {

              if (cval == 'a')

              {

                     sum_a++;

              }

              else if (cval == 'e')

              {

                     sum_e++;

              }

              else if (cval == 'i')

              {

                     sum_i++;

              }

              else if (cval == 'o')

              {

                     sum_o++;

              }

              else if (cval == 'u')

              {

                     sum_u++;

              }

       }

       cout<<"元音字母a的个数为:"<<sum_a<<endl;

       cout<<"元音字母e的个数为:"<<sum_e<<endl;

       cout<<"元音字母i的个数为:"<<sum_i<<endl;

       cout<<"元音字母o的个数为:"<<sum_o<<endl;

       cout<<"元音字母u的个数为:"<<sum_u<<endl;

}

练习5.10

#include <iostream>

#include <string>

#include <vector>

using namespace std;

void main()

{     

       char cval;

       int char_a = 0,char_e = 0,char_i = 0,char_o = 0,char_u = 0;

       while (cin >> cval)

       {

              switch (cval)

              {

                     case 'a':

            case 'A':

                            ++char_a;

                            break;

                     case 'e':

                     case 'E':

                            ++char_e;

                            break;

                     case 'i':

                     case 'I':

                            ++char_i;

                            break;

                     case 'o':

                     case 'O':

                            ++char_o;

                            break;

                     case 'u':

                     case 'U':

                            ++char_u;

                            break;

              }

       }

       cout<<"元音字母a的个数为:"<<char_a<<endl;

       cout<<"元音字母e的个数为:"<<char_e<<endl;

       cout<<"元音字母i的个数为:"<<char_i<<endl;

       cout<<"元音字母o的个数为:"<<char_o<<endl;

       cout<<"元音字母u的个数为:"<<char_u<<endl;

}

练习5.11

#include <iostream>

#include <string>

#include <vector>

using namespace std;

void main()

{

    char cval;

    int char_a = 0, char_e = 0, char_i = 0, char_o = 0, char_u = 0, sum_space = 0, sum_table = 0, sum_newline = 0;

    while (cin >> std::noskipws >> cval) // 此处的std::noskipws表示的是不忽略任何地方的空白(包括制表符和空格等)

    {

         switch (cval)

         {

         case 'a':

         case 'A':

             ++char_a;

             break;

         case 'e':

         case 'E':

             ++char_e;

             break;

         case 'i':

         case 'I':

             ++char_i;

             break;

         case 'o':

         case 'O':

             ++char_o;

             break;

         case 'u':

         case 'U':

             ++char_u;

             break;

         case ' ':

             ++sum_space;

             break;

         case '\t':

             ++sum_table;

             break;

         case '\n':

             ++sum_newline;

             break;

         }

    }

    cout << "元音字母a的个数为:" << char_a << endl;

    cout << "元音字母e的个数为:" << char_e << endl;

    cout << "元音字母i的个数为:" << char_i << endl;

    cout << "元音字母o的个数为:" << char_o << endl;

    cout << "元音字母u的个数为:" << char_u << endl;

    cout << "空格的个数为:" << sum_space << endl;

    cout << "制表符的个数为:" << sum_table << endl;

    cout << "换行符的个数为:" << sum_newline << endl;

}

练习5.12

#include "stdafx.h"

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

    string sIn = "";

    int nNnmff = 0, nNumfl = 0, nNumfi = 0;

    string sff = "ff";

    string sfl = "fl";

    string sfi = "fi";

    while (getline(cin, sIn))

    {

         if (sIn == sff)

         {

             ++nNnmff;

         }

         else if (sIn == sfl)

         {

             ++nNumfl;

         }

         else if (sIn == sfi)

         {

             ++nNumfi;

         }

         cout << nNnmff << " " << nNumfi << " " << nNumfl << endl;

    }



    system("pause");

}

练习5.13

  1. 每个case和default后面都没有break
  2.  ix超出了作用域
  3. 每个case只能对应一个值
  4.  case后面的对应值应为一个常量,可以加const修饰符,将ival、jval、kval变成常量

P166

练习5.14

#include<iostream>

#include<string>

#include<vector>

#include <iostream>

#include <string>

#include <vector>

using namespace std;

void main()

{

    string My_string, before_string, max_repeatstring;

    int repeat_number = 0, flag = 0;

    while (cin >> My_string)

    {

         if (My_string == before_string)

         {

             ++repeat_number;

         }

         else

         {

             repeat_number = 1;

             before_string = My_string;

         }


        if (flag < repeat_number)

         {

             flag = repeat_number;

             max_repeatstring = before_string;

         }//设置flag,max_repeatstring用来保存当前比较完字符串后的最大重复次数和对应字符串

    }

    if (flag == 1)

    {

         cout << "没有重复的字符串出现" << endl;

    }

    else

    {

         cout << "单词" << max_repeatstring << "出现了" << flag << "次" << endl;

    }

}

P168

练习5.15

  1. ix作用域在for循环内,本处ix应该在循环外有定义
  2. 缺少一个;号
  3. ++sz,永远也结束不了

练习5.16

更倾向于用for,因为可以准确取到某个位置的值

练习5.17

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	bool bIs = false;
	vector <int> vec1 = { 0,1,1,2 };
	vector <int> vec2 = { 0,1,1,2,3,5,8 };
	int size1 = vec1.size();
	int size2 = vec2.size();
	int realSize = size1 > size2 ? size2 : size1;
	for (int i = 0; i < realSize; i++)
	{
		if (vec1.at(i) == vec2.at(i))

		{
			bIs = false;
			continue;
		}
		else
		{
			bIs = true;
			break;
		}
	}
	if (bIs)

	{

		cout << "不存在前缀关系" << endl;

	}
	else
	{
		cout << "是前缀关系" << endl;
	}

	system("pause");
}

P170

练习5.18

  1. do后多条语句,需要使用花括号
  2. do循环体中需要使用到变量ival,应该将其定义在循环体外部
  3. ival超出了作用域

练习5.19

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

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

    string str1, str2;

    do

    {

         int size1 = str1.size();

         int size2 = str2.size();

         if (size1 < size2)

         {

             cout << str1 << endl;

         }

         else

         {

             cout << str2 << endl;

         }


    } while (cin >> str1 >> str2);


    system("pause");

}

P171

练习5.20

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void main()
{
	string str;
	vector<string> text;
	while (cin >> str)
	{
		text.push_back(str);
	}
	auto beg = text.begin();
	while (beg!= text.end())
	{
		str = *beg;
		++beg;
		if (beg == text.end())
		{
			cout << "无重复单词:" << endl;
			break;
		}
		else if (str == *beg)
		{
			cout << "重复单词:" << *beg << endl;
			break;
		}
	}
	system("pause");
}

练习5.21

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void main()
{
	string str;
	vector<string> text;
	while (cin >> str)
	{
		text.push_back(str);
	}
	auto beg = text.begin();
	while (beg != text.end())
	{
		str = *beg;
		++beg;
		if (beg == text.end())
		{
			cout << "无重复单词:" << endl;
			break;
		}
		else if (str == *beg)
		{
			if (isupper(str[0]))
			{
				cout << "重复单词:" << *beg << endl;
				break;
			}
			else {
				continue;
			}
		}
	}
	system("pause");
}

P172

练习5.22

int sz;
    do
    {
        sz = get_size();
    }
    while(sz<=0)

P177

练习5.23
 

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

    int num1, num2;

    cin >> num1 >> num2;

    cout << num1 / num2 << endl;


    system("pause");

}

练习5.24

#include <iostream>

#include <string>

#include <vector>

using namespace std;


int main()

{

    int num1, num2;

    cin >> num1 >> num2;

    if (num2 == 0)

    {

         throw runtime_error("Data Error!");

    }

    cout << num1 / num2 << endl;


    system("pause");

}

练习5.25

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	int num1, num2;
	char tag;
begin:
	try
	{
		while (cin >> num1 >> num2)
		{
			if (num2 == 0)
			{
				throw runtime_error("Data Error!");
				break;
			}
			cout << num1 / num2 << endl;
		}
	}
	catch (runtime_error err)
	{
		cout << err.what();
		cout << "again ?(Y or N)" << endl;
		cin >> tag;
		if (tag == 'Y')
			goto begin;
	}
	system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值