1、编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替

  使用if 结构:

#include<stdio.h>
#define NONBLANK 'a';
// repalce string of blanks with a single blank
int main(void)
{
int c, lastc; // c负责记录当前输入字符的ASCII值, lastc记录前一个输入字符的ASCII值 lastc = NONBLANK; // 符号常量NONBLANK负责把lastc初始化为一个任意的非空格字符
while((c = getchar()) != EOF)
{
if(c != ' ')
putchar(c);
if(c == ' ')
if(lastc != ' ') // 输出一个或一串空格中的第一个空格
putchar(c);
lastc = c; }
return 0;
}

  使用if-else 语法结构:

#include<stdio.h>
#define NONBLANK 'a';
int main(void)
{
int c, lastc; lastc = NONBLANK;
while((c = getchar()) != EOF)
{
if(c != ' ')
putchar(c);
else if(lastc != ' ')
putchar(c);
lastc = c; }
return 0;
}

  使用逻辑或(||)操作符:

#include<stdio.h>
#define NONBLANK 'a';
int main(void)
{
int c, lastc; lastc = NONBLANK;
while((c = getchar()) != EOF)
{
if(c != ' ' || lastc != ' ')
putchar(c);
lastc = c; }
return 0;
}

2、单词计数

  要求统计行数、单词数与字符数,这里单词的定义比较宽松,它是任何其中不包含空格、制表符或换行符的字符序列。

#include<stdio.h>
#define IN 1 // inside a word
#define OUT 0 // outside a word
/* count lines, words, and characters in input */
int main(void)
{
int c, nl, nw, nc, state; state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if(state == OUT) // 每当遇到单词的第一个字符, 它就作为一个新单词加以统计
{
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
return 0;
}

Question: 如何测试程序?

 首先进行常规测试,然后测试边界条件,对于该程序而言,边界条件有:

  没有输入

  没有单词(只有换行符)

  没有单词(只有空格、制表符和换行符)

  每个单词各占一行(没有空格和制表符)

  单词出现于文本行行首

  单词出现于一串空格之后的情况

3、编写一个程序,以每行一个单词的形式打印其输入

#include<stdio.h>
#define IN 1 // inside a word
#define OUT 0 // outside a word
// print input one word per line
int main(void)
{
int c, state; // state: 记录程序的处理过程是否正处于某个单词的内部 state = OUT;
while((c = getchar()) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t')
{
if(state == IN)
{
putchar('\n'); // finish the word
state = OUT;
}
}
else if(state == OUT)
{
state = IN; // beginning of word
putchar(c);
}
else
putchar(c); // inside a word
state = IN;
}
}

 下面是我画的一个流程图:

4、编写程序,打印输入中单词长度的直方图

  水平方向的直方图:

#include<stdio.h>
#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
// print horizontal histogram
int main(void)
{
int c, i, nc, state;
int len; // length of each bar
int maxvalue; // maximum value for wl[]
int ovflow; // number of overflow words
int wl[MAXWORD]; // word length counters state = OUT;
nc = 0; // number of chars in a word
ovflow = 0; // number of words >= MAXWORD
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; // 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)
{ // 变量len根据MAZHIST和maxvalue的值计算得出的wl[i]所对应的直方图长度, 如果wl[i]大于零,就至少打印一个星号
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;
}

  由于垂直方向的所有直方图需要同步打印,所以垂直方向的比较难控制,注意与上述程序的区别:

#include<stdio.h>
#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
// print horizontal histogram
int main(void)
{
int c, i, j, nc, state;
int len; // length of each bar
int maxvalue; // maximum value for wl[]
int ovflow; // number of overflow words
int wl[MAXWORD]; // word length counters state = OUT;
nc = 0; // number of chars in a word
ovflow = 0; // number of words >= MAXWORD
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; // 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] * 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);
return 0;
}

Getting started with the basics of programming exercises_1的更多相关文章

  1. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  2. Getting started with the basics of programming exercises_4

    1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...

  3. Getting started with the basics of programming exercises_3

    1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...

  4. Getting started with the basics of programming exercises_2

    1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...

  5. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  6. C语言学习书籍推荐《Practical C++ Programming》下载

    下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...

  7. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  8. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  9. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

随机推荐

  1. 2019.9.21 csp-s模拟测试49 反思总结

    没赶上昨天的考试,不过我这种人考不考都没有多少提升吧. 挺服气的一场考试,有生以来参加的最让人想笑的考试. T1:养花 取模,区间询问最大值,有点套路化的预处理答案…难点也在预处理上.容易想到分块然后 ...

  2. Faster RCNN算法demo代码解析

    一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...

  3. php thrift TServerSocket实现端口复用

    <?php namespace Message\Controller; use Think\Controller; use Thrift\Exception\TException; use Th ...

  4. ORACLE 使用笔记

    ORACLE TRUNC()函数 TRUNC():类似截取函数,按指定的格式截取输入的数据. 1.[trunc(for dates)]TRUNC()函数处理日期 语法格式:TRUNC(date[,fm ...

  5. GBRT(GBDT)(MART)(Tree Net)(Tree link)

    源于博客 GBRT(梯度提升回归树)有好多名字,标题全是它的别名. 它是一种迭代的回归树算法,由多棵回归树组成,所有树的结论累加起来得到最终结果.在被提出之初与SVM一起被认为是泛化能力较强的算法. ...

  6. SPSS和Mplus如何做非线性中介调节效应分析?如倒U形曲线

    SPSS和Mplus如何做非线性中介调节效应分析?如倒U形曲线 传统的线性回归模型用的比较多,但有时候变量之间的关系更符合非线性关系,此时使用非线性模型其拟合度会更好,模型预测效果更佳.在非线性关系中 ...

  7. 基于TCP编程的socket

    什么是TCP/IP.UDP? TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域 ...

  8. hdu1536 sg打表

    标记数组用bool型防止超时.输入的f[ ]要排序. #include<stdio.h> #include<string.h> #include<algorithm> ...

  9. UITextField 自定义clearButton背景色

    一个鸡贼的解决方案,适用于自定义clearButton的样式,直接修改背景图即可 1. 实现基于UITextField的category并添加如下方法及声明 - (void)setLightStyle ...

  10. C++中的union

    1:,像任何类一样,union可以指定保护标记使成员成为公用的.私有的或受保护的.默认情况下,union 表现得像 struct:除非另外指定,否则 union 的成员都为 public 成员. 2: ...