void main()
{
extract();
BinToDec();
}
int BinToDec()
{
int decimal = 0, base = 1, rem, num;
int x = atoi(right);
num = x;
while (x != 0)
{
rem = x % 10;
decimal = decimal + rem*base;
x = x / 10;
base = base * 2;
}
printf("the decimal equivalent of the binary number %d is: %d", num, decimal);
}
void extract()
{
int i;
char foo[29];
printf("Enter the number and opperatorn");
scanf("%s",foo);
int index;
int len = strlen(foo);
for (i = 0; i < len; i++)
{
if (foo[i] == '+' || foo[i] == '-' || foo[i] == '*' || foo[i] == '/' || foo[i] == '%')
{
char op = foo[i];
printf("%c", op);
index = i;
}
}
char left[14];
for (int j = 0; j < index; j++)
{
left[j] = foo[j]);
printf("%c", left[j]);
}
char right[14];
for (int k = index + 1; k < len; k++)
{
right[k] = foo[k];
printf("%c", right[k]);
}
}
}
我在此代码中的目的是使用函数"extract"中数组"foo"中附加在数组"left"和"right"中的值,并将其传递给函数"BinToDec"并将返回的值存储在某个称为"十进制"的变量中。这是因为,稍后,我想将 BinToDec 的返回值用于我正在处理的其他函数。
//you use two shared global variables for example
int leftop,rightop;
void extract();
//make the bintodec takes one side as input & returns the value so its more generic and you call inside the extract function for example
int BinToDec(char * operand);
int main()
{
extract();
}
// now the function bintoDec takes char* something like 1111 & turns it into decimal 15 & return it
//so it doesn't really care whether its the right of the left operand
// it just return the binary value of the decimal you gave it to it
int BinToDec(char * operand)
{
int decimal=0, base=1, rem, num;
int x = atoi(operand);
num = x;
while(x != 0)
{
rem = x % 10;
decimal = decimal + rem*base;
x = x / 10;
base = base*2;
}
printf("the decimal equivalent of the binary number %d is: %dn", num,
decimal);
//here the returned value here we assign to the leftop and rightop depens on the input
return decimal;
}
void extract(){
int i;
char foo[29];
printf("Enter the number and opperatorn");
scanf("%s",foo);
int index;
int len = strlen(foo);
for (i=0; i < len; i++)
{
if(foo[i] == '+' || foo[i] == '-' || foo[i] == '*' || foo[i] == '/' ||foo[i] == '%'){
char op = foo[i];
printf("%c", op);
index = i;
}
}
char left[14];
for(int j=0; j < index; j++){
left[j] = foo[j];
printf("%c", left[j]);
}
//now you can get both values of left & right operands like this
// now after we split the operands we call BinToDec with the left operand & give the returns value
//which represents the binary value of that left operand to the "leftop" global variable so you can use it in your other functions
leftop = BinToDec(left);
char right[14];
i=0;
//side note : your code had right[k]=foo[k] which is wrong because right array starts from 0 to 13 & k can go till 27
// which will cause you an error
for(int k=index + 1; k < len; k++){
right[i] = foo[k];
printf("%c", right[i]);
i++;
}
//now same thing to the right operand we call the function we gave it the binary value & it returns us the decimal value & we assign it this time to the rightop global variable for other uses outside this function
rightop=BinToDec(right);
}