A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

习题1:  定义一个函数,给函数传递任意多个浮点数,计算出这些数的平均值,从键盘输入任意个值,并输出平均值,以说明这个函数的执行过程。
  1. /* Exercise 8.1 A function to calculate an average */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>

  5. double average(double data[], int count)
  6. {
  7.   double sum = 0.0;
  8.   for(int i = 0 ; i<count ; sum += data[i++])
  9.     ;
  10.   return sum/count;
  11. }

  12. #define CAPACITY_INCREMENT 6  /* Increment in the capacity for data values */

  13. int main(void)
  14. {
  15.    double *data = NULL;      /* Pointer to array of data values          */
  16.    double *newdata = NULL;   /* Pointer to new array of data values      */
  17.    double *averages = NULL;  /* Pointer to array of averages             */
  18.    int count = 0;            /* Number of data values                    */
  19.    int capacity = 0;         /* Number of data values that can be stored */
  20.    char answer = 'n';

  21.    do
  22.    {
  23.      if(count == capacity)
  24.      {
  25.        capacity += CAPACITY_INCREMENT;
  26.        /* Create new array of pointers */
  27.        newdata = (double*)malloc(capacity*sizeof(double));
  28.        /* Create an array of values of type double for each new day and store the address */
  29.        if(data)
  30.        {
  31.          /* Copy the existing values to the new array */
  32.          for(int i = 0 ; i<count ; i++)
  33.            newdata[i] = data[i];
  34.          free(data);          /* Free memory for the old array of pointers */
  35.        }
  36.        data = newdata;        /* copy the address of the new array of pointers */
  37.        newdata = NULL;        /* Reset the pointer                             */
  38.      }

  39.      printf("Enter a data value: ");
  40.      scanf(" %lf", data+count++);
  41.      printf("Do you want to enter another (y or n)? ");
  42.      scanf(" %c", &answer);
  43.    } while(tolower(answer) != 'n');

  44.    printf("\nThe  average of thew values you entered is %10.2lf\n", average(data, count));
  45.    free(data);
  46.    return 0;
  47. }
复制代码


习题2:  定义一个函数,返回其整数参数的字符串表示。例如,如果这个参数是 25,函数就返回"25"。如果参数是-98,函数就返回"-98"。用于适当的main()版本说明函数的执行过程。
  1. /* Exercise 8.2 A function to return a string representation of an integer */
  2. #include <stdio.h>
  3. #include <stdbool.h>

  4. /* Convert an integer to a string        */
  5. /* Caller must allocate string array     */
  6. /* Function returns the string to allow  */
  7. /* Use of the function in an expression. */
  8. char* itoa(int n, char str[])
  9. {
  10.   int i = 0;               /* Loop counter              */
  11.   bool negative = 0;       /* Indicate negative integer */
  12.   int length = 0;          /* Length of string          */
  13.   int temp = 0;            /* Temporary storage         */

  14.   if(negative = (n<0))     /* Is it negative?  */
  15.     n = -n;                /* make it positive */

  16.   /* Generate digit characters in reverse order */
  17.   do
  18.   {
  19.     str[i++] = '0'+n%10;    /* Create a rightmost digit        */
  20.     n /= 10;                /* Remove the digit                */
  21.   }while(n>0);              /* Go again if there's more digits */

  22.   if(negative)              /* If it was negative */
  23.     str[i++] = '-';         /* Append minus       */
  24.   str[i] = '\0';            /* Append terminator  */
  25.   length = i;               /* Save the length    */

  26.   /* Now reverse the string in place */
  27.   /* by switching first and last,    */
  28.   /* second and last but one, etc    */
  29.   for(i = 0 ; i<length/2 ;i++)
  30.   {
  31.     temp = str[i];
  32.     str[i] = str[length-i-1];
  33.     str[length-i-1] = temp;
  34.   }
  35.   return str;                /* Return the string */
  36. }


  37. int main(void)
  38. {
  39.    char str[15];              /* Stores string representation of integer */
  40.    long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};

  41.    for (int i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
  42.      printf("Integer value is %d, string is %s\n", testdata[i], itoa(testdata[i],str));
  43.    return 0;
  44. }
复制代码



习题3:  扩展为上一题定义的函数,使函数接受两个参数,以指定结果的字段宽度,使返回的字符串表示右对齐。例如,如果第一个变元的值是-98,字段宽度变元是5,返回的字符串就应是"  -98"。用适当的main()版本说明函数的执行过程。
  1. /* Exercise 8.3 A function to return a string representation of an integer with a given width */
  2. #include <stdio.h>

  3. /* Convert an integer to a string with a fixed width.      */
  4. /* if the widh is too small, the minimum width is assumed. */
  5. char* itoa(int n, char str[], int width)
  6. {
  7.   int i = 0;               /* Loop counter              */
  8.   int j = 0;               /* Loop counter              */
  9.   int negative = 0;        /* Indicate negative integer */
  10.   int length = 0;          /* Length of string          */
  11.   int temp = 0;            /* Temporary storage         */

  12.   if(negative = (n<0))     /* Is it negative?  */
  13.     n = -n;                /* make it positive */

  14.   /* Generate digit characters in reverse order */
  15.   do
  16.   {
  17.     str[i++] = '0'+n%10;    /* Create a rightmost digit        */
  18.     n /= 10;                /* Remove the digit                */
  19.   }while(n>0);              /* Go again if there's more digits */

  20.   if(negative)              /* If it was negative */
  21.     str[i++] = '-';         /* Append minus       */
  22.   str[i] = '\0';            /* Append terminator  */
  23.   length = i;               /* Save the length    */

  24.   /* Now reverse the string in place */
  25.   /* by switching first and last,    */
  26.   /* second and last but one, etc    */
  27.   for(i = 0 ; i<length/2 ;i++)
  28.   {
  29.     temp = str[i];
  30.     str[i] = str[length-i-1];
  31.     str[length-i-1] = temp;
  32.   }

  33.   /* Shift the string to the right and insert blanks */
  34.   if(width>length)
  35.   {
  36.     for(i=length, j = width ; i>= 0 ; i--, j--)
  37.       str[j] = str[i];
  38.     for(i = 0 ; i<width-length ; i++)
  39.       str[i] = ' ';
  40.   }
  41.   return str;                /* Return the string */
  42. }


  43. int main(void)
  44. {
  45.    char str[15];              /* Stores string representation of integer */
  46.    long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};

  47.    for (int i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
  48.      printf("Integer value is %10d, string is %s\n", testdata[i], itoa(testdata[i],str, 14));
  49.    return 0;
  50. }
复制代码








您需要登录后才可以回帖 登录 | 加入黑马