如何输出字符串各部分之间的空格
How to output space between parts of a string
这是我的代码。我想把十进制数转换成十六进制和二进制。我想我做到了,但对于二进制部分,我希望它每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 ans1
和string 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());
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数