C++ basic review series — sun bujian 1208

C++ basic review series 01 (input/output class, calling mathematical function class)

C++ basics review series 2 (print graphics (loop), classic problems)

C++ basic review series 3 (recursive algorithm) {Fibonacci function, Hanoi issues}

C++ basics review series 4 (summary of scattered data)

C++ basics review series 5

I. Input and output classes

(1) To the decimal place:

#include < iomanip> cout<<setiosflags(ios::fixed)<< setPrecision (2); Cout.setf (ios::fixed); cout<<setprecision(2); Cout <<fixed<< setPrecision (2); We want to keep n decimal places setPrecision (n)Copy the code

(2) Width control/left/right alignment

Use the setw(n) function, remember the header #include< iomanip > setw(n) function is a C++ function that sets the field width to be used in the output operation, where n represents the field width

The code is as follows:

If you want to use a special symbol as a placeholder, let’s use @ as an example. To use a special symbol as a placeholder, you need to setfill(‘x’) — where ‘x’ is a character,

The code is as follows:

By default, setw() aligns output to the right, but if you want to align output to the left, you need to add setiosFlags (ios::left).

(3) Any integer gets the value of each digit

#include <iostream> using namespace std; void sb(int n) { if(n>9) { sb(n/10); Cout <<n%10<<" "; } else cout<<n<<" "; } int main() {int n=0; cin>>n; sb(n); cout<<endl; return 0; }Copy the code

Call mathematical function class

1. Open square

double sqrt(double x);

2. Take the constant e to the x

double exp(double x);

3. Raise x to the y power

double pow(double x, double y);

4. Find the logarithm of (x)

double log(double x);

Strives for the logarithmic lg (x)

double log10(double x);

For others, use the base change formula

5. Find the absolute value of x

int abs(x);

long int abs(long int x);

double fabs(double x);

6. Trig functions

For sinusoidal

double sin(double x);

Strives for the cosine

double cos(double x);

O tangent

double tan(double x);

The arctangent

double atan(double x);

7. Integer function

Take up the whole

double ceil(double x);

Take down the whole

double floor(double x);

8. Generate random numbers 0 to 32767

int rand(void); A*(10^B) = A*(10^B) Note: B must be an integer!