本文地址: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章的更多相关文章

  1. C++primer习题--第4章

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址. [习题 4.7] 编写必要的代码将一个数 ...

  2. C++primer习题--第3章

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter3-ans.html,转载请注明源地址. [习题 2.11]编写程序,要求用户输入 ...

  3. 《C++Primer》第五版习题答案--第一章【学习笔记】

    C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...

  4. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...

  5. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  6. 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 ...

  7. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  8. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  9. [C++ Primer Plus] 第10章、对象和类(二)课后习题

    1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...

随机推荐

  1. ubuntu怎么连接centos远程桌面

    1.系统软件设置CentOS端:查看是否安装了vnc软件# rpm -q vnc vnc-serverpackage vnc is not installedvnc-server-4.1.2-14.e ...

  2. 哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)I - B-旅行

    题目描述 小z放假了,准备到RRR城市旅行,其中这个城市有N个旅游景点.小z时间有限,只能在三个旅行景点进行游玩.小明租了辆车,司机很善良,说咱不计路程,只要你一次性缴费足够,我就带你走遍RRR城. ...

  3. hdu 2433 Travel(还不会)

    Problem Description       One day, Tom traveled to a country named BGM. BGM is a small country, but ...

  4. Python Socket多线程并发

    1.SocketServer模块编写的TCP服务器端代码 Socketserver原理图 服务端: import SocketServer #导入SocketServer,多线程并发由此类实现 cla ...

  5. shell中的条件判断if和测试

    (一)条件判断 if 中-z 到 -d 的意思 [ -a file ] 若file存在,则为真. [ -b file ] 若file存在且是一个块特殊文件,则为真. [ -c file ] 若file ...

  6. 可以在GitHub或者码云里 直接搜索 项目 比如 哔哩哔哩

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Search · 哔哩哔哩 哔哩哔哩 · 搜索 - 码云 还有就是 以前的项目 可以不要 ...

  7. 【最小表示法】BZOJ2176-Strange string(unsigned char!!!)

    [题目大意] 给定一个字符串S = {S1, S2, S3 … Sn}, 如果在串SS中, 子串T(|T| = n)为所有长度为n的SS的字串中最小的(字符串的比较), 则称T为”奇怪的字串”. 你的 ...

  8. Android为什么需要广播Broadcast

       在Android系统中,为什么需要广播机制呢?广播机制,本质上它就是一种组件间的通信方式,如果是两个组件位于不同的进程当中,那么可以用Binder机制来实现,如果两个组件是在同一个进程中,那么它 ...

  9. [ZHOJ1131]Find K Min

    题目大意: 给你一个数列,求其中第K大的数. 思路: 类似于快速排序的思想,每次可以确定出当前的的x在数组中的位置. 然后根据位置选择该往左找还是往右找. #pragma GCC optimize(3 ...

  10. BFS洪水

    试题描述: 已经连续下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图 ...