编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置。当用户按下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的更多相关文章

  1. acdream B - 郭式树 (水题 卡cin,cout, 卡LL)

    题目 输入正好是long long的最大, 但是答案超long long 所以用unsigned, 不能用cin cout否则一定超时: 不能用abs(), abs 只用于整数. unsigned   ...

  2. printf scanf cin cout的区别与特征

    printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...

  3. C++输入输出流 cin/cout 及格式化输出简介

    C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...

  4. 892B. Wrath#愤怒的连环杀人事件(cin/cout的加速)

    题目出处:http://codeforces.com/problemset/problem/892/B 题目大意:一队人同时举刀捅死前面一些人后还活着几个 #include<iostream&g ...

  5. cin,cout,printf,scanf效率对比

    From:http://www.cnblogs.com/killerlegend/p/3918452.html Author:KillerLegend Date:2014.8.17 杭电OJ之3233 ...

  6. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  7. cin/cout与scanf/printf的比较

    转自http://www.cnblogs.com/penelope/articles/2426577.html  cin .cout   基本说明: cin是标准输入流对象,代表标准输入设备(键盘), ...

  8. cin cout getline string

    1.C++ code, When we want to read a number whatever the type is int or double , just use cin >> ...

  9. C++输入输出常用格式(cin,cout,stringstream)

    输入格式 1.cin>>a; 最基本的格式,适用于各种类型.会过滤掉不可见字符例如空格,TAB,回车等 2.cin>>noskipws>>ch[i]; 使用了 no ...

随机推荐

  1. HDU_4960 2014多校9 Another OCD Patient DP

    其实现在想起来是个巨简单的DP,模型就跟LCS很像,比赛的时候居然没想出来,在聪哥提醒下还卡了个地方 就是说给定一串n个数字的序列,可以连续合并,最终使得序列是回文的,题目也给定了合并数字所需的代价, ...

  2. java基础源码 (3)--Annotation(注解)

    借鉴博客地址:https://www.cnblogs.com/skywang12345/p/3344137.html /** * The common interface extended by al ...

  3. hdu 1087 最长上升序列和 dp

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. 老出BUG怎么办?游戏服务器常见问题解决方法分享

    在游戏开发中,我们经常会遇到一些技术难题,而其引发的bug则会影响整个游戏的品质.女性向手游<食物语>就曾遇到过一些开发上的难题,腾讯游戏学院专家团Wade.Zc.Jovi等专家为其提供了 ...

  5. POJ 2771 最大点独立集

    这是经典的最大点独立集 还是可以转化成最大匹配数,为什么呢,因为求出最大匹配数之和,匹配的边的两个端点互斥,只能去一个,所以最后结果就用总点数-最大匹配数即可 #include <iostrea ...

  6. stm32f4 dma + uart idle + double 调试小记

    使用 stm32f4 调试uart 接收, 使用 空闲中断,dma 双缓冲模式,有以下几点需要注意的. 调试的时候断点不要打在 if (USART_GetITStatus(USART6, USART_ ...

  7. (5)opencv的基础操作和矩阵的掩模操作

    不懂的,可以简单,看看这个网址:https://blog.csdn.net/xiongwen_li/article/details/78503491 图片放到了桌面,所以,图片的路径就是桌面了,剩余的 ...

  8. [SDOI2016]游戏(树剖+李超树)

    趁着我把李超树忘个一干二净的时候来复习一下吧,毕竟马上NOI了. 题解:看着那个dis就很不爽,直接把它转换成深度问题,然后一条直线x->y,假设其lca为z,可以拆分成x->z和z-&g ...

  9. CSS3新特性—animate动画

    1.animate介绍 1. @keyframes 自定义动画名称 { from { } to { } } 2. 通过动画名称调用动画集 animation-name: 动画集名称. 3. 属性介绍: ...

  10. Unity3D一些基本的概念和一些基本操作

    场景:整个游戏由场景组成,一个游戏至少要有一个场景,如果把所有的游戏画面放在一个场景里也是可以的,如果游戏非常非常的大,如果所有的东西都放到一个场景里那么结构就不是那么清晰了而且处理起来就会麻烦一些, ...