Как я могу использовать возвращаемое значение массива (в моем случае «right» и «left») из одной функции («extract») в другую функцию («BinToDec»)? - PullRequest
0 голосов
/ 01 февраля 2019
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]);
    }    
}

}

Мое намерение в этом коде состоит в том, чтобы использовать значения, добавленные в массив «left» и «right» из массива «foo» в функции «extract» и передатьэто в функцию "BinToDec" и сохранить возвращаемое значение в некоторой переменной с именем "десятичная".Это потому, что позже я хочу использовать возвращаемое значение из BinToDec для какой-то другой функции, над которой я работаю.

1 Ответ

0 голосов
/ 01 февраля 2019
//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);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...