运行代码时它没有响应(但它在后台运行正常)

时间:2017-05-24 20:00:23

标签: c

我建立了一个显示守卫动作的程序。首先,在街道上有100个(我在代码中放置了10个,因为它更容易发现错误)(从1到100)。起初他们都在,但是: -

  1. 在他第一次经过他们之后,他把所有人都关了。
  2. 在他第二次经过他们后,他改变了灯光 代替2和它的倍数(2 4 6 8 ...) - 那些现在已经开始和相反的。
  3. 他第三次改变了第三名和第三名 3(3 6 9 ....) - 现在正在关闭的那些和相反的。
  4. 他第四次改变了第四名的成绩和倍数 4(4 8 12 ....)
  5. 该程序显示最后关闭了多少盏灯。 现在我构建了代码,编译器显示没有警告,也没有错误,当它运行它停止响应。 (编译器设置为显示所有警告。)

    有没有人知道代码有什么问题?

    #include<stdio.h>
    #define N 100
    int firstwalk(int light[N]);
    int secondtwalk(int light[N]);
    int thirdtwalk(int light[N]);
    int fourthtwalk(int light[N]);
    int main()
    {
        int light[N] = {0};
        int i;
        for (i = 0;i < N;i++)
        {
            light[i] = 1;
        }
        printf("Lights before the first walk-  ");
        for (i = 0;i < N;i++)
        {
            printf("%d ", light[i]);
        }
        firstwalk(light);
        secondtwalk(light);
        thirdtwalk(light);
        fourthtwalk(light);
        printf("the lights that were off are :-  ");
        for (i = 0;i < N;i++)
        {
            if (light[i] == 0)
                printf("%d ", i+1);
        }
    
    }
    int firstwalk(int light[N])
    {
        int i;
        for (i = 0;i < N;i++)
        {
            light[i] = 0;
        }
        printf("Lights after the first walk-  ");
        for (i = 0;i < N;i++)
        {
            printf("%d ", light[i]);
        }
        printf("\n");
        return light;
    
    }
    int secondtwalk(int light[N])
    {
        int i;
        for (i = 1;i < N;i= i * 2 + 1)
        {
            light[i] = 1;
    
        }
        printf("Lights after the second walk-  ");
        for (i = 0;i < N;i++)
        {
            printf("%d ", light[i]);
        }
        printf("\n");
        return light;
    }
    int thirdtwalk(int light[N])
    {
        int i;
        for (i = 2;i < N;i= i * 3 + 2)
        {
            if (light[i] == 1)
                light[i] = 0;
            else light[i] = 1;
        }
        printf("Lights after the third walk-  ");
        for (i = 0;i < N;i++)
        {
            printf("%d ", light[i]);
        }
        printf("\n");
        return light;
    }
    int fourthtwalk(int light[N])
    {
        int i;
        for (i = 3;i < N;i= i * 4 + 3)
        {
            if (light[i] == 1)
                light[i] = 0;
            else light[i] = 1;
        }
        printf("Lights after the third walk-  ");
        for (i = 0;i < N;i++)
        {
            printf("%d ", light[i]);
        }
        printf("\n");
        return light;
    }
    

    感谢。

1 个答案:

答案 0 :(得分:4)

代码中的主要错误基本上就是您访问阵列的方式,您不断超越阵列边界,这里有一个可能的版本修复:

for(int i=0;i<N;i++) light[i * 3 + 2]

考虑一下,让我们说你做for(int i=0;i<N;i++) light[i*k1+k2] 之类的事情,现在......当i = 3-&gt;光[11],i = 4-&gt;光[14],依此类推......而且你只为10个元素的数组分配了内存。

更好的模式将取代以下内容:

for(int i=k2;i<N;i+=k1) light[i]

通过以下方式:

#include <stdio.h>

#define N 10

#define LIGHTS_ALL -1
#define LIGHTS_OFF 0
#define LIGHTS_ON 1

char *lights_str(int light[N], int status_light) {
    char s[256] = {0};
    int n = 0;

    for (int i = 0; i < N; i++) {
        if (status_light == LIGHTS_OFF) {
            if (light[i] == LIGHTS_OFF) n += sprintf(&s[n], "%d ", i);
            continue;
        }
        if (status_light == LIGHTS_ON) {
            if (light[i] == LIGHTS_ON) n += sprintf(&s[n], "%d ", i);
            continue;
        }

        n += sprintf(&s[n], "%d", light[i]);
    }

    return s;
}

void firstwalk(int light[N]) {
    for (int i = 0; i < N; i++) {
        light[i] = 0;
    }
}

void secondtwalk(int light[N]) {
    for (int i = 1; i < N; i += 2) {
        light[i] = 1;
    }
}

void thirdtwalk(int light[N]) {
    for (int i = 2; i < N; i += 3) {
        light[i] = light[i] == 1 ? 0 : 1;
    }
}

void fourthtwalk(int light[N]) {
    for (int i = 3; i < N; i += 4) {
        light[i] = light[i] == 1 ? 0 : 1;
    }
}


int main() {
    int light[N] = {0};
    char *walks[256] = {"first", "second", "third", "fourth"};

    void (*p[4])(int light[N]);
    p[0] = firstwalk;
    p[1] = secondtwalk;
    p[2] = thirdtwalk;
    p[3] = fourthtwalk;
    for (int i = 0; i < 4; i++) {
        printf("\nLights before the %s walk-\t%s", walks[i],
               lights_str(light, LIGHTS_ALL));
        p[i](light);
        printf("\tLights after the %s walk-\t%s", walks[i],
               lights_str(light, LIGHTS_ALL));
    }
    printf("\n\nFinal status\tLights on\t%s\tLights off\t%s",
           lights_str(light, LIGHTS_OFF), lights_str(light, LIGHTS_ON));
}

它是一个更安全的版本,保证不超出数组边界

在您理解了上述解释之后,下一步就是将代码重构为更简洁的内容:

try(BufferedReader format1 = new BufferedReader(new 
                                         FileReader("favorites.txt"))) {

            String line = format1.readLine(); //Used to look through file
            String found = ""; //Stores the address that was the same

            //Loop searches file, I only plan to have a max of 20 favorites
            for (int counter = 0; counter < 20; counter++) {

                System.out.println(line); //Just used so I can make sure the 
                                            loop works

                if(line.equals(input)) {
                    counter = 20;
                    found = line;
                }

                line = format1.readLine();
            }
            format1.close();

            if(!(found.equals(input))) {
                BufferedWriter format2 = new BufferedWriter(new 
                                         FileWriter("favorites.txt", true));
                format2.write(input);
                format2.newLine();

                format2.close();


            }           
    } catch (IOException e1) {
        System.out.println("ERROR! Favorite not Added.");
    }

事实上,不要停在这里,尝试进一步重构,直到代码变得越来越好......我会把这个任务留给你:)