简体   繁体   English

我只是不确定我要去哪里。 编写程序以打印c中的素因式分解并练习与之链接

[英]I just am not sure where I am going wrong. Writing a program to print out prime factorization in c and practicing linking along with it

In this program I keep getting a floating point exception at the end. 在这个程序中,我总是在最后得到一个浮点异常。 There are two main files I am working with. 我正在使用两个主要文件。 The first is the "main" listed below: 第一个是下面列出的“主要”:

int main(){ 
int ans;
do{
printf("Enter an integer greater than 1:\n");
scanf("%d", &ans);
}while(ans <= 1);

printf("%d = ", ans);

int d = 2;

while(ans >= d){

if(ans == d){
        printf("%d ^ %d", d, factor_power(ans, d));
        ans = ans / (d ^ (factor_power(ans, d)));
}

else{
        printf("%d ^ %d * ", d , factor_power(ans , d));
        ans = ans/(d ^ (factor_power(ans, d)));
        d++;

        }
}

printf("\n");

return 0;
}

The file that contains the factor_power() method is here: 包含factor_power()方法的文件在这里:

int factor_power(int n, int d){
int p = 1;

do{
if( n % (d ^ p) == 0)
        p ++;
}while(n % (d^(p+1)) == 0);

return p;
}

both include my header, math.h, and stdio.h. 都包括我的头文件math.h和stdio.h。 I am just so lost on where the floating point exception is coming from. 我对浮点异常来自何处感到迷茫。 The program is supposed to print out the prime factors like: 该程序应该打印出主要因素,例如:

1200 = 2^4 * 3^1 * 5*2. 1200 = 2 ^ 4 * 3 ^ 1 * 5 * 2。

Amy feedback is much appreciated. 艾米的反馈意见非常感谢。

Try following: 请尝试以下操作:

 int ipow(int base, int exp)
 {
 int power = 1;
 while (exp)
 {
    if (exp & 1)
        power *= base;
    exp >>= 1;
    base *= base;
  }

 return power;
 }


int factor_power(int n, int d){
 int p = 1;

 do{
 if( (n % ipow(d,p)) == 0)
    p++;
 } while((n % ipow(d,(p+1))) == 0);

  return p;
  }

You may have to include "math.h" if u already hadn't. 如果您尚未添加“ math.h”,则可能必须包含。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写了一个打印素数的程序,但没有得到所需的输出。我哪里出错了? - wrote a program for printing the prime numbers not getting the desired output .where i am going wrong? 流程,派生,管道程序...我在哪里出错? - Processes, fork, pipes program…Where am I going wrong? 编写一个质因数分解程序 C - writing a prime factorization program C 在这个简单的 C 程序中分配内存时我哪里出错了? - Where am I going wrong when allocating memory in this simple C program? 获取以下代码的细分错误。 我无法弄清楚我要去哪里 - Getting segmentation fault for the code below. I am not able to figure out where am I going wrong 我在哪里错这个推理? - Where am I going wrong about this reasoning? 这是用于评估后缀表达式的 C 代码。 我添加了评论以查看我哪里出错了 - This is C code for evaluation of a postfix expression. I have added comments to see where I am going wrong 它仍然崩溃,我要去哪里错了? - It's still crashing, where am I going wrong? 我的内存管理哪里出问题了? - Where am I going wrong with my memory management? 我正在尝试创建 ac 程序来获取带有 array 的数字的因数,出了什么问题? - I am trying to create a c program to get the factors of a number with array , what is going wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM