Chapter 2 Basic Control Flow
Zero warning Compilation
Int pennies = static_cast <int> (100 * (amount_due-amount_paid ));
Enter OK
# 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;
}
}
Function annotation Style
/**
Computing
@ Param x
@ Param y
@ Return
**/
# Include <iostream>
# Include <string>
# Include <cmath>
Using namespace STD;
/**
Test whether two floating point numbers are similar.
@ Param x
@ Param y
@ Return true if near phase recognition is 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;
}
Chapter 9
1. There is a better way to achieve data item sequence in C ++: vector structure.
2. Vector <double> salaries;
Salaries [0] = 32000;
If a vector is defined without a length parameter, it will be empty and cannot retain elements.
3.