当前位置: 首页 > news >正文

C/C++输入输出(1)

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)
%Lflong 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;
}

注:

  1. scanf函数的占位符后面一般不会加\n,\n是换行,一般在输出的时候才使用。 
  2. scanf函数中存储数据的变量前面必须加上&运算符,因为scanf()需要的是地址,必须将变量的地址取出来传给scanf函数。
  3. scanf函数中指定的格式和给程序输入的数据格式要严格的匹配,否则可能不能得到想要的值。
  4. 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;
}

http://www.lryc.cn/news/547237.html

相关文章:

  • 前端面试场景题葵花宝典之四
  • 探索Elasticsearch:索引的CRUD
  • Java数据结构第十六期:走进二叉树的奇妙世界(五)
  • 【开源免费】基于SpringBoot+Vue.JS疫情管理系统(JAVA毕业设计)
  • 有关Java中的集合(1):List<T>和Set<T>
  • 使用 Spring Boot 实现前后端分离的海康威视 SDK 视频监控
  • 在 Apache Tomcat 中,部署和删除项目
  • 宇树科技G1人形机器人:从炫技到实用,AI驱动下的进化跃迁‌
  • 给定计算预算下的最佳LLM模型尺寸与预训练数据量分配
  • H5DS编辑器是如何让企业快速构建动态页面
  • 面试题汇总(一)
  • 论坛系统测试报告
  • 算法比赛中处理输入和输出
  • llama.cpp: GGUF格式及模型量化参数介绍
  • PGlite:浏览器中运行的PostgreSQL
  • 【C++】vector(上):vector的常用接口介绍
  • 【算法】二分查找(上)
  • 【人工智能】GPT-4 vs DeepSeek-R1:谁主导了2025年的AI技术竞争?
  • linux nginx 安装后,发现SSL模块未安装,如何处理?
  • 蓝桥杯 - 每日打卡(类斐波那契循环数)
  • 深入探索C++17文件系统库:std::filesystem全面解析
  • LLM | 论文精读 | GIS Copilot : 面向空间分析的自主GIS代理
  • Unity 适用Canvas 为任一渲染模式的UI 拖拽
  • 基于遗传算法的无人机三维路径规划仿真步骤详解
  • windows下使用Hyper+wsl实现ubuntu下git的平替
  • 基于Java+SpringCloud+Vue的前后端分离的房产销售平台
  • 以影像技术重构智能座舱体验,开启驾乘互动新纪元
  • deepseek在pycharm 中的配置和简单应用
  • LLM大型语言模型(一)
  • 尚庭公寓项目记录