big c++ 读书笔记
第4章 基本控制流
零警告编译
int pennies = static_cast<int>(100 * (amount_due - amount_paid));
输入确定
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
double area;
cout<< "Please enter the area of a square:";
cin>>area;
if(cin.fail())
{
cout<<"error:bad input\n";
return 1;
}
if( area < 0)
{
cout<<"error : negaive area\n";
return 1;
}
}
函数注释风格
/**
计算
@param x
@param y
@return
**/
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
/**
测试两个浮点数是否相近
@param x
@param y
@return true 若近识相等则为真
**/
bool approx_equal(double x,double y)
{
const double EPSION = 1E-14;
if(x==0) return fabs(y) <= EPSION;
if(y==0) return fabs(x) <= EPSION;
return fabs(x-y) / max(fabs(x),fabs(y)) <= EPSION;
}
int main()
{
double x;
cout<<"Enter a number;";
cin >>x;
double y;
cout<<"Enter another number;";
cin>>y;
if(approx_equal(x,y))
cout<<"the numbers are approximately equal.\n";
else
cout<<" the numbers are different.\n";
return 0;
}
第九章
1. C++中 实现数据项序列有更好的办法: 向量结构。
2.vector<double> salaries;
salaries[0] = 32000;
若定义向量时不带长度参数,它将是空的并不能保持元素。
3.