如何输出字符串各部分之间的空格

How to output space between parts of a string

本文关键字:各部 之间 空格 字符串 何输出 输出      更新时间:2023-10-16

这是我的代码。我想把十进制数转换成十六进制和二进制。我想我做到了,但对于二进制部分,我希望它每4位数之间有空格(如1111 1111 1111 1111 1111)。我想知道如何改变它("answers2"是字符串)


#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
    //output inital prompt
    cout << "ConvInteger- Conversion to Hexadecimal and Binary               Representationn";
    //read in input 
    int decimal;
    cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
    cin >> decimal;
    if (decimal == -1) {
        cout << "Program Terminated by usern";
        return 0;
    }
    cout << "Decimal:         " << decimal << endl;
    // calculates the decimal to Hexdecimal
    string ans = "";
    string hexdecimal = "0123456789ABCDEF";
    int i;
    for (i = 4; i > 0; i--) {
        ans = hexdecimal[decimal % 16] + ans;
        decimal = decimal / 16;
    }
    cout << "hexdecimal:      " << ans << endl;
    //calculates the decimal toBinary
    int ans1, i1;
    for (i1 = 16; i1 > 0; i1--) {
        string ans1 = "";
        string binary = "01";
        ans1 = binary[decimal % 2] + ans1;
        decimal = decimal / 2;
    }
    cout << "binary:      " << ans1 << endl;
    return 0;
    system("PAUSE");
}

代码中的几个逻辑错误:

  • int ans1string ans1名称相同。字符串ans1具有局部作用域,仅在for循环中可见。在外面打印ans1,你不会得到想要的结果。
  • 循环直到decimal数变为零。
  • 从十进制到十六进制的转换已经完成了decimal equal to 0

空间- after every 4 bit add space (' ') to ans1的解

下面是正确的代码:

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
    //output inital prompt
    cout << "ConvInteger- Conversion to Hexadecimal and Binary               Representationn";
    //read in input 
    int decimal ,temp;
    cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
    cin >> decimal;
    if (decimal == -1) {
        cout << "Program Terminated by usern";
        return 0;
    }
    cout << "Decimal:         " << decimal << endl;
    temp = decimal;
    // calculates the decimal to Hexdecimal
    string ans = "";
    string hexdecimal = "0123456789ABCDEF";
    int i;
    for (i = 4; i > 0; i--) {
        ans = hexdecimal[decimal % 16] + ans;
        decimal = decimal / 16;
    }
    cout << "hexdecimal:      " << ans << endl;
    //calculates the decimal toBinary
   int /*ans1*,/i1 ,space = 1;
   string ans1 = "";
   string binary = "01";
   decimal = temp;
   cout<<decimal<<endl;
    for (i1 = 16; decimal > 0; i1--) {
        //string ans1 = "";
        //string binary = "01";
        ans1 += binary[decimal % 2];
        ans1=space == 4? ans1+' ' : ans1;
        space == 4 ? space = 1 : space++;
        decimal = decimal / 2;
    }
    cout << "binary:      " << ans1 << endl;
    return 0;

}

    //(this part go into input)        
     int decimal, decimal1;
     decimal1 =decimal;
  //calculates the decimal to Binary
    int i1;
    for (i1=1;i1<=16;i1++){
    string ans1 ="";
    string binary="01";
    ans1 = binary[decima1l%2]+ans1;
        if (i1%4==0) ans1=binary[decimal1%2]+ans1+" ";
    decimal1=decimal1/2;
   }
     cout<<"binary:      "<<ans1<<endl;  

这个能行吗?如果不是,为什么?

你可以这样做:

// this function add a space every 4 digits
std::string add_spaces(const std::string& str_nbr)
{
  std::string str;
  // we walk through the string in the reverse order
  // note that there is no incrementation in this loop, it's normal
  for(std::string::const_reverse_iterator crit = str_nbr.rbegin(); crit != str_nbr.rend();)
  {
    // Then we walk through 4 digits or less if we arrive at the front
    for(unsigned short int k = 0; k < 4 && crit != str_nbr.rbegin(); ++k, ++crit)
      str = *crit + str;
    if(crit != str_nbr.rend())
      str = std::string(1,' ') + str;
  }
  return str;
}

关键字提高你的技能,很好地理解这个例子:字符串,迭代器,反向迭代器

另一种方法:

int digits = 0;
for (i1 = 16; i1 > 0; i1--) {
ans1 += binary[decimal % 2];
digits++;
if(digits == 4){
ans1 += " ";
digits = 0;
}
 decimal = decimal / 2;
}

顺便说一下,十六进制数计算后,您的十进制数输入将丢失

您总是可以通过编写自己的函数来使用老式的方法。我已经纠正了你的代码的逻辑错误,并添加了函数addchar添加一个字符到字符串和反转反转数组。我知道这不是有效的,但它是简单的。

#include<iostream.h>
#include<string.h>
void addchar(char an[20],char c)
{
 int l=strlen(an);
 an[l]=c;
 an[l+1]='';
 }
void reverse(char an[20])
 {
 int i,temp;
 int l=strlen(an);
 for(i=0;i<l/2;i++)
  {
   temp=an[i];
   an[i]=an[l-i-1];
   an[l-i-1]=temp;
  }
 }
void todecimal(int decimal)
 {
 i1,decimal=2  ;
 char ans1[20]="";
 char binary[3] ="01";
 for (i1 = 1; i1 <=17; i1++) {

    addchar(ans1,binary[decimal%2]);
    decimal = decimal / 2;
    if(i1%4==0)
      {
       addchar(ans1,' ');
       }
    }
   reverse(ans1);
   cout << "binary:      " << ans1 << endl;
  }
string str_;
while(dec_ > 0)
{
  str_.append(1,((dec_%2) == 0)?'0':'1');
  dec_ /= 2;
}
std::revervse(str_.begin(),str_.end());