Re: [C]非常郁闷的一个问题
- From: "李磊" <lilei1008@xxxxxxxxx>
- To: ghostunix@xxxxxxxxxxxxx
- Date: Thu, 29 May 2008 13:30:32 +0800
2008/5/29 Thomas X. Iverson <txi@xxxxxxxxxxxxx>:
> 在做一道指针的习题,要求输入任意多个浮点数进行平均数的计算,动态分配内存
> 书上的代码如下:
> /* Exercise 7.1 Calculating a floating-point average using pointers */
> /*********************************************************************
> * In this solution we allocate a some memory and when it is full *
> * allocate a new, larger amount of memory and copy the contents of *
> * the old memory to the new. We then free the old memory. This *
> * process repeats as often as necessary. *
> *********************************************************************/
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> int main(void)
> {
> double *values = NULL; /* Pointer to memory holding data values
> */
> double *temp = NULL; /* Pointer to newly allocated memory
> */
> double sum = 0.0; /* Sum of values
> */
> int capacity = 0; /* Maximum number of values that can be stored
> */
> int increment = 5; /* Capacity increment for dynamic allocation
> */
> int count = 0; /* Number of values read
> */
> int i = 0; /* Index to array
> */
> char answer = 'n';
>
> do
> {
> if(count == capacity) /* Check if there is spare memory */
> {
> capacity += increment; /* Increase the capacity of memory by increment
> */
> temp = (double*)malloc((capacity)*sizeof(double)); /* and allocate it
> */
> if(temp == NULL) /* If memory was not allocated */
> { /* Output a message and end */
> printf("Memory allocation failed. Terminating program.");
> exit(1);
> }
> if(values == NULL) /* Are there any values? */
> values = temp; /* No - so just copy address of new memory */
> else /* Yes - so copy data from old to new */
> {
> for(i = 0 ; i<count ; i++)
> *(temp+i) = *(values+i);
> free(values); /* Free the old memory */
> values = temp; /* Copy address of new */
> }
> temp = NULL; /* Reset pointer */
> }
>
> printf("Enter a value: ");
> scanf("%lf", values+count++);
>
> printf("Do you want to enter another(y or n)? ");
> scanf(" %c", &answer);
> }while(tolower(answer) == 'y');
>
> /* Now sum the values */
> for(i = 0 ; i<count ; i++)
> sum += *(values+i);
>
> /* Output the average */
> printf("\n The average of the the values you entered is %.2lf.\n",
> sum/count);
> free(values); /* We are done - so free the memory */
> }
>
> 在理解了这个算法后,我自己重新实现了一遍(算是默写性质的复习):
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<ctype.h>
>
> int main(void)
> {
> double *temp=NULL;
> double *value=NULL;
> double sum=0.0;
> int i;
> int count=0;
> int capa=0;
> int incre=5;
> char answer='n';
>
> do
> {
> if(count==capa)
> {
> capa+=incre;
> temp=(double *)malloc((capa)*sizeof(double));
> if(temp==NULL)
> {
> printf("Failed\n");
> return 1;
> }
> if(value==NULL)
> value=temp;
> else
> {
> for(i=0;i<count;i++)
> {
> *(temp+i)=*(value+i);
> }
> free(value);
> value=temp;
> }
> temp=NULL;
> }
> printf("Enter a value:");
> scanf("%lf",value+count++);
> printf("Enter another?:");
> scanf(" %c",&answer);
> }while(tolower(answer)=='y');
>
> for(i=0;i<count;i++);
狂晕.............检查下这句.......
>
> {
> sum+=*(value+i);
> }
> printf("%.2lf",sum/count);
> free(value);
> }
>
> 问题出现了,这两段代码我怎么看都没有觉得有问题,可是就是有两个问题:
> 1.我自己写的这个代码无法很好的处理scanf输入缓冲问题
> 2.我自己写的这个代码计算出的平均值总是0,我后来加了个printf查看sum和count的值,sum总是为0,count没有问题,不知道为什么
>
>
> --
> Keep It Simple Stupid
> http://blog.ghostunix.org
> ghosTM55
>
>
- Follow-Ups:
- Re: [C]非常郁闷的一个问题
- From: Ben Ben
- Re: [C]非常郁闷的一个问题
- From: Thomas X. Iverson
- References:
- [C]非常郁闷的一个问题
- From: Thomas X. Iverson
Other related posts:
- » Re: [C]逻辑移位与算术移位的问题
- » Re: [C]逻辑移位与算术移位的问题
- » Re: [C]逻辑移位与算术移位的问题
- » Re: [C]逻辑移位与算术移位的问题
- » Re: [C]逻辑移位与算术移位的问题
- » Re: [C]逻辑移位与算术移位的问题
- » C语言里的算术移位和逻辑移位运算例程
- » Re: [C]关于指针的问题
- » Re: [C]关于指针的问题
- » Re: [C]关于指针的问题
- » Re: [C]关于指针的问题
- » Re: [C]关于指针的问题
- » Re: [C]关于指针的问题
- » Re: [C]非常郁闷的一个问题
- » Re: [C]非常郁闷的一个问题
- » Re: [C]非常郁闷的一个问题
- » Re: [C]非常郁闷的一个问题
- » Re: [C]非常郁闷的一个问题
- » 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: [C]非常郁闷的一个问题
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » Re: 既然大家都在学C语言,出道题大家试试?
- » 回复: 既然大家都在学 C语言,出道题大家试试?
- Re: [C]非常郁闷的一个问题
- From: Ben Ben
- Re: [C]非常郁闷的一个问题
- From: Thomas X. Iverson
- [C]非常郁闷的一个问题
- From: Thomas X. Iverson