3. 输入输出详解

3.1 输入输出控制符

1)在printf和scanf中可以使用"%"开头的控制符,说明要输入或者输出的数据类型以及格式.
2)常见的输入输出控制符如下:

在这里插入图片描述

3.2 读取输入数据

1)用scanf可以一次读入多个类型不同的变量,只要输入的各项之间用空格分隔即可.

#include<iostream>
#include<cstdio>    //使用printf,scanf必须要有这一行
using namespace std;

int main(){
    int n;
    char c;
    float m;
    scanf("%d%c%f", &n, &c, &m);
    printf("%d %c %f", n, c, m);
    return 0;
}
输入:23m123
输出:23 m 123.000000

2)输入字符时,不会跳过空格(空格也会被当做字符输入),输入其他类型时会跳过空格.

#include<iostream>
#include<cstdio>    //使用printf,scanf必须要有这一行
using namespace std;

int main(){
    int n;
    char c;
    float m;
    scanf("%d%c%f", &n, &c, &m);
    printf("%d %c %f", n, c, m);
    return 0;
}
输入:12 k 123 				把空格读入c  k读入m 导致输入出错
输出:12   0.000000     

3)如果在输入中有scanf中出现的非控制字符,则这些字符会被跳过.

#include<iostream>
#include<cstdio>    //使用printf,scanf必须要有这一行
using namespace std;

int main(){
    int n,m;
    char c;
    float f;
    scanf("%d %c,%f:%d", &n, &c, &f, &m);
    printf("%d %c %f %d", n, c, f, m);
    return 0;
}
输入:12 k,3.14:4
输出:12 k 3.140000 4

4)控制printf输出整数的宽度:

%nd(如%4d, &12d):以n字符宽度输出整数,宽度不足时用空格填充
%0nd(如%04d,%012d):以n字符宽度输出整数,宽度不足时用0填充
#include<iostream>
#include<cstdio>    
using namespace std;
int main(){
    int n = 123;
    printf("%04d, %5d, %12d, %012d", n, n, 123456, 123456);
}
输出:0123,   123,       123456, 000000123456

5)控制printf输出浮点数宽度:

%.nf(如%.4f%.8f):输出浮点数,精确到小数点后n位.
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    float f = 12.34;
    printf("%.3f, %.5f", f, f, f);
}
输出:12.340, 12.34000

6)double精度高于float,所以尽量使用double.
7)double类型变量用%lf输入!

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    float f ,c;
    scanf("%f", &f);
    c = 5 * (f -32)/9;
    printf("%.5f", c);
    return 0;
}
输入:1501
输出:816.11108
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    double f ,c;
    scanf("%lf", &f);
    c = 5 * (f -32)/9;
    printf("%.5f", c);
    return 0;
}
输入:1501
输出:816.11111

使用double的结果更准确!!!

3.3 使用cin和cout

1)用C++的cout输出.

#include<iostream>
using namespace std;
int main(){
    int n1, n2;
    double f;
    char c;
    cin>> n1 >> n2 >> c >> f;
    cout<< n1 <<","<< n2 <<","<< c <<","<< f <<endl;
}
输入:5 10k 1.34
输出:5,10,k,1.34

2)用cin读入所有的输入字符,包括空格回车.

#include<iostream>
#include<stdio.h>    //使用EOF需要包含的头文件
using namespace std;
int main(){
    char c;
    //cin.get()从输入里面读取一个字符
    while((c = cin.get()) != EOF){
        cout<< char(c) ;
    }
}
输入:qwerrgt1235
输出:qwerrgt1235

3)用scanf读入所有的输入字符,包括空格回车.

#include<iostream>
#include<stdio.h>  
using namespace std;
int main(){
    char c;
    //cin.get()从输入里面读取一个字符
    while(scanf("%c", &c) != EOF){
        printf("%c" ,c);
    }
}

cin cout速度比scanf printf慢 数据量大的时候使用!!!
scanf和printf 两者不要混用!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值