函数的运行顺序与我输入的顺序不同

时间:2014-10-12 23:15:57

标签: c excel

首先感谢您花时间阅读这篇文章。我几乎已经找到了这个程序但是有一些我似乎无法动摇的问题。这是程序

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

    FILE *fp;
    char str[100];
    char* filename;
    char* filename = str;
    int timer;


struct Pumps
{
    char cityname[30];
    int volume;
    float priceperpump;
    float priceperactiv; /*150 dollars each time the pump turns on*/
    int activated;
    int hourInput;
    int hourOutput;
};

struct Pumps Pumps1;
struct Pumps Pumps2;
struct Pumps Pumps3;
struct Pumps Pumps4;

void functionInput(void)
{
    /*City 1*/
    strcpy(Pumps1.cityname, "Valdez");
    Pumps1.priceperactiv = 210;

    /*City 2*/
    strcpy(Pumps2.cityname, "Glenallen");
    Pumps2.priceperactiv = 210;

    /*City 3*/
    strcpy(Pumps3.cityname, "Tok");
    Pumps3.priceperactiv = 210;

    /*City 4*/
    strcpy(Pumps4.cityname, "Fairbanks");
    Pumps4.priceperactiv = 210;
}

int uniform_distribution_ranges(int rangeLow, int rangeHigh)
{
    double myRand = rand()/(1.0 + RAND_MAX);
    int range = rangeHigh - rangeLow +1;
    int myRand_Scaled = (myRand * range ) + rangeLow;
    return myRand_Scaled;
}


void volumefinal( struct Pumps pump );

int main(void)
{


    printf("\n Enter the filename :");

    gets(str);

    printf("\n Creating %s.csv file",str);

    filename=strcat(filename,".csv");

    functionInput();

    fp=fopen(filename,"w");

volumefinal( Pumps1 );
volumefinal( Pumps2 );
volumefinal( Pumps3 );
volumefinal( Pumps4 );

fclose(fp);

return 0;

}

void volumefinal( struct Pumps pump )
{

    fp=fopen(filename,"a");

    fprintf(fp,"%s,\t",pump.cityname);

    int rangeLow, rangeHigh;

    /* Define Volume, initial with uniform distribution */
    rangeLow=40;
    rangeHigh=80;

    pump.volume = uniform_distribution_ranges(rangeLow, rangeHigh);

    /*Define Input,  with uniform distribution */
    rangeLow=4;
    rangeHigh=10;

    pump.hourInput = uniform_distribution_ranges(rangeLow, rangeHigh);  

    /*Define Output,  with uniform distribution */
    rangeLow=15;
    rangeHigh=20;

    pump.hourOutput = uniform_distribution_ranges(rangeLow, rangeHigh); 

    srand ( time(0) );

    for(timer=0;timer<24;timer++)
    {

    /*Define input,with uniform distribution */

        pump.activated = (rand()% 2);   
        if(pump.volume < 45)
        {
            pump.activated = 0;
            pump.volume = pump.volume + pump.hourInput;
        }

        else
        {
            pump.activated = 1;
            pump.volume = pump.volume + pump.hourInput - pump.hourOutput;
        }

        fprintf(fp,"%d,\t",pump.activated);
        fprintf(fp,"%d,\t",pump.volume);

    }
    fprintf(fp,"\n");
}

因此程序应该模拟泵操作24小时,增量为1小时,记录泵每小时是否开启及其产生的体积。每个城市及其入口将位于不同的行,订单应为Valdez Glenallen Tok和Fairbanks。然而,当我运行程序时,从上到下的顺序变为Fairbanks Valdez Glenallen和Tok。更令人烦恼的是我可以改变顺序了 volumefinal(Pumps1),volumefinal(Pumps2),volumefinal(Pumps3),volumefinal(Pumps4) 至 volumefinal(Pumps4),volumefinal(Pumps3),volumefinal(Pumps2),volumefinal(Pumps1)

在excel表上,该市的订单仍然是Fairbanks Valdez Glenallen和Tok。此外,即使它们应具有随机值,最后两行似乎也是相同的。老实说,我无法看到我所缺少的任何帮助将不胜感激。谢谢你的时间。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您每次尝试重新打开文件:

void volumefinal( struct Pumps pump )

如果删除该调用,则可以正常工作:

    void volumefinal( struct Pumps pump )
    {
        // fp=fopen(filename,"a"); <----

        fprintf(fp,"%s,\t",pump.cityname);

        int rangeLow, rangeHigh;
        // ..
    }