test 1-6
验证表达式 getchar() != EOF 的值是 0 还是 1。
//test1-6
#include<stdio.h>
int main()
{
int c;
//持续输入字符,直至输入EOF
while (c = getchar() != EOF)
{
printf("when getchar is not EOF,c=%d\n", c);
}
printf("when getchar is EOF,c=%d\n", c);
}
运行结果
test 1-7
编写一个打印 EOF 值的程序
int main()
{
printf(" EOF is %d\n", EOF);
}
运行结果
test 1-8
编写一个统计空格、制表符与换行符个数的程序
int main()
{
//对字符进行计数
int char_Num, space_Num, tab_Num, enter_Num,letter_Num;
int c = 0;
char_Num = 0;
space_Num = 0;
tab_Num = 0;
enter_Num = 0;
letter_Num = 0;
while ((c=getchar()) != EOF)
{
char_Num++;
if (c == ' ')
{
space_Num++;
}
else if (c == '\t')
{
tab_Num++;
}
else if (c == '\n')
{
enter_Num++;
}
else if ((c != ' ') && (c != '\t') && (c != '\n'))
{
letter_Num++;
}
}
printf("character number is %d\n", char_Num);
printf("space number is %d\n", space_Num);
printf("tab number is %d\n", tab_Num);
printf("enter number is %d\n", enter_Num);
printf("letter number is %d\n", letter_Num);
}
test 1-9
编写一个将输入复制到输出的程序,并将其中连续的多个空格 用一个空格代 替。
int main()
{
int c;
while ((c = getchar())!= EOF)
{
putchar(c);
while (c == ' ')
{
//如果c是空格,读取下一个字符,下一个字符不是空格则输出,仍是空格就继续读取
c = getchar();
if (c != ' ')
{
putchar(c);
}
}
}
}
test 1-10
编写一个将输入复制到输出的程序,并将其中的制表符替换 为\t,把回退符 替换为\b,把反斜杠替按为\\。这样可以将制表符和回退 符以可见的方式显示出来。
int main()
{
int c;
//用getchar无法捕获回退符,使用getch可以
//getchar按下回车后才开始获取,getch是每个字符都获取
while ((c = getch()) != EOF)
{
if (c == '\t')
{
printf("\\t");
}
if (c == '\b')
{
printf("\\b");
}
if (c == '\\')
{
printf("\\");
}
putchar(c);
}
}
test 1-11
你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?
#include<stdio.h>
#define START 0
#define END 1
int main()
{
int char_Num, line_Num, word_Num;
int c = 0;
int inputFlag = START;
int lastChar = 0;
char_Num = 0;
line_Num = 0;
word_Num = 0;
while ((c = getchar()) != EOF)
{
if (char_Num == 0)
{
lastChar = c;//第一个字符,只进一次
}
char_Num++;
if (c == '\n')
{
line_Num++;
}
if (c == ' ' ||c=='\n'||c=='\t')
{
if ((lastChar != ' ') && (lastChar != '\n') && (lastChar != '\t'))
{
inputFlag = END;
}
else
{
inputFlag = START;
}
}
if (inputFlag == END)
{
word_Num++;
inputFlag = START;
}
lastChar = c;
}
printf("character number is %d\n", char_Num);
printf("line number is %d\n", line_Num);
printf("word number is %d\n", word_Num);
}
test 1-12
编写一个程序,以每行一个单词的形式打印其输入。
#include<stdio.h>
int main()
{
int c = 0;
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\t')
{
c ='\n';
}
putchar(c);
}
}
test 1-13
编写一个程序,打印输入中单词长度的直方图。水平方向的 直方图比较容易 绘制,垂直方向的直方图则要困难些
//垂直长度直方图
#include<stdio.h>
#define START 0
#define END 1
#define MAXLENGTH 20
#define MAX_WORD_NUM 20
int main()
{
int word[MAX_WORD_NUM] = { 0 };
int char_Num, word_Num, high;
int c = 0;
int i = 0;
int inputFlag = START;
int lastChar = 0;
char_Num = 0;
word_Num = 0;
high = 0;
while ((c = getchar()) != EOF)
{
if (char_Num == 0)
{
lastChar = c;//第一个字符,只进一次
}
char_Num++;
if (c != ' ' && c != '\n' && c != '\t')
{
word[i]++;
}
if (c == ' ' || c == '\n' || c == '\t')
{
if ((lastChar != ' ') && (lastChar != '\n') && (lastChar != '\t'))
{
inputFlag = END;
}
i++;
}
if (inputFlag == END)
{
word_Num++;
inputFlag = START;
}
lastChar = c;
}
printf("word number is %d\n", word_Num);
for (int i = 0; i < word_Num; i++)
{
if (word[i] > high)
{
high = word[i];
}
}
//从最高的一行开始打印(横着打印)
//每打印一行最大高度就减一
for (int j = high; j >= 0; j--)
{
for (int k = 0; k < word_Num; k++)
{
//如果单词长度小于最大长度就打印空格
if (word[k] < j)
{
printf(" ");
}
if((word[k] >= j)&&(j != 0))
{
printf(" * ");
}
if (j == 0)
{
printf("word:%d ", k);
}
}
printf("\n");
}
}
//水平直方图
#include<stdio.h>
#define START 0
#define END 1
#define MAXLENGTH 20
int main()
{
int char_Num, word_Num;
int c = 0;
int inputFlag = START;
int lastChar = 0;
char_Num = 0;
word_Num = 0;
int word[10] = { 0, };
int i = 0;
while ((c = getchar()) != EOF)
{
if (char_Num == 0)
{
lastChar = c;//第一个字符,只进一次
}
char_Num++;
if (c != ' ' || c != '\n' || c != '\t')
{
word[i]++;
}
if (c == ' ' || c == '\n' || c == '\t')
{
if ((lastChar != ' ') && (lastChar != '\n') && (lastChar != '\t'))
{
inputFlag = END;
}
else
{
inputFlag = START;
}
i++;
}
if (inputFlag == END)
{
word_Num++;
inputFlag = START;
}
lastChar = c;
}
printf("word number is %d\n", word_Num);
for (int j = 0; j < word_Num; j++)
{
printf("word %d :", j);
for (; word[j] > 0; word[j]--)
{
printf("*");
}
printf("\n");
}
}
test 1-14
编写一个程序,打印输入中各个字符出现频度的直方图。
//水平直方图
#include<stdio.h>
#define START 0
#define END 1
#define MAXLENGTH 20
int main()
{
int c = 0;
int character[26] = { 0, };
int character_Num = 26;
while ((c = getchar()) != EOF)
{
for (int i = 0; i < 26; i++)
{
if (c == ('A' + i) || c == ('a' + i))
{
character[i]++;
}
}
}
for (int j = 0; j < character_Num; j++)
{
printf("character %c :", 'A' + j);
for (; character[j] > 0; character[j]--)
{
printf("*");
}
printf("\n");
}
}
test 1-15
重新编写 1.2 节中的温度转换程序,使用函数实现温度转换计算。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
float Temperature1(float f);
float Temperature2(float c);
int main()
{
float fahr,celsius;
fahr = 0;
celsius = 0;
printf("please input fahr\n");
scanf_s("%f",&fahr);
celsius = Temperature1(fahr);
printf("%f\t %f\n", fahr, celsius);
printf("please input celsius\n");
scanf_s("%f", &celsius);
celsius = Temperature2(celsius);
printf("%f\t %f\n", fahr, celsius);
}
//华氏温度转换为摄氏温度
float Temperature1(float f)
{
return 5 * (f - 32) / 9;
}
//摄氏温度转换为华氏温度
float Temperature2(float c)
{
return (c * 9) / 5 + 32;
}