c++ primer第五版----学习笔记(五)

1.空语句:最简单的语句,只包含一个单独的分号

复合语句:指用花括号括起来的语句和声明的序列,复合语句也被称为块

2.if语句

//语法形式:
if (condition)
    statement

//if、else语句
if (condition)
    statement
else
    statement1

//悬垂else:就c++而言,规定else与离它最近的尚未匹配的if匹配,消除了程序的二义性
if (grade % 10 >= 3)
    if (grade % 10 > 7)
        lettergrade += '+';
else                                //此else与内层if匹配
    lettergrade += '-';

3.switch语句

(1)case标签:case关键字和它对应的值,必须是整型常量表达式

(2)在下一个case标签之前应该有一条break语句;但有时候希望多个值能共享同一组操作,就故意省略break语句

(3)default标签:程序没有任何一个case标签能匹配上switch的值,就进入default语句

(4)不允许跨过变量的初始化语句直接跳到该变量作用域内的另一个位置

4.迭代语句

//while语句
while (condition)
    statement

//传统的for语句
for (init-statemen; condition; expression)  //init-statemen可以为声明语句、表达式语句、空语句
    statement

//范围for语句
for (declaration : expression)     //确保类型相同最好使用auto说明符
    statement

//do while语句
do 
    statement
while (condition);

5.跳转语句

(1)break语句:终止离它最近的while、do while、for或switch语句,并从这些语句之后的第一条语句开始执行

(2)continue语句:终止最近的循环中的当前迭代并立即开始下一次迭代

(3)goto语句:从goto语句无条件跳转到同意函数中的另外一条语句(最好不要使用,因为它使得程序既难理解又难修改)

6.try语句块和异常处理

(1)try语句块:

//try语句块通用语法形式
try {
    program-statements
} catch (exception-declearation) {
    handle-statements
} catch (exception-declearation) {
    handle-statements
}//.......

(2)标准异常:c++标准库中定义了一组类,用于报告标准库函数遇到的问题(书p176)

7.部分问题解答

5.4:

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

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

5.9、5.10、5.11、5.12:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0,sum_space = 0;
    unsigned sum_table = 0, sum_newline = 0, sum_ff = 0, sum_fl = 0, sum_fi = 0;
    char ch, char_brefore = '\0';
    while (cin >> ch)
    {
	switch(ch)
	{
	case 'a':
	case 'A':
		++acnt;
		break;
	case 'e':
	case 'E':
		++ecnt;
		break;
	case 'i':
		if (char_brefore == 'f')
		{
			++sum_fi;
		}
	case 'I':
		++icnt;
		break;
	case 'o':
	case 'O':
		++ocnt;
		break;
	case 'u':
	case 'U':
		++ucnt;
		break;
	case ' ':
		++sum_space;
		break;
	case '\t':
		++sum_table;
		break;
	case '\n':
		++sum_newline;
		break;
	case 'f':
		if (char_brefore == 'f')
		{
			++sum_ff;
		}
		break;
	case 'l':
		if (char_brefore == 'f')
		{
			++sum_fl;
			break;
		}
	}
	char_brefore = ch;
    }
    cout << "元音字母a的个数为; " << acnt << endl;
    cout << "元音字母e的个数为: " << ecnt << endl;
    cout << "元音字母i的个数为: " << icnt << endl;	
    cout << "元音字母o的个数为: " << ocnt << endl;
    cout << "元音字母u的个数为: " << ucnt << endl;	
    cout << "空格的个数为: " << sum_space << endl;
    cout << "制表符的个数为: " << sum_table << endl;
    cout << "换行符的个数为: " << sum_newline << endl;	
    cout << "字母序列ff的个数为: " << sum_ff << endl;	
    cout << "字母序列fl的个数为: " << sum_fl<< endl;
    cout << "字母序列fi的个数为: " << sum_fi << endl;
    system("pause");
    return 0;
}

5.14:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string my_string, before_string ;
    vector<string> v;//将不重复的单词记录
    vector<int> v1;//记录单词出现的次数;
    int repeat_number = 1;
    while (cin >> my_string)
    {
	if (my_string == before_string)
	{
		++repeat_number;
	}
	else
	{
		v.push_back(my_string);
		v1.push_back(repeat_number);
		repeat_number = 1;
		before_string = my_string;
	}
    }
    v1.push_back(repeat_number);//把末尾单词出现的次数压入vector
    int a = 0;
    auto it = v1.begin();
    for (it; it != v1.end(); ++it)
    {
	if (*it > a)
	{
	    a = *it;
	}
    }
    for (int i = 0; i < v1.size(); ++i)
    {
          if (a > 1)
	  {
	        if (v1[i] == a)
	        {
                    //注意如果输入没有单词连续出现的情况,注意判断a的大小,否则v[i-1]可能会报错
	            cout << "单词" << v[i - 1] << "出现的次数" << v1[i] << "次" << endl;		        }
	  }
    }
    if (a == 1)
    {
	  cout << "没有单词连续出现!" << endl;
    }
    system("pause");
    return 0;
}

5.17:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    vector<int> v1;
    vector<int> v2;
    int n, m;
    cout << "输入第一个vector对象: ";
    while (cin >> n)
    {
	v1.push_back(n);
    }
    cin.clear();//重置输入流
    cin.ignore();//忽略最后一个输入字符
    cout << endl << "输入第二个vector对象: ";
    while (cin >> m)
    {
	v2.push_back(m);
    }
    int small_size = v1.size() > v2.size() ? v2.size() : v1.size();//返回更小的vector
    int flag = 0;
    for (int i = 0; i < small_size; ++i)
    {
	if (v1[i] != v2[i])
	{
		cout << "false" << endl;
		flag = 1;
		break;
	}
    }
    if (flag != 1)
    {
	cout << "true" << endl;
    }
    system("pause");
    return 0;
}

5.20、5.21:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string my_string, before_string;
    int flag = 0;
    cout << "请输入单词: ";
    while (cin >> my_string)
    {
	if (my_string[0] > 'Z' || my_string[0] < 'A')
	{
		cout << "请继续输入单词: ";
		continue;
	}
        if (my_string == before_string)
	{
		cout << "重复单词为: " << my_string << endl;
		flag = 1;
		break;
	}
	else
	{
		before_string = my_string;
	}
    }
    if (flag == 0)
    {
	cout << "无重复单词出现" << endl;
    }
    system("pause");
    return 0;
}

5.23、5.24、5.25:

#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
int main()
{
    int a, b;
    cout << "请输入相除的两个整数: ";
    while (cin >> a >> b)
    {
	try
	{
	    if (b == 0) throw std::runtime_error("被除数不能为0");
	    cout << static_cast<double>(a) / b << endl;
	}
	catch (runtime_error err)
	{
	    cout << err.what()
		    << "\n是否需要重新输入? Enter y or n: " << endl;
	    char c;
	    cin >> c;
	    if (!cin || c == 'n')
	    {
	    	break;
	    }
	    else
	    {
	    	cout << "请输入相除的两个整数: ";
	    }
	}
    }
    system("pause");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值