C程序设计(第5版)谭浩强习题解答 第7章 用函数实现模块化程序设计

C程序设计(第5版)谭浩强习题解答

第7章 用函数实现模块化程序设计

1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输人。

//7.1.1
#include 
int main()
{
	int hcf(int, int);
	int lcd(int, int, int);
	int u, v, h, l;
	scanf("%d,%d", &u, &v);
	h = hcf(u, v);
	printf("H.C.F=%d\n", h);
	l = lcd(u, v, h);
	printf("L.C.D=%d\n", l);
	return 0;
}

int hcf(int u, int v)
{
	int t, r;
	if (v > u)
	{
		t = u; u = v; v = t;
	}
	while ((r = u % v) != 0)
	{
		u = v;
		v = r;
	}
	return(v);
}

int lcd(int u, int v, int h)
{
	return(u*v / h);
}

//7.1.2
#include 
int Hcf, Lcd;
int main()
{
	void hcf(int, int);
	void lcd(int, int);
	int u, v;
	scanf("%d,%d", &u, &v);
	hcf(u, v);
	lcd(u, v);
	printf("H.C.F=%d\n", Hcf);
	printf("L.C.D=%d\n", Lcd);
	return 0;
}

void hcf(int u, int v)
{
	int t, r;
	if (v > u)
	{
		t = u; u = v; v = t;
	}
	while ((r = u % v) != 0)
	{
		u = v;
		v = r;
	}
	Hcf = v;
}

void lcd(int u, int v)
{
	Lcd = u * v / Hcf;
}

2.求方程 ax2+bx+c=0的根,用3个函数分别求当:b2−4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a,b,c的值。

#include 
#include 
float x1, x2, disc, p, q;
int main()
{
	void greater_than_zero(float, float);
	void equal_to_zero(float, float);
	void smaller_than_zero(float, float);
	float a, b, c;
	printf("input a,b,c:");
	scanf("%f,%f,%f", &a, &b, &c);
	printf("equation: %5.2f*x*x+%5.2f*x+%5.2f=0\n", a, b, c);
	disc = b * b - 4 * a*c;
	printf("root:\n");
	if (disc > 0)
	{
		greater_than_zero(a, b);
		printf("x1=%f\t\tx2=%f\n", x1, x2);
	}
	else if (disc == 0)
	{
		equal_to_zero(a, b);
		printf("x1=%f\t\tx2=%f\n", x1, x2);
	}
	else
	{
		smaller_than_zero(a, b);
		printf("x1=%f+%fi\tx2=%f-%fi\n", p, q, p, q);
	}
	return 0;
}

void greater_than_zero(float a, float b)
{
	x1 = (-b + sqrt(disc)) / (2 * a);
	x2 = (-b - sqrt(disc)) / (2 * a);
}

void equal_to_zero(float a, float b)
{
	x1 = x2 = (-b) / (2 * a);
}

void smaller_than_zero(float a, float b)
{
	p = -b / (2 * a);
	q = sqrt(-disc) / (2 * a);
}

3.写一个判素数的函数,在主函数输人一个整数,输出是否为素数的信息。

#include 
int main()
{
	int prime(int);
	int n;
	printf("input an integer:");
	scanf("%d", &n);
	if (prime(n))
		printf("%d is a prime.\n", n);
	else
		printf("%d is not a prime.\n", n);
	return 0;
}

int prime(int n)
{
	int flag = 1, i;
	for (i = 2; i < n / 2 && flag == 1; i++)
		if (n%i == 0)
			flag = 0;
	return(flag);
}

4.写一个函数,使给定的一个3X3的二维整型数组转置,即行列互换。

#include 
#define N 3
int array[N][N];
int main()
{
	void convert(int array[][3]);
	int i, j;
	printf("input array:\n");
	for (i = 0; i < N; i++)
		for (j = 0; j < N; j++)
			scanf("%d", &array[i][j]);
	printf("\noriginal array :\n");
	for (i = 0; i < N; i++)
	{
		for (j = 0; j < N; j++)
			printf("%5d", array[i][j]);
		printf("\n");
	}
	convert(array);
	printf("convert array:\n");
	for (i = 0; i < N; i++)
	{
		for (j = 0; j < N; j++)
			printf("%5d", array[i][j]);
		printf("\n");
	}
	return 0;
}

void convert(int array[][3])
{
	int i, j, t;
	for (i = 0; i < N; i++)
		for (j = i + 1; j < N; j++)
		{
			t = array[i][j];
			array[i][j] = array[j][i];
			array[j][i] = t;
		}
}

5.写一个函数,使输人的一个字符串按反序存放,在主函数中输入和输出字符串。

#include  
#include 
int main()
{
	void inverse(char str[]);
	char str[100];
	printf("input string:");
	scanf("%s", str);
	inverse(str);
	printf("inverse string:%s\n", str);
	return 0;
}

void inverse(char str[])
{
	char t;
	int i, j;
	for (i = 0, j = strlen(str); i < (strlen(str) / 2); i++, j--)
	{
		t = str[i];
		str[i] = str[j - 1];
		str[j - 1] = t;
	}
}

6.写一个函数,将两个字符串连接。

#include 
int main()
{
	void concatenate(char string1[], char string2[], char string[]);
	char s1[100], s2[100], s[100];
	printf("input string1:");
	scanf("%s", s1);
	printf("input string2:");
	scanf("%s", s2);
	concatenate(s1, s2, s);
	printf("\nThe new string is %s\n", s);
	return 0;
}

void concatenate(char string1[], char string2[], char string[])
{
	int i, j;
	for (i = 0; string1[i] != '\0'; i++)
		string[i] = string1[i];
	for (j = 0; string2[j] != '\0'; j++)
		string[i + j] = string2[j];
	string[i + j] = '\0';
}

7.写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出。

#include 
int main()
{
	void cpy(char[], char[]);
	char str[80], c[80];
	printf("input string:");
	gets(str);
	cpy(str, c);
	printf("The vowel letters are:%s\n", c);
	return 0;
}

void cpy(char s[], char c[])
{
	int i, j;
	for (i = 0, j = 0; s[i] != '\0'; i++)
		if (s[i] == 'a' || s[i] == 'A' || s[i] == 'e' || s[i] == 'E' || s[i] == 'i' ||
			s[i] == 'I' || s[i] == 'o' || s[i] == 'O' || s[i] == 'u' || s[i] == 'U')
		{
			c[j] = s[i];
			j++;
		}
	c[j] = '\0';
}

8.写一个函数,输人一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如输人1990,应输出“1 9 9 0”。

#include 
#include 
int main()
{
	char str[80];
	void insert(char[]);
	printf("input four digits:");
	scanf("%s", str);
	insert(str);
	return 0;
}

void insert(char str[])
{
	int i;
	for (i = strlen(str); i > 0; i--)
	{
		str[2 * i] = str[i];
		str[2 * i - 1] = ' ';
	}
	printf("output:\n%s\n", str);
}

9.编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果。

#include 
int letter, digit, space, others;
int main()
{
	void count(char[]);
	char text[80];
	printf("input string:\n");
	gets(text);
	printf("string:");
	puts(text);
	letter = 0;
	digit = 0;
	space = 0;
	others = 0;
	count(text);
	printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n", letter, digit, space, others);
	return 0;
}

void count(char str[])
{
	int i;
	for (i = 0; str[i] != '\0'; i++)
		if ((str[i] >= 'a'&& str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
			letter++;
		else if (str[i] >= '0' && str[i] <= '9')
			digit++;
		else if (str[i] == 32)
			space++;
		else
			others++;
}

10.写一个函数,输人一行字符,将此字符串中最长的单词输出。

#include 
#include 
int main()
{
	int alphabetic(char);
	int longest(char[]);
	int i;
	char line[100];
	printf("input one line:\n");
	gets(line);
	printf("The longest word is :");
	for (i = longest(line); alphabetic(line[i]); i++)
		printf("%c", line[i]);
	printf("\n");
	return 0;
}

int alphabetic(char c)
{
	if ((c >= 'a' && c <= 'z') || (c >= 'A'&&c <= 'z'))
		return(1);
	else
		return(0);
}

int longest(char string[])
{
	int len = 0, i, length = 0, flag = 1, place = 0, point;
	for (i = 0; i <= strlen(string); i++)
		if (alphabetic(string[i]))
			if (flag)
			{
				point = i;
				flag = 0;
			}
			else
				len++;
		else
		{
			flag = 1;
			if (len >= length)
			{
				length = len;
				place = point;
				len = 0;
			}
		}
	return(place);
}

11.写一个函数,用“起泡法”对输人的10个字符按由小到大顺序排列。

#include 
#include 
#define N 10
char str[N];
int main()
{
	void sort(char[]);
	int i, flag;
	for (flag = 1; flag == 1;)
	{
		printf("input string:\n");
		scanf("%s", &str);
		if (strlen(str) > N)
			printf("string too long,input again!");
		else
			flag = 0;
	}
	sort(str);
	printf("string sorted:\n");
	for (i = 0; i < N; i++)
		printf("%c", str[i]);
	printf("\n");
	return 0;
}

void sort(char str[])
{
	int i, j;
	char t;
	for (j = 1; j < N; j++)
		for (i = 0; (i < N - j) && (str[i] != '\0'); i++)
			if (str[i] > str[i + 1])
			{
				t = str[i];
				str[i] = str[i + 1];
				str[i + 1] = t;
			}
}

12.用牛顿迭代法求根。方程为ax3+bx2+cx+d=0,系数a,b,c,d的值依次为1,2,3,4,由主函数输人。求x在1附近的一个实根。求出根后由主函数输出。

#include 
#include 
int main()
{
	float solut(float a, float b, float c, float d);
	float a, b, c, d;
	printf("input a,b,c,d:");
	scanf("%f,%f,%f,%f", &a, &b, &c, &d);
	printf("x=%10.7f\n", solut(a, b, c, d));
	return 0;
}

float solut(float a, float b, float c, float d)
{
	float x = 1, x0, f, f1;
	do
	{
		x0 = x;
		f = ((a*x0 + b)*x0 + c)*x0 + d;
		f1 = (3 * a*x0 + 2 * b)*x0 + c;
		x = x0 - f / f1;
	} while (fabs(x - x0) >= 1e-3);
	return(x);
}

13.用递归方法求n阶勒让德多项式的值,递归公式为

#include 
#define N 10
#define M 5
float score[N][M];
float a_stu[N], a_cour[M];
int r, c;

int main()
{
	int i, j;
	float h;
	float s_var(void);
	float highest();
	void input_stu(void);
	void aver_stu(void);
	void aver_cour(void);
	input_stu();
	aver_stu();
	aver_cour();
	printf("\n  NO.     cour1   cour2   cour3   cour4   cour5   aver\n");
	for (i = 0; i < N; i++)
	{
		printf("\n NO %2d ", i + 1);
		for (j = 0; j < M; j++)
			printf("%8.2f", score[i][j]);
		printf("%8.2f\n", a_stu[i]);
	}
	printf("\naverage:");
	for (j = 0; j < M; j++)
		printf("%8.2f", a_cour[j]);
	printf("\n");
	h = highest();
	printf("highest:%7.2f   NO. %2d   course %2d\n", h, r, c);
	printf("variance %8.2f\n", s_var());
	return 0;
}

void input_stu(void)
{
	int i, j;
	for (i = 0; i < N; i++)
	{
		printf("\ninput score of student%2d:\n", i + 1);
		for (j = 0; j < M; j++)
			scanf("%f", &score[i][j]);
	}
}


void aver_stu(void)
{
	int i, j;
	float s;
	for (i = 0; i < N; i++)
	{
		for (j = 0, s = 0; j < M; j++)
			s += score[i][j];
		a_stu[i] = s / 5.0;
	}
}

void aver_cour(void)
{
	int i, j;
	float s;
	for (j = 0; j < M; j++)
	{
		s = 0;
		for (i = 0; i < N; i++)
			s += score[i][j];
		a_cour[j] = s / (float)N;
	}
}

float highest()
{
	float high;
	int i, j;
	high = score[0][0];
	for (i = 0; i < N; i++)
		for (j = 0; j < M; j++)
			if (score[i][j] > high)
			{
				high = score[i][j];
				r = i + 1;
				c = j + 1;
			}
	return(high);
}

float s_var(void)
{
	int i;
	float sumx, sumxn;
	sumx = 0.0;
	sumxn = 0.0;
	for (i = 0; i < N; i++)
	{
		sumx += a_stu[i] * a_stu[i];
		sumxn += a_stu[i];
	}
	return(sumx / N - (sumxn / N)*(sumxn / N));
}

14.输人10个学生5门课的成绩,分别用函数实现下列功能:

①计算每个学生的平均分;
②计算每门课的平均分;
③找出所有50个分数中最高的分数所对应的学生和课程;
④计算平均分方差:
其中,x;为某一学生的平均分。
#include 
#define N 10
#define M 5
float score[N][M];
float a_stu[N], a_cour[M];
int r, c;

int main()
{
	int i, j;
	float h;
	float s_var(void);
	float highest();
	void input_stu(void);
	void aver_stu(void);
	void aver_cour(void);
	input_stu();
	aver_stu();
	aver_cour();
	printf("\n  NO.     cour1   cour2   cour3   cour4   cour5   aver\n");
	for (i = 0; i < N; i++)
	{
		printf("\n NO %2d ", i + 1);
		for (j = 0; j < M; j++)
			printf("%8.2f", score[i][j]);
		printf("%8.2f\n", a_stu[i]);
	}
	printf("\naverage:");
	for (j = 0; j < M; j++)
		printf("%8.2f", a_cour[j]);
	printf("\n");
	h = highest();
	printf("highest:%7.2f   NO. %2d   course %2d\n", h, r, c);
	printf("variance %8.2f\n", s_var());
	return 0;
}

void input_stu(void)
{
	int i, j;
	for (i = 0; i < N; i++)
	{
		printf("\ninput score of student%2d:\n", i + 1);
		for (j = 0; j < M; j++)
			scanf("%f", &score[i][j]);
	}
}


void aver_stu(void)
{
	int i, j;
	float s;
	for (i = 0; i < N; i++)
	{
		for (j = 0, s = 0; j < M; j++)
			s += score[i][j];
		a_stu[i] = s / 5.0;
	}
}

void aver_cour(void)
{
	int i, j;
	float s;
	for (j = 0; j < M; j++)
	{
		s = 0;
		for (i = 0; i < N; i++)
			s += score[i][j];
		a_cour[j] = s / (float)N;
	}
}

float highest()
{
	float high;
	int i, j;
	high = score[0][0];
	for (i = 0; i < N; i++)
		for (j = 0; j < M; j++)
			if (score[i][j] > high)
			{
				high = score[i][j];
				r = i + 1;
				c = j + 1;
			}
	return(high);
}

float s_var(void)
{
	int i;
	float sumx, sumxn;
	sumx = 0.0;
	sumxn = 0.0;
	for (i = 0; i < N; i++)
	{
		sumx += a_stu[i] * a_stu[i];
		sumxn += a_stu[i];
	}
	return(sumx / N - (sumxn / N)*(sumxn / N));
}

15.写几个函数:

①输人10个职工的姓名和职工号;
②按职工号由小到大顺序排序,姓名顺序也随之调整;
③要求输人一个职工号,用折半查找法找出该职工的姓名,从主函数输人要查找的职工号,输出该职工姓名。
#include 
#include 
#define N 10
int main()
{
	void input(int[], char name[][8]);
	void sort(int[], char name[][8]);
	void search(int, int[], char name[][8]);
	int num[N], number, flag = 1, c;
	char name[N][8];
	input(num, name);
	sort(num, name);
	while (flag == 1)
	{
		printf("\ninput number to look for:");
		scanf("%d", &number);
		search(number, num, name);
		printf("continue ot not(Y/N)?");
		getchar();
		c = getchar();
		if (c == 'N' || c == 'n')
			flag = 0;
	}
	return 0;
}

void input(int num[], char name[N][8])
{
	int i;
	for (i = 0; i < N; i++)
	{
		printf("input NO.: ");
		scanf("%d", &num[i]);
		printf("input name: ");
		getchar();
		gets(name[i]);
	}
}

void sort(int num[], char name[N][8])
{
	int i, j, min, templ;
	char temp2[8];
	for (i = 0; i < N - 1; i++)
	{
		min = i;
		for (j = i; j < N; j++)
			if (num[min] > num[j])  min = j;
		templ = num[i];
		strcpy(temp2, name[i]);
		num[i] = num[min];
		strcpy(name[i], name[min]);
		num[min] = templ;
		strcpy(name[min], temp2);
	}
	printf("\n result:\n");
	for (i = 0; i < N; i++)
		printf("\n %5d%10s", num[i], name[i]);
}

void search(int n, int num[], char name[N][8])
{
	int top, bott, mid, loca, sign;
	top = 0;
	bott = N - 1;
	loca = 0;
	sign = 1;
	if ((n < num[0]) || (n > num[N - 1]))
		loca = -1;
	while ((sign == 1) && (top <= bott))
	{
		mid = (bott + top) / 2;
		if (n == num[mid])
		{
			loca = mid;
			printf("NO. %d , his name is %s.\n", n, name[loca]);
			sign = -1;
		}
		else if (n < num[mid])
			bott = mid - 1;
		else
			top = mid + 1;
	}
	if (sign == 1 || loca == -1)
		printf("%d not been found.\n", n);
}

16.写一个函数,输人一个十六进制数,输出相应的十进制数。

#include 
#define MAX 1000
int main()
{
	int htoi(char s[]);
	int c, i, flag, flag1;
	char t[MAX];
	i = 0;
	flag = 0;
	flag1 = 1;
	printf("input a HEX number:");
	while ((c = getchar()) != '\0' && i < MAX&& flag1)
	{
		if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')
		{
			flag = 1;
			t[i++] = c;
		}
		else if (flag)
		{
			t[i] = '\0';
			printf("decimal  number %d\n", htoi(t));
			printf("continue or not?");
			c = getchar();
			if (c == 'N' || c == 'n')
				flag1 = 0;
			else
			{
				flag = 0;
				i = 0;
				printf("\ninput a HEX number:");
			}
		}
	}
	return 0;
}

int htoi(char s[])
{
	int i, n;
	n = 0;
	for (i = 0; s[i] != '\0'; i++)
	{
		if (s[i] >= '0'&& s[i] <= '9')
			n = n * 16 + s[i] - '0';
		if (s[i] >= 'a' && s[i] <= 'f')
			n = n * 16 + s[i] - 'a' + 10;
		if (s[i] >= 'A' && s[i] <= 'F')
			n = n * 16 + s[i] - 'A' + 10;
	}
	return(n);
}

17.用递归法将一个整数n转换成字符串。例如,输人483,应输出字符串”483”。n的位数不确定,可以是任意位数的整数。

#include 
int main()
{
	void convert(int n);
	int number;
	printf("input an integer: ");
	scanf("%d", &number);
	printf("output: ");
	if (number < 0)
	{
		putchar('-'); putchar(' ');   //先输出一个‘-’号和空格 
		number = -number;
	}
	convert(number);
	printf("\n");
	return 0;
}

void convert(int n)
{
	int i;
	if ((i = n / 10) != 0)
		convert(i);
	putchar(n % 10 + '0');
	putchar(32);
}

18.给出年、月、日,计算该日是该年的第几天。

#include 
int main()
{
	int sum_day(int month, int day);
	int leap(int year);
	int year, month, day, days;
	printf("input date(year,month,day):");
	scanf("%d,%d,%d", &year, &month, &day);
	printf("%d/%d/%d ", year, month, day);
	days = sum_day(month, day);                  //调用函数sum_day 
	if (leap(year) && month >= 3)                  //调用函数leap 
		days = days + 1;
	printf("is the %dth day in this year.\n", days);
	return 0;
}

int sum_day(int month, int day)         //函数sum_day:计算日期 
{
	int day_tab[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int i;
	for (i = 1; i < month; i++)
		day += day_tab[i];      //累加所在月之前天数 
	return(day);
}                         //函数leap:判断是否为闰年 

int leap(int year)
{
	int leap;
	leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
	return(leap);
}

你可能感兴趣的:(C语言,C程序设计,C程序设计习题解答)