zoukankan      html  css  js  c++  java
  • 第二十一章流 3用cin输入 简单

    //第二十一章流 3用cin输入
    // 1 字符串的输入
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	int x;
    	cin>>hex>>x;
    	cout<<x;
    	system("pause");
        return 0;
    }*/
    
    // 2 字符串的输入问题
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char word[12];
    	cin>>word;
    	cout<<word<<endl;
        return 0;
    }*/
    
    // 3 get()函数
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch;
    	//ch = cin.get();
    	//cout<<"ch:"<<ch<<endl;
        //循环获取字符串
    	while( (ch=cin.get()) !='\n')
    	{
    	   cout<<ch;
    	}
    	cout<<"程序结束"<<endl;	
    	return 0;
    }*/
    
    //输出Enter键和'\n'的ASCII码值
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	int c;
    	c = int('\n');
    	cout<<c<<endl;
    	c = cin.get();
    	cout<<c<<endl;
    
        return 0;
    }*/
    //回车符'\r'和换行符'\n'是不同的,它的ASCII码值是13,回车符的作用是回到该行的开头,但是并不住下移动一行
    
    //回车符'\r'覆盖开头的字符串
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	//cout<<"hello\r"<<"give me";
        cout<<"hello\r"; //由于回车符而不是换行符,因此暂时存放在缓冲区中的字符有可能不会立即显示出来
    	return 0;
    }*/
    
    //需要输入时新缓冲区
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	//char ch[10];
    	//cout<<"hello\r";
    	//cin>>ch;
        
    	//char ch;
    	//while((ch=cin.get()) !='s')
    	//{
    	   //cout<<ch;
    	//}
    	//cout<<"程序结束"<<endl;
    	
    	char ch;
    	cin>>ch;
    	while(ch!='\n')
    	{
    	   cout<<ch;
    	   cin>>ch;
    	}
    	cout<<"程序结束"<<endl;
    	return 0;
    }*/
    
    // 4 带字符引用参数的get()函数
    /*#include <iostream>
    using namespace std;
    int main()
    {
       char a, b, c;
       cin.get(a).get(b).get(c);
       //由于cin.get(a)返回一个cin对像,因此可以省略掉输入cin对像的过程,这样直接在后面加上成员运算符(.)即可
       cout<<"a:"<<a<<endl;
       cout<<"b:"<<b<<endl;
       cout<<"c:"<<c<<endl;
       return 0;
    }*/
    //输入缓冲和输出缓冲是两码事,在执行输入时会首先新输入缓冲中的数据并将数据写入程度中者设备,而输出则是遇到endl或者flush方才新输出缓冲区中的数据,哪里个缓冲区满就自动清空哪里个缓冲区的数据,在程序结束时会按照次序清空缓冲多中的数据,不要混为一谈
    
    //循环到文件结束
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch;
    	while(cin.get(ch)){
    	   cout<<ch;
    	}
    	cout<<"程序结束"<<endl;
        return 0;
    }*/
    
    
    // 5 带两个参数的get()函数
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch[20];
    	cin.get(ch,20);
    	cout<<ch;
    	cout<<"程序结束"<<endl;
    
        return 0;
    }*/
    //带两个参数get()函数的缺点
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch1[20];
    	char ch2[20];
    	cout<<"请输入第一串字符:";
    	cin.get(ch1,20);
    	cout<<"字符串1为:"<<ch1<<endl;
    	cout<<"请输入第二串字符:"<<endl;
    	cin.ignore(); //解决方法用cin.ignore()来丢弃缓冲区中的换行符'\n'
    	cin.get(ch2,20);
    	cout<<"字符串口2为"<<ch2<<endl;
    	cout<<"程序结束"<<endl;
        return 0;
    }*/
    
    // 6 带3个参数的get()函数
    //istream &get(char *buffer, streamsize num, char delim)
    //读取字符到buffer直到已读入num-1个字符,或者碰到EOF或delim(delim直到下一次不会被读取)
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch1[30];
    	char ch2[30];
    	cout<<"请输入第一串字符:";
    	cin.get(ch1,30,'s');
    	cout<<"字符串1为:"<<ch1<<endl;
    	cout<<"请输入第二串字符:";
    	//cin.ignore(); 解决方法调用带两个参数ginore()
    	
    	//cin.ignore(1024,'\n');
    
    
    	//第二种清空缓冲区的方法
    	cin.ignore(numeric_limits<streamsize>::max(),'\n');
    	//numeric_limits<streamsize>::max()返回缓冲区的大小
    	//ignore函数在此将把输入缓冲区中的数据清空
    
    	
    	cin.get(ch2,30);
    	cout<<"字符串口2为:"<<ch2<<endl;
    	cout<<"程序结束"<<endl;
    	
        return 0;
    }*/
    
    // 7 getline()函数
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch1[30];
    	char ch2[30];
    	cout<<"请输入第一串字符:";
    	cin.getline(ch1,30);
    	cout<<"字符串1为:"<<ch1<<endl;
    	cout<<"请输入第二串字符:";
    	cin.getline(ch2,30);
    	cout<<"字符串2为:"<<ch2<<endl;
    	cout<<"程序结束"<<endl;
        return 0;
    }*/
    //清空缓冲区
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch1[30];
    	char ch2[30];
    	cout<<"请输入第一串字符:";
    	cin.getline(ch1,30,'\s');
    	cout<<"字符串1为:"<<ch1<<endl;
    	cout<<"请输入第二串字符:";
    	cin.ignore(1024,'\n');
    	cin.getline(ch2,30);
    	cout<<"字符串2为:"<<ch2<<endl;
    	cout<<"程序结束"<<endl;
        return 0;
    }*/
    
    // 8 read()函数 读取指定数目的字符,并将它们存储在指定的位置中
    // 输出乱字符"烫"程序代码如下
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	//char ch[30]; 
    	//解决乱码问题, 是因为read读取的数据没有字符串结束符
    	char ch[30]={0};
    	cout<<"请输入字符:";
    	cin.read(ch,30);
    	cout<<ch<<endl;
    	cout<<"程序结束"<<endl;
    }*/
    //超出字符进入缓冲区
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	//char ch[30]; 
    	//解决乱码问题, 是因为read读取的数据没有字符串结束符
    	char ch1[30]={0};
    	char ch2[30];
    	cout<<"请输入第一串字符:";
    	cin.read(ch1,5);
    	cout<<"字符串1为:"<<ch1<<endl;
    	cout<<"请输入第二串字符:";
    	cin.ignore(1024,'\n');
    	cin.getline(ch2,30);
    	cout<<"第二串字符为:"<<ch2<<endl;
    	cout<<"程序结束"<<endl;
    }*/
    
    
    // 9 gcount()函数 用于输入流,并返回上一次输入操作被读入的字符数目
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch[30];
    	//cin>>ch;
    	cin.getline(ch,30);
    	int i=cin.gcount();
    	cout<<"输入缓冲区中共有"<<i<<"个字符"<<endl;
        return 0;
    }*/
    
    //10 peek()函数 用于输入流中,并返回在流中的下一个字符,如果处于文件的尾处,则返回EOF
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char Peek;
    	char ch[30];
    	int i=0;
    	while((Peek=cin.peek()) !='c' && Peek!='\n')
    	{
    		cin.get(ch[i++]);
    	}
    	ch[i] = '\0';
    	cout<<ch;
        return 0;
    }*/
    
    
    // 11 putback()函数 它将一个字符插入到输入流中的字符串中,
    /*#include <iostream>
    using namespace std;
    int main()
    {
    	char ch;
    	while(cin.get(ch))
    	{
    		if(ch=='#'){
    			cin.putback('$');
    		}else{
    		    cout<<ch;
    		}
    	}
        return 0;
    }*/
    

      

  • 相关阅读:
    在ensp上配置Trunk接口
    在ensp上VLAN基础配置以及Access接口
    在ensp上的ARP及Proxy ARP
    在ensp上简单的配置交换机
    1000000 / 60S 的 RocketMQ 不停机,扩容,平滑升级!
    DE1-SOC 只要加载驱动VNC就断开(DE1-SOC 只要加载驱动串口就卡住)
    通过U盘拷贝文件到DE1-SOC 的 Linux系统
    Linux 系统响应来自 FPGA 端的中断的中断号到底怎么对应?(GIC控制器)
    HPS 访问 FPGA 方法之五—— 通过FPGA 中断访问
    HPS 访问 FPGA 方法之四—— 编写 Linux 字符设备驱动
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2709694.html
Copyright © 2011-2022 走看看