黑马程序员技术交流社区
标题:
1.1关于函数方面基础习题含答案——提高能力才是硬道理
[打印本页]
作者:
wangxiaoit
时间:
2014-12-17 16:57
标题:
1.1关于函数方面基础习题含答案——提高能力才是硬道理
习题1: 定义一个函数,给函数传递任意多个浮点数,计算出这些数的平均值,从键盘输入任意个值,并输出平均值,以说明这个函数的执行过程。
/* Exercise 8.1 A function to calculate an average */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
double average(double data[], int count)
{
double sum = 0.0;
for(int i = 0 ; i<count ; sum += data[i++])
;
return sum/count;
}
#define CAPACITY_INCREMENT 6 /* Increment in the capacity for data values */
int main(void)
{
double *data = NULL; /* Pointer to array of data values */
double *newdata = NULL; /* Pointer to new array of data values */
double *averages = NULL; /* Pointer to array of averages */
int count = 0; /* Number of data values */
int capacity = 0; /* Number of data values that can be stored */
char answer = 'n';
do
{
if(count == capacity)
{
capacity += CAPACITY_INCREMENT;
/* Create new array of pointers */
newdata = (double*)malloc(capacity*sizeof(double));
/* Create an array of values of type double for each new day and store the address */
if(data)
{
/* Copy the existing values to the new array */
for(int i = 0 ; i<count ; i++)
newdata[i] = data[i];
free(data); /* Free memory for the old array of pointers */
}
data = newdata; /* copy the address of the new array of pointers */
newdata = NULL; /* Reset the pointer */
}
printf("Enter a data value: ");
scanf(" %lf", data+count++);
printf("Do you want to enter another (y or n)? ");
scanf(" %c", &answer);
} while(tolower(answer) != 'n');
printf("\nThe average of thew values you entered is %10.2lf\n", average(data, count));
free(data);
return 0;
}
复制代码
习题2: 定义一个函数,返回其整数参数的字符串表示。例如,如果这个参数是 25,函数就返回"25"。如果参数是-98,函数就返回"-98"。用于适当的main()版本说明函数的执行过程。
/* Exercise 8.2 A function to return a string representation of an integer */
#include <stdio.h>
#include <stdbool.h>
/* Convert an integer to a string */
/* Caller must allocate string array */
/* Function returns the string to allow */
/* Use of the function in an expression. */
char* itoa(int n, char str[])
{
int i = 0; /* Loop counter */
bool negative = 0; /* Indicate negative integer */
int length = 0; /* Length of string */
int temp = 0; /* Temporary storage */
if(negative = (n<0)) /* Is it negative? */
n = -n; /* make it positive */
/* Generate digit characters in reverse order */
do
{
str[i++] = '0'+n%10; /* Create a rightmost digit */
n /= 10; /* Remove the digit */
}while(n>0); /* Go again if there's more digits */
if(negative) /* If it was negative */
str[i++] = '-'; /* Append minus */
str[i] = '\0'; /* Append terminator */
length = i; /* Save the length */
/* Now reverse the string in place */
/* by switching first and last, */
/* second and last but one, etc */
for(i = 0 ; i<length/2 ;i++)
{
temp = str[i];
str[i] = str[length-i-1];
str[length-i-1] = temp;
}
return str; /* Return the string */
}
int main(void)
{
char str[15]; /* Stores string representation of integer */
long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};
for (int i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
printf("Integer value is %d, string is %s\n", testdata[i], itoa(testdata[i],str));
return 0;
}
复制代码
习题3: 扩展为上一题定义的函数,使函数接受两个参数,以指定结果的字段宽度,使返回的字符串表示右对齐。例如,如果第一个变元的值是-98,字段宽度变元是5,返回的字符串就应是" -98"。用适当的main()版本说明函数的执行过程。
/* Exercise 8.3 A function to return a string representation of an integer with a given width */
#include <stdio.h>
/* Convert an integer to a string with a fixed width. */
/* if the widh is too small, the minimum width is assumed. */
char* itoa(int n, char str[], int width)
{
int i = 0; /* Loop counter */
int j = 0; /* Loop counter */
int negative = 0; /* Indicate negative integer */
int length = 0; /* Length of string */
int temp = 0; /* Temporary storage */
if(negative = (n<0)) /* Is it negative? */
n = -n; /* make it positive */
/* Generate digit characters in reverse order */
do
{
str[i++] = '0'+n%10; /* Create a rightmost digit */
n /= 10; /* Remove the digit */
}while(n>0); /* Go again if there's more digits */
if(negative) /* If it was negative */
str[i++] = '-'; /* Append minus */
str[i] = '\0'; /* Append terminator */
length = i; /* Save the length */
/* Now reverse the string in place */
/* by switching first and last, */
/* second and last but one, etc */
for(i = 0 ; i<length/2 ;i++)
{
temp = str[i];
str[i] = str[length-i-1];
str[length-i-1] = temp;
}
/* Shift the string to the right and insert blanks */
if(width>length)
{
for(i=length, j = width ; i>= 0 ; i--, j--)
str[j] = str[i];
for(i = 0 ; i<width-length ; i++)
str[i] = ' ';
}
return str; /* Return the string */
}
int main(void)
{
char str[15]; /* Stores string representation of integer */
long testdata[] = { 30L, -98L, 0L, -1L, 999L, -12345L};
for (int i = 0 ; i< sizeof testdata/sizeof(long) ; i++)
printf("Integer value is %10d, string is %s\n", testdata[i], itoa(testdata[i],str, 14));
return 0;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2