资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++输出控制

一.输出cout需要用到头文件#include

创新互联公司专注于企业全网整合营销推广、网站重做改版、广阳网站定制设计、自适应品牌网站建设、H5页面制作商城建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为广阳等各大城市提供网站开发制作服务。

1.方法:cout << expression << endl;

or  

            cout << "A statement\n";

2.换行可以用endl, 也可以用以前C学过的

  个人认为在输出用双撇号括起来的句子中用\n换行更方便,在输出表达式的时候用endl更为自然

3.关于cout的实质:

        cout实际上把要输出的内容全部转换成字符到输出流中,以字符的形式输出,即使是换行符\n也是转换成相应的字符。

4.头文件#include 中可以用到的与cout有关的无参控制方法:

    endl, flush, dec, hex, left, right,fixed, showpoint

二.控制cout的输出需要用到头文件#include

1.控制输出位数:

a.     setprecision(int n)

    利用cout  << setprecision(int n) << expression << endl;

    控制在本次输出过程中每次输出数字的时候都输出setprecision(int n)中的参数所示的n位数字,包含小数。

    如

   

#include
#include
using namespace std;
int main() {
    double x = 123.453;
    cout << setprecision(4) << "x = " << x << endl;
    return 0;
}

    输出结果为:x = 123.5;

    可见,setprecision(int n)会在保证输出位数(包括小数部分)为n的前提下进行四舍五入(round);

如果代码改成这样

#include
#include
using namespace std;
int main() {
    double x = 123.453;
    cout << setprecision(8) << "x = " << x << endl;
    return 0;
}

输出结果为:x = 123.453

可见setprecision(int n) 并不会为了凑齐n位的输出而添加后导0

如果要添加后导0使得输出结果共有n位(整数部分加小数部分),则需要用到showpoint,详见下文

   又如

#include
#include
using namespace std;
int main() {
    double x = 123.4530000000;
    cout << setprecision(8) << "x = " << x << endl;
    return 0;
}

 输出结果为:x = 123.453

b.   fixed

我们可以发现这样输出没有把x后面的0输出,要实现把后面的0输出,需要添加fixed

结合了fixed 和 setprecision(int n)后保证了输出结果的小数部分有n位

把上面代码的输出改成

cout << fixed << setprecision(8) << "x = " << x << endl;

输出结果为:x = 123.45300000;

c.   showpoint

上面代码在结合了fixed 和 setprecision(int n)后保证了输出结果的小数部分有n位,但是如果要令输出结果一共有n位,则需要用到showpoint

#include
#include
using namespace std;
int main() {
    double x = 123.453;
    cout << showpoint << setprecision(8) << "x = " << x << endl;
    return 0;
}

输出结果为:x = 123.45300;

三.在输出的时候进行类型转换

     适用范围:转换后的变量不需要保存,只使用一次

 1.转换为整型:

#include
using namespace std;
int main() {
    int x = 1234;
    cout << dec << x << endl;
    cout << hex << x << endl;
    cout << oct << x << endl;
    return 0;

}

输出结果:

1234

4d2

2322

2.  setw(int n)的用法

设置输出的宽度为n,默认为像右(right)对齐

#include
#include
using namespace std;
int main() {
    int i1 = 1, i2 = 11, i3 = 111, i4 = 1111,
    i5 = 11111, i6 = 111111;
    cout << setw(6) << i1 << endl
    << setw(6) << i2 << endl
    << setw(6) << i3 << endl
    << setw(6) << i4 << endl
    << setw(6) << i5 << endl
    << setw(6) << i6 << endl << endl << endl;
    cout << left << setw(6) << i1 << endl
    << setw(6) << i2 << endl
    << setw(6) << i3 << endl
    << setw(6) << i4 << endl
    << setw(6) << i5 << endl
    << setw(6) << i6 << endl;
    return 0;
}

输出结果:

          1

        11 

      111

    1111

  11111

111111

1

11

111

1111

11111

111111

3.科学计数法表示

用到scientific

#include
#include
using namespace std;
int main() {
    double i = 123300000000;
    cout << setprecision(4) << scientific << i << endl;
    return 0;
}

输出结果:

1.233e+011


网页名称:C++输出控制
文章位置:http://cdkjz.cn/article/goohss.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220