本章主要工作是编写了一个书店程序,关于语句块,main函数等非常基础的知识不会再提。我使用的IDE是code:blocks,由于VS2010不支持C++11就没有用VS。书中的命令行运行程序即在cmd中运行,大家可以自己尝试,本文不作示范。
知识点1
- 首先要认识的iostream库,库内包含两个基础类型istream(输入流)和ostream(输出流)。
- 标准库定义4个IO对象,cin(标准输入), cout(标准输出), cerr(输出警告或错误), clog(输出程序运行时的信息),>> 输入运算符,<<输出运算符,分别对流进行输入输出操作。
- 没有using namespace std;时,使用命名空间内的名字必须用std::(如std::cout)
习题1
1.1 (略)
1.2 运行后显示process returned -1
1.3
#include<iostream>
int main()
{
std::cout <<" Hello, world." << std::endl;
return 0;
}
1.4
#include<iostream>
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<iostream>
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
知识点2
- 注释,同C //单行 /* /多行,注意:注释是不能嵌套的
习题2
1.7 自己试一下,应该都会有这句
warning: "/" within comment [-Wcomment]|
1.8 第一个和第二个合法,后两个均不合法,第三个最后加一个 " 就可以打印/,第四个是<<后面全被注释掉
知识点3
- while循环:while(condition){ statement }
- for循环:for(初始化计数变量;判断条件;++), 通常都用前++形式防止出错
- 读取不定量数据常用while(cin >> val),输入ctrl+z可终止输入
- if语句,和C中if基本相同
习题3
1.9
#include<iostream>
int main()
{
int sum = 0, val = 50;
while(val <= 100)
{
sum += val;
val++;
}
std::cout << "The sum of 50 to 100 is " << sum
<< std::endl;
return 0;
}
1.10
#include<iostream>
int main()
{
int i = 10;
while(i >= 0)
{
std::cout << i-- << " ";
}
return 0;
}
1.11
#include<iostream>
int main()
{
int i, j;
std::cout << "Enter two numbers:" << std::endl;
std::cin >> i >> j;
std::cout << "The numbers between " << i << " and "
<< j << "is ";
while(i < j)
{
std::cout << i << " ";
i++;
}
while(i > j)
{
std::cout << j << " ";
j++;
}
return 0;
}
1.12 从-100一直加到100 sum的终值是0
1.13
#include<iostream>
int main()
{
//50到100相加
int sum = 0;
int v1, v2;
for(int i = 50; i <= 100; ++i)
sum += i;
std::cout << "Sum of 50 to 100 inclusive is "
<< sum << std::endl;
//输出10到0
for(int i = 10; i >= 0; i--)
std::cout << i << " ";
std::cout << std::endl;
//输出两个整数范围内所有整数
std::cout << "Enter two numbers: " << std::endl;
std::cin >> v1 >> v2;
for(v1; v1 < v2; v1++)
std::cout << v1 << " ";
for(v2; v2 < v1; v2++ )
std::cout << v2 << " ";
return 0;
}
1.14 for循环指定次数,while循环可不固定次数
1.15 14页指出了语法错误,声明错误,类型错误。
1.16
#include<iostream>
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的话
1 occurs 7 times
1.18
#include<iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal){
int cnt = 1;
while (std::cin >> val){
if (val == currVal)
++cnt;
else{
std::cout << currVal << " occurs "
<< cnt << "times" <<std::endl;
currVal = val;
cnt = 1;
}
}//while
std::cout << currVal << " occurs "
<<cnt << " times " << std::endl;
}//if
return 0;
}
1.19
#include<iostream>
int main()
{
int i, j;
std::cout << "Enter two numbers:" << std::endl;
std::cin >> i >> j;
std::cout << "The numbers between " << i << " and "
<< j << "is ";
if (i < j){
while(i <= j)
{
std::cout << i << " ";
i++;
}
}
else{
while(i >= j)
{
std::cout << j << " ";
j++;
}
}
return 0;
}
知识点4
- 类,定义了一个数据结构和与其关联的一组操作。类类型是面向对象程序语言一个的特征
- 成员函数(类中的函数),通过点运算符访问。
习题4
Sales_item.h
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
class Sales_item
{
public:
Sales_item(const std::string &book):isbn(book),units_sold(0),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:
Sales_item & operator+=(const Sales_item&);
public:
double avg_price() const;
bool same_isbn(const Sales_item &rhs)const
{
return isbn == rhs.isbn;
}
Sales_item():units_sold(0),revenue(0.0){}
public:
std::string isbn;
unsigned units_sold;
double revenue;
};
using std::istream;
using std::ostream;
Sales_item operator+(const Sales_item &,const Sales_item &);
inline bool operator==(const Sales_item &lhs,const Sales_item &rhs)
{
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);
}
inline Sales_item & Sales_item::operator +=(const Sales_item &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline Sales_item operator+(const Sales_item &lhs,const Sales_item &rhs)
{
Sales_item ret(lhs);
ret += rhs;
return ret;
}
inline istream& operator>>(istream &in,Sales_item &s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if(in)
s.revenue = s.units_sold * price;
else
s = Sales_item();
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 0;
}
#endif
1.20
#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<stdio.h>
#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 << "False input!";
return 0;
}
1.22
#include<stdio.h>
#include"Sales_item.h"
int main()
{
Sales_item book, total;
std::cin >> total;
while(std::cin >> book && (total.isbn == book.isbn))
{
total += book;
} //不太懂两次ctrl+z才可退出
std::cout << total << std::endl;
return 0;
}
1.23,24
#include<stdio.h>
#include"Sales_item.h"
int main()
{
Sales_item currbook, book;
if (std::cin >> currbook){
int cnt = 1;
while(std::cin >> book){
if (currbook.isbn == book.isbn)
cnt++;
else{
std::cout << currbook.isbn << " has sold " <<cnt
<< "times" << std::endl;
currbook = book;
cnt = 1;
}
}
std::cout << currbook.isbn << " has sold " <<cnt
<< "times" << std::endl;
}
return 0;
}
1.25
#include<iostream>
#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;
}
}
std::cout << total << std::endl;
}
else{
std::cerr << "No data!" << std::endl;
return -1;
}
return 0;
}