标题:求助:一道编程题
只看楼主
欣欣20
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-12
 问题点数:0 回复次数:4 
求助:一道编程题

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "malloc.h"

#define LEN sizeof(struct Node)

int n;
float m=0;

struct Node
{
float numbers;
struct Node *next;
};

struct Node *creat(void)
{
struct Node *p1,*p2;
struct Node *head;
n=0;
p1=p2=(struct Node *)malloc(LEN);
scanf("%f",&p1->numbers);
head=NULL;
while(p1->numbers!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Node* )malloc(LEN);
scanf("%f",&p1->numbers);
}
p2->next=NULL;
return(head);
}

void print(struct Node *head)
{
struct Node *p;
printf("\nNow,These %d records are :\n",n);
p=head;
if(head!=NULL)
do
{
printf("%f\t",p->numbers);
p=p->next;
}
while(p!=NULL);

}

float mean(struct Node *head)
{
struct Node *p;
p=head;
if(head!=NULL)
do
{
m+=p->numbers;
p=p->next;
}while(p!=NULL);
return m/n;
}

float deviation(struct Node *head)
{
struct Node *p;
float q=0;
p=head;
if(head!=NULL)
do
{
q+=(p->numbers-m/n)*(p->numbers-m/n);
p=p->next;
}while(p!=NULL);
return sqrt(q/(n-1));
}

int main()
{
float x,y;
struct Node *a;
printf("Please input 10 float numbers,end with 0.\n");
a=creat();
print(a);
x=mean(a);
printf("\nThe mean of 10 float numbers is %f",x);
y=deviation(a);
printf("\nThe deviation of 10 float numbers is %f\n",y);
return 0;

}




调试的时候出现警告:
warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data

请问一下该怎样改啊?

搜索更多相关主题的帖子: 编程 include numbers 
2006-04-13 10:09
欣欣20
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-12
得分:0 
这题是要来计算平均值和标准方差的...
2006-04-13 10:11
名人
Rank: 1
等 级:新手上路
威 望:1
帖 子:205
专家分:0
注 册:2006-3-3
得分:0 
以下是引用欣欣20在2006-4-13 10:09:00的发言:

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "malloc.h"

#define LEN sizeof(struct Node)

int n;
float m=0;

struct Node
{
float numbers;
struct Node *next;
};

struct Node *creat(void)
{
struct Node *p1,*p2;
struct Node *head;
n=0;
p1=p2=(struct Node *)malloc(LEN);
scanf("%f",&p1->numbers);
head=NULL;
while(p1->numbers!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Node* )malloc(LEN);
scanf("%f",&p1->numbers);
}
p2->next=NULL;
return(head);
}

void print(struct Node *head)
{
struct Node *p;
printf("\nNow,These %d records are :\n",n);
p=head;
if(head!=NULL)
do
{
printf("%f\t",p->numbers);
p=p->next;
}
while(p!=NULL);

}

float mean(struct Node *head)
{
struct Node *p;
p=head;
if(head!=NULL)
do
{
m+=p->numbers;
p=p->next;
}while(p!=NULL);
return m/n;
}

float deviation(struct Node *head)
{
struct Node *p;
float q=0;
p=head;
if(head!=NULL)
do
{
q+=(p->numbers-m/n)*(p->numbers-m/n);
p=p->next;
}while(p!=NULL);
return (float)sqrt(q/(n-1));//类型转换一下
}

int main()
{
float x,y;
struct Node *a;
printf("Please input 10 float numbers,end with 0.\n");
a=creat();
print(a);
x=mean(a);
printf("\nThe mean of 10 float numbers is %f",x);
y=deviation(a);
printf("\nThe deviation of 10 float numbers is %f\n",y);
return 0;

}




调试的时候出现警告:
warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data

请问一下该怎样改啊?


未必钱多乐便多,财多累己招烦恼。 清贫乐道真自在,无牵无挂乐逍遥。
2006-04-13 10:42
欣欣20
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-12
得分:0 
2006-04-13 10:52
ot1122
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-4-20
得分:0 
return sqrt(q/(n-1));他返回的double类型的,而你的函数要得是float.所以出现warning.
2006-04-22 10:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-57202-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.348505 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved