int main(){
int i,j,k
int a[16], af[16], ab[16]//af存储反码,ab存储补码第0位为符号位
char b[16]
scanf("%s", b)
for(i = 0b[i] != '\0'兄团睁i++){
a[i] = b[i] - '0'
}
j = i-1
ab[0] = af[0] = a[0]//符号位始终不变
if(a[0] == 1){//当从屏幕输入的是负数,则作相应变换
for(i = ji >0i--){
af[i] = 1 - a[i]
ab[i] = a[i]
if(a[i] == 1){
i--
break
}
}
k = i
for(i >0i--)
af[i] = 1 - a[i]
for(i = ki >0i--)//补码从末位数起第一个不为0,以后均取反
ab[i] = 1 - a[i]
}
else{//如果是正数,则全部复制
for(i = ji >0i--){//正数反码、补码都与原码相同
af[i] = a[i]
ab[i] = a[i]
}
}
printf("对应的反码羡岁是:\n")
for(i = 0i <= ji++)
printf("%d", af[i])
printf("\n")
printf("对应的补码是:\n")
for(i = 0i <= ji++)
printf("%d", ab[i])
}
在vc下编译测试通过。另,本代码未考虑输入非或和法问题,所输入的数字智能由1和0组成
Private Sub Command1_Click()
Dim a(16) As Integer, x As Integer
x = CInt(Text1.Text)
If x >= 0 Then a(16) = 0 Else a(16) = 1
x = Abs(x)
For i = 1 To 15
a(i) = x Mod 2
x = x \ 2
Next i
Text2.Text = ""
For i = 16 To 1 Step -1
Text2.Text = Text2.Text &a(i)
Next i
If a(16) = 0 Then
Text3.Text = Text2.Text
Text4.Text = Text2.Text
Exit Sub
End If
For i = 1 To 15
a(i) = 1 - a(i)
Next i
Text3.Text = a(16)
For i = 15 To 1 Step -1
Text3.Text = Text3.Text &a(i)
Next i
Text4.Text = a(16)
a(1) = a(1) + 1
i = 1
While a(i) = 2 And i <15
a(i) = 0
a(i + 1) = a(i + 1) + 1
i = i + 1
Wend
For i = 15 To 1 Step -1
Text4.Text = Text4.Text &a(i)
Next i
End Sub
#include<iostream.h>main()
{
char cc[8]
int a[8],b[8],i,j,c=0
for(i=0i<8i++)
{
cin>> cc[i]
a[i]=b[i]=cc[i]-48
}
if(a[0]==1)
{
cout<<"原码为:"
for(i=0i<8i++)//原码
cout<<a[i]
cout<<"反码为:"
cout<< "1"
for(i=1i<8i++)//负数反补不同
{
if(a[i]==0) //反码
cout<<"1"
else
cout<<中空枝"0"
}
cout<<"补码为:"
cout<< "1"
if(b[7]==0)
{b[7]=0c++}
if(c)
{
for(i=1i<7i++)
{
if(b[i]==0)
b[i]=1
else
b[i]=0
}
b[7]=0
b[6]++
for(i=6b[i]==2i--)
{
b[i]=0
b[i-1]++ 亏指
}
for(i=1i<8i++)
cout<<b[i]
}
else
{
for(i=1i<卖敏7i++)
{
if(a[i]==0)
cout<<"1"
else
cout<<"0"
}
cout<<"1"
}
}
else
{
cout<<"原码为:"
for(i=0i<8i++)//正数原反补一样
cout<<a[i]
cout<<"反码为:"
for(i=0i<8i++)
cout<<a[i]
cout<<"补码为:"
for(i=0i<8i++)
cout<<a[i]
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)