【问题标题】:Floating point exception(core dumped) error ,in my c code浮点异常(核心转储)错误,在我的 c 代码中
【发布时间】:2020-03-15 14:49:42
【问题描述】:

我正在尝试使用二进制数和逻辑运算符而不使用&。当我输入示例 1111 和 1000 时,发生了浮点异常(core dumped)。我正在等待此代码相同长度的两个二进制数,打印后例如 print:1001 和 1111 = 1001

  #include<stdio.h>
int length(int a,int b);
int andop(int a,int b);
int main(){
    int first,sec;
    do{
        printf("First integer:  ");
        scanf("%d",&first);
        printf("\nSecond integer:  ");
        scanf("%d",&sec);} 
    while(andop(first,sec)==0); 
    printf("\n%d AND %d= %d",first,sec,andop(first,sec));
    return 0;
}

int andop(int a,int b){

   int a_1,b_1;
   int result=0;

    a_1=a;
    b_1=b;
    while(a_1>1){/*Checking if first is binary or not,the loop briefly checks if the number in each digits either 1 or 0,and if it dont returns 0 it also quit the loop and stop asking for new numbers*/
        if (a_1%10>1){
             printf("\nInteger should be binary,please enter 2 new integers\n");
             return 0; 
         }
     a_1=a_1/10; 
    }   
    while(b_1>1){/*Checking if first is binary ,the loop briefly checks if the number in each digits either 1 or 0,and if it dont returns 0 it also quit the loop and stop asking for new numbers*/
        if (b_1%10>1){
            printf("\nInteger should be binary,please enter 2 new integers\n");
            return 0;
      }
      b_1=b_1/10; 
    }

    while (length(a,b)>0){ 

    result=result+(a%10)*(b%10);  
    a=a/10;
    b=b/10;
    if(length(a,b) == 0){
    break;
    }
    result=result*10;
    }
    return result;
    }

 int length(int a,int b){
   if(a == 0 || b == 0){
    return 0;
   }
   int temp_a,temp_b;
   int length_a=0,length_b=0;

   temp_a=a;/*i assign the number into the temporary variable _a and _b*/ 
   temp_b=b;
   while(temp_a>0){//checking how many digit a is
        temp_a=temp_a/10;
        length_a++;
   }
   while(temp_b>0){//checking how many digit b is
        temp_b=temp_b/10;
        length_b++;
   }

   if(length_!=length_b){/* If they don't have same digits ,print an error message and continue to taking number from user*/
         printf("\nInteger should have same length,please enter 2 new integers\n");
         return 0;
     }   

   return length_a;

}

【问题讨论】:

  • 错误是“除以零”,对吧?所以检查你的部门。
  • 我到底在哪里做这个?
  • 查看您的所有/ 运算符。如果除数是常数(0 除外),则不是问题。这应该会迅速减少可能性。并使用调试器。
  • while(length_a/length_b!=1) {...} 循环不好。使用if(length_a != length_b) { ..}
  • 是的,我做了一些编辑,但我仍然犯了同样的错误。它可能应该在 int andop(int ,int) 但我找不到问题所在?

标签: c binary operators


【解决方案1】:

以下代码是问题的根源:

   while(length_a/length_b!=1){/* If they don't have same digits ,print an error message and continue to taking number from user*/
         printf("\nInteger should have same length,please enter 2 new integers\n");
         return 0;
     }   

您正在使用一种非常奇怪的方法来测试两个变量(length_alength_b)是否相同!此外,如果length_b 为零(在某个阶段看起来是这样),则除法将导致错误。

您可以只使用两个变量的简单比较:

    if (length_a != length_b) {/* If they don't have same digits ,print an error message and continue to taking number from user*/
        printf("\nInteger should have same length,please enter 2 new integers\n");
        return 0;
    }

编辑:您的代码中还有许多其他错误,但我给出的建议将解决您所询问的具体错误。

【讨论】:

  • 我根据你的建议更改了我的代码,但我仍然遇到同样的错误。问题应该在不同的部分
  • @jamesMoriarty 您是否保留了原始代码并使用了我建议的代码?否则,我真的看不出你怎么可能得到 same 错误,因为你的代码中没有其他地方可以除以(或取模)零值。
【解决方案2】:

在您的程序中,由于函数长度被零除而发生浮点异常。 在对 length() 进行任何操作之前,您应该检查 a 或 b 是否为 0。 尝试在长度的开头添加这个

if(a == 0 || b == 0)
    return 0;

另外,在 andop 中将结果乘以 10 之前检查 length() 是否返回 0 即将 length(a, b) 更改为

if(length(a, b) == 0)
    break;

即使在此之后,您的程序也会以相反的顺序打印结果,因此您必须以相反的顺序打印结果。尝试为此创建一个函数。

【讨论】:

  • 另外,如果结果类似于1000000010,将这些变量作为int 将产生问题,它只会打印110。因为 printf 打印 int 不会打印任何起始 0。尝试为此使用字符串。
  • 我在上面编辑了我的代码,并且不允许先生使用字符串,我仍然遇到同样的错误,我认为在这段代码中,我正在尝试将我使用的任何数字除以 0,并且如果我处理此错误,我可以在没有字符串的情况下进行反转吗?看起来可能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多