1.getchar和putchar
1.1getchar()
函数原型:
1 int getchar(void);
getchar()函数返回用户从键盘输入的字符,使用时不带有任何参数。
程序运行到这个命令就会暂停,等待用户从键盘输入,等同于使用cin或scanf()方法读取一个字符。
getchar()函数原型定义在头文件<cstdio>。
#include"cstdio"
#include"iostream"
using namespace std;int main()
{int ch;ch=getchar();cout << ch << endl;cout << (char)ch << endl;return 0;
}
因为getchar()返回的是字符类型,所以不会忽略起首的空白字符,总是返回当前读取的第一个字符。
如果读取失败,返回常量EOF,由于EOF通常是-1,所以返回值的类型要设为int,而不是char。
我们可以直接按Ctrl+z来实现读取失败。
1.2putchar()
函数原型:
1 int putchar(int character);
putchar()函数将它的参数字符输出到屏幕,它的原型定义在头文件<cstdio>。
#include"cstdio"int main()
{int ch=0;ch=getchar();putchar(ch);return 0;
}
当操作成功时,putchar()返回输出的字符,否则返回EOF。
2.scanf和printf
2.1printf
printf()函数原型:
1 int printf(const char* format,……);
2.1.1基本用法
printf()的作用是将参数文本输出到屏幕上。
#include <cstdio>int main()
{printf("Hello World");return 0;
}
printf()本身是不会在结尾处换行的,所以我们可以在结尾处增加一个\n,从而实现换行。
2.1.2占位符
printf()可以在输出文本中指定占位符。
#include <cstdio>int main()
{printf("There are %d apples\n", 3);return 0;
}
在上面我们能够看出%d就是占位符,表示这个位置要用其他值来替换。
同时在使用中,我们也可以使用多个占位符,使用顺序也是和占位符的顺序是一致的。
常见的占位符:
占位符 | 介绍 |
%d | 十进制整数 |
%lld | 十进制long long int类型 |
%f | 小数(包含float和double) |
%Lf | long double类型浮点数 |
%c | 字符 |
%s | 字符串 |
2.1.3格式化输出
2.1.3.1限定宽度
printf()允许限定占位符的最小宽度。
#include"cstdio"int main()
{printf("%5d",123);return 0;
}
上面示例中,%5d表示这个占位符的宽度至少为5位。如果不满5位,对应的值的前面会添加空格。
输出端值默认是右对齐,即输出内容前面会有空格;如果希望改成左对齐,在输出内容后面添加空格,我们可以在%后面插入一个-号。
2.1.3.2限定小数位数
输出小数时,有时希望限定小数位数。
#include"cstdio"int main(){printf("%.2f",12.345);return 0;
}
当我们希望小数点后面只保留两位,占位符可以写成%.2f。
最小宽度和小数位数这两个限定值,都可以用*代替,通过printf()的参数传入。
#include"cstdio"int main(){printf("%*.*f",6,2,12.345);return 0;
}
上面%*.*f的两个星号通过printf()的两个参数6和2传入。
2.2scanf
scanf()函数原型:
1 int scanf( const char* format,……);
2.2.1基本用法
scanf()函数用于读取用户的键盘输入。
程序运行到scanf()这个语句时,会停下来,等待用户从键盘输入。
#include"cstdio"int main()
{int i=0;scanf("%d",&i);printf("%d\n",i);return 0;
}
注:
- scanf函数的占位符后面一般不会加\n,\n是换行,一般在输出的时候才使用。
- scanf函数中存储数据的变量前面必须加上&运算符,因为scanf()需要的是地址,必须将变量的地址取出来传给scanf函数。
- scanf函数中指定的格式和给程序输入的数据格式要严格的匹配,否则可能不能得到想要的值。
- scanf()处理数值占位符时,会自动过滤空白字符,包括空格、制表符、换行符等。
2.2.2scanf的返回值
scanf()的返回是一个整数,表示成功读取的变量个数。
如果没有读取任何项,或者匹配失败,则返回0.如果在成功读取任何数据之前,发生了读取错误或者读取到文件结尾,则返回常量EOF(-1)。
#include"cstdio"int main()
{int a=0,b=0;float f=0;int r=scanf("%d %d %f",&a,&b,&f);printf("a=%d,b=%d,f=%f\n",a,b,f);printf("%d\n",r);return 0;
}
如果输入两个数后,按ctrl+z,提前结束输入。
如果输入的数据都不能匹配成功的话,则输出的r是0。
如果一个数字都不输入,直接按ctrl+z,输出的r是-1,也就是EOF。
2.3练习
练习1:浮点除法
#include"cstdio"
#include"iostream"
using namespace std;int main()
{int a=0;int b=0;cin >> a >> b;printf("%.3f",a*1.0/b);return 0;
}
练习2:B2012 甲流疫情死亡率 - 洛谷
#include"cstdio"int main()
{int a=0,b=0;scanf("%d %d",&a,&b);printf("%.3f%%\n",b*100.0/a);return 0;
}
练习3: B2013 温度表达转化 - 洛谷
#include"cstdio"int main()
{double F=0;scanf("%lf",&F);double C=5*(F-32)/9.0;printf("%.5lf",C);return 0;
}
练习4:B2015 计算并联电阻的阻值 - 洛谷
#include"cstdio"int main()
{float r1=0,r2=0;scanf("%f %f",&r1,&r2);printf("%.2f",r1*r2*1.0/(r1+r2));return 0;
}
练习5:B2014 与圆相关的计算 - 洛谷
#include"cstdio"
#include"iostream"
using namespace std;double p=3.14159;
double r;int main()
{cin >> r ;printf("%.4lf %.4lf %.4lf",r*2,r*2*p,r*r*p);return 0;
}
练习6:B2004 对齐输出 - 洛谷
#include"cstdio"int main()
{int a=0,b=0,c=0;scanf("%d %d %d",&a,&b,&c);printf("%8d %8d %8d",a,b,c);return 0;
}
练习7:信息学奥赛一本通(C++版)在线评测系统
#include"iostream"
#include"cstdio"
using namespace std;int a,b,c,d,e;int main()
{cin >> a >> b >> c >> d >> e;a/=3;e+=a;b+=a;b/=3;a+=b;c+=b;c/=3;b+=c;d+=c;d/=3;c+=d;e+=d;e/=3;d+=e;a+=e;printf("%5d%5d%5d%5d%5d",a,b,c,d,e);return 0;
}
3.cin和cout
- cin是C++中提供的标准输入流对象
- cout是C++中提供的标准输出流对象
- cin和cout的输入输出非常方便,不需手动控制格式,能够自动识别变量类型
3.1基本用法
#include <iostream>
using namespace std;int main()
{int a;char c;float f;cin >> a; // 读取⼀个整数cin >> c; // 读取⼀个字符cin >> f; // 读取取⼀个浮点数cout << "打印结果:"<<endl;cout << a << endl;cout << c << endl;cout << f << endl;return 0;
}
练习:
练习1:P5705 【深基2.例7】数字反转 - 洛谷
#include"iostream"
using namespace std;int main()
{char a,b,c,d,e;cin >> a >> b >> c >> d >> e;cout << e << d << c << b << a << endl;return 0;
}
练习2:P5708 【深基2.习2】三角形面积 - 洛谷
#include"cstdio"
#include"cmath"
#include"iostream"
using namespace std;int main()
{double a,b,c;cin >> a >> b >> c;double p=(a+b+c)/2;printf("%.1f",sqrt(p*(p-a)*(p-b)*(p-c)));return 0;
}