温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ 输出控制

发布时间:2020-06-08 12:36:30 来源:网络 阅读:325 作者:Jayce_SYSU 栏目:移动开发

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

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

or  

            cout << "A statement\n";

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

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


3.关于cout的实质:

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


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

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


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

1.控制输出位数:


a.     setprecision(int n)


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

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

    如

   

#include <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
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 <iostream>
#include <iomanip>
using namespace std;
int main() {
    double x = 123.453;
    cout << showpoint << setprecision(8) << "x = " << x << endl;
    return 0;
}


输出结果为:x = 123.45300;



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

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

 1.转换为整型:


#include <iostream>
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 <iomanip>
#include <iostream>
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 <iomanip>
#include <iostream>
using namespace std;
int main() {
    double i = 123300000000;
    cout << setprecision(4) << scientific << i << endl;
    return 0;
}


输出结果:

1.233e+011
















向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI