C++问题,帮帮忙吧好人一生平安。

2024-12-21 23:45:42
推荐回答(1个)
回答1:

int main()
{
FILE *fp;
int pos=0,neg=0,zero=0;
int psum=0,nsum=0,num;
fp=fopen("number.dat" , "r" );
if ( fp==NULL )
{
printf("open file error\n");
return -1;
}
while(!feof(fp))
{
if ( fscanf(fp,"%d" , &num)!=1 )
break ;
if ( num > 0 )
{
pos++;
psum+=num ;
}
else if ( num < 0 )
{
neg++;
nsum+=num ;
}
else
zero++;
}
fclose(fp);
printf("pos:%d sum:%d\n" , pos , psum );
printf("neg:%d sum:%d\n" , neg , nsum );
printf("zero:%d\n" , zero );
return 0;
}