gpt4 book ai didi

c - 将以16为底的数字转换为以2为底的数字并求和

转载 作者:行者123 更新时间:2023-11-30 21:38:42 26 4
gpt4 key购买 nike

我有一个程序,以16为底数控制数字并将其转换为2。然后,它使用二进制找到SUM和Cout。如您所知,A ... F是以16为底的有效数字。因此,我需要使用Char对其进行扫描。这就是为什么我使用“ char” .btw不允许使用字符串和函数的原因。提前致谢!

#include<stdio.h>
int main() {
char numsix;
int choice, base;
int i,length;
int num=0, decisum, bin=0;
int a, b, c , d ;
int rm, location = 1;
int sum, cout, part1, part2, part3; //Part1 is (A AND B) ,Part2 is (C_IN AND (A XOR B)) ,Part3 is (A XOR B)
printf("Welcome to Full-Adder! \n");
printf("(1)Compute and display the outputs");
printf("\n(2)Quit");
printf("\nYou choose:");
scanf("%d", &choice);

while (choice != 2) { //instructor's suggest
printf("You have chosen option 1");
printf("\n Which base will you use to enter data lines (base 16/8/2)?");
scanf("%d", &base);
if (base == 16) { //User is not allowed to enter integers greater than 7.Integers less than 7 have same value in 8 16 and 10.
printf("Please enter input");
do {
scanf("%c",&numsix);
printf("%c",numsix);
num=num+numsix;
i++;
if(i==1) {
num=num-10;
}
} while(numsix != 10 );

printf("%d",num); //User is able to use chracters from A to F in 16 base.We're able to control the input with that way.

if (num <= 7 && num >= 0) { //We're controlling whether the input is suitable to evaluate or not!
while (num>0) { //Heximal to Binary!
rm = num % 2;
bin = (rm*location) + bin;
num /= 2;
location *= 10;
}
a = bin / 100; //for abc we're finding here a ,b and c's own value!
b = bin / 10 - a * 10;
c = bin - (a * 100 + b * 10);
if (a == b) { //We're finding Sum below
sum = 0;
if (sum == c) {
sum = 0;
}
else {
sum = 1;
}
}
else {
sum = 1;
if (sum == c) {
sum = 0;
}
else {
sum = 1;
}
}
if (a == b) { //We're finding cout below
part3 = 0;
}
else {
part3 = 1;
}
if (c == part3) {
if (c == 1) {
part2 = 1;
}
else {
part2 = 0;
} //Part1 is (A AND B) ,Part2 is (C_IN AND (A XOR B)) ,Part3 is (A XOR B)
}
if (a == b) {
if (a == 1) {
part1 = 1;
}
else {
part1 = 0;
}
}
if (part1 == part2) {
if (part1 == 0) {
cout = 0;
}
else {
cout = 1;
}
}
else {
cout = 1;
}
printf("Sum:%d", sum);
printf("C-out:%d", cout);
printf("\n Welcome to Full-Adder! \n"); //to be able to start the program again
printf("(1)Compute and display the outputs");
printf("\n(2)Quit");
printf("\nYou choose:");
scanf("%d", &choice);
}
else if (num>=17 && num<=22 || num==8 || num==9 ) {
printf("cannot be convered to 3 digits at binary",numsix); //We're controlling whether the input is suitable to evaluate or not!
printf("\n Try Again.");
printf("\n Welcome to Full-Adder! \n"); //to be able to start the program again
printf("(1)Compute and display the outputs");
printf("\n(2)Quit");
printf("\nYou choose:");
scanf("%d", &choice);
}
else {
printf("is not valid a number in base 16", numsix); //We're controlling whether the input is suitable to evaluate or not!
printf("\nTry Again? \n");
printf("\n Welcome to Full-Adder! \n"); //to be able to start the program again
printf("(1)Compute and display the outputs");
printf("\n(2)Quit");
printf("\nYou choose:");
scanf("%d", &choice);
}
}
}
}

最佳答案

这段代码有很多问题,我主要看到的是初始化之前您正在使用i

int i,length;   






i++;
if (i == 1) {
num = num - 10;
}

关于c - 将以16为底的数字转换为以2为底的数字并求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com