博客
关于我
第15节 数据的输入
阅读量:312 次
发布时间:2019-03-03

本文共 1410 字,大约阅读时间需要 4 分钟。

一.用scanf函数输入

1.scanf函数的功能:从键盘获得数据scanf(格式描述,变量地址); #include 
2.格式控制符%d,%i:用来输入整型数据,长整型ld,短整型hd;%o:用来输入八进制整数;%x:用来输入十六进制整数;%u:用来输入无符号十进制数;%c:用来输入单个字符;%f:用来输入浮点数,可用小数或指数形式输入;
int a,b;scanf("%d %d",&a,&b);printf("%d\n",a+b);

二.输入数据的分割

int a,b;scanf("%d %d",&a,&b);   2空格3 /2回车3 /2Tab3scanf("%d%d",&a,&b);    2空格3 /2回车3 /2Tab3scanf("%d,%d",&a,&b);    2逗号3scanf("%dand%d",&a,&b);  2and3scanf("a=%d,b=%d,c=%d",&a,&b,&c); a=2,b=3,c=4

三.不同类型数据的自然分割

#include 
int main(){ int a, b, c;char op;scanf("%d%c%d",&a,&op,&b);if(op=='+'){ c=a+b;printf("会算%c,结果是:%d\n", op, c);}else{ printf("不会算%c\n", op);}return 0;}

四.输入的数据都被暂放在"缓冲区"

#include 
int main(){ int a, b, c; scanf_s("%d", &a); scanf_s("%d", &b); scanf_s("%d", &c); printf("%d,%d,%d,%d", a, b, c);}输入输出结果:1 2 3 4 51,2,3

五.指定宽度的输入

#include 
int main(){ int a, b; scanf_s("%2d%3d", &a,&b); printf("%d,%d", a, b);}输入输出结果:1234567812,345

六.注意控制字符和类型的匹配

#include 
int main(){ double a, b; int c, d; scanf_s("%lf%lf", &a, &b); scanf_s("%d", &c); scanf_s("%d", &d); printf("%f,%f,%d,%d", a, b, c, d);}输入输出结果:1 2 3 41.000000,2.000000,3,4

七.实践项目

用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,问在键盘上如何输入?

#include 
int main(){ int a, b; float x, y; scanf_s("a=%d b=%d", &a, &b); scanf_s("%f %f", &x, &y); printf("%d %d %f %e", a, b, x, y); return 0;}输入输出结果:a=3 b=78.5 71.823 7 8.500000 7.182000e+01

转载地址:http://yjqm.baihongyu.com/

你可能感兴趣的文章
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>