Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过链表循环-无止境循环_C_Arrays_List_Loops_Linked List - Fatal编程技术网

C 通过链表循环-无止境循环

C 通过链表循环-无止境循环,c,arrays,list,loops,linked-list,C,Arrays,List,Loops,Linked List,自学编程,我决定做一个简单的瓦特计算器。每个州有不同的(平均)电价/KWh。我的问题从代码中的第34行开始。curr->next从不为空 我做错了什么 #include<stdio.h> #include<stdlib.h> #include<string.h> float calculate(float w, float cents, float h); struct states { char *name; char *price;

自学编程,我决定做一个简单的瓦特计算器。每个州有不同的(平均)电价/KWh。我的问题从代码中的第34行开始。curr->next从不为空

我做错了什么

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


float calculate(float w, float cents, float h);

struct states 
{
    char *name;
    char *price;
    struct states *next;
};

int main()
{
    float watts,hours,cents;
    int a = 0;
    struct states *curr, dict[52];
    char st_ab[52][3] = {"ID","ND","WA","AR","UT","LA","WY","SD","NE","KY","WV","OK","OR","MT","MS","TN","IN","NC","MS","IA","KS","VA","NM","MN","SC","AZ","GA","AL","CO","OH","TX","IL","FL","NV","MI","WI","PA","MD","DC","DE","MA","RI","CA","ME","VT","NJ","NH","AK","CT","NY","HI"};
    char st_pr[52][5] = {"8.0","8.1","8.2","8.7","8.8","8.9","8.9","9.0","9.0","9.0","9.2","9.2","9.4","9.6","9.7","9.8","10.0","10.2","10.3","10.5","10.5","10.5","10.7","10.9","11.0","11.1","11.1","11.1","11.2","11.2","11.3","11.7","11.7","11.7","13.0","13.0","13.2","13.7","13.7","13.7","14.8","14.9","15.2","15.5","16.1","16.3","16.5","17.5","18.1","18.1","33.2"};
    char state[2];

    for(a=0;a<=52;a++)
    {
        memset(&dict[a],0,sizeof(struct states)); /* zero out structure */
        if(a==52)
        {
            break;  /* if this is the last struct, leave it NULL so we can loop through linked list */
        }
        dict[a].next=&dict[a+1];        
    }

    for(curr = dict;curr ->next != NULL;curr = curr->next)  /* fill our list with state abbr. and cost/KWh */
    {
        curr->name = st_ab[a];
        curr->price = st_pr[a];
        a++;
    }

    puts("Dictionary loaded!");
    printf(" Two letter state code: ");                 /* get info from user */
    scanf("%s",state);

    printf(" Wattage of appliance: ");
    scanf("%f",&watts);

    printf(" Hours of use per day: ");
    scanf("%f",&hours);

    for(curr = dict; curr->next != NULL; curr=curr->next)
    {
        printf("comparing %s with %s\n", curr->name,state);  /* search for our state, set cents respectively */
        if((strcmp(curr->name,state)) == 0)
        {
            printf("State found! -- %s\n",state);
            cents=atof(curr->price);
            printf("%f cents\n",cents);
            break;
        } else {
            printf("state didn't match\n");
            continue;
        }
    }

    printf("\nAverage cost per day: %.2f\n",  calculate(watts,cents,hours));
    printf("Average cost per year: %.2f\n\n", (calculate(watts,cents,hours)) * 365);    

    return 0;

}

float calculate(float w, float c, float h)
{   
    float kwh = (w/1000) * h;
    float cos = kwh*(c/100);
    return(cos);
}
#包括
#包括
#包括
浮动计算(浮动w、浮动美分、浮动h);
结构状态
{
字符*名称;
煤焦*价格;
结构状态*下一步;
};
int main()
{
浮动瓦特,小时,美分;
int a=0;
结构状态*curr,dict[52];
char st_ab[52][3]={“ID”、“ND”、“WA”、“AR”、“UT”、“LA”、“WY”、“SD”、“NE”、“KY”、“WV”、“OK”、“OR”、“MT”、“MS”、“TN”、“IN”、“NC”、“MS”、“IA”、“KS”、“VA”、“NM”、“MN”、“SC”、“AZ”、“GA”、“AL”、“CO”、“OH”、“TX”、“IL”、“FL”、“NV”、“MI”、“WI”、“PA”、“DC”、“DE”、“MA”、“RI”、“CA”、“ME”、“VT”、“NJ”、“NH”、“AK”、“CT”、“NY”、“HI”};
[52][5][5][5][[5][[5][[5][5]{{{“8.0”、“8.0”、“8.0”、“8.8”、“8.9”、“8.9”、“8.0”、“8.0”、“8.0”、“8.0”、“8.0”、“8.0”、“8.1”、“8”、“8.0”、“8”、“8”、“8.1”、“8”、“8”、“8”、“8”、“8”、“8”、“8.7”、“8”、“8”、“8”、“8”、“8”、“8”、“8”、“8”、“8”、“8.9”、“8”、“8”、“8”、“8”、“8.9”、“8”、“8”、“8.9”、“8”、“8”、“8”、“8”、“8”、“8.9”、“8”、“8”、“8.9”、“8”、“8”、“8”、“8”、“8.9”、“8”、“8”、“8”、“8”、“8”、“8”、“8.9”、“8.9”、“8”、“8”、“8”、“8”、“8”、“8.9”、“8.9”、“8”、“8”、“8”、“8”、“8”、“8”、“8”、“第16.1条、第16.3条、第16.5条、第17.5条、第18.1条、,"18.1","33.2"};
半焦态[2];
对于(a=0;anext!=NULL;curr=curr->next)/*用状态缩写和成本/KWh填写我们的列表*/
{
curr->name=st_ab[a];
货币->价格=st_pr[a];
a++;
}
puts(“已加载词典!”);
printf(“两个字母的状态代码:”);/*从用户处获取信息*/
scanf(“%s”,状态);
printf(“设备瓦数:”);
scanf(“%f”,瓦特);
printf(“每天使用小时数:”);
scanf(“%f”、&hours);
for(curr=dict;curr->next!=NULL;curr=curr->next)
{
printf(“比较%s和%s\n”,curr->name,state);/*搜索我们的状态,分别设置美分*/
如果((strcmp(curr->name,state))==0)
{
printf(“找到状态!--%s\n”,状态);
美分=atof(货币->价格);
printf(“%f分\n”,分);
打破
}否则{
printf(“状态不匹配\n”);
继续;
}
}
printf(“\n每天平均成本:%.2f\n”,计算(瓦特、美分、小时);
printf(“每年平均成本:%.2f\n\n”,(计算(瓦特、美分、小时))*365);
返回0;
}
浮点数计算(浮点数w、浮点数c、浮点数h)
{   
浮充千瓦时=(w/1000)*小时;
浮动cos=千瓦时*(c/100);
返回(cos);
}
“curr->next从不为null”-那么,如果停止条件为curr->next等于null,您希望for循环如何停止?

问题在于

dict[a].next = &dict[a+1];
C标准保证
&dict[a+1]
永远不会为NULL,即使它超过了数组的末尾。这会导致下一个循环超过列表的末尾,并产生未定义的行为(程序可能会执行任何操作)。在第一个循环之后,执行

dict[51].next = NULL;
以结束链接列表


编辑:使用
for(a=0;a
for(a=0;a)进行快速响应真是太棒了!经过其他一些小调整后,它现在工作得非常好!