코드 세그먼트 을 사용하여 평균 및 표준 편차를 계산하려고합니다. 컴파일 오류가 발생하지 않습니다. 내가 .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;
}
가능성이 파일로 끝나는
main()
2)를 사용하여 일관된 형식 지정
3) 각종 주물이 의심되기 전에 프로토 타입을 추가 '\ n'은 실패합니다. 'if (* str! = ''&& * str! = '\ 0')'. 모든 공백을 허용하도록 토큰 화와'if()'를 변경하십시오. – chux
다른 문제들 중에'getStandardDeviation()'은'return' 문을 가지고 있지 않습니다. 컴파일러에서 경고를 보내지 않으면 컴파일러 옵션을 수정하여 문제에 대한 기본적인 경고를 얻거나 더 나은 컴파일러를 얻어야합니다. GCC를 사용한다면 적어도'gcc -Wall'을 사용하십시오; 바람직하게는'gcc -Wall -Wextra' (그리고 나는 그것보다 더 엄격한 경고를 사용한다). –
너무 오래. 너무 길어. 또한, 당신은 불필요하고 못생긴 몇 가지 캐스팅을 가지고 있습니다. '(char *) malloc'이나'(int) strtol'도 필요 없습니다. –