简体   繁体   English

错误导致代码执行

[英]Wrong results in code execution

I am trying to create a program in c code, as part of a high school education project, and i have some trouble with my printing results. 作为高中教育项目的一部分,我试图用C代码创建一个程序,但是我的打印结果有些麻烦。 The main idea is a program which is looking for consequtive non square free numbers teams.The amount of that numbers depends on the value of the preprocessor defined parameter K. For example, if the defined parameter K is set to the value 2, the program should print, after check 2 consecutive non square free numbers. 主要思想是一个程序,该程序正在寻找相应的非平方无数字组。该数字的数量取决于预处理器定义的参数K的值。例如,如果将定义的参数K设置为值2,则该程序在检查2个连续的非正方形自由数字后,应打印。 There is no upper number limit for checking. 没有上限检查。 However, the code i wrote works successfully only for the parameter one. 但是,我编写的代码仅适用于参数一。 Is ther any correction i shoyld do in order to working properly? 为了正常工作,我应该进行任何更正吗? There is the part of the code i have made: 我编写了部分代码:

#include <stdio.h>
#include <stdlib.h>
/* Including the math library for sqrt function */
#include <math.h>
/* Testing Environment values */
#define K 2

int main (void) {
    // Declaration and appropriate Initialization of Variables Section
    unsigned int num = 0, last = 0, count = 0, i, j, lim, Div, temp, start, first, flag;
    long long int div;

    // Non-Squarefree number check section
    for (i = 1; count < K; i++) {
        flag = 0 ;

        for (j = 2; (j <= sqrt(i)) && (count < K); j++) {
            while (( (i % (j * j)) == 0) && (count < K) ) {
                flag = 1;

                if (count = 0) {
                    num = i; /* Storage of the first checked number in case of success finding the K requested numbers */
                }

                count++;
                printf("count++: %d \n", count);
                Div = j;
                last = i;   /* Storage of the last checked number that mets the requested critiria */
                printf("last: %d \n", last);
                break;
            }

            if (flag = 0) { /* Reinitialization of variable values in case of squarefree number */
                count = 0;
                printf("count_null: %d \n", count);
                num = 0;
                printf("count: %d \n", count);
                break;
            }

            break;
        }
    }

    /* test execution prints */
    printf("----------------------------------------") ;
    printf("K: %d \n", K) ;
    printf("last: %d \n", last) ;
    printf("num: %d \n", num);
    start = num;
    return 1;
}

Have a look at these pieces, most notably your if statements. 看看这些部分,最值得注意的是您的if语句。

if (count = 0) {

    // Storage of the first checked number in case
    // of success finding the K requested numbers

    num = i;
}

if (flag = 0) {

    // Reinitialization of variable values in case
    // of squarefree number

    count = 0;
    printf("count_null: %d \n", count);
    num = 0;
    printf("count: %d \n", count);
    break;
}

It's a common issue when getting started with C programming, and it's likely the reason why your code doesn't work. 这是开始使用C编程时的常见问题,这很可能是您的代码无法正常工作的原因。 The comparison operator is == , not = because it's already used as the assignment operator. 比较运算符是== ,不是=因为它已经用作赋值运算符。

This means if (count = 0) doesn't run if count equals 0. First it will execute what you put in the statement, which is "set count to 0", or count = 0 . 这意味着if (count = 0) count等于0, if (count = 0)不运行。首先它将执行您在语句中输入的内容,即“将count设置为0”或count = 0 Then if that operation returns 1 (true), the contents of your if statement will execute. 然后,如果该操作返回1(true),则将执行if语句的内容。 On my system (Linux, GCC) count = 0 always returns 0, but I didn't check if it's defined as standard behavior. 在我的系统(Linux,GCC)上, count = 0总是返回0,但是我没有检查它是否被定义为标准行为。 This mean num = i will never execute, and just passing through that if it will always set count to 0. Which is likely not what you want. 这意味着num = i永远不会执行,并且如果它将始终将count设置为0,则只会执行该操作。这可能不是您想要的。 Same for if (flag = 0) . if (flag = 0)

I don't know what the code is supposed to do, maybe that's exactly what you wanted to do. 我不知道代码应该做什么,也许那正是您想要做的。 But even if you did, you should never set your variables in an if statement. 但是即使这样做,也永远不要在if语句中设置变量。 It's confusing to read and slows you down, and leads to errors. 令人困惑的阅读会使您放慢速度,并导致错误。 If you really wanted to do what the code you posted does, it would go something like this: 如果您真的想做您发布的代码所要做的事情,它将是这样的:

count = 0;

if (0) { // 0 = false
    num = i;
}

This is what your code does. 这就是您的代码所做的。 But I'm 99% sure you didn't want that, so start with changing your if s to use == . 但是我99%的确定您不想要那样,所以首先将if改为使用== Let me know if that fixes the issue, and if there's anything more that doesn't work, we'll work it out. 让我知道这是否可以解决问题,如果还有其他无法解决的问题,我们会解决。 Hope that helps! 希望有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM