当前位置: 首页 > news >正文

C++ Primer第五版_第三章习题答案(1~10)

文章目录

      • 练习3.1
      • 练习3.2
        • 一次读入一行
        • 一次读入一个词
      • 练习3.3
      • 练习3.4
        • 大的字符串
        • 长度大的字符串
      • 练习3.5
        • 未隔开的
        • 隔开的
      • 练习3.6
      • 练习3.7
      • 练习3.8
      • 练习3.9
      • 练习3.10

练习3.1

使用恰当的using 声明重做 1.4.1节和2.6.2节的练习。

// 1.4.1
#include <iostream>using std::cin;
using std::cout;
using std::endl;int main()
{int sum = 0;for (int val = 1; val <= 10; ++val) sum += val;cout << "Sum of 1 to 10 inclusive is " << sum << endl;return 0;
}// 2.6.2
#include <iostream>
#include <string>
#include "exercise2_42.h"using std::cin;
using std::cout;
using std::endl;
using std::cerr;int main()
{Sales_data data1, data2;double price = 0;  cin >> data1.bookNo >> data1.units_sold >> price;data1.revenue = data1.units_sold * price;cin >> data2.bookNo >> data2.units_sold >> price;data2.revenue = data2.units_sold * price;if (data1.bookNo == data2.bookNo){unsigned totalCnt = data1.units_sold + data2.units_sold;double totalRevenue = data1.revenue + data2.revenue;cout << data1.bookNo << " " << totalCnt<< " " << totalRevenue << " ";if (totalCnt != 0)cout << totalRevenue / totalCnt << endl;elsecout << "(no sales)" << endl;return 0;  }else{  cerr << "Data must refer to the same ISBN" << endl;return -1; }
}

练习3.2

编写一段程序从标准输入中一次读入一行,然后修改该程序使其一次读入一个词。

一次读入一行

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::getline;int main()
{string s;while (getline(cin,s)){cout << s << endl;}return 0;
}

一次读入一个词

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s;while (cin >> s){cout << s << endl;}return 0;
}

练习3.3

请说明string类的输入运算符和getline函数分别是如何处理空白字符的。

  • 类似 is >> s 的读取,string对象会忽略开头的空白并从第一个真正的字符开始,直到遇见下一空白为止。
  • 类似 getline(is, s) 的读取,string对象会从输入流中读取字符,直到遇见换行符为止。

练习3.4

编写一段程序读取两个字符串,比较其是否相等并输出结果。如果不相等,输出比较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

大的字符串

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string str1, str2;while (cin >> str1 >> str2){if (str1 == str2)cout << "The two strings are equal." << endl;elsecout << "The larger string is " << ((str1 > str2) ? str1 : str2);}return 0;
}

长度大的字符串

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string str1, str2;while (cin >> str1 >> str2){if (str1.size() == str2.size())cout << "The two strings have the same length." << endl;elsecout << "The longer string is " << ((str1.size() > str2.size()) ? str1 : str2) << endl;}return 0;

练习3.5

编写一段程序从标准输入中读入多个字符串并将他们连接起来,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分割开来。

未隔开的

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string result, s;while (cin >> s){result += s;}cout << result << endl;return 0;
}

隔开的

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string result, s;while (cin >> s){result += s + " ";}cout << result << endl;return 0;
}

练习3.6

编写一段程序,使用范围for语句将字符串内所有字符用X代替。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";for (auto &x : s){x = 'X';}cout << s << endl;return 0;
}

练习3.7

就上一题完成的程序而言,如果将循环控制的变量设置为char将发生什么?先估计一下结果,然后实际编程进行验证。

如果设置为char,那么原来的字符串不会发生改变。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";for (char x : s){x = 'X';}cout << s << endl;return 0;
}

练习3.8

分别用while循环和传统for循环重写第一题的程序,你觉得哪种形式更好呢?为什么?

范围for语句更好,不直接操作索引,更简洁。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";decltype(s.size()) i = 0;while (i != s.size()){s[i] = 'X';++i;}cout << s << endl;for (i = 0; i != s.size(); ++i){s[i] = 'Y';}cout << s << endl;return 0;
}

练习3.9

下面的程序有何作用?它合法吗?如果不合法?为什么?

string s;
cout << s[0] << endl;

不合法。使用下标访问空字符串是非法的行为。

练习3.10

编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this, is. a :string!";string result;for (auto x : s){if (!ispunct(x)){result += x;}}cout << result << endl;return 0;
}
http://www.lryc.cn/news/40210.html

相关文章:

  • 小样本学习
  • python打包成apk界面设计,python打包成安装文件
  • pytorch转onnx踩坑日记
  • 极智AI | GPT4来了,ChatGPT又该升级了
  • 智能优化算法之灰狼优化算法(GWO)的实现(Python附源码)
  • leetCode热题10-15 解题代码,思路
  • 同步辐射GISAXS和GIWAXS的原理及应用领域
  • OpManager 进行网络性能管理
  • 面试被问到向上转型和向下转型时,怎么回答?
  • 加密月解密:概述,基础篇
  • DC-DC升压模块隔离高压稳压电源直流变换器12v24v48v转600V1000V1100V1500V2000V3000V
  • pandas数据分析(三)
  • cpu performance profiling
  • vue2启动项目npm run dev报错 Error: Cannot find module ‘babel-preset-es2015‘ 修改以及问题原因
  • *9 set up 注意点
  • linux目录——文件管理
  • 使用new bing简易教程
  • idea插件分享 显著提高开发效率
  • 文心一言发布我怎么看?
  • 100. 增减序列
  • 操作系统之进程的初步认识(1)
  • 【Java】你真的懂封装吗?一文读懂封装-----建议收藏
  • 使用MobaXterm ssh远程登录Ubuntu 20.04
  • 蓝桥杯历年真题训练
  • Spring事务报错: org.springframework.transaction.UnexpectedRollbackException
  • Spring:IOC和AOP
  • 【笔记】效率之门——Python中的函数式编程技巧
  • Java【多线程基础2】 Thread类 及其常用方法
  • JVM调优实战及常量池详解
  • ChatGPT研究分析:GPT-4做了什么