这是我的代码。我想将十进制数更改为十六进制和二进制。我想我做到了但是对于二进制部分我希望它在每4个数字之间有空格(如1111 1111 1111 1111)。我想知道如何更改它(“ans2”是字符串)
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//output inital prompt
cout << "ConvInteger- Conversion to Hexadecimal and Binary Representation\n";
//read in input
int decimal;
cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
cin >> decimal;
if (decimal == -1) {
cout << "Program Terminated by user\n";
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");
}
答案 0 :(得分:0)
你可以像这个例子那样做:
// 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;
}
提高技能的关键字,以便很好地理解这个例子: string,iterator,reverse iterator
答案 1 :(得分:0)
代码中几乎没有逻辑错误:
int ans1
和string ans1
具有相同的名称。 string 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 Representation\n";
//read in input
int decimal ,temp;
cout << "Enter a Positive Decimal Integer(<65535,-1 quits): ";
cin >> decimal;
if (decimal == -1) {
cout << "Program Terminated by user\n";
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;
这个有用吗?如果不是为什么?
答案 2 :(得分:0)
另一种方法:
int digits = 0;
for (i1 = 16; i1 > 0; i1--) {
ans1 += binary[decimal % 2];
digits++;
if(digits == 4){
ans1 += " ";
digits = 0;
}
decimal = decimal / 2;
}
顺便提一下,在十六进制数计算后,十进制数输入丢失了
答案 3 :(得分:0)
你总是可以通过编写自己的函数以老式的方式处理它。我已经纠正了你的代码的逻辑错误,并添加了函数addchar来为字符串添加一个字符并反向反转数组。我知道它效率不高但很简单。
#include<iostream.h>
#include<string.h>
void addchar(char an[20],char c)
{
int l=strlen(an);
an[l]=c;
an[l+1]='\0';
}
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;
}
答案 4 :(得分:0)
string str_;
while(dec_ > 0)
{
str_.append(1,((dec_%2) == 0)?'0':'1');
dec_ /= 2;
}
std::revervse(str_.begin(),str_.end());