打印最长输入行的程序:
#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)
if (len > max){
max = len;
copy(longest,line);
}
if (max > 0)
printf("%s",longest);
return 0;
}
/* getline函数:将一行读入到s并返回其长度 */
int getline(char s[], int lim)
{
int c, i;
for (i = 0;i <= lim-1 && (c=getchar())!=EOF && c!='\n';++i)
s[i] = c;
if (c = '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy函数:将from复制到to;这里假定to足够大 */
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i] != '\0'))
++i;
}
练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。
#include <stdio.h>
/* ex1-9,编写一个将输入复制到输出的程序,并将其中的连续多个空格用一个空格代替*/
#define NONBLACK 'a'
/*main()
{
int i = 0,c,c_prior; //加入i使得一开始连续输入空格时不输出
c_prior = NONBLACK; //初始化,防止一开始输入空格时出现错误
while((c = getchar()) != EOF){
if (c != ' '){
putchar(c);
i++;
}
else if(c_prior != ' '&& i > 0)
putchar(c);
c_prior = c;
}
getch();
} */
//or版本
main()
{
int c,c_prior;
c_prior = NONBLACK;
while((c = getchar()) != EOF){
if(c != ' ' || c_prior != ' ')
putchar(c);
c_prior = c;
}
getch();
}
练习1-12
#include <stdio.h>
// ex 1-12 以每行一个单词的形式打印其输入
#define IN 1 // inside a word
#define OUT 0 // outside a word
main()
{
int c,state;
state = OUT;
while((c = getchar()) != EOF){
if(c == ' ' || c== '\n' || c == '\t' ){
if(state == IN){
putchar('\n'); //finish a word
state = OUT;
}
else if(state == OUT){
state = IN;
putchar(c);
}
else
putchar(c);
}
}
}
练习1-13
水平直方图:
#include <stdio.h>
#include <stdlib.h>
//ex 1-13
//print horizontal histogram
#define MAXHIST 15 /* max length of histogram */
#define MAXWORD 11 /* max length of a word */
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
main()
{
int c,i,nc,state;
int len; /* length pf each word */
int maxvalue; /* maxunum value for wl[] */
int ovflow; /* number of overflow word */
int wl[MAXWORD];/*word length counters */
state = OUT;
nc = 0; /*number of chars in a word */
ovflow = 0;
for (i = 0;i < MAXWORD; ++i)
wl[i] = 0;
while((c = getchar()) != '\n'){
if (c == ' ' || c == '\t'){
state = OUT;
if (nc > 0)
if (nc < MAXWORD)
++wl[nc];
else
++ovflow;
nc = 0;
}else if (state == OUT){
state = IN;
nc = 1; /* beginning of a new word */
}else
nc++; /*inside a word */
}
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; */
len = wl [i];
while (len > 0){
putchar('*');
--len;
}
putchar('\n');
}
if (ovflow > 0)
printf("There are %d words >= %d\n", ovflow , MAXWORD);
system("pause");
}
垂直直方图:
#include <stdio.h>
#include <stdlib.h>
//ex 1-13
//print horizontal histogram
#define MAXHIST 15 /* max length of histogram */
#define MAXWORD 11 /* max length of a word */
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
main()
{
int c,i,j,nc,state;
int len; /* length pf each word */
int maxvalue; /* maxunum value for wl[] */
int ovflow; /* number of overflow word */
int wl[MAXWORD];/*word length counters */
state = OUT;
nc = 0; /*number of chars in a word */
ovflow = 0;
for (i = 0;i < MAXWORD; ++i)
wl[i] = 0;
while((c = getchar()) != '\n'){
if (c == ' ' || c == '\t'){
state = OUT;
if (nc > 0)
if (nc < MAXWORD)
++wl[nc];
else
++ovflow;
nc = 0;
}else if (state == OUT){
state = IN;
nc = 1; /* beginning of a new word */
}else
nc++; /*inside a word */
}
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] >= 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);
system("pause");
}
练习1-20detab函数
#include <stdio.h>
#include <stdlib.h>
// ex 1-20 编写程序detab
#define TABINC 8 /* tab increment size */
/* replace tabs with the proper number of blanks */
main()
{
int c,pos,nb;
nb = 0; /*number of blanks necessary */
pos = 1;/*position of character in line */
while((c = getchar()) != EOF){
if (c == '\t'){
nb = TABINC - (pos - 1) % TABINC;
while(nb > 0){
putchar(' ');
++pos;
--nb;
}
}else if (c == '\n'){
pos = 1;
putchar(c);
}else{
++pos;
putchar(c);
}
}
system("pause");
}