C 将base16数字转换为base2并求和
我有一个程序,它控制以16为基数的数字,并将其转换为以2为基数的数字。然后,它使用二进制查找SUM和Cout。如你们所知,A…F是以16为基数的有效数字。因此,我需要使用Char来扫描它们。这就是我使用“char”的原因。顺便说一句,不允许使用字符串和函数。提前谢谢C 将base16数字转换为base2并求和,c,char,C,Char,我有一个程序,它控制以16为基数的数字,并将其转换为以2为基数的数字。然后,它使用二进制查找SUM和Cout。如你们所知,A…F是以16为基数的有效数字。因此,我需要使用Char来扫描它们。这就是我使用“char”的原因。顺便说一句,不允许使用字符串和函数。提前谢谢 #include<stdio.h> int main() { char numsix; int choice, base; int i,length; int num=0, decisum
#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);
}
}
}
}
#包括
int main(){
char numsix;
整数选择,基数;
int i,长度;
int num=0,分取和,bin=0;
INTA、b、c、d;
int rm,位置=1;
int sum,cout,part1,part2,part3;//part1是(A和B),part2是(C_IN和(A或B)),part3是(A或B)
printf(“欢迎使用全加程序!\n”);
printf(“(1)计算并显示输出”);
printf(“\n(2)退出”);
printf(“\n您选择:”);
scanf(“%d”,选择(&C);
while(选项!=2){//讲师建议
printf(“您选择了选项1”);
printf(“\n您将使用哪个基数输入数据行(基数16/8/2)?”;
scanf(“%d”、&base);
如果(base==16){//用户不允许输入大于7的整数。小于7的整数在8、16和10中具有相同的值。
printf(“请输入”);
做{
scanf(“%c”&numsix);
printf(“%c”,numsix);
num=num+numsix;
i++;
如果(i==1){
num=num-10;
}
}而(numsix!=10);
printf(“%d”,num);//用户可以在16进制中使用从A到F的字符。我们可以用这种方式控制输入。
如果(num=0){//我们正在控制输入是否适合计算!
而(num>0){//Heximal到Binary!
rm=num%2;
仓位=(rm*位置)+仓位;
num/=2;
位置*=10;
}
a=bin/100;//对于abc,我们在这里找到a、b和c自身的值!
b=箱/10-a*10;
c=料仓-(a*100+b*10);
如果(a==b){//我们在下面找到和
总和=0;
如果(总和=c){
总和=0;
}
否则{
总和=1;
}
}
否则{
总和=1;
如果(总和=c){
总和=0;
}
否则{
总和=1;
}
}
如果(a==b){//我们在下面找到cout
第3部分=0;
}
否则{
第3部分=1;
}
如果(c==第3部分){
如果(c==1){
第2部分=1;
}
否则{
第2部分=0;
}//第1部分是(A和B),第2部分是(C_IN和(A异或B)),第3部分是(A异或B)
}
如果(a==b){
如果(a==1){
第1部分=1;
}
否则{
第1部分=0;
}
}
如果(第1部分==第2部分){
如果(第1部分==0){
cout=0;
}
否则{
cout=1;
}
}
否则{
cout=1;
}
printf(“总和:%d”,总和);
printf(“C-out:%d”,cout);
printf(“\n欢迎使用全加程序!\n”);//以便能够再次启动程序
printf(“(1)计算并显示输出”);
printf(“\n(2)退出”);
printf(“\n您选择:”);
scanf(“%d”,选择(&C);
}
否则,如果(num>=17&&num此代码中存在许多问题,我看到的主要问题是您在初始化它之前正在使用I
:
int i,length;
请,并学习如何创建。do while部件中的主要问题。它发生在添加后。我更改了您提到的问题,但它不起作用:(
i++;
if (i == 1) {
num = num - 10;
}