- 1、本文档共33页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C++Primer习题(第三章附答案)
练习3.1 略
练习 3. 2 编写⼀段程序从标准输⼊中⼀次读⼊⼀整⾏,然后修改程序使其⼀次读⼊⼀个词。
#include iostream
#includestring
using namespace std ;
使⽤ ⼀次读⼊⼀整⾏,回车结束
// getline
int main()
{
string line ;
cout please enter your line ,even space endl;
循环读取, 结束
while (getline(cin, line)) // Ctrl+c
cout line endl;
return 0;
}
#include iostream
#includestring
using namespace std ;
使⽤ ⼀次读⼊⼀单词,空⽩结束
// cin
int main()
{
string word ;
cout please enter your word ,not space endl;
循环读取, 结束
while (cinword) // Ctrl+c
cout word endl;
return 0;
}
练习3.3 请说明string类的输⼊运算符和getline函数是如何分别处理空⽩字符的
#includeiostream
#includestring
using namespace std ;
int main()
{
string word, line ;
cout please select the way to read the string endl;
cout 1 means read string by word endl;
cout 2 means read string by line endl;
char ch ;
cin ch ;
if (ch == 1)
{
cout please enter string : welcome to c++ family endl;
cin word ;
cout system read the string is : word endl;
}
else if (ch == 2)
{
cout please enter string : welcome to c++ family endl;
getline(cin, line);
cout system read the string is : line endl;
}
else
{
cout input error,please try again endl;
}
return 0;
}
练习3.4 编写⼀段程序读⼊两个字符窜,⽐较其是否相等并输出结果。结果不相等,输出较⼤的那个字符串。改写上述程序,⽐较输⼊的两
个字符串是否等长,如果不等长,输出长度较⼤的那个字符串。
#includeiostream
#includestring
using namespace std ;
int main()
{
string word1, word2 ;
cout please enter two strings endl;
cin word1 word2 ;
if (word1 == word2)
{
cout word1 endl;
}
else if (word1word2)
{
cout word1 endl;
}
else
{
cout word2 endl;
}
return 0;
}
#includeiostream
#includestring
using namespace std ;
int main()
{
string word1, word2 ;
cout please ent
您可能关注的文档
- 2022-2023学年江西省赣州市赣州中学高考化学二模试卷含解析 .pdf
- 2023学年高二上学期12月阶段考试文科地理试题 .pdf
- 2023年环境影响评价工程师之环评法律法规模拟题库及附答案 .pdf
- 2023年一级建造师-民航机场工程管理与实务考试备考题库附带答案1.pdf
- 2023年面试题及答案 .pdf
- 2021年高考仿真试题(三)(理综化学) .pdf
- 2022年北京市丰台区中考语文模拟试题(含答案) .pdf
- 2022年江苏省徐州市中考物理考前模拟试题及答案解析 .pdf
- 2023届广西柳州市高三第二次模拟考试英语试题 .pdf
- 《战略管理》模拟题 .pdf
- TZSM-校史馆展览展示工程服务规范及编制说明.pdf
- TSEEPLA-生态环境标准实施评估指南及编制说明.pdf
- 化妆品中羟吡啶酮的测定 高效液相色谱法编制说明.pdf
- QJKL023-2020 佳木斯市恺乐农药有限公司企业标准240gpL乳氟禾草灵乳油.pdf
- QHTY031-2020 过氧化氢分解仪.pdf
- TGLAC-基于北斗的智能网联车载设备技术要求及编制说明.pdf
- QALKS0001-2020 新能源车用机械式变速器总成台架试验标准.pdf
- TSSM-基于计量方法学的碳排放核算指南 铝冶炼企业及编制说明.pdf
- 产品碳足迹评价规范 污泥干化焚烧系统集成装备及编制说明.pdf
- DBS333018-2024 食品安全地方标准 藕粉生产卫生规范.pdf
最近下载
- XX同仁堂医药有限公司GSP质量体系内审报告.docx
- 民航运输服务专业学生的职业生涯规划.pptx VIP
- JGJ321-2014:点挂外墙板装饰工程技术规程.pdf VIP
- 逆变器的研究与设计 论文.doc
- 4篇 国有企业领导干部在国企改革深化提升行动总结会上的讲话推进会上的汇报发言.docx VIP
- 18 文言文二则 《囊萤夜读》说课课件(共18张PPT).pptx VIP
- IEC 61215-1-1 地面用光伏组件设计鉴定和定型-第1-1部分:地面用光伏组件特殊试验要求(1).pdf VIP
- 2024常州市溧阳市溧城街道社区工作者招聘考试真题题库及答案.docx
- 2020-2025年中国黑猪行业投资潜力分析及行业发展趋势报告.docx
- 充电模块CAN通讯协议指导书-02.pdf
文档评论(0)