1057. 数零壹(20) cin,get,gets,getline输入总结

来源:互联网 发布:淘宝c店开店流程 编辑:程序博客网 时间:2024/06/06 18:09

#include<vector>#include<iostream>using namespace std;int check(char x){if(x>='A' && x<='Z'){return x-'A'+1;}else if(x>='a' && x<='z'){return x-'a'+1;}else{    return -1;}}int main(){vector <int> two;char str[100000]={0};int i=0,y=0,sum=0,count0=0,count1=0;//cin>>str;//cin无法接受空格、Tab键gets(str);  //或者string str;getline(cin, str);for(i=0;str[i]!='\0';i++){y=check(str[i]);if(y>=1 && y<=26) sum+=y;}while(sum){two.push_back(sum%2);sum/=2;}for(i=0;i<two.size();i++){if(two[i]==0){count0++;}else if(two[i]==1){count1++;}else{cout<<"二进制数有误"<<endl;return 0;}}    cout<<count0<<" "<<count1<<endl;return 0;}

①cin>>:无法接收空格、Tab键且以空格、Tab键、回车符为分隔符;

 

②cin.get( ):可以接收空格、Tab键且以回车符为结束符;

一:可输入单个字符

格式:

char ch;

ch=cin.get( );/cin.get(ch);

二:可输入字符串

格式:

cin.get(字符数组名,元素个数)

 

③getline( ):可接收空格、Tab键且以回车符为结束符;

格式:

string str;//字符串变量

getline(cin,str);

 

④cin.getline( ):可接收空格、Tab键且以回车符结束;

格式:cin.getline(字符数组名,元素个数)

char m[20];

cin.getline(m,20);

其中,③和④类似,单数getline( )属于string流类,而cin.getline( )属于istream流类,是不一样的函数。

 

⑤gets(字符数组名)用于接收字符串,可包括空格、Tap键且以回车符结束;

 

⑥getchar(字符变量名)用于接收单个字符且以回车符结束,一般需要两个。前一个接收字符,后一个接收回车符。

 

⑦putchar(字符变量名或整常数)

 

===============总结================================

1、gets(字符数组名),getline(cin,字符串)比较常用

2、有3个接收字符数组,gets, cin.get, cin.getline

   只有1个接收字符串,getline

===================================================



阅读全文
1 0
原创粉丝点击