(一)输入输出

(一)输入输出

1、读取
#include <iostream>
#include <cstring>
using namespace std;

int main()
{

    int a;
    int i[10];
    memset(i,0,sizeof(i));

    while(scanf("%d",&a) != EOF){
            i[0]++;
    }
    cout<<"(1) "<<i[0]<<endl;

    while(cin>>a){
            i[1]++;
    }
    cout<<"(2) "<<i[1]<<endl;

    while(scanf("%d",&a)!=-1){
        i[3]++;
    }
    cout<<"(3) "<<i[3]<<endl;

    return 0;
}

结果:
在这里插入图片描述

2、输出
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
    // left right fixed scientific setprecision setfill 具有后效性
    // setw(10) 不具有后效性
    // fixed和setprecision的作用还在。只要你设置了,会一直在(除非你把fixed清除,setprecision()修改)
    //%md m位右对齐输出,高位补空格
    //%0md  m位右对齐输出,高位补0

    double n;
    cin>>n;
    cout<<n<<endl; // 默认以6精度,所以输出为 12.3457

    // 可以这样认为,默认省去了setprecision(6)这句代码
    cout << setprecision(4) << n << endl; // 改成4精度,所以输出为12.35
    cout << "-------" << endl;


    cout << fixed << setprecision(4) << n << endl;
    // 加了fixed意味着是固定点方式显示,所以这里精度指的是小数位,输出为12.3457

    cout <<  n << endl;
    // fixed和setprecision的作用还在,依然显示12.3457

    cout.unsetf( ios::fixed );
    // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35   setprecision(4)的作用仍存在
    cout << n << endl;
    // 恢复成原来的样子,输出为12.3457
    cout << "-------" << endl;


    cout << setprecision(6) << n << endl;
    cout << scientific << n << endl; // scientific与 fixed 类似 一个是科学计数,一个是小数形式
    cout.unsetf( ios::scientific);
    cout << n << endl;
    cout << "-------" << endl;


    // setfill必须写在开头
    cout << setfill('*')<< right << setw(10) << n <<  endl;
    cout << setfill('#')<< setw(10) << n <<  endl;

    // setw(10) 不具有后效性 所以需要在每个n前加上
    cout << setfill('*')<< left <<  n <<  endl;
    cout << setfill('#')<< setw(10) << n <<  endl;
    cout<<"-------"<<endl;


    int m;
    cin>>m;
    printf("%6d\n",m);  //%md m位右对齐输出,高位补空格
    printf("%06d\n",m);//%0md  m位右对齐输出,高位补0



    return 0;

}


输出结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值