I have never typed C++ code since I retired. In C++ language, when solving the problem about floating point type, I have encountered a similar situation before, but it seems that there is no card data, basically using a setprecision function to reserve a few significant digits AC. But this time I got stuck on a set of data when calculating the average of any five numbers, and the problem was as follows:

#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main(a){
    float a,b,c,d,e;
    cin>>a>>b>>c>>d>>e;
    float ave = (a+b+c+d+e)*1.0/5;
    / / cout < < setiosflags (ios: : fixed) < < setprecision (2) < < ave * 1.0 < < endl;
    cout<<setprecision(2)<<ave*1.0<<endl;
    //printf("%.2f",ave);
    return 0;
}
/* * Problem: Enter five consecutive numbers, ranging from 0.00 to 2.00, and output their average value, reserving two decimal places. * * * /

/* Use case: 1.82 1.86 1.88 1.65 1.78 Output should be: 1.80 Your output is: 1.8 ** */
Copy the code

Let’s take a look at this code from beginning to end.

#include

contains all of the C++ header files, so you may not remember many of the smaller ones.

The source code for bits/stdc++. H is as follows:

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
3.1, as Published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h * This is an implementation file for a precompiled header. */

/ / 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
Copy the code

Include is an I/O flow control header file, similar to the formatted output file in C, just remember, some specific operators and functions can refer to the table below.

The operator role
dec Set the integer to decimal
hex Set the integer to hexadecimal
oct Set the integer to octal
setbase(n) Set integer to base n (n=8,10,16)
setfill(n) Set character padding. C can be a character constant or a character variable
setprecision(n) Sets the significant number of a floating point number to n bits
setw(n) Set the field width to N bits
setiosflags(ios::fixed) Sets the display of a floating point number to a fixed decimal number
setiosflags(ios::scientific) Sets the scientific representation of floating-point numbers
setiosflags(ios::left) Output left aligned
setiosflags(ios::right) Output right aligned
setiosflags(ios::skipws) Ignore leading whitespace
setiosflags(ios::uppercase) In scientific notation output E with hexadecimal output X in uppercase output, otherwise lowercase
setiosflags(ios::showpos) A “+” sign is displayed when a positive number is printed
setiosflags(ios::showpoint) Force display of decimal point
resetiosflags() Terminates the output format state that has been set, specifying content in parentheses

Floating-point numbers but one thing to keep in mind is that the significant digits of a floating-point number default to 6. You can use the setPrecision (n) operator to change the number of significant digits displayed. But there are two important pitfalls to be aware of:

  • If the significant number is less than the number to display, setPrecision drops it
  • The trailing zero will be omitted

What if we want to output the number of decimal places we want?

C++ defines an ios::fixed operator in the iostream header, which prints output data to the screen as a decimal point. So we can artificially control the output to keep the number of digits after the decimal point.

Setiosflags (ios::fixed) is a function defined in. This operator performs actions in the area specified by the argument. We pass in the argument ios::fixed, which specifies actions in the form of a decimal point. And move the number to the right of the decimal point as accurately as possible.

For example, let’s take the example above:

cout<<ave*1.0<<endl;                                                (1)
cout<<setprecision(2)<<ave*1.0<<endl;                               (2)
cout<<setiosflags(ios::fixed)<<ave*1.0<<endl;                       (3)
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ave*1.0<<endl;      (4)
Copy the code

From what has been described above, we can easily get the following results:

(1) = 1.798
(2) = 1.8
(3) = 1.798000
(4) = 1.80
Copy the code