cin cout
编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置。当用户按下enter键,数据输入停止。程序自动对所有的整数进行求和并打印出结果。
需要解决两个问题,提取数字,提取连续数字。
//c
#include<stdio.h> void main()
{
int sum=0;;
int i;
char ch; while(scanf("%d",&i)==1)
{
sum+=i; while((ch=getchar())==' ')//屏蔽空格
; if(ch=='\n')
break; ungetc(ch,stdin);//将变量ch中存放的字符退回给stdin输入流。 } printf("sum; %d\n",sum);
}
//c++
#include<iostream> using namespace std; int main()
{
int sum=0; cout<<"请输入"; int i; while(cin>>i)
{
sum+=i; while(cin.peek()==' ')
{
cin.get();
}
if(cin.peek()=='\n')
break;
} cout<<"sum:"<<sum<<endl; return 0; }
①表达式cin>>i返回输入流对象本身也就是cin,但如果读到文件尾或者提取操作符遇到一个非法值,返回值是false。
#include<iostream> using namespace std; int main()
{
char buf[20]; cin.ignore(7);
cin.getline(buf,10); cout<<buf<<endl; return 0;
}
/*
输入:12345 12345 12345 12345
输出:2345 1234
*/
②cin.ignore(7)忽视前七个字符
③cin.getline(buff,10)获取十个到buf中存放(第十位是'\0)
④using namespace std;名字空间,c++所有标识符都是在同一个特殊的名字空间(std)中定义的。如果没有使用这条指令,需要用std::cout这样的语法来调用输出流对象。
#include<iostream> using namespace std; int main()
{
const int SIZE=50;
char buf[SIZE]; cout<<"请输入一段文本:";
cin.read(buf,20); cout<<"字符串收集到的字符数为:"
<<cin.gcount()<<endl; cout<<"输入文本的信息是:";
cout.write(buf,20);
cout<<endl; return 0; }
题目:程序向用户提出一个“Y/N"问题。然后把用户输入的值赋值给answer变量。
#include<iostream> int main()
{
char answer; std::cout<<"请问可以格式化你的硬盘吗?【Y/N】"<<"\n";
std::cin>>answer; switch(answer)
{
case'Y':
case'y':
std::cout<<"随便格式化硬盘是不好的"<<"\n";
break;
case'N':
case'n':
std::cout<<"您的选择是明智的"<<"\n";
break;
default:
std::cout<<"您的输入不符合要求"<<"\n";
break;
}
return 0;
}
题目:摄氏转换
#include<iostream> int main()
{
//华氏温度==摄氏温度 *9.0/5.0+32;
const short ADD_SUBTRACT=32;
const double RATIO=9.0/5.0; double tempIn,tempOut;
char typeIn,typeOut; std::cout<<"请以【xx.C】或者【xx.F】格式输入一个温度:";
std::cin>>tempIn>>typeIn;
std::cin.ignore(100,'\n');//避免回车的影响
std::cout<<"\n"; switch(typeIn)
{
case'C':
case'c':
tempOut=tempIn*RATIO+ADD_SUBTRACT;
typeOut='F';
typeIn='C';
break;
case'F':
case'f':
tempOut=(tempIn-ADD_SUBTRACT)/RATIO;
typeOut='C';
typeIn='F';
break;
default:
typeOut='E';
break;
}
if(typeOut!='E')
{
std::cout<<tempIn<<typeIn
<<"="<<tempOut<<typeOut<<"\n";
}
else
{
std::cout<<"输入错误\n";
}
return 0;
}
重载(overloading)可以是参数个数,数据类型不同,但不能是返回值不同。
#include<iostream> void convertTemperature(double tempIn,char typeIn);
void convertTemperature(int tempInInt,char typeIn); int main()
{
//华氏温度==摄氏温度 *9.0/5.0+32
double tempIn;
int tempInInt;
char typeIn; std::cout<<"请以【xx.C】或者【xx.F】格式输入一个温度:";
std::cin>>tempIn>>typeIn;
std::cin.ignore(100,'\n');//避免回车的影响
std::cout<<"\n";
convertTemperature(tempIn,typeIn); std::cout<<"请以【xx C】或者【xx F】格式输入一个温度:";
std::cin>>tempInInt>>typeIn;
std::cin.ignore(100,'\n');//避免回车的影响
std::cout<<"\n";
convertTemperature(tempInInt,typeIn); return 0;
}
void convertTemperature(double tempIn,char typeIn)
{
const short ADD_SUBTRACT=32;
const double RATIO=9.0/5.0; double tempOut;
char typeOut; switch(typeIn)
{
case'C':
case'c':
tempOut=tempIn*RATIO+ADD_SUBTRACT;
typeOut='F';
typeIn='C';
break;
case'F':
case'f':
tempOut=(tempIn-ADD_SUBTRACT)/RATIO;
typeOut='C';
typeIn='F';
break;
default:
typeOut='E';
break;
}
if(typeOut!='E')
{
std::cout<<tempIn<<typeIn
<<"="<<tempOut<<typeOut<<"\n";
}
else
{
std::cout<<"输入错误\n";
}
}
void convertTemperature(int tempInInt,char typeIn)
{
const short ADD_SUBTRACT=32;
const double RATIO=9.0/5.0; int tempOut;
char typeOut; switch(typeIn)
{
case'C':
case'c':
tempOut=tempInInt*RATIO+ADD_SUBTRACT;
typeOut='F';
typeIn='C';
break;
case'F':
case'f':
tempOut=(tempInInt-ADD_SUBTRACT)/RATIO;
typeOut='C';
typeIn='F';
break;
default:
typeOut='E';
break;
}
if(typeOut!='E')
{
std::cout<<tempInInt<<typeIn
<<"="<<tempOut<<typeOut<<"\n";
}
else
{
std::cout<<"输入错误\n";
}
}
cin cout的更多相关文章
- acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
题目 输入正好是long long的最大, 但是答案超long long 所以用unsigned, 不能用cin cout否则一定超时: 不能用abs(), abs 只用于整数. unsigned ...
- printf scanf cin cout的区别与特征
printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...
- C++输入输出流 cin/cout 及格式化输出简介
C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...
- 892B. Wrath#愤怒的连环杀人事件(cin/cout的加速)
题目出处:http://codeforces.com/problemset/problem/892/B 题目大意:一队人同时举刀捅死前面一些人后还活着几个 #include<iostream&g ...
- cin,cout,printf,scanf效率对比
From:http://www.cnblogs.com/killerlegend/p/3918452.html Author:KillerLegend Date:2014.8.17 杭电OJ之3233 ...
- scanf printf gets() puts(),cin cout
最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...
- cin/cout与scanf/printf的比较
转自http://www.cnblogs.com/penelope/articles/2426577.html cin .cout 基本说明: cin是标准输入流对象,代表标准输入设备(键盘), ...
- cin cout getline string
1.C++ code, When we want to read a number whatever the type is int or double , just use cin >> ...
- C++输入输出常用格式(cin,cout,stringstream)
输入格式 1.cin>>a; 最基本的格式,适用于各种类型.会过滤掉不可见字符例如空格,TAB,回车等 2.cin>>noskipws>>ch[i]; 使用了 no ...
随机推荐
- HDU 4862 JUMP 最小费用最大流
2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...
- HDOJ 1722--Cake(切蛋糕问题)
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. Input 每行有两个数p和q ...
- 第二十一篇 关联管理器(RelatedManager)
关联管理器(RelatedManager) lass RelatedManager "关联管理器"是在一对多或者多对多的关联上下文中使用的管理器.它存在于下面两种情况: Forei ...
- POJ 3295:Tautology
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10482 Accepted: 3982 Descri ...
- file:///D:/Program%20Files/Microsoft%20Visual%20Studio%2011.0/VC/VCWizards/CodeWiz/MFC/Variable/HTML
title VS2005 VS2008添加变量,添加函数,添加类时弹出 Script Error 解决办法 问现象描述 : 问题大家都清楚了.不赘述 错误提示 :file:///C:/Progra ...
- MySQL8.0安装caching_sha2_password问题
MySQL安装之后无法用工具连接上本地数据库 详情原因可见: https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-pl ...
- java获取键盘事件
转 <script type="text/javascript" language=JavaScript charset="UTF-8"> docu ...
- SqlServer 集合运算符
1.集合运算符概述 (1)集合运算符运用与集合之间的运算. (2)多元集合: 指的是来自两个输入查询的集合,可能包含重复项 (3)T-SQL 支持三种集合运算符 union .intersect .e ...
- 51Nod1049 最大子段和
我们来先看题: N个整数组成的序列a1,a2,a3,-,an,求该序列如ai+ai+1+-+aj的连续子段和的最大值.当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,-2,和最大的 ...
- 吴裕雄--天生自然 PHP开发学习:echo 和 print 语句
<?php echo "<h2>PHP 很有趣!</h2>"; echo "Hello world!<br>"; ec ...