《The C Programming Language》答案(第一章)
P1
#include <stdio.h>
main()
{
printf("hello, world\n");
}
P2
warning: unknown escape sequence: '\c'
P3
#include <stdio.h>
main()
{
float fahr,celsius;
float lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
printf("fahr celsius\n");
while(fahr<=upper){
celsius = (5.0/9.0)*(fahr-32.0);
printf("%3.0f %6.1f\n",fahr,celsius);
fahr = fahr+step;
}
}
P4
#include <stdio.h>
main()
{
float fahr,celsius;
float lower,upper,step;
lower = 0;
upper = 300;
step = 20;
celsius = lower;
printf("celsius fahr\n");
while(celsius<=upper){
//celsius = (5.0/9.0)*(fahr-32.0);
fahr=(9.0/5.0)*celsius+32.0;
printf("%3.0f %6.1f\n",celsius,fahr);
celsius = celsius+step;
}
}
P5
#include <stdio.h>
main()
{
float fahr,celsius;
float lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = upper;
printf("fahr celsius\n");
for(fahr;fahr>=lower;fahr-=step){
celsius = (5.0/9.0)*(fahr-32.0);
//fahr=(9.0/5.0)*celsius+32.0;
printf("%3.0f %6.1f\n",fahr,celsius);
//celsius = celsius+step;
}
}
P6
#include <stdio.h>
//the answer is 1
main()
{
printf("%d\n",getchar()!=EOF);
}
P7
#include <stdio.h>
//the answer is -1
main()
{
printf("%d\n",EOF);
}
P8
#include <stdio.h>
//press ctrl+Z in windows, ctrl+D in Linux
main()
{
int tmp,emptyCount,tableCount,lineCount;
emptyCount=0;
tableCount=0;
lineCount=0;
while(1){
tmp=getchar();
if(tmp==EOF)
break;
if(tmp==' ')
++emptyCount;
if(tmp=='\t')
++tableCount;
if(tmp=='\n')
++lineCount;
}
printf("Empties: %d\nTables: %d\nLines: %d\n",emptyCount,tableCount,lineCount);
}
P9
#include <stdio.h>
//ctrl+Z in windows, ctrl+D in Linux to end the input
main(void)
{
int c, blank_flag;
blank_flag = 0;
while (1){
c = getchar();
if(c==EOF)
break;
if (c == ' ') {
if (!blank_flag) {
blank_flag = 1;
putchar(c);
}
} else {
blank_flag = 0;
putchar(c);
}
}
}
P10
#include <stdio.h>
//ctrl+Z in windows, ctrl+D in Linux to end the input.
main(void)
{
int c;
while (1){
c = getchar();
if(c==EOF)
break;
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
printf("%c", c);
}
}
P11
行首空格,空行
P12
#include <stdio.h>
//ctrl+Z in windows, ctrl+D in Linux to end the input
int main(void)
{
int c, lineflag;
lineflag = 0;
while (1){
c = getchar();
if(c==EOF)
break;
if (c == ' ' || c == '\t' || c &