/*练习1-3 修改温度转行程序,使之能在转换表的顶部打印一个标题。*/
#include <stdio.h>
/* 当fahr= 0,20,...,300时,分别
打印华氏温度和摄氏温度对照表*/
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0; /* 温度表的下限 */
upper = 300; /* 温度表的上限 */
step = 20; /* 步长 */
fahr = lower;
printf("打印华氏温度-摄氏温度对照表\n");
while (fahr <= upper){
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
getch();
}
/*练习1-4 编写一个程序打印摄氏温度转换为相应华氏温度的转换表。*/
#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("打印摄氏温度-华氏温度对照表\n");
celsius = lower;
while (celsius <= upper){
fahr = (9.0*celsius) / 5 + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
celsius = celsius + step;
}
getch();
}
/*练习1-5 修改温度转换程序,要求以逆序(即按照300度到0度的顺序)打印温度转换表*/
#include <stdint.h>
main()
{
int fahr;
for (fahr = 300; fahr >= 0; fahr = fahr - 20)
printf("%3d %6.1f\n", fahr, (5.0 / 9.0)*(fahr - 32));
getch();
}
/*练习1-6 验证表达式getchar() != EOF的值是0还是1。 */
#include <stdio.h>
main()
{
int c;
while (c = getchar() != EOF)
printf("%d\n", c);
printf("%d - at EOF\n", c);
}
/*练习1-7 编写一个打印EOF值得程序。*/
#include <stdio.h>
main()
{
printf("EOF is %d\n", EOF);
getch();
}
/*练习1-8 编写一个统计空格、制表符与换行符个数的程序。*/
#include <stdio.h>
main()
{
int c, n1, n2, n3;
n1 = 0;
n2 = 0;
n3 = 0;
while ((c = getchar()) != EOF){
if (c == ' ')
++n1;
if (c == '\t')
++n2;
if (c == '\n')
++n3;
}
printf("%d %d %d\n", n1, n2, n3);
}
/*练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。*/
#include <stdio.h>
#define NONBLANK 'a'
main()
{
int c, lastc;
lastc = NONBLANK;
while ((c = getchar()) != EOF){
if (c != ' ')
putchar(c);
if (c == ' ')
if (lastc != ' ')
putchar(c);
lastc = c;
}
}
/*练习1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\。这样可以将制表符和回退符以可见的方式显示出来。*/
#include <stdio.h>
main()
{
int 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);
}
}
/*练习1-12 编写一个程序,以每行一个单词的形式打印其输入。*/
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int c, state;
state = OUT;
while ((c = getchar()) != EOF){
if (c == ' ' || c == '\n' || c == '\t'){
if (state == IN){
putchar('\n');
state = OUT;
}
}
else if (state == OUT){
state = IN;
putchar(c);
}
else
putchar(c);
}
}
/*练习1-13 编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易绘制,垂直方向的直方图则要困难些。*/
#include <stdio.h>
#define MAXHIST 15
#define MAXWORD 11
#define IN 1
#define OUT 0
main()
{
int c, i, nc, state;
int len;
int maxvalue;
int ovflow;
int wl[MAXWORD];
state = OUT;
nc = 0;
ovflow = 0;
for (i = 0; i < MAXWORD; i++)
wl[i] = 0;
while ((c = getchar()) != EOF){
if (c == ' ' || c == '\n' || c == '\t'){
state = OUT;
if (nc > 0)
if (nc < MAXWORD)
++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);
}
/*习题1-13 垂直方向的直方图*/
#include <stdio.h>
#define MAXHIST 15
#define MAXWORD 11
#define IN 1
#define OUT 0
main()
{
int c, i, j, nc, state;
int maxvalue;
int ovflow;
int wl[MAXWORD];
state = OUT;
ovflow = 0;
for (i = 0; i < MAXWORD; ++i)
wl[i] = 0;
while ((c = getchar()) != EOF){
if (c == ' ' || c == '\n' || c == '\t'){
state = OUT;
if (nc > 0)
if(nc < MAXWORD)
++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 = MAXHIST; i > 0; --i){
for (j = 1; j < MAXWORD; ++j)
if (wl[j] * MAXHIST / maxvalue >= i)
printf(" *");
else
printf(" ");
putchar('\n');
}
for (i = 1; i < MAXWORD; ++i)
printf("%4d", i);
putchar('\n');
for (i = 1; i < MAXWORD; ++i)
printf("%4d", wl[i]);
putchar('\n');
if (ovflow > 0)
printf("There are %d words >= %d\n", ovflow, MAXWORD);
}
/*练习1-14 编写一个程序,打印输入中各个字符出现频度的直方图*/
#include <stdio.h>
#include <ctype.h>
#define MAXHIST 15
#define MAXCHAR 128
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 = 1; i < MAXCHAR; ++i)
if (cc[i] > maxvalue)
maxvalue = cc[i];
for (i = 1; 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');
}
}
/*练习1-15 重新编写1.2节中的温度转换程序,使用函数实现温度转换计算。*/
#include <stdio.h>
float tempe(float fahr);
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, tempe(fahr));
fahr = fahr + step;
}
}
float tempe(float fahr)
{
return (5.0/9.0) * (fahr-32.0);
}
/*练习1-16 修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多地打印文本。*/
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = getline(line,MAXLINE)) > 0){
printf("%d, %s", len, line);
if(len > max){
max = len;
copy(longest, line);
}
}
if(max > 0)
printf("%s",longest);
return 0;
}
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;
++j;
++i;
}
s[j] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
/*编写一个程序,打印长度大于80个字符的所有输入行。*/
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
main()
{
int len;
char line[MAXLINE];
while((len = getline(line,MAXLINE)) > 0)
if(len > 80)
printf("%d, %s", len, line);
}
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] = '\n';
++j;
++i;
}
s[j] = '\0';
return i;
}
/*练习1-18 编写一个程序,删除每个输入行末尾的空格及制表符,并删除完全是空格的行。*/
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
int removec(char s[]);
main()
{
char line[MAXLINE];
while(getline(line,MAXLINE) > 0)
if(removec(line) > 0)
printf("%s", line);
return 0;
}
int removec(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;
}
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] = '\n';
++j;
++i;
}
s[j] = '\0';
return i;
}
/*练习1-19 编写函数reverse(s),将字符串s中的字符顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。*/
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void reverse(char s[]);
main()
{
char line[MAXLINE];
while(getline(line, MAXLINE) > 0){
reverse(line);
printf("%s",line);
}
}
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] = '\n';
++j;
++i;
}
s[j] = '\0';
return i;
}
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;
}
}
/*练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方。假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位。n应该作为变量还是符合常量呢?*/
#include <stdio.h>
#define TABINC 8
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;
}
}
}
/*练习1-22 编写一个程序,把较长的输入行“折”成短一些的两行或者多行,折行的位置在输入行的第n列之前的最后一个非空格之后。要保证程序能够智能地处理输入行很长以及在指定的列前没有空格或者制表符时的情况。*/
#include <stdio.h>
#define MAXCOL 10
#define TABINC 8
char line[MAXCOL]; /*输入行*/
int exptab(int pos);
int findblnk(int pos);
int newpos(int pos);
void printl(int pos);
main()
{
int c, pos;
pos = 0;
while ((c = getchar()) != EOF){
line[pos] = c;
if (c == '\t')
pos = exptab(pos);
else if (c == '\n'){
printl(pos);
pos = 0;
}else if (++pos >= MAXCOL){
pos = findblnk(pos);
printl(pos);
pos = newpos(pos);
}
}
}
void printl(int pos)
{
int i;
for (i = 0; i < pos; ++i)
putchar(line[i]);
if (pos > 0)
putchar('\n');
}
int exptab(int pos)
{
line[pos] = ' ';
for (++pos; pos < MAXCOL && pos % TABINC != 0; ++pos)
line[pos] = ' ';
if (pos < MAXCOL)
return pos;
else {
printl(pos);
return 0;
}
}
int findblnk(int pos)
{
while (pos > 0 && line[pos] != ' ')
--pos;
if (pos == 0)
return MAXCOL;
else
return pos+1;
}
int newpos(int pos)
{
int i, j;
if (pos <= 0 || pos >= MAXCOL)
return 0;
else {
i = 0;
for (j = pos; j < MAXCOL; ++j){
line[i] = line[j];
++i;
}
return i;
}
}