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 ...
随机推荐
- Web开发——服务器端应用技术简单比较
在开发动态网站时,离不开服务器端技术,服务器端技术主要有CGI.ASP.PHP.ASP.NET和JSP. 1.CGI CGI(Common Gateway Interface 通用网关接口)是最早用来 ...
- HDU 6024 Building Shops
$dp$. $dp[i]$表示到$i$位置,且$i$位置建立了的最小花费,那么$dp[i] = min(dp[k]+cost[i+1][k-1])$,$k$是上一个建的位置.最后枚举$dp[i]$,加 ...
- RxSwift 系列(六)
前言 本篇文章将要学习RxSwift中数学和集合操作符,在RxSwift中包括了: toArray reduce concat toArray 将一个Observable序列转化为一个数组,并转换为一 ...
- 初探 Spring Boot
近些年Spring Boot都特别火,一直都想来学习学习,奈何近期公司项目繁忙,一直都没有时间来学习,今天终于是休息一天,于是来一睹 SpringBoot 的风采. 一.什么是Spring Boot ...
- Angular Material Starter App
介绍 Material Design反映了Google基于Android 5.0 Lollipop操作系统的原生应用UI开发理念,而AngularJS还发起了一个Angular Material ...
- 「NOI2017」游戏
「NOI2017」游戏 题目描述 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 \(A\).\(B\).\ ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Map之类的东西
http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html http://blog.csdn.net/aqzwss/article/de ...
- Ubuntu14.04 安装中文输入法
1 安装fcitx sudo apt-get install fcitx-table-py 这里py是拼音的意思,安装关依赖库和框架都会自动安装 2 把Ubuntu的系统环境改成中文 ...
- [转]Android之Activity的几种跳转方式
1.显示调用方法 Intent intent=new Intent(this,OtherActivity.class); //方法1 Intent intent2=new Intent(); in ...