2014-02-07 2 views
1

코드 세그먼트 을 사용하여 평균 및 표준 편차를 계산하려고합니다. 컴파일 오류가 발생하지 않습니다. 내가 .txt 파일을 사용하여 실행하면 출력 이 방법은 다음과 나타납니다 : 잘못된 요약과평균 및 표준 편차에 대한 계산이 잘못됨

" 
"is not a valid integer 

, 평균과 표준 편차도 Can't input double values.

int main (int argc, char *argv[]) 
{ 

     if(buffer == NULL) 
     { 

     } 

     double result = fread(buffer,1,lSize,file); 
     if(result != lSize){ 

     } 
     else{ 

     char *str = strtok(buffer," ,"); 

     int count = 0; 
     while(str != NULL){ 
      double val = (int) strtol(str,&str,10); 

      if(*str != ' ' && *str != '\0') 
      { 


      } 
      else{ 

      if(count == 0) 
      { 

       root = malloc(sizeof(struct node)); 
       root->next = NULL; 
       root->value = val; 
       current = root; 
       count++; 

      } 
      else{ 

       double x = createNode(current,val);  
      } 
      } 

      str = strtok(NULL," ,"); 
     } 

     } 
     current = root; 
     printf("Sum is %lf and link count is %lf\n", 
      getSummation(current), getNodeCount(current)); 

     double mean = getMean(getSummation(current),getNodeCount(current)); 
     double stdev = getStandardDeviation(root, mean,getNodeCount(current)); 



     fclose(file); 
     free(buffer); 
    } 
    } 
    return 0; 
} 


int createNode(struct node *n,double val) 
{ 
    if(n != NULL){ 
    while(n->next != NULL) 
    { 
     n = n->next; 
    } 
    n->next = malloc(sizeof(struct node)); 
    n = n -> next; 
    n -> value = val; 
    n -> next = NULL; 
    } 
    return 1; 
} 

int getSummation(struct node *element){ 
    double sum = 0.0f; 
    if(element != NULL) 
    { 
    while(element != NULL) 
    { 
     sum = sum + element->value; 
     element= element->next; 
    } 
    } 
    else 
    { 
    printf("Null Point Exception thrown"); 
    } 
    return sum; 
} 

int getNodeCount(struct node *link) 
{ 
    int count = 0; 

    while(link != NULL) 
    { 

    } 

    return count; 
} 

double getMean(double summation,int numberOfNodes) 
{ 

} 

double getStandardDeviation(struct node *link, double mean,int linkCount) 
{ 


    while(link != NULL) 
    { 

    } 

    stdev = sqrt((diffrenceSummation)/(linkCount - 1)); 
    return stdev; 
} 
+1

가능성이 파일로 끝나는 main()

//double getSummation(struct node*element); int getSummation(struct node *element); int createNode(struct node *n,double val); int getNodeCount(struct node *link); #include <math.h> 

2)를 사용하여 일관된 형식 지정

//printf("Summation is %lf and link count is %lf\n", // getSummation(current), getNodeCount(current)); printf("Summation is %d and link count is %d\n", getSummation(current), getNodeCount(current)); 

3) 각종 주물이 의심되기 전에 프로토 타입을 추가 '\ n'은 실패합니다. 'if (* str! = ''&& * str! = '\ 0')'. 모든 공백을 허용하도록 토큰 화와'if()'를 변경하십시오. – chux

+0

다른 문제들 중에'getStandardDeviation()'은'return' 문을 가지고 있지 않습니다. 컴파일러에서 경고를 보내지 않으면 컴파일러 옵션을 수정하여 문제에 대한 기본적인 경고를 얻거나 더 나은 컴파일러를 얻어야합니다. GCC를 사용한다면 적어도'gcc -Wall'을 사용하십시오; 바람직하게는'gcc -Wall -Wextra' (그리고 나는 그것보다 더 엄격한 경고를 사용한다). –

+0

너무 오래. 너무 길어. 또한, 당신은 불필요하고 못생긴 몇 가지 캐스팅을 가지고 있습니다. '(char *) malloc'이나'(int) strtol'도 필요 없습니다. –

답변

0

1)

// mean = (double)summation/(double)numberOfNodes; 
mean = summation/numberOfNodes; 
// double linkValue = (double) link->value; 
double linkValue = link->value; 
// buffer = (char*) malloc(sizeof(char)*lSize); 
buffer = malloc(lSize); 
// double val = (int) strtol(str,&str,10); 
double val = strtol(str,&str,10); 
+0

코드 세그먼트에 따라 변경했는데 올바른 정수가 아닙니다. – user3283912

+0

일단 유효한 정수로 표시된 10 개의 실수를 입력하고 링크 수를 9로 입력하면 합계가 계산됩니다. 9 개의 숫자 만 추가 할 수 있습니다. 이중 값을 입력 할 수 없습니다. 누구든지 도움을 줄 수 있습니다. – user3283912

+0

이 값으로 텍스트 파일에 추가 한 값 160 591 114 229 230 270 128 1657 624 1503 – user3283912

관련 문제