1.1 编写一个简单的C++程序
1.一个函数的定义包含四部分:返回类型,函数名,形参列表,函数体。
2.当return语句包括一个值时,此返回值的类型必须与函数的返回类型相容。
3.程序所处理的数据都保存在变量中,而每个变量都有自己的类型。如果一个名为v的变量的类型为T,我们通常说“v具有类型T”,或等价的,“v是一个T类型变量。”
1.2 初识输入输出
1.标准输入输出对象
cin:标准输入
cout:标准输出
cerr:标准错误
clog:输出运行时的一般信息
2.namespace即“命名空间”,也称“名称空间” ,cout和cin都是定义在名为std的命名空间中的。命名空间可以帮助我们避免不经意的名字名字定义冲突,以及使用库中相同名字导致的冲突。标准库定义的所有名字都在命名空间std中。
3.当使用标准库中的一个名字时,必须显示说明我们想使用来自命名空间std中的名字。例如std::cout,通过使用作用域运算符(::)来指出我们想使用定义在命名空间std中的名字cout。
1.3 注释简介
1.单行注释以双斜线(//)开始,以换行符结束。当前行双斜线右侧的所有内容都会被编译器忽略,这种注释可以包括任何文本,包括额外的双斜线。
2.多行注释以/*开始,以*/结束,可以包含除*/以外的任意内容。编译器将落在/*和*/之间的所有内容都当做注释。
1.4 控制流
1.while语句
while语句的形式为:
- while(condition)
- {
- statement;
- }
只要条件语句的判断为真,那么循环体就会一直执行下去,知道条件语句得出的结果为假才终止循环。
2.for语句
for语句的形式为:
- for(init-statement;condition;expression)
- {
- statement;
- }
循环头控制循环体的执行次数,它由三部分组成:一个初始化语句(init-statement),一个循环条件(condition),一个表达式(expression)
3.if语句
- if(condition)
- {
- statement;
- }
1.5 类的初识
1.在C++中,我们通过定义一个类来定义自己的数据结构。一个类定义了一个类型,以及与其关联的一组操作。类机制是C++最重要的特性之一。
2.成员函数是定义为类的一部分的函数,有时也被称为方法。我们通常以一个类对象的名义来调用成员函数。
PS:部分练习答案
练习1.3
- #include <iostream>
- int main()
- {
- std::cout<<"Hello, World"<<std::endl;
- return 0;
- }
练习1.4
- #include <iostream>
- int main()
- {
- int v1 = 0,v2 = 0;
- std::cout<<"请输入两个数:";
- std::cin>>v1>>v2;
- std::cout<<"它们的乘积为:"<<v1<<"*"<<v2<<"="<<v1*v2<<std::endl;
- return 0;
- }
练习1.5
- #include <iostream>
- int main()
- {
- int v1 = 0,v2 = 0;
- std::cout<<"请输入两个数:";
- std::cin>>v1>>v2;
- std::cout<<"它们的乘积为:";
- std::cout<<v1;
- std::cout<<"*";
- std::cout<<v2;
- std::cout<<"=";
- std::cout<<v1*v2;
- std::cout<<std::endl;
- return 0;
- }
练习1.6
该程序是不合法的,它会返回一个"expected primary-expression before '<<' token"的错误。
改法有两种
其一:去掉中间的分号
- std::cout << "The sum of " << v1
- << " and " << v2
- << " is " << v1 + v2 << std::endl;
- std::cout << "The sum of " << v1;
- std::cout << " and " << v2
- << " is " << v1 + v2 << std::endl;
练习1.9
- #include <iostream>
- int main()
- {
- int sum = 0,v = 50;
- while(v<=100)
- {
- sum+=v;
- v++;
- }
- std::cout<<sum<<std::endl;
- return 0;
- }
练习1.10
- #include <iostream>
- int main()
- {
- int v = 10;
- while(v>=0)
- {
- std::cout<<v--<<std::endl;
- }
- return 0;
- }
练习1.11
- #include <iostream>
- int main()
- {
- int a,b,_max,_min;
- std::cout<<"请输入两个数:";
- std::cin>>a>>b;
- _max = a>b?a:b;
- _min = a>b?b:a;
- std::cout<<"这两个数区间内的整数有:"<<std::endl;
- while(_min<=_max)
- {
- std::cout<<_min++<<" ";
- }
- std::cout<<std::endl;
- return 0;
- }
练习1.13
- //1-9
- #include <iostream>
- int main()
- {
- int sum = 0,v;
- for(v = 50;v<=100;v++)
- {
- sum+=v;
- }
- std::cout<<sum<<std::endl;
- return 0;
- }
- //1-10
- #include <iostream>
- int main()
- {
- int v = 10;
- for(v = 10;v>=0;v--)
- {
- std::cout<<v<<std::endl;
- }
- return 0;
- }
- //1-11
- #include <iostream>
- int main()
- {
- int a,b,_max,_min,i;
- std::cout<<"请输入两个数:";
- std::cin>>a>>b;
- _max = a>b?a:b;
- _min = a>b?b:a;
- std::cout<<"这两个数区间内的整数有:"<<std::endl;
- for(i = _min;i<=_max;i++)
- {
- std::cout<<i<<" ";
- }
- std::cout<<std::endl;
- return 0;
- }
练习1.16
- #include <iostream>
- int main()
- {
- int sum = 0,a;
- while(std::cin>>a)
- {
- sum+=a;
- }
- std::cout<<sum<<std::endl;
- return 0;
- }
练习1.19
- #include <iostream>
- int main()
- {
- int a,b,t;
- std::cout<<"请输入两个数:";
- std::cin>>a>>b;
- if(a>b)
- {
- t = a;
- a = b;
- b = t;
- }
- std::cout<<"这两个数区间内的整数有:"<<std::endl;
- while(a<=b)
- {
- std::cout<<a++<<" ";
- }
- std::cout<<std::endl;
- return 0;
- }
练习1.20
首先是接下来几个代码要用到的头文件Sales_item.h
- #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 <iostream>
- #include <string>
- 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() = default;
- Sales_item(const std::string &book): bookNo(book) { }
- 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; // explicitly initialized
- 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 <iostream>
- #include "Sales_item.h"
- int main()
- {
- Sales_item book;
- while(std::cin>>book)
- {
- std::cout<<book<<std::endl;
- }
- return 0;
- }
练习1.21
- #include <iostream>
- #include "Sales_item.h"
- int main()
- {
- Sales_item book1,book2;
- std::cin>>book1>>book2;
- if(book1.isbn()==book2.isbn())
- {
- std::cout<<book1+book2<<std::endl;
- }
- else
- {
- std::cout<<"两本书的isbn必须相同"<<std::endl;
- }
- return 0;
- }
练习1.22
- #include <iostream>
- #include "Sales_item.h"
- int main()
- {
- Sales_item book,sum;
- if(std::cin>>sum)
- {
- while(std::cin>>book)
- {
- if(book.isbn()==sum.isbn())
- sum+=book;
- else
- {
- std::cout<<sum<<std::endl;
- sum = book;
- }
- }
- std::cout<<sum<<std::endl;
- }
- else
- {
- std::cout<<"没有输入?"<<std::endl;
- }
- return 0;
- }
练习1.23
- #include <iostream>
- #include "Sales_item.h"
- int main()
- {
- Sales_item book,now;
- if(std::cin>>now)
- {
- int cnt = 1;
- while(std::cin>>book)
- {
- if(book.isbn()==now.isbn())
- {
- cnt++;
- }
- else
- {
- std::cout<<now.isbn()<<"有"<<cnt<<"次销售记录"<<std::endl;
- now = book;
- cnt = 1;
- }
- }
- std::cout<<now.isbn()<<"有"<<cnt<<"次销售记录"<<std::endl;
- }
- else
- {
- std::cout<<"没有输入?"<<std::endl;
- }
- return 0;
- }