所以这是我的代码,它读取糖尿病 csv 文件并计算平均省份糖尿病。当我在 Geany 中执行时,没有显示任何内容。这是为什么?
csv 文件是链接https://docs.google.com/spreadsheets/d/1lGwmjMVhZV3jKdEQ-Fekqci-57GezlNYpRUvX1TMbHY/edit#gid=0
// Include libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototypes
void getData(FILE* fp, double* provincialAverages, double* nationalAverage, double* yearlyAverages, double* ageGroupAverages);
void calculateAverages(double* provincialAverages, double* nationalAverage, double* yearlyAverages, double* ageGroupAverages);
int main(){
// Create file pointer
FILE *fp;
// Create arrays for the provincial, national, yearly, and age group averages
double provincialAverages[4];
double nationalAverage;
double yearlyAverages[35];
double ageGroupAverages[9];
// Open the file
fp = fopen("statscan_diabetes.csv", "r");
if (fp == NULL){
printf("Error opening file.\n");
return 1;
}
// Call the functions
getData(fp, provincialAverages, &nationalAverage, yearlyAverages, ageGroupAverages);
calculateAverages(provincialAverages, &nationalAverage, yearlyAverages, ageGroupAverages);
// Close the file
fclose(fp);
return 0;
}
// Function to read the data from the file and store it in the arrays
void getData(FILE *fp, double *provincialAverages, double *nationalAverage, double *yearlyAverages, double *ageGroupAverages){
// Initialize variables
char line[200];
char *token;
double sum, count;
int i, j;
// Read through the file line by line
while (fgets(line, sizeof(line), fp) != NULL){
// Tokenize each line
token = strtok(line, ",");
// Initialize the sum and count to 0
sum = 0;
count = 0;
// Read through each token
while (token != NULL){
// Skip the first 4 tokens
if (i < 4){
token = strtok(NULL, ",");
i++;
}
// Get the province, age group, and value
else if (i == 4){
// Check the province
if (strcmp(token, "Ontario") == 0){
j = 0;
}
else if (strcmp(token, "Quebec") == 0){
j = 1;
}
else if (strcmp(token, "British Columbia") == 0){
j = 2;
}
else if (strcmp(token, "Alberta") == 0){
j = 3;
}
else if (strcmp(token, "Canada excluding territories") == 0){
j = 4;
}
token = strtok(NULL, ",");
i++;
}
// Get the year
else if (i == 5){
if (j < 4){
// Add the value to the yearly average array
yearlyAverages[(int)atof(token)-2015+(j*7)] += atof(strtok(NULL, ","));
}
else{
// Add the value to the national average array
nationalAverage[(int)atof(token)-2015] += atof(strtok(NULL, ","));
}
token = strtok(NULL, ",");
i++;
}
// Get the age group and value
else if (i == 6){
// Add the value to the age group average array
if (strcmp(token, "35 to 49 years") == 0){
ageGroupAverages[j*3] += atof(strtok(NULL, ","));
}
else if (strcmp(token, "50 to 64 years") == 0){
ageGroupAverages[j*3+1] += atof(strtok(NULL, ","));
}
else if (strcmp(token, "65 years and over") == 0){
ageGroupAverages[j*3+2] += atof(strtok(NULL, ","));
}
// Add the value to the provincial average array
if (j < 4){
sum += atof(strtok(NULL, ","));
count++;
}
break;
}
}
// Calculate the average for the provincial array
if (j < 4){
provincialAverages[j] = sum/count;
}
// Reset the counter
i = 0;
}
}
// Function to calculate the averages
void calculateAverages(double *provincialAverages, double *nationalAverage, double *yearlyAverages, double *ageGroupAverages){
// Calculate the averages
printf("Provincial Averages:\n");
printf("Ontario: %.2f%%\n", provincialAverages[0]);
printf("Quebec: %.2f%%\n", provincialAverages[1]);
printf("British Columbia: %.2f%%\n", provincialAverages[2]);
printf("Alberta: %.2f%%\n\n", provincialAverages[3]);
printf("National Average: %.2f%%\n\n", *nationalAverage);
printf("Yearly Averages:\n");
for (int i = 0; i < 35; i++){
printf("%d: %.2f%%\n", i+2015, yearlyAverages[i]);
}
printf("\n");
printf("Age Group Averages:\n");
printf("35 to 49 years: %.2f%%\n", ageGroupAverages[0]);
printf("50 to 64 years: %.2f%%\n", ageGroupAverages[1]);
printf("65 years and over: %.2f%%\n", ageGroupAverages[2]);
printf("\n");
}
我试图弄清楚哪里出了问题,但找不到。