如何将小数转换为二进制的补码字符串
How to convert decimals to two's complement string
我真的是C 的新手,我对程序有些困惑。该程序应该取2个整数,并使用L-Size Bit将它们转换为两个的补充字符串,但是当我运行程序时,我一直在将字符串订阅的范围错误置于范围错误中。我可能会有很多错误,如果你们可以指出一些大错误,那就太好了。我的程序:
string reverse(string s)
{
string x="";
for(int i=s.length()-1;i>=0;i--)
{
x += s[i];
}
return x;
}
string twosComplementStringAddition(string a,string b)
{
string c;
int count;
for(int i=0;i<a.length();i++)
{
if(count>0)
c[i]='1';
else if(a[i]=='1' & b[i]=='1')
{
c[i]='0';
count++;
}
else if((a[i]=='1' & b[i]=='0')|| (a[i]=='0' & b[i]=='1'))
c[i]='1';
else if(a[i]=='0' & b[i]=='0')
c[i]='0';
count=0;
}
return c;
}
string negative(string a)
{
for(int i=0;i<a.length();i++)
{
if(a[i]=='1')
a[i]='0';
else
a[i]='1';
}
reverse(a);
string x="1";
twosComplementStringAddition(a,x);
reverse(a);
return a;
}
string decimalToTwosComplementString(int a, int L)
{
string s="";
L= s.length();
int b;
for(int i=0;i<L-1;i++)
{
b=a%2;
if(b==1)
s[i]='1';
else if(b==0)
s[i]='0';
a=a/2;
}
reverse(s);
if(a<0)
{
negative(s);
return s;
}
else
return s;
}
int twosComplementStringToDecimal(string a)
{
int result=0;
if(a[0]=='0')
{
reverse(a);
for(int i=0;i<a.length();i++)
{
if(a[i]=='1')
{
result= result + pow(2,static_cast<float>(i));
}
else if(a[i]=='0')
{
result = result + 0;
}
}
}
else
{
negative(a);
int resulta=0;
reverse(a);
for(int i=0;i<a.length();i++)
{
if(a[i]=='1')
{
resulta= resulta + pow(2,static_cast<float>(i));
}
else if(a[i]=='0')
{
resulta = resulta + 0;
}
}
return resulta;
}
return result;
}
int main()
{
//Read in the bit pattern size
int L;
do
{
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
}while (L <= 0);
//Read in two integers a and b
int a, b;
cout << "Enter an integer a ";
cin >> a;
cout << "Enter an integer b ";
cin >> b;
//Calculate the decimal arithmetic sum of a and b and print the result
int c1 = a + b;
cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
//Compute the two's complement representations of a and b
//Each integer must be represented in L-bits pattern
//Also these two's complement representations must be returned as string data types
string A = decimalToTwosComplementString(a, L);
string B = decimalToTwosComplementString(b, L);
//Print the two's complement representations of a and b
cout << "The two's complement of " << a << " ist " << A << endl;
cout << "The two's complement of " << b << " ist " << B << endl;
//Compute the binary sum of the two's complement representations of a and b
//The result must be returned as L-bit pattern string data type
string C = twosComplementStringAddition(A, B);
//Print the two's complement representation binary sum
cout << "The binary sum of " << A << " and " << B << " is " << C << endl;
//Convert the two's complement representation binary sum to decimal and print
int c2 = twosComplementStringToDecimal(C);
cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;
system("Pause");
return 0;
}
对于任何愿意提供帮助的人,谢谢您的宝贵时间!编辑:几个小时后,我终于使它起作用了,谢谢大家帮助初学者!
您是从一个空字符串开始的,并尝试访问不存在的字符串中的项目:
string c;
//...
for(int i=0;i<a.length();i++)
{
//...
c[i]='0'; // <-- The string is empty, but you're trying to access element i.
//...
}
要修复错误,将c
字符串大小调整到适当的大小,以使这些条目存在。
string c;
c.resize(a.length());
//..
。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 有根的二进制搜索树.保留与其父级的链接
- 多态二进制函数
- 正在读取二进制文件(is_open)
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 反转 18 位 int 的二进制补码
- 在计算负数的二进制补码时如何实现"+1"加法
- 如何将小数转换为二进制的补码字符串
- 十进制到二进制,有两个补码
- 如何强制编译器或解释器查看二进制数二的补码?
- 有符号双字到无二进制补码的无符号双字
- 二进制补码形式所需的最小位数
- C++中的二进制补码 - 数组下标的类型"int[int]"无效
- C++ 中二进制二进制补码整数的读取值
- 获取在输入文件中存储为字符串的二进制数的 2'补码
- 为什么我在计算二进制数的补码时会出现分割错误?