C语言程序不能成功运行

两个关于疫情数据的代码第一个是用数组完成的,第二个是用链表完成的,两个都不能成功运行数组的可以打开文件但不能进入循环,链表的运行之后无任何结果,且都没有报错和警告,调试的报错只有这个,请大家看看是怎么回事

img


#include
#define population 3000000
#define cost_of_lock_down_population 60000000
#define ratio 3
#define shelter_size 1000
#define shelter_time 7
#define cost_of_shelter 200
int day[40] = { 1 };
int infection[40] = { 1 };
int shelter = 1;
long cost[40] = { 200 };
int ID[population] = { 1 };
int new[40] = { 1 };
long sum = 0;
int t[40] = { 1 };
int shelter_[cost_of_lock_down_population / shelter_size] = { 1 };
int main()
{
    FILE* fp1 = fopen("infected_day_x.txt", "a+");
    FILE* fp2 = fopen("cost_of_lock_down_population_day_x.txt", "a+");
    FILE* fp3 = fopen("shelter_day_x.txt", "a+");
    for (int i = 1; i <= 47; i++)
    {
        fprintf(fp1, "\n第%d天\t感染人数:%d\n感染的ID为:\t", day[i - 1], infection[i - 1]);
        fprintf(fp2, "第%d天\t方舱消耗金额:%ld\n", day[i-1],cost[i-1]);
        day[i] = day[i - 1] + 1;
        while (i < 40)
        {
            new[i] = new[i - 1] * ratio;
            infection[i] += new[i];
            for (int b = 0; b < new[i]; b++)
            {
                ID[t[i - 1] + b] = 1;
            }
            t[i] = new[i] + t[i - 1];
        }
        while (day[i] > shelter_time)
        {
            for (int b = 1; b <= new[i - 7]; b++)
            {
                ID[t[i - 7] - b] = 0;
            }
            infection[i] -= new[i - 7];
            if (infection[i] <= 0)
            {
                break;
            }
        }
        for (int b = 0; b < t[i]; b++)
        {
            if (ID[b] != 0)
            {
                fprintf(fp1, "%d\t", b+1);
                int n = 0;
                n++;
                if (n <= shelter_size)
                {
                    fprintf(fp3, "\n方舱%d\nID为%d\t", shelter, b + 1);
                }
                else
                {
                    shelter++;
                    n -= shelter_size;
                }
                shelter_[i] = shelter;
            }
        }
        cost[i] = infection[i] * cost_of_shelter;
        sum += cost[i];
        sum += cost_of_lock_down_population;
    }
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    FILE* fp4 = fopen("total_cost.txt", "a+");
    fprintf(fp4, "总消耗数为%ld", sum);
    fclose(fp4);
    FILE* fp5 = fopen("infected_day.csv", "a+");
    FILE* fp6 = fopen("cost_day.csv", "a+");
    FILE* fp7 = fopen("shelter_day.csv", "a+");
    for (int i=0; i<47;i++)
    {
        fprintf(fp5, "%d",day[i]);
        fprintf(fp5, "\n");
        fprintf(fp5, "%d\n",infection[i]);
        fprintf(fp6, "%d",day[i]);
        fprintf(fp6, "\n");
        fprintf(fp6, "%d\n",cost[i]);
        fprintf(fp7, "%d",day[i]);
        fprintf(fp7, "\n");
        fprintf(fp7, "%d\n",shelter_[i]);
    }
    fclose(fp5);
    fclose(fp6);
    fclose(fp7);
    return 0;
}
```c
#include
#include

#define population 3000000
#define cost_of_lock_down_population 60000000
#define ratio 3
#define shelter_size 1000
#define shelter_time 7
#define cost_of_shelter 200

typedef struct _id
{
    int number;
    struct _id* next2;
}id;
typedef struct _ID
{
    int a;
    id* ids;
    struct _ID* next3;
}ID;

typedef struct _num
{
    int day;
    int infection;
    long cost;
    int shelter;
}num;
typedef struct _node
{
    num n;
    ID* IDs;
    struct _node* next;
}node;
int main()
{
    node* head = NULL;
    id* head2 = NULL;
    ID* head3 = NULL;
    int i = 1;
    int b = 0;
    long sum = 0;
    int new = 0;
    for (; i <= 40; i++)//从开始到结束共计40天
    {
        node* p = (node*)malloc(sizeof(node));
        if (p == NULL)
        {
            printf("no");
            exit(0);
        }
        if (i == 1)//赋初值
        {
            p->n.day = 1;
            p->n.infection = 1;
            p->n.cost = 0;
            p->n.shelter = 1;
            new = (p->n.infection) * ratio;
        }
        else
        {
            (p->n.day)++;
            for (int t = p->n.infection + 1; t <= new * ratio; t++)
            {
                id* p2 = (id*)malloc(sizeof(id));
                if (p2 == NULL)
                {
                    printf("no");
                    exit(0);
                }
                p2->number = t;
                p2->next2 = NULL;
                id* last2 = head2;
                if (last2)
                {
                    while (last2->next2)
                    {
                        last2 = last2->next2;
                    }
                    last2->next2 = p2;
                }
                else
                {
                    head2 = p2;
                }
                b++;
            }
            ID* p3 = (ID*)malloc(sizeof(ID));
            if (p3 == NULL)
            {
                printf("no");
                exit(0);
            }
            p3->a = b;
            p3->ids = head2;
            p3->next3 = NULL;
            ID* last3 = head3;
            if (last3)
            {
                while (last3->next3)
                {
                    last3 = last3->next3;
                }
            }
            else
            { 
                head3 = p3;
            }
            while (i <= 15)
            {
                new *= ratio;
            }
            p->n.infection += new;
            while (p->n.day > shelter_time)
            {
                ID* phead3 = head3->next3;
                p->n.infection -= p3->a;
                free(head3);
                head3 = phead3;
            }
            if (p->n.infection <= 0)
            {
                break;
            }
            p->n.cost = (p->n.infection) * cost_of_shelter;
            p->n.shelter = (p->n.infection) % shelter_size + 1;
            p->IDs = head3;
            p->next = NULL;
            node* last = head;
            if (last)
            {
                while (last->next)
                {
                    last = last->next;
                }
                last->next = p;
            }
            else
            {
                head = p;
            }
        }
        sum += p->n.cost;
        sum += cost_of_lock_down_population;
    }
    FILE* fp1 = fopen("infected_day_x.txt", "a+");
    node* move = head;
    id* move2 = move->IDs->ids;
    ID* move3 = move->IDs;
    while (move != NULL)
    {
        fprintf(fp1, "\n第%d天\t感染人数:%d\n感染的ID为:\t", move->n.day, move->n.infection);
        while (move3 != NULL)
        {
            while (move2 != NULL)
            {
                fprintf(fp1, "%d\t", move2->number);
                move2 = move2->next2;
            }
            move3 = move3->next3;
        }
        move = move->next;
    }
    fclose(fp1);
    FILE* fp2 = fopen("cost_of_lock_down_population_day_x.txt", "a+");
    while (move != NULL)
    {
        fprintf(fp2, "第%d天\t方舱消耗金额:%ld\n", move->n.day, move->n.cost);
        move = move->next;
    }
    fclose(fp2);
    FILE* fp3 = fopen("shelter_day_x.txt", "a+");
    while (move != NULL)
    {
        fprintf(fp3, "第%d天\t方舱使用数量为%d\n", move->n.day, move->n.shelter);
        move = move->next;
    }
    fclose(fp3);
    FILE* fp4 = fopen("total_cost.txt", "a+");
    fprintf(fp4, "总消耗数为%ld", sum);
    fclose(fp4);
    FILE* fp5 = fopen("infected_day.csv", "a+");
    FILE* fp6 = fopen("cost_day.csv", "a+");
    FILE* fp7 = fopen("shelter_day.csv", "a+");
    while (move != NULL)
    {
        fprintf(fp5, "%d", move->n.day);
        fprintf(fp5, "\n");
        fprintf(fp5, "%d\n", move->n.infection);
        fprintf(fp6, "%d", move->n.day);
        fprintf(fp6, "\n");
        fprintf(fp6, "%d\n", move->n.cost);
        fprintf(fp7, "%d", move->n.day);
        fprintf(fp7, "\n");
        fprintf(fp7, "%d\n", move->n.shelter);
        move = move->next;
    }
    fclose(fp5);
    fclose(fp6);
    fclose(fp7);
    return 0;
}



```

你的代码中有一个问题是在这个循环中:

for (int i = 1; i <= 47; i++)

你在循环中又嵌套了一个循环:

while (i < 40)

而这个嵌套循环的判断条件与外层循环的判断条件相矛盾,所以会导致程序死循环。

你可以把这个嵌套循环改为:

for (int j = 1; j <= 40; j++)

另外,你的程序会计算47天的疫情数据,而在前40天的循环中,你的文件输出和变量计算都是以40天为标准的,所以在第41天到第47天的输出会有问题。

最后,在输出"总消耗数"时,你的程序会计算47天的方舱费用,但是每天的方舱费用是没有累加的,所以这个费用数字会过大。

#include<stdio.h>
#define population 3000000
#define cost_of_lock_down_population 60000000
#define ratio 3
#define shelter_size 1000
#define shelter_time 7
#define cost_of_shelter 200
int day[47] = { 1 };
int infection[47] = { 1 };
int shelter = 1;
long cost[47] = { 200 };
int ID[population] = { 1 };
int new[47] = { 1 };
long sum = 0;
int t[47] = { 1 };
int shelter_[cost_of_lock_down_population / shelter_size] = { 1 };
int main()
{
    FILE* fp1 = fopen("infected_day.txt", "a+");
    FILE* fp2 = fopen("cost_of_lock_down_population_day.txt", "a+");
    FILE* fp3 = fopen("shelter_day.txt", "a+");
    for (int i = 1; i <= 47; i++)
    {
        fprintf(fp1, "\n第%d天\t感染人数:%d\n感染的ID为:\t", day[i - 1], infection[i - 1]);
        fprintf(fp2, "第%d天\t方舱消耗金额:%ld\n", day[i-1],cost[i-1]);
        day[i] = day[i - 1] + 1;
        for (int j = 1; j <= 40; j++)
        {
            new[i] = new[i - 1] * ratio;
            infection[i] += new[i];
            for (int b = 0; b < new[i]; b++)
            {
                ID[t[i - 1] + b] = 1;
            }
            t[i] = new[i] + t[i - 1];
        }
        while (day[i] > shelter_time)
        {
            for (int b = 1; b <= new[i - 7]; b++)
            {
                ID[t[i - 7] - b] = 0;
            }
            infection[i] -= new[i - 7];
            if (infection[i] <= 0)
            {
                break;
            }
        }
        for (int b = 0; b < t[i]; b++)
        {
            if (ID[b] != 0)
            {
                fprintf(fp1, "%d\t", b+1);
                int n = 0;
                n++;
                if (n <= shelter_size)
                {
                    fprintf(fp3, "\n方舱%d\nID为%d\t", shelter, b + 1);
                }
                else
                {
                    shelter++;
                    n -= shelter_size;
                }
                shelter_[i] = shelter;
            }


我帮你改了部分代码,你试试

第28行~37行的那个while循环是死循环。while改成if后,数组实现的貌似没有bug了。
程序效率很低,但是不知道你要实现什么功能,不知道怎么改,你把功能说一下吧。

这个问题可能是因为您的循环条件有误,导致程序无法进入循环。例如,在第一个循环中,您使用了i < 40作为循环条件,但是在循环体内部又使用了i来计算新的感染人数。这可能导致程序无法进入循环。
另外,在链表部分,您在循环中没有进行任何链表操作,也没有定义链表变量,所以无法进行链表操作。
建议重新检查您的循环条件,并确保程序能够正确进入循环,并添加链表操作。

这仔细看了眼你贴的代码,发现题主你的代码行循环存在循环指针溢出/越界的问题,你可以着重检查下你那两个循环,并且你这些嵌套执行效果低下,你也可以贴下 你要实现的需求,或者我可以给你提供思路或者代码实现都行。

你贴出来的代码23行处的for循环的循环条件导致数组访问越界

    for (int i = 1; i <= 47; i++)

应为

    for (int i = 1; i <= 40 i++)

这个错误信息表明程序试图访问了不能访问的内存,这可能是因为程序中存在越界访问、野指针等问题。在这种情况下,需要检查代码中的数组、指针等变量的访问是否正确,并修复错误。另外,还需要注意编译选项和编译环境的影响,如果编译选项不当会导致编译出的程序存在隐藏问题。

检查文件路径是不是对的

检测下指针或循环越界问题吧

用的什么工具呢? 打断点调试下, 在异常的代码前一行打个断点, 看下变量值, 一般就能找到异常的原因

这个错误是数组超出了范围,指针越界了哇