这一章主要概括的介绍C++中的一些特性,能够知道如何编译并运行一个C++程序。练习题答案在最后。
C++中必须有一个main函数,其返回类型为int,0表示运行成功,其他指出错误类型。
int main() {
return 0;
}
可以用CC xxx.cc来编译程序,其中CC为编译器的名字。在UNIX和Linux系统中,可以用echo $?来查看程序的返回值。
C++利用库来提供输入输出机制,标准库定义了四个IO对象,cin,cout,cerr,clog。
#include 和 头文件的名字必须写在同一行。
<<运算符接收两个对象,左侧的必须是一个ostream对象,右侧是要打印的值,<<将右侧的值写到ostream对象,计算结果是左侧的ostream对象,于是我们就可以多次连续使用<<将输出请求连接起来。
endl的作用是结束当前行,并将缓冲区的内容刷到设备中,因此可以保证所有输出真正写入输出流中。
std::cout这种写法表明我们使用的是std命名空间中的cout。
输入运算符>>和<<差不多,不过是把左侧流中的值写入到右侧对象。
错误的注释比没有注释更糟糕。
多行注释中间不能有*/,一般调试的时候使用单行注释。
当多行注释跨越多行,最好显示指出注释,可以在每一行的开头加一个*
语句块是指用花括号包围的零条或多条语句的序列,语句块也是语句的一种。
for循环中的表达式可以有多个,用逗号分隔,例如
#include
int main() {
for (int i = 0, j = 0; i < 2, j < 2; ++i, ++j)
std::cout << i << " " << j << '\n';
std::cout << std::endl;
return 0;
}
当我们输入数量不定的数据时,可能需要用文件结束符来指定输入结束,在windows系统中,按住ctrl键同时按住z键,然后按enter或esc键,在其他系统中,使用Ctrl+D。
一个类定义了一个类型,以及与之相关的一组操作。
对于包含来自标准库的文件应使用<>,而不是来自标准库的应使用""
大多数操作系统支持文件重定向,这种机制允许我们将标准输入和标准输出与相关文件关联起来。格式如下:
$ prog <infile >outfile
<代表输入,>代表输出
点运算符.只能用于类类型的对象,其左侧运算对象必须是一个类类型的对象,右侧是该对象的一个成员,运算结果为右侧对象指定的成员。
调用运算符()用来调用函数。
练习答案部分参考:第 1 章
1.1 略
1.2 返回的错误标识为255.C++基础–main函数返回值问题
1.3
#include
int main() {
std::cout << "Hello, World" << std::endl;
return 0;
}
1.4
#include
int main() {
std::cout << "Enter two nums:";
int val1, val2;
std::cin >> val1 >> val2;
std::cout << val1 << " * " << val2 << " = " << val1 * val2 << 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 不合法,分号表示一个语句的结束。
1.7
#include
int main() {
/*
* 注释不能嵌套/**/
*/
std::cout << "Enter two nums:";
int val1, val2;
std::cin >> val1 >> val2;
std::cout << val1 << " * " << val2 << " = " << val1 * val2 << std::endl;
return 0;
}
错误信息如下:
test.cc: In function ‘int main()’:
test.cc:6:3: error: expected primary-expression before ‘/’ token
6 | */
| ^
1.8
std::cout << "/*"; // 合法,输出:/*
std::cout << "*/"; // 合法,输出:*/
std::cout << /* "*/" */; // 不合法
// 编译器提示warning: missing terminating '"' character
// 编译器把第一对/* "*/识别为注释界定符;剩余的" */;识别为源码。
std::cout << /* "*/" /* "/*" */; // 合法,输出 /*
// 首(/* "*/)尾(/*" */)为界定符;" /* "为源码
// /* "*/" /* "/*" */整个这句的一开始是界定符/*,编译器把和它匹配的最近*/之间的内
// 容( ")识别为注释;接着是",标志着下一个和它匹配的最近"之间的内容( /* )是字符
// 串;接着是界定符/*,编译器把和它匹配的最近*/之间的内容(" )识别为注释。
// 原则:从左往右,就近匹配。
编译器从左至右的寻找,就近匹配。
1.9
#include
int main() {
int val = 50, sum = 0;
while (val <= 100) {
sum += val;
++val;
}
std::cout << sum << std::endl;
return 0;
}
输出3825
1.10
#include
int main() {
int val = 10;
while (val >= 0) {
std::cout << val << ' ';
--val;
}
std::cout << std::endl;
}
1.11
#include
int main() {
std::cout << "Enter two nums:";
int val1, val2;
std::cin >> val1 >> val2;
if (val1 > val2) {
int temp = val1;
val1 = val2;
val2 = temp;
}
while (val1 <= val2) {
std::cout << val1 << ' ';
++val1;
}
std::cout << std::endl;
}
1.12 sum终值为0
1.13 略,可看上面的链接
1.14 while 循环适合循环次数不确定的情景;for 循环适合循环次数事先已知。
1.15 略
1.16
#include
int main() {
int sum = 0, val;
while (std::cin >> val) {
sum += val;
}
std::cout << sum << std::endl;
return 0;
}
1.17 如果输入的值都相等,程序输出相等值的个数;如果没有重复值程序则输出每个数字出现次数为1
1.18 略
1.19
#include
int main() {
std::cout << "Enter two nums:";
int val1, val2;
std::cin >> val1 >> val2;
if (val1 > val2) {
int temp = val1;
val1 = val2;
val2 = temp;
}
while (val1 <= val2) {
std::cout << val1 << ' ';
++val1;
}
std::cout << std::endl;
}
1.20 Sales_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
// 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
Sales_item(): units_sold(0), revenue(0.0) { }
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
unsigned units_sold;
double revenue;
};
// 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 "sales_item.h"
int main() {
Sales_item value;
while (std::cin >> value) {
std::cout << value << std::endl;
}
return 0;
}
1.21
#include
#include "sales_item.h"
int main() {
Sales_item item1, item2;
std::cin >> item1 >> item2;
std::cout << item1 + item2 << std::endl;
return 0;
}
1.22
#include
#include "sales_item.h"
int main() {
Sales_item item_sum, item;
std::cin >> item_sum;
while (std::cin >> item) {
item_sum += item;
}
std::cout << item_sum << std::endl;
return 0;
}
1.23
#include
#include "sales_item.h"
int main() {
Sales_item cur_item, item;
if (std::cin >> cur_item) {
int cnt = 1;
while (std::cin >> item) {
if (item.isbn() == cur_item.isbn()) {
cnt++;
}
else {
std::cout << cur_item.isbn() << " occurs " << cnt << " times" << std::endl;
cur_item = item;
cnt = 1;
}
}
}
return 0;
}
1.24
// 测试数据
0-201-78345-X 4 25.0
0-201-78345-X 1 25.0
0-201-78345-X 2 25.0
0-201-78345-X 9 25.0
0-207-78345-X 9 31.0
0-207-78345-X 3 31.0
0-202-78345-X 3 20.0
0-202-78345-X 3 20.0
// 运行结果
0-201-78345-X occurs 4 times
0-207-78345-X occurs 2 times
// 接着仍可以继续输入测试数据
1.25
#include
#include "sales_item.h"
int main() {
Sales_item total;
if (std::cin >> total) {
Sales_item trans;
while (std::cin >>trans) {
if (total.isbn() == trans.isbn()) {
total += trans;
}
else {
std::cout << total << std::endl;
total = trans;
}
}
}
else {
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}