我编写了一个c脚本,为另一个应用程序生成一个文本文件。浮点数'b'得到一个非常高的值,我不知道为什么。我有一个几乎相同的文件,其中不会发生这种情况。我是C的新手,所以任何帮助都会受到赞赏 - 我很欣赏这可能是一个非常简单的错误。代码如下。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define LARGESTR 1000
#define INITIALTIME 40
void clear_string(char *s,int l)
{
int i;
for(i=0; i<l; i++)
s[i]='\0';
}
int main(void)
{
char buffer[LARGESTR];
int ramp_flow = 5, car_count_seg1 = 45, car_count_seg2 = 45, car_count_seg3 = 45, fw_upstream_queue = 20, onramp_queue = 60, a;
float outflow_count, percentout = 0.7, b;
//Total Input to system
for(a=(INITIALTIME + 10); a<(INITIALTIME + 250); a+=10) {
if((a>=10) && (a<600)) {
b=b+1.39+0.556;
}
if((a>=600) && (a<1200)) {
b=b+(((a*9.167)-5000)/360)+(((a*1.67)-800)/360);
}
if((a>=1200) && (a<2700)) {
b=b+16.67+3.33;
}
if((a>=2700) && (a<3300)) {
b=b+(((a*(-10))+33000)/360)+(((a*(-2))+6600)/360);
}
if((a>=3300) && (a<4800)) {
}
}
printf("%f\n", b);
b = b + car_count_seg1 + car_count_seg2 + car_count_seg3 + fw_upstream_queue + onramp_queue;
printf("%f\n", b);
outflow_count = b*percentout;
printf("%f\n", outflow_count);
FILE *fp;
fp=fopen("1rampproblem.pddl","w+");
clear_string(buffer,LARGESTR);
sprintf(buffer,"\n(define (problem trafficjuncprob)\n(:domain traffic-junction)\n(:objects)\n(:init");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (outflow-count) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (critical-density) 45)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maximum-density) 270)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maximum-flow) 20)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (time) %d)",INITIALTIME);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (zero) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (toggle) 1)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (count) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (ramp-flow) %d)",ramp_flow);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (ramp-flow-realised) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (car-count-seg1) %d)",car_count_seg1);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (car-count-seg2) %d)",car_count_seg2);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (car-count-seg3) %d)",car_count_seg3);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maxflow-0-1) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maxflow-1-2) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maxflow-2-3) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (maxflow-3-4) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (flow-0-1) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (flow-1-2) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (flow-2-3) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (flow-3-4) 0)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (fw-upstream-queue) %d)",fw_upstream_queue);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(= (onramp-queue) %d)",onramp_queue);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n)\n\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(:goal (> (outflow-count) %f)\n)\n", outflow_count);
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
clear_string(buffer,LARGESTR);
sprintf(buffer,"(:metric minimize (total-time))\n)");
fprintf(fp,"%s",buffer);
fprintf(fp,"\n");
fclose(fp);
return 0;
}
答案 0 :(得分:4)
b
声明中没有初始化程序:
float outflow_count, percentout = 0.7, b;
所以它在第一次使用之前的值是通过以前使用该单词而在堆栈中留下的任何东西。每个后续值都取决于其初始值,因此没有预期具有任何特定值的基础。
我建议初始化它。
其余部分是关于如何提问的提示。你应该做的第一件事就是删除程序中不需要重现bug的所有内容。由于在第一个循环之后打印b
时出现问题,我之后删除了所有内容,然后删除了因此未引用的声明:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define LARGESTR 1000
#define INITIALTIME 40
int main(void)
{
int a;
float b;
//Total Input to system
for(a=(INITIALTIME + 10); a<(INITIALTIME + 250); a+=10) {
if((a>=10) && (a<600)) {
b=b+1.39+0.556;
}
if((a>=600) && (a<1200)) {
b=b+(((a*9.167)-5000)/360)+(((a*1.67)-800)/360);
}
if((a>=1200) && (a<2700)) {
b=b+16.67+3.33;
}
if((a>=2700) && (a<3300)) {
b=b+(((a*(-10))+33000)/360)+(((a*(-2))+6600)/360);
}
if((a>=3300) && (a<4800)) {
}
}
printf("%f\n", b);
return 0;
}
在这个精简程序中,问题更加明显。
您的问题报告应该包含一个精简程序,其中只包含相关代码,b
的预期值,您预期的原因以及您实际获得的内容。