我试图获得最高临时低温时遇到麻烦,平均而言

时间:2015-12-07 21:22:17

标签: c

我遇到了麻烦,为此获得正确的输出 功能

void getStats(int month, const struct DailyData yearData[],int sz,struct MonthlyStatistic* monthly)

有人可以提供帮助吗?这是我的任务。我只需要有人指出我正确的方向。

Your function must find minimum temperature, the maximum temperature, the
average temperature and total precipitation for a given month using the 
data in the array with a matching month. The results are passed back to 
the calling function by storing them into the appropriate data members of 
monthStats.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*V 1.1:
Changes:

1) declared i outside of loop, you don't need -std=c99 to compile anymore
2) added a check for existence of historicaldata.csv
*/

/*an instance of this struct holds the weather data for a single day*/
struct DailyData{
    int day;
    int month;
    int year;
    float high;
    float low;
    float precipitation;
    char condition;
};

/*an instance of this struct holds summary information for the weather
for a given month*/
struct MonthlyStatistic{
    float minTemperature;
    float maxTemperature;
    float averageTemperature;
    float totalPrecipitation;
};
int readDailyData(FILE* fp, struct DailyData allData[]);

int getRelevantRecords(int yearWanted, const struct DailyData allData[], int sz,  struct DailyData yearData[]);
void sortYearData(struct DailyData yearData[], int sz);
void getStats(int month, const struct DailyData yearData[], int sz, struct MonthlyStatistic* monthStats);
void printMonthlyStatistic(int month, const struct MonthlyStatistic* monthly);
void graphLine(int month, const struct MonthlyStatistic* monthly);
void printVerbose(const struct DailyData yearData[],int sz);
char symbolToDraw(char condition, float avgTemp);
float average(float first, float second);
void draw( char c, int num );


int main(void){
    FILE* fp = fopen("historicaldata.csv","r");
    if(!fp){
        printf("you need to put historicaldata.csv into this directory\n");
        exit(0);
    }
    struct DailyData  allData[3000];
    struct DailyData yearData[366];
    int numTotalRecords;

    numTotalRecords = readDailyData(fp, allData);
    int year;
    int reportType;
    int i;
    struct MonthlyStatistic monthly[12];
    printf("Please enter the year for the report: ");
    scanf("%d",&year);
    printf("Please enter the type of report you wish to generate:\n");
    printf("1) summary\n");
    printf("2) detailed\n");
    scanf("%d",&reportType);
    int numDays = getRelevantRecords(year,allData,numTotalRecords,yearData);
    sortYearData(yearData,numDays);

    for(i=0;i<12;i++){
        getStats(i+1,yearData,numDays,&monthly[i]);
    }

    printf("Weather summary for %d\n",year);
    printf("|   Month   | High  |  Low  |  Avg  | Precip  |\n");
    printf("|-----------|-------|-------|-------|---------|\n");
    for(i=0;i<12;i++){
        printMonthlyStatistic(i+1,&monthly[i]);
    }
    printf("\n\n");
    printf("Precipitation Graph\n\n");
    for (i=0;i<12;i++){
        graphLine(i+1,&monthly[i]);
    }
    if(reportType == 2){
        printf("\n\n");
        printf("Detailed report:\n");
        printVerbose(yearData,numDays);
    }
    return 0;
}
int readDailyData(FILE* fp, struct DailyData allData[]){
    int i=0;
    while(fscanf(fp,"%d,%d,%d,%f,%f,%f,%c\n",
        &allData[i].year,&allData[i].month,&allData[i].day,
        &allData[i].high,&allData[i].low,&allData[i].precipitation,
        &allData[i].condition) == 7){
        i++;
    }
    return i;
}
int getRelevantRecords(int yearWanted, const struct DailyData allData[], int sz,  struct DailyData yearData[]){
int i;
int j=0;
    for(i = 0; i < sz; i++){ //here is my for loop incrimenting i there are 3000 files

        if(allData[i].year == yearWanted){
                //copying a record to another struct and if the year wanted matches and incimenting a variable to count how many times it happened
         yearData[i] = allData[i];
         j++;
        }
    }
    return j;
}
void sortYearData(struct DailyData yearData[], int sz){
    int i;
    int temp;
    int temp1;
    for(i = 0; i < sz ; i++){
        if( yearData[i].month > yearData[i+1].month){
            temp1=yearData[i].month;
            yearData[i].month=yearData[i+1].month;
            yearData[i+1].month=temp1;
        }else if(yearData[i].month = yearData[i+1].month && yearData[i].day > yearData[i+1].day){
            temp=yearData[i].day;
            yearData[i+1].day=yearData[i].day;
            yearData[i+1].day=temp;
        }
    }
}
void getStats(int month, const struct DailyData yearData[],int sz,struct MonthlyStatistic* monthly){
    float runningTotal = 0;
    int totalItems = 0;
    float highTemp = 0;
    float lowTemp = 0;
    float temp;
    int i;
    for(i = 0; i < sz; i++){
        if(month = yearData[i].month){
            runningTotal += (yearData[i].high + yearData[i].low / 2);
            totalItems ++;
    }
    highTemp+=yearData[i+1].high;
    lowTemp+=yearData[i].low;
}
monthly->averageTemperature = runningTotal / totalItems;
monthly->maxTemperature = highTemp;
monthly->minTemperature = lowTemp;

}
void printMonthlyStatistic(int month,const struct MonthlyStatistic* monthly){
    if(month == 1){
        printf("|%10January|%10.2f| %.2f | %.2f  |  %.2f   |\n", monthly->maxTemperature, monthly->minTemperature, monthly->averageTemperature, monthly->totalPrecipitation);
    }if(month == 2){
        printf("|   Febuary |\n");
    }
}
void graphLine(int month,const struct MonthlyStatistic* monthly){
    //put your code here
 //  printf(" %d | %d\n", month ,monthly->totalPrecipitation);
}
void printVerbose(const struct DailyData allData[],int sz){
    //put your code here
}
char symbolToDraw(char condition, float avgTemp){
//this function was made so that if the condition is equal to any of these
    char c = '~';
    char p = ';';
    char s = '@';
    char l = '*';
    if(avgTemp < 0 && condition == 'p'){
       condition = '*';
    }else if (condition == 'p'){
        condition = ';';
    }else if( condition == 's'){
        condition = '@';
    }else if( condition == 'c'){
        condition = '~';
    }
return condition;
}
float average(float first, float second){
    float avg;
    avg = (first + second)/2;
    return avg;// this function is for average it takes the first paramater and the second and adds it together then divides by the total of paramters to return a value
}
void draw( char c, int num ){
    int i;//This function is for drying the amount of symbols needed
    for(i=0; i < num; i++){
        printf("%c", c);
        }
}

1 个答案:

答案 0 :(得分:1)

错误计算平均值,最小值和最大值。更正如下。

void getStats(int month, const struct DailyData yearData[],int sz,
    struct MonthlyStatistic* monthly){
  float runningTotal = 0;
  int totalItems = 0;

  float highTemp = -FLT_MAX;
  float lowTemp = FLT_MAX;

  // float temp;
  int i;
  for(i = 0; i < sz; i++){
    // if(month = yearData[i].month){
    if(month == yearData[i].month){
        // runningTotal += (yearData[i].high + yearData[i].low / 2);
        runningTotal += (yearData[i].high + yearData[i].low) / 2;
        totalItems ++;
    }

    // highTemp+=yearData[i+1].high;
    if (TempyearData[i].high > high) high = TempyearData[i].high;
    // lowTemp+=yearData[i].low;
    if (TempyearData[i].low < low) low = TempyearData[i].low;
  }
  ...