【C程序设计语言】【第二版】【习题答案】【第一章】

练习1-1

#include <stdio.h>

int main()
{
	printf("hello world!");
	return 0;
}


练习1-2

#include <stdio.h>

int main()
{
	printf("1 \n 2");
	return 0;
}

练习1-3

#include <stdio.h>

int main()
{
	int fahr, celsius;
	int lower, upper, step;

	lower = 0;
	upper = 300;
	step = 20;

	fahr = lower;

	//增长的表头
	printf("fahr\tcelsius\n");

	while (fahr <= upper)
	{
		celsius = 5 * (fahr - 32) / 9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
	return 0;
}


练习1-4

#include <stdio.h>

int main()
{
	int fahr, celsius;
	int lower, upper, step;

	lower = 0;
	upper = 300;
	step = 20;

	celsius = lower;

	//增长的表头
	printf("celsius\tfahr\n");

	while (celsius <= upper)
	{
		//celsius = 5 * (fahr - 32) / 9;
		fahr = (9.0*celsius) / 5.0 + 32.0;
		printf("%d\t%d\n", celsius, fahr);
		celsius = celsius + step;
	}
	return 0;
}


练习1-5

#include <stdio.h>

int main()
{
	int fahr, celsius;
	int lower, upper, step;

	lower = 0;
	upper = 300;
	step = 20;

	celsius = upper;

	//增长的表头
	printf("celsius\tfahr\n");

	while (celsius >= lower)
	{
		fahr = (9.0*celsius) / 5.0 + 32.0;
		printf("%d\t%d\n", celsius, fahr);
		celsius = celsius - step;
	}
	return 0;
}


练习1-6

#include <stdio.h>

//验证表达式getchar()!=EOF的值是0仍是1

int main()
{
	int c;

	while (c = getchar() != EOF)
		printf("%d\n", c);

	return 0;
}

练习1-7

#include <stdio.h>

//编写一个打印EOF值的程序

int main()
{
	printf("EOF is %d", EOF);

	return 0;
}


练习1-8

#include <stdio.h>


int main()
{
	int n1 = 0;	//空格
	int n2 = 0;	//制表符
	int n3 = 0; //换行符

	char c;

	while ((c = getchar()) != EOF)
	{
		if (c == ' ')
			n1++;
		
		if (c == '\t')
			n2++;

		if (c == '\n')
			n3++;
	}

	printf("\n空格计数=%d,制表符计数=%d,换行计数=%d\n", n1, n2, n3);

	return 0;
}

练习1-9

#include <stdio.h>


int main()
{
	char c;
	int flag = 0; //空格标识

	while ((c = getchar()) != EOF)
	{
		if (c == ' ')
		{
			if (flag == 0)
			{
				putchar(c);
			}
			flag = 1;
		}
		else
		{
			putchar(c);
			flag = 0;
		}
	}

	return 0;
}

练习1-10

#include <stdio.h>


int main()
{
	char c;

	while ((c = getchar()) != EOF)
	{
		if (c == '\t')
			printf("\\t");

		if (c == '\b')
			printf("\\b");

		if (c == '\\')
			printf("\\\\");

		if (c != '\t')
			if (c != '\b')
				if (c != '\\')
					putchar(c);
	}

	return 0;
}

练习1-11

#include <stdio.h>

#define IN	1	/* 在单词内 */
#define OUT	0	/* 在单词外 */

/* 统计输入的行数, 单词数与字符数 */
int main()
{
	char c;
	int n1, nw, nc, state;

	state = OUT;
	n1 = nw = nc = 0;

	while ((c = getchar()) != EOF)
	{
		++nc;
		if (c == '\n')
			++n1;

		if (c == ' ' || c == '\n' || c == '\t')
		{
			state = OUT;
		}

		else if (state == OUT)
		{
			state = IN;
			++nw;
		}

	}

	printf("%d %d %d \n", n1, nw, nc);

	return 0;
}

练习1-12

#include <stdio.h>

#define IN	1	/* 在单词内 */
#define OUT	0	/* 在单词外 */

/* 统计输入的行数, 单词数与字符数 */
int main()
{
	char c;
	int n1, nw, nc, state;

	state = OUT;
	n1 = nw = nc = 0;

	while ((c = getchar()) != EOF)
	{
		++nc;
		if (c == '\n')
			++n1;

		if (c == ' ' || c == '\n' || c == '\t')
		{
			state = OUT;
		}
		else if (state == OUT)
		{
			state = IN;
			++nw;
			putchar('\n');
			putchar(c);
		}
		else if (state == IN)
		{
			putchar(c);
		}

	}

	printf("%d %d %d \n", n1, nw, nc);

	return 0;
}

练习1-13

#include <stdio.h>

#define	MAXHIST	15
#define	MAXWORD	11
#define	IN		1
#define	OUT		0

int main()
{
	int c, i, nc, state;
	int len;
	int maxvalue;
	int ovflow;
	int wl[MAXHIST];

	state = OUT;
	nc = 0;
	ovflow = 0;
	for (i = 0; i < MAXHIST; i++)
		wl[i] = 0;

	while ((c = getchar()) != EOF)
	{
		if (c == ' ' || c == '\n' || c == '\t')
		{
			state = OUT;
			if (nc>0)
			{
				if (nc < MAXHIST)
					++wl[nc];
				else
					++ovflow;

				nc = 0;
			}
		}
		else if (state == OUT)
		{
			state = IN;
			nc = 1;
		}
		else
			++nc;
	}

	maxvalue = 0;
	for (i = 1; i < MAXWORD; ++i)
	{
		if (wl[i]>maxvalue)
		{
			maxvalue = wl[i];
		}
					
	}
		
	for (i = 1; i < MAXWORD; ++i)
	{
		printf("%5d - %5d : ", i, wl[i]);
		if (wl[i]>0)
		{
			if ((len = wl[i] * MAXHIST / maxvalue) <= 0)
			{
				len = 1;
			}
		}
		else
		{
			len = 0;
		}

		while (len > 0)
		{
			putchar('*');
			--len;
		}
		putchar('\n');
	}

	if (ovflow > 0)
	{
		printf("There are %d words >= %d\n", ovflow, MAXWORD);
	}
	return 0;
}

练习1-14

#include <stdio.h>
#include <ctype.h>

#define MAXHIST	15
#define MAXCHAR	128

int main()
{
	int c, i;
	int len;
	int maxvalue;
	int cc[MAXCHAR];

	//初始化数组
	for (i = 0; i < MAXCHAR; ++i)
	{
		cc[i] = 0;
	}

	while ((c = getchar()) != EOF)
	{
		if (c < MAXCHAR)
			++cc[c];
	}

	maxvalue = 0;
	//找出最大值
	for (i = 0; i < MAXCHAR; ++i)
	{
		if (cc[i]>maxvalue)
		{
			maxvalue = cc[i];
		}
	}

	//输出
	for (i = 0; i < MAXCHAR; ++i)
	{
		if (isprint(i))
		{
			printf("%5d - %c - %5d : ", i, i, cc[i]);
		}
		else
		{
			printf("%5d -   - %5d : ", i, cc[i]);
		}

		if (cc[i] > 0)
		{
			if ((len = cc[i] * MAXHIST / maxvalue) <= 0)
			{
				len = 1;
			}
		}
		else
		{
			len = 0;
		}

		while (len > 0)
		{
			putchar('*');
			--len;
		}

		putchar('\n');
	}
	return 0;
}

练习1-15

#include <stdio.h>

float celsius(float fahr);

int main()
{
	float fahr;
	int lower, upper, step;

	lower = 0;
	upper = 300;
	step = 20;

	fahr = lower;
	while (fahr <= upper)
	{
		printf("%3.0f %6.1f\n", fahr, celsius(fahr));
		fahr = fahr + step;
	}
	return 0;
}

float celsius(float fahr)
{
	return (5.0 / 9.0)*(fahr - 32.0);
}

练习1-16

#include <stdio.h>
#define MAXLINE 10	//容许输入行的最大长度

int getline(char line[], int maxline);
void copy(char to[], char from[]);

//打印最长输入行
int main()
{
	int len;	//当前行长度
	int max;	//目前为止发现的最长行的长度
	char line[MAXLINE];		//当前输入行
	char longest[MAXLINE];	//用于保存最长的行

	max = 0;
	while ((len = getline(line, MAXLINE)) > 0)
	{
		if (len > max)
		{
			max = len;
			copy(longest, line);
		}
	}

	if (max > 0)	//存在这样的行
	{
		printf("%s", longest);
	}
	return 0;
}

//读入一行到s中而且返回长度
int getline(char s[], int lim)
{
	int c, i, j;

	j = 0;
	for (i = 0; (c = getchar()) != EOF&&c != '\n'; ++i)
	{
		if (i < lim - 2)
		{
			s[j] = c;
			++j;
		}
		
	}
	if (c == '\n')
	{
		s[j] = c;
		++i;
		++j;
	}

	s[j] = '\0';

	return i;
}

//将from复制到to
void copy(char to[], char from[])
{
	int i;

	i = 0;
	while ((to[i] = from[i]) != '\0')
	{
		++i;
	}
}


练习1-17

#include <stdio.h>
#define MAXLINE 10	//容许输入行的最大长度

int getline(char line[], int maxline);
void copy(char to[], char from[]);

//打印最长输入行
int main()
{
	int len;	//当前行长度
	char line[MAXLINE];		//当前输入行
	char longest[MAXLINE];	//用于保存最长的行

	while ((len = getline(line, MAXLINE)) > 0)
	{
		if (len > 80)
		{
			printf("%s", line);
		}
	}

	return 0;
}

//读入一行到s中而且返回长度
int getline(char s[], int lim)
{
	int c, i, j;

	j = 0;
	for (i = 0; (c = getchar()) != EOF&&c != '\n'; ++i)
	{
		if (i < lim - 2)
		{
			s[j] = c;
			++j;
		}
		
	}
	if (c == '\n')
	{
		s[j] = c;
		++i;
		++j;
	}

	s[j] = '\0';

	return i;
}

//将from复制到to
void copy(char to[], char from[])
{
	int i;

	i = 0;
	while ((to[i] = from[i]) != '\0')
	{
		++i;
	}
}

练习1-18

#include <stdio.h>
#define MAXLINE 10	//容许输入行的最大长度

int getline(char line[], int maxline);
int remove(char s[]);

//打印最长输入行
int main()
{
	char line[MAXLINE];		//当前输入行

	while ( getline(line, MAXLINE) > 0)
	{
		if (remove(line) > 0)
		{
			printf("%s", line);
		}
	}

	return 0;
}

int remove(char s[])
{
	int i;

	i = 0;
	while (s[i] != '\n')
	{
		++i;
	}
	--i;

	while (i >= 0 && (s[i] == ' ' || s[i] == '\t'))
	{
		--i;
	}

	if (i >= 0)
	{
		++i;
		s[i] = '\n';
		++i;
		s[i] = '\0';
	}

	return i;
}

//读入一行到s中而且返回长度
int getline(char s[], int lim)
{
	int c, i, j;

	j = 0;
	for (i = 0; (c = getchar()) != EOF&&c != '\n'; ++i)
	{
		if (i < lim - 2)
		{
			s[j] = c;
			++j;
		}
		
	}
	if (c == '\n')
	{
		s[j] = c;
		++i;
		++j;
	}

	s[j] = '\0';

	return i;
}

练习1-19

#include <stdio.h>
#define MAXLINE 1000	//容许输入行的最大长度

int getline(char line[], int maxline);
void reverse(char s[]);

//打印最长输入行
int main()
{
	char line[MAXLINE];		//当前输入行

	while ( getline(line, MAXLINE) > 0)
	{
		reverse(line);
		printf("%s", line);
	}

	return 0;
}

void reverse(char s[])
{
	int i, j;
	char temp;

	i = 0;
	while (s[i] != '\0')
	{
		i++;
	}
	i--;
	if (s[i] == '\n')
	{
		i--;
	}

	j = 0;
	while (j < i)
	{
		temp = s[j];
		s[j] = s[i];
		s[i] = temp;
		--i;
		++j;
	}
}

//读入一行到s中而且返回长度
int getline(char s[], int lim)
{
	int c, i, j;

	j = 0;
	for (i = 0; (c = getchar()) != EOF&&c != '\n'; ++i)
	{
		if (i < lim - 2)
		{
			s[j] = c;
			++j;
		}
		
	}
	if (c == '\n')
	{
		s[j] = c;
		++i;
		++j;
	}

	s[j] = '\0';

	return i;
}

练习1-20

#include <stdio.h>

#define TABINC	8

int main()
{
	int c, nb, pos;

	nb = 0;
	pos = 1;
	while ((c = getchar()) != EOF)
	{
		if (c == '\t')
		{
			nb = TABINC - (pos - 1) % TABINC;
			while (nb > 0)
			{
				putchar(' ');
				++pos;
				--nb;
			}
		} 
		else if (c == '\n')
		{
			putchar(c);
			pos = 1;
		}
		else
		{
			putchar(c);
			++pos;
		}
	}
	return 0;
}

练习1-21

#include <stdio.h>

#define TABINC	8

int main()
{
	int c, nb, nt, pos;

	nb = 0;
	nt = 0;
	for (pos = 1; (c = getchar()) != EOF; ++pos)
	{
		if (c == ' ')
		{
			if (pos%TABINC != 0)
			{
				++nb;
			}
			else
			{
				nb = 0;
				++nt;
			}
		}
		else
		{
			for (; nt > 0; --nt)	//输出制表符
			{
				putchar('\t');
			}
			if (c == '\t')
			{
				nb = 0;
			}
			else
			{
				for (; nb > 0; --nb)
				{
					putchar(' ');
				}
			}

			putchar(c);
			if (c == '\n')
			{
				pos = 0;
			}
			else if (c == '\t')
			{
				pos = pos + (TABINC - (pos - 1) % TABINC) - 1;
			}
		}
	}
	return 0;
}