C++primer习题--第1章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址。
【习题 1.3】
编一个程序,在标准输出上打印“Hello, World”。
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World\n";
return ;
}
【习题 1.4】
我们的程序利用内置加法操作符“+”来产生两个数的和。编写程序,使用乘法操作符“*”产生两个数的积。
#include <iostream>
using namespace std;
int main( )
{
cout<<"Enter two numbers: "<<endl;
int v1, v2;
cin>>v1>>v2;
cout<<"The product of"<<v1<<" and "<<v2<<" is "<<v1 * v2<<endl;
system("PAUSE");
return ;
}
【习题 1.5】
我们的程序使用了一条较长的输出语句。重写程序,使用单独的语句打印每一个操作数。
#include <iostream>
using namespace std;
int main( )
{
cout<<"Enter two numbers: "<<endl;
int v1, v2;
cin>>v1>>v2;
cout<<"The sum of";
cout<<v1;
cout<<" and ";
cout<<v2;
cout<<" is ";
cout<<v1 + v2;
cout<<endl;
system("PAUSE");
return ;
}
【习题 1.10】
用 for 循环编程,求从 50~100 的所有自然数的和。然后用 while 循环重写该程序。
用for循环:
#include <iostream>
using namespace std;
int main( )
{
int i, sum=;
for(i=; i<=; ++i)
sum+=i;
cout<<"sum of 50 to 100 is "<<sum<<endl;
system("PAUSE");
return ;
}
用 while 循环:
#include <iostream>
using namespace std;
int main( )
{
int i, sum;
i=;
sum=;
while(i++<=)
sum+=i;
cout<<"sum of 50 to 100 is "<<sum<<endl;
system("PAUSE");
return
【习题 1.11】
用 while 循环编程,输出 10~0 递减的自然数。然后用 for循环重写该程序。
用 while 循环:
#include <iostream>
using namespace std;
int main( )
{
int i=;
while(i>=)
cout<<i--<<endl;
return ;
}
用 for循环:
#include <iostream>
using namespace std;
int main( )
{
for(int i=; i >=; --i)
cout<<i<<endl;
return ;
}
【习题 1.16】
编写程序,输出用户输入的两个数中的较大者。
#include <iostream>
using namespace std;
int main( )
{
int a, b;
cout<<"请输入两个数: ";
cin>>a>>b;
cout<<"较大的一个数是: "<< ((a >= b)? a : b)<<endl;
system("PAUSE");
return ;
}
【习题 1.17】
编写程序,要求用户输入一组数。输出信息说明其中有多少个负数
#include <iostream>
using namespace std;
int main( )
{
int num, a;
num=;
while(cin>>a)
if(a < ) num++;
cout<<"输入的负数数量为: "<<num<<endl;
return ;
}
【习题 1.18】
编写程序,提示用户输入两个数幵将这两个数范围内的每个数写到标准输出。
#include <iostream>
using namespace std;
void print(int a, int b)
{
a++;
while(a < b)
cout<<a++<<" ";
cout<<endl;
}
int main( )
{
int a,b,i;
cout<<"请输入两个数: ";
cin>>a>>b;
if(a < b)
print(a,b);
else
print(b,a);
return ;
}
【习题 1.21】
本书配套网站(http://www.awprofessional.com/cpp_primer)的第1章的代码目录下有 Sales_ item.h 源文件。复制该文件到你的工作目录。编写程序,循环遍历一组书的销售交易, 读入每笔交易幵将交易写至标准输出。
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main( )
{
Sales_item book;
cout<<"输入交易:"<<endl;
while(cin>>book) {
cout<<"售出书的本数、总收入、平均价格:"
<<endl;
cout<<book<<endl;
}
system("PAUSE");
return ;
}
附上 Sales_ item.h 源文件:
#ifndef SALESITEM_H
#define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream>
#include <string> class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
// added constructors to initialize from a string or an istream
Sales_item(const std::string &book):
isbn(book), units_sold(), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);
// other members as before public:
// operations on Sales_item objects
double avg_price() const;
bool same_isbn(const Sales_item &rhs) const
{ return isbn == rhs.isbn; }
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(), revenue(0.0) { }
// private members as before
private:
std::string isbn;
unsigned units_sold;
double revenue; }; // nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&); inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
// must be made a friend of Sales_item
return lhs.units_sold == rhs.units_sold &&
lhs.revenue == rhs.revenue &&
lhs.same_isbn(rhs);
} inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
return !(lhs == rhs); // != defined in terms of operator==
} using std::istream; using std::ostream; // assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} // assumes that both objects refer to the same isbn
inline
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs); // copy lhs into a local object that we'll return
ret += rhs; // add in the contents of rhs
return ret; // return ret by value
} inline
istream&
operator>>(istream& in, Sales_item& s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
// check that the inputs succeeded
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item(); // input failed: reset object to default state
return in;
} inline
ostream&
operator<<(ostream& out, const Sales_item& s)
{
out << s.isbn << "\t" << s.units_sold << "\t"
<< s.revenue << "\t" << s.avg_price();
return out;
} inline
double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return ;
}
#endif
【习题 1.22】
编写程序,读入两个具有相同 ISBN 的 Sales_item 对象幵产生它们的和。
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main( )
{
Sales_item trans1, trans2;
cout<<"读入交易:"<<endl;
cin>>trans1>>trans2;
if(trans1.same_isbn(trans2))
cout<<"两笔交易具有相同的ISBN,和为:"<<endl<<trans1 + trans2;
else
cout<<"两笔交易没有相同的ISBN";
return ;
}
【习题 1.23】
编写程序,读入几个具有相同 ISBN 的交易,输出所有读入交易的和。
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main( )
{
Sales_item sum, trans;
cout<<"读入交易:"<<endl;
if(cin>>sum) {
while(cin>>trans) {
if(sum.same_isbn(trans))
sum += trans;
else {
cout<<"不同的ISBN!"<<endl;
return -;
}
}
} else {
cout<<"no data!"<<endl;
return -;
}
return ;
}
【习题 1.24】
编写程序,读入几笔不同的交易。对于每笔新读入的交易,要确定它的 ISBN 是否和以前的交易的 ISBN 一样,并且记下每一个 ISBN 的交易的总数。
通过给定多笔不同的交易来测试程序。这些交易必须代表多个不同的 ISBN,但是每个ISBN 的记录应分在同一组。
#include <iostream>
#include <map>
#include <string>
#include "Sales_item.h"
using namespace std;
int main( )
{
Sales_item trans;
cout<<"读入交易:"<<endl;
cin>>trans;
map<string, int> count;
count[trans.getIsbn()]++;
while(cin>>trans) {
++count[trans.getIsbn()];
}
map<string, int>::iterator it;
for(it=count.begin(); it!=count.end(); it++)
cout<<it->first<<":"<<it->second<<endl;
system("PAUSE");
return ;
}
C++primer习题--第1章的更多相关文章
- C++primer习题--第4章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址. [习题 4.7] 编写必要的代码将一个数 ...
- C++primer习题--第3章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址. [习题 2.11]编写程序,要求用户输入 ...
- 《C++Primer》第五版习题答案--第一章【学习笔记】
C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...
- 《C++Primer》第五版习题答案--第二章【学习笔记】
C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- [C++ Primer Plus] 第10章、对象和类(二)课后习题
1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...
随机推荐
- 转:Ubuntu16.04下配置php+vscode+xdebug开发环境
转:https://blog.csdn.net/its_my_dream/article/details/78549619 操作系统是Ubuntu16.04,首先要安装和配置php运行环境,我这里用的 ...
- Coding.net简单使用指南
注意:大家创建项目时一定要选择创建公开仓库!不然别人看不到! Coding.net是一个代码托管平台,简单来说这东西就是一个你在线存放代码的地方. 至于为什么要把代码存到这东西上呢?很多好处,比如防丢 ...
- OutputStreamWriter API 以及源码解读
OutputStreamWriter是字符流与字节流之间的桥梁. 通过它写入的字符流可以通过特殊的字符集转化为字节流.这个特殊的字符集可以指定,也可以采用平台默认的字符集. 每一次调用write()方 ...
- jquery DOM操作(一)
上一篇文章是记录了jquery中选择器的作用,这里只要记录下jquery中的DOM操作,还是按照上篇的格式来. 下面是测试用的html代码,接下来DOM的操作会在下面的代码下进行. <body& ...
- XSS漏洞自动化攻击工具XSSer
XSS漏洞自动化攻击工具XSSer XSS是Web应用常见的漏洞.利用该漏洞,安全人员在网站注入恶意脚本,控制用户浏览器,并发起其他渗透操作.XSSer是Kali Linux提供的一款自动化XSS ...
- django配置Ueditor
1.安装DjangoUeditor pip install DjangoUeditor 2.在Django中安装DjangoUedito app,在INSTALL_APPS里面增加DjangoUedi ...
- django基础入门
1. http协议 1.1 请求协议 请求协议格式: 请求首行: // 请求方式 请求路径 协议和版本,例如:GET /index.html HTTP/1.1 请求头信息: // 请求头名称:请求头内 ...
- iOS 发光字流水
{ CAGradientLayer *_gradientLayer; NSInteger count; } - (void)addLabel{ // 创建UILabel UILabel *label ...
- FLV视频在IIS6.0下不能播放 处理的方法
FLV视频在IIS6.0下不能播放 Flash视频由于其较高的压缩率和优越的下载速度,前景普遍看好,同时也为Flash课件增色不少.然而,在FLV视频播放中,却有两个头痛的问题 一.FLV视频在 ...
- ARM 调用约定 calling convention
int bar( int a, int b, int c, int d, int e, int f, int g ) { ]; array2[ ] = a + b; array2[ ] = b + c ...