c - 如何使用数组 ("right"和 "left"(在我的情况下)的返回值从一个函数( "extract")到另一个函数 ("BinToDec")?

标签 c

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 opperator\n");
    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: %d\n", 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 opperator\n");
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);

}

关于c - 如何使用数组 ("right"和 "left"(在我的情况下)的返回值从一个函数( "extract")到另一个函数 ("BinToDec")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54470066/

相关文章:

c - 使用 c 忽略 libpcre 中的大小写

c - 将指向内存的指针初始化为 "handles"

c++ - C/C++ : How to convert 6bit ASCII to 7bit ASCII

c++ - 在 CLion 中调试时如何切换到 cmd/powershell?

c - XCode 4.3 - 从 XCode 4.2 升级后,perl 找不到 C 编译器

C : trying to remove spaces from a string without using any string library functions

c - C 中的短整型文字

c - 如何不在我的 shell 上显示信号?

c - C API 应该返回值还是错误代码?

在 C 中将字符串切成 2 个字符字符串的一部分,然后转换为十六进制格式(char * 到 unsigned char)