C++ Prmier(5e)第1章习题

1.1

使用的是visual studio 2017社区版,文件名后缀为.cpp

int main()
{
	return 0;
}

1.2

int main()
{
	return -1;
}

1.3

#include
int main()
{
	std::cout << "Hello, World!" << std::endl;
	return 0;
}

1.4

#include
int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The product of " << v1 << " and " << v2
		<< " is " << v1 * v2 << std::endl;
	return 0;
}

1.5

#include
int main()
{
	std::cout << "Enter two numbers:";
	std::cout << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The sum of ";
	std::cout << v1;
	std::cout << " and ";
	std::cout << v2;
	std::cout << "is ";
	std::cout << v1 + v2;
	std::cout << std::endl;
	return 0;
}

1.6

std::cout << "The sum of " << v1;
          << " and " << v2;
		  << " is " << v1 + v2 << std::endl;

不合法,前2行后面有分号,代表语句结束。更改如下

std::cout << "The sum of " << v1;
std::cout << " and " << v2;
std::cout << " is " << v1 + v2 << std::endl;

1.8

std::cout << "/*";   // 合法
std::cout << "*/";    // 合法
std::cout << /* "*/" */;   // 不合法
std::cout << /* "*/" /* "/*" */;   // 合法

1.9

#include
int main()
{
	int sum = 0, val = 50;
	while (val <= 100)
	{
		sum += val;
		++val;
	}
	std::cout << "Sum of 50 to 100 inclusive is "
		<< sum << std::endl;
	return 0;
}

1.10

#include
int main()
{
	int val = 10;
	while (val >= 0)
	{
		std::cout << val << std::endl;
		val--;
	}
	return 0;
}

1.11

#include
int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	while (v1 <= v2)
	{
		std::cout << v1;
		++v1;
	}
	return 0;
}

1.12

#include
int main()
{
	int sum = 0;
	for (int i = -100; i <= 100; i++)
		sum += i;
	std::cout << "The sum of -100 to 100 is " << sum << std::endl;
	return 0;
}

1.13

/* 从50加到100 */
#include

using namespace std;

int main()
{
	int sum = 0;
	for (int i = 50; i != 101; ++i)
		sum += i;
	cout << sum << endl;
	return 0;
}

1.14

for更适合循环的开始和结束已知;while适合条件不确定。

1.16

#include
int main()
{
	int sum = 0, value = 0;
	while (std::cin >> value)
	{
		sum += value;
	}
	std::cout << "Sum is: " << sum << std::endl;
	return 0;
}

1.17

如果输入的所有值都是相等的,则只会输出一行,表示这个值输入的次数;如果输入的所有值都是不等的,则会出现输入值的数量的行数。

1.18

/* 运行就自己运行了,对着书上敲一遍也是好的 */
#include

using namespace std;

int main()
{
	int currVal = 0, val = 0;
	if (cin >> currVal)
	{
		int cnt = 1;
		while (cin >> val)
		{
			if (val == currVal)
				++cnt;
			else
			{
				cout << currVal << " occurs " << cnt << " times" << endl;
				currVal = val;
				cnt = 1;
			}
		}
		cout << currVal << " occurs " << cnt << " times" << endl;
	}
	return 0;
}

1.19

#include

using namespace std;

int main()
{
	int val1, val2;
	cout << "Enter two numbers:" << endl;
	cin >> val1 >> val2;
	if (val1 > val2)
	{
		for (int i = val1; i != val2 - 1; --i)
			cout << i << " ";
		cout << endl;
	}
	else
	{
		for (int i = val2; i != val1 - 1; --i)
			cout << i << " ";
		cout << endl;
	}
	return 0;
}

1.20 1.21 1.22

这是头文件Sale_item.h


/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
*     Pearson Education, Inc.
*     Rights and Permissions Department
*     One Lake Street
*     Upper Saddle River, NJ  07458
*     Fax: (201) 236-3290
*/
 
/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/
 
#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined 
#define SALESITEM_H
 
//#include "Version_test.h" 
 
// Definition of Sales_item class and related functions goes here
#include 
#include 
 
class Sales_item {
	// these declarations are explained section 7.2.1, p. 270 
	// and in chapter 14, pages 557, 558, 561
	friend std::istream& operator>>(std::istream&, Sales_item&);
	friend std::ostream& operator<<(std::ostream&, const Sales_item&);
	friend bool operator<(const Sales_item&, const Sales_item&);
	friend bool
		operator==(const Sales_item&, const Sales_item&);
public:
	// constructors are explained in section 7.1.4, pages 262 - 265
	// default constructor needed to initialize members of built-in type
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
	Sales_item() = default;
#else
	Sales_item() : units_sold(0), revenue(0.0) { }
#endif
	Sales_item(const std::string &book) :
		bookNo(book), units_sold(0), revenue(0.0) { }
	Sales_item(std::istream &is) { is >> *this; }
public:
	// operations on Sales_item objects
	// member binary operator: left-hand operand bound to implicit this pointer
	Sales_item& operator+=(const Sales_item&);
 
	// operations on Sales_item objects
	std::string isbn() const { return bookNo; }
	double avg_price() const;
	// private members as before
private:
	std::string bookNo;      // implicitly initialized to the empty string
#ifdef IN_CLASS_INITS
	unsigned units_sold = 0; // explicitly initialized
	double revenue = 0.0;
#else
	unsigned units_sold;
	double revenue;
#endif
};
 
// used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{
	return lhs.isbn() == rhs.isbn();
}
 
// 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.isbn() == rhs.isbn();
}
 
inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
	return !(lhs == rhs); // != defined in terms of operator==
}
 
// assumes that both objects refer to the same ISBN
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
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
}
 
std::istream&
operator>>(std::istream& in, Sales_item& s)
{
	double price;
	in >> s.bookNo >> 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;
}
 
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
	out << s.isbn() << " " << s.units_sold << " "
		<< s.revenue << " " << s.avg_price();
	return out;
}
double Sales_item::avg_price() const
{
	if (units_sold)
		return revenue / units_sold;
	else
		return 0;
}
#endif

/* (主程序)记得把头文件包括进来哦 */
#include
#include "Sale_item.h"

using namespace std;

int main()
{
	Sales_item book;
	cin >> book;
	cout << book << endl;
	return 0;
}

1.23 1.24

#include
#include "Sale_item.h"

using namespace std;

int main()
{
	Sales_item item1, item2;
	int cnt = 1;
	cin >> item1 >> item2;
	if (item1.isbn() == item2.isbn())
		++cnt;
	else
		cnt = 1;
	cout << cnt << endl;
	return 0;
}

1.25

#include
#include "Sale_item.h"

using namespace std;

int main()
{
	Sales_item total;
	if (cin >> total)
	{
		Sales_item trans;
		while (cin >> trans)
		{
			if (total.isbn() == trans.isbn())
				total += trans;
			else
			{
				cout << total << endl;
				total = trans;
			}
		}
		cout << total << endl;
	}
	else
	{
		cerr << "No data?!" << endl;
		return -1;
	}
	return 0;
}

你可能感兴趣的:(C++)