C++输入输出格式问题

来源:互联网 发布:亚马逊德国站情况知乎 编辑:程序博客网 时间:2024/06/05 23:02


一)

为什么c++中输入一个整数后,再输入一个带空格的字符串会输入不了字符串?

因为cin通过空格、换行符、制表符来确定结束位置,所以只有1被附给data,如果你要将“1 2 3 4 55”作为一个字符串输出,你可以这样写:


c++怎么求一个带空格字符串长度

getline(cin,s);
s.length();


      int i = 0;
 string data;
cin >> i;
 cin.get();  //这应该懂吧,读取换行符,因为换行符在你按回车时还在输入队列中
 getline(cin, data);  //正因为cin.get()将换行符读取了,这行代码可以读取一行,否则将直接读取换行符,这样子这一行的内容都存进data字符变量中
 cout << i << endl << data;
 system("pause");
 return 0;

实例,输入一行字符串,输出重复部分,并且要求第一个按顺序保留!!

 int main()
 {
     string str;
     getline(cin, str);
     int num = str.size();
     int hash[260] = { 0 };
     for (int i = 0; i < num; i++)
     {
         hash[str[i]]++;
     }

     for (int i = num - 1; i >= 0; i--)
     {
         if (hash[str[i]]>1)
         {
             hash[str[i]]--;
             str.erase(i, 1);
             // str.erase(i);

         }

     }
     cout << str << endl;    
     system("pause");
     return 0;

 }



、---------------------------------------------------

string str = "112233";
     str.erase(0, 4);
     cout << str <<endl;

删掉了从第0个开始的4个字符,剩下输出为33.


----------------------------------------------------------------

int main()
{
    string str;
    set<string> deletstr;
    while (cin >> str)
    {
        deletstr.insert(str);
    }
    string str1;
    map<string, int> strlist;
    cin.clear();
    while (cin >> str1)
    {
        if (!deletstr.count(str1))
        {
            strlist[str1]++;
        }
    }
    system("pause");
    return 0;

}

两个while 之间加cin.clear()  否则第二个while不执行!


三)

输入一串字符串,有数字和字母组成,将分割的数字找出来,并找出在最大的数字字符串!

 int main()
 {

    
     char s[100];
     char ns[100][100] = {0};

     //cin >> s;
     cin.getline(s, 100);

     int n = strlen(s);
     int j = 0;
     int i = 0;
     int index = 0;
     int a = 0, b = 0, c = 0;
     for (j = 0, i = 0,index=0; i <= n; i++)
     {
        

         if (s[i]>='0'&&s[i] <= '9')
         {
            
             if (a == 0&&b==1&&c==1)
             {
                 index++;
             }
             ns[index][j] = s[i];
             j++;
             a = 1; b = 0,c=1;
         }
         else if (s[i]<'0' || s[i] > '9'||s[i]=='\0')
         {
             if (a == 1 && b == 0)
             {
                 ns[index][j] ='\0';
             }
             j=0;
             a = 0; b = 1;
         }

     }
     cout << s<< endl;
    
     for (int i = 0; i <= index; i++)
     {
         for (int j = 0; j < strlen(ns[i]); j++)
         {
             cout << ns[i][j];

         }
         cout << endl;
     }
    
     char *max = ns[0];
     for (int i = 1; i <= index; i++)
     {
         if (strlen(ns[i]) > strlen(max))
         {
             max = ns[i];
         }
         else if (strlen(ns[i]) == strlen(max))
         {
             if (strcmp(ns[i], max))
                 max = ns[i];
         }
     }
     cout << max << endl;
     system("pause");
     return 0;
 }


四) 输入一行数字,保存到数组中!

参考了这篇博客,http://blog.csdn.net/qsyzb/article/details/21649805

这篇博客的第一种方法没有考虑输入的数字串中有负数的情况,稍加改动

方法一:

[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int a[50];  
  6.     int i = 0;  
  7.     char c;  
  8.     while((c=getchar())!='\n')  
  9.     {  
  10.         if(c!=' ')//把这句判断条件改动  
  11.         {  
  12.             ungetc(c,stdin);  
  13.             cin>>a[i++];  
  14.         }  
  15.     }  
  16.     for(int j=0;j<i;j++)  
  17.     {  
  18.         cout<<"a["<<j<<"]:"<<a[j]<<endl;  
  19.     }  
  20. }  
或者用下面的方式也可以达到这种要求

方法二:

[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int a[20];  
  7.     int i = 0;  
  8.     char c;  
  9.     cin>>a[i++];  
  10.     while((c=getchar())!='\n')  
  11.     {  
  12.         cin>>a[i++];  
  13.     }  
  14.     for(int j=0;j<i;j++)  
  15.     {  
  16.         cout<<"a["<<j<<"]:"<<a[j]<<endl;  
  17.     }  
  18. }  

输入 (输入中包含负数)

3 4 -6 9 -2 -4 8 5

输出


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

输入 (输入中不包含负数)

3 4 610 900 2 404 8 5

输出


五) 输入一行数字,不管是空格还是换行,以输入-2为结束标志, 将其保存到数组中!

主要考虑是大数的情况和结尾有0的情况! 

方法一,利用string和stringstream   (#include<sstream>)实现string和int转换

  string arr[100];
    int arr1[100] = {0};
    string str;
    int k = 0;
    while (cin >> str&&str != "-2")
    {
        arr[k] = str;
        stringstream stream;
        stream << arr[k];
        stream >> arr1[k];
        k++;

    }

方法二  更加通用,直接利用int数组

int ntmp;
    int ntmp1[100];
    int ind = 0;
    while (cin >> ntmp&&ntmp != -2)
    {
        ntmp1[ind++] = ntmp;
    }


六) 将一个整数翻转输出 ,反转一个整数,比如:x=1234  return 4321x=-4567 return -7654

我的做法:

int main()
{


    char numstr[100];
    stringstream stream;
    int num = -123;
    
    stream << num;
    stream >> numstr;

    int len = strlen(numstr);
    int i = 0, j = len-1;
    if (num < 0)
        i = 1;

    while (i < j)
    {
        char c = numstr[i];
        numstr[i] = numstr[j];
        numstr[j] = c;
        i++;
        j--;
    }
    stream.str(" ");
    stream.clear();
    
    
    int second;
    stream << numstr; //插入bool值
    stream >> second; //提取出int
    std::cout << second << std::endl;


方法2

int main()
{
    
    int num1 = -1230000;
    if (num1 != 0)
    {
        int num = num1;
        if (num1<0)
            num = -1 * num1;
        string str = "";
        while (num != 0)
        {
            str = str + char(num % 10 + '0');
            num /= 10;
        }
        if (num1 < 0)
        {
            str = "-" + str;
        }
        cout << str << endl;
        stringstream stream;
        int second;
        stream << str; //
        stream >> second; //
        std::cout << second << std::endl;
    }
    else
        cout << 0 << endl;