c - 函数在 c 中的 if else 语句中不起作用

标签 c function

我想编写基本转换。 我要求使用 if - else 来使用第一个基地和最后一个基地。 函数herhangibirinedonusum 在 main 方法中工作。 但是这个函数在 if else 语句中不起作用,你能帮我吗?

我编辑了我的代码。问题是函数在 if-else 语句中不起作用。 谢谢。

#include <stdio.h>
#include <math.h>
#include <string.h>

int herhangibirinedonusum(long int girilen_sayi, int ilk_taban, int son_taban) 
{
    char taban_rakamlari[16] ={'0', '1', '2', '3', '4', '5', '6', '7',
                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    int son_donusum[64];

    long int donusturulen;
    donusturulen = 0;

    int index;
    index = 0;

    int i;

    while (girilen_sayi != 0) {
        donusturulen += (girilen_sayi % 10) * (int) pow(ilk_taban, i);
        girilen_sayi /= 10;
        i++;
    }

    while (donusturulen != 0) {
        son_donusum[index] = donusturulen % son_taban;
        donusturulen = donusturulen / son_taban;
        ++index;
    }

    --index; 
    printf("\n\nDonusturulmus sayi = ");
    for (; index >= 0; index--){
        printf("%c", taban_rakamlari[son_donusum[index]]);
    }
    printf("\n");
}

int main() 
{
    int a, b, c, d, e;
    long int sayi;
    herhangibirinedonusum(110010, 2, 10);

    printf("\nGirilecek sayı tabanını seçiniz:");
    printf("\n1: 2'lik taban");
    scanf("%i", &a);

    if (a == 1) {
        printf("\nLütfen dönüştürülecek tabanı seçiniz:");
        printf("\n1: 8'lik tabana çevir");
        scanf("%i", &b);

        printf("Lütfen sayıyı giriniz:");
        scanf("%ld", &sayi);

        if (b == 1) {
            herhangibirinedonusum(sayi, 2, 8);
        }
        else
        {
            printf("\nwrong choose");
        }
    }

}

解决方案:数组和所有变量一开始就必须是空闲的。

int herhangibirinedonusum(long int girilen_sayi, int ilk_taban, int son_taban) {
    char taban_rakamlari[16] ={'0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    int son_donusum[64];
    int z;
    for (z = 0; z < 64; z++) {
        son_donusum[z] = 0;
    }

    long int donusturulen = 0;
    int index = 0;
    int i = 0;

    //sayi ilk olarak onluk tabana cevrilir.
    while (girilen_sayi != 0) {
        donusturulen += (girilen_sayi % 10) * (int) pow(ilk_taban, i);
        girilen_sayi /= 10;
        i++;
    }

    /* onluk tabandan istenilen tabana donusum yapilan kisim */
    while (donusturulen != 0) {
        son_donusum[index] = donusturulen % son_taban;
        donusturulen = donusturulen / son_taban;
        ++index;
    }

    // onluk tabandan herhangi bir tabana gecis yaparken kalanlardan once bolum
    // sonrasinda da kalanlar sagdan sola dogru yazilir.
    // Bu kisimda da ayni islemi yapiyoruz. Diziye tersten baslayarak
    // yazdiriyoruz.
    --index; /* diziyi tersten yazdirmak icin index sayisini azaltarak */
    printf("\n\nDonusturulmus sayi = ");
    for (; index >= 0; index--) /* diziyi tersten okuyarak yazdirilan kisim */ {
        printf("%c", taban_rakamlari[son_donusum[index]]);
    }
    printf("\n");

    return 1;

}

最佳答案

您没有说过要从函数herhangibirinedonusum返回任何值,请添加return SOMETHING(用变量替换SOMETHING),以便在int main()中 它将知道哪些值跟在 if 语句后面,哪些不跟在 if 语句后面(否则:停止 int main() 中的循环)。另外,如果您尝试在以下位置启动循环:

if (b == 1) {
        herhangibirinedonusum(sayi, 2, 8);
    } "

确保有一个 while 循环:

while(i=1){
   herhangibirinedonusum(sayi, 2, 8)
   if("herhangibirinedonusum==VALUE){ /*REPLACE WITH NEEDED VALUE*/
      continue;
      /*(or do something else)*/
   }
   else{
      break;
      /*(or do something else)*/
   }
}

此外,当您将 donusturulen 声明为 long int 时,没有必要说:

long int donustrululen;
donustrululen = 0;

您应该使用: long int donustrululen = 0 因为在声明变量后无需说变量等于 0(索引变量相同),因为它会使代码更长(这不是您应该做的 - 尝试使代码尽可能短但仍保持其工作),并且不要将函数命名那么长。

关于c - 函数在 c 中的 if else 语句中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41729861/

相关文章:

c - 编写函数以使用英特尔 MKL 库进行稀疏矩阵乘法

在 Solaris 下不能用信号量编译代码?

objective-c - 在不传递 nil 的情况下迭代函数中可变数量的参数

python - 从类属性组合函数

javascript - 当事件触发时,如何在 javascript 中使用数组来匹配 div?

c - union 成员如何拥有指向 union 实例的指针?

c - C11 aligned_alloc 分配的内存的 realloc 是否保持对齐?

c++ - 如何将 vector 传递给函数?

c - 用 C 中的指针合并两个序列 - "Violation Call"

c - 销毁(免费)链表式堆栈中的所有内容?