1.题略
#include <stdio.h> int main(void)
{
int ch,i=;
printf("Please enter text here(end with Ctrl + Z):\n");
while (ch=getchar() != EOF)
i++;
printf("There are %d characters in text.\n", i);
return ;
}
运行结果
输入第一个Ctrl+Z时,并没有结束,下一行再输入Ctrl+Z才检测到EOF。说明我的控制台环境下,文件结尾形式是一行的开始位置Ctrl+Z,而不是任意位置的Ctrl+Z。
2.题略
/* */
#include <stdio.h>
int main(void)
{
int i=, ch; while ((ch = getchar()) != EOF)
{
if (ch == '\n')
printf("\\n%3d \n",ch); //换行符就是一个字符,同时它提示读入行缓冲区中的数据
else if (ch == '\t')
printf("\\t%3d ",ch);
else if ((ch < ' ') && (ch != '\n') && (ch != '\t'))
printf("^%c%3d ",ch+,ch);
else
printf("%-2c%3d ",ch,ch);
i++;
if(i% == )
putchar('\n');
}
}
3.题略
/*统计大小字母个数*/
#include <stdio.h>
#include <ctype.h> //关联函数isupper(),islower() int main(void)
{
int count_up = , count_low = ;
int count_other = ;
int ch; printf("Please enter some text: \n");
while ((ch = getchar()) != EOF)
{
if (isupper(ch)) //isupper(ch)函数:ch是大写字母的话,函数返回真值1
count_up++;
else if (islower(ch)) //islower(ch)函数:ch是小写字母的话,函数返回真值1
count_low++;
else
count_other++;
}
printf("There are %d upper letters\n", count_up);
printf("and %d lower letters\n", count_low);
printf("and %d other letters\n", count_other);
return ;
}
运行结果
4.题略
/*报告单词中的字母数*/
#include <stdio.h>
#include <ctype.h> int main(void)
{
int CountLet = , CountWrd =;
int ch, ch_pre=' '; printf("Please enter some text (ctrl+z to quit): \n");
while ((ch = getchar()) != EOF)
{
if (isalpha(ch))
CountLet++;
if ((isspace(ch) || ispunct(ch)) && isalnum(ch_pre))
CountWrd++;
ch_pre = ch;
}
printf("There are %d letters and %d words.\n", CountLet, CountWrd);
printf("There are %d letters in a word on average.\n", CountLet / CountWrd); return ;
}
运行结果
5.题略
/*问大小后再猜数*/
#include <stdio.h>
int main(void)
{
int min,max,mid,ch; printf("请输入被猜整数的范围:min(较小的数)和max(较大的数)\n");
scanf("%d%d",&min,&max);
printf("min = %d, max = %d\n",min, max);
mid = min + (max-min)/;
printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
while((ch = getchar ()) != 'y')
{
if (ch == 'b')
{
max = mid;
mid = min + (max-min)/;
printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
}
else if (ch == 's')
{
min = mid;
mid = min + (max-min)/;
printf("is it %d? (please enter y(yes), b(big), s(small))", mid);
}
else
{
if (ch == '\n')
continue;
else
printf("Please just enter y, b, s.\n");
}
}
printf("the number is %d!\n",mid); return ;
}
自己写的有点复杂了可能,不过运行起来还是可行的
6.题略
#include <stdio.h>
#include <ctype.h> char get_first(); int main(void)
{
printf("get_first() is %c", get_first());
} char get_first()
{
int ch;
printf("Please enter some words:\n");
while ((ch = getchar()) && (isspace(ch) == ))
continue;
ch = getchar();
return ch;
}
8.题略
/**/
#include<stdio.h>
#include<ctype.h>
float get_float(void);
char get_first(void); int main(void)
{
char select;
float num1,num2;
while()
{
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract:\n");
printf("m.multiply d.divide\n");
printf("q.quit\n");
select = get_first();
if( select != 'a' && select != 's' && select != 'm' && select != 'd')
{
printf("Bye.\n");
break;
}
printf("Enter first number:");
num1 = get_float();
printf("Enter second number:");
num2 = get_float();
while( select == 'd' && num2 == )
{
printf("Enter a number other than 0:");
num2 = get_float();
}
switch(select)
{
case 'a': printf("%.2f + %.2f = %.2f\n",num1, num2, num1 + num2); break;
case 's': printf("%.2f - %.2f = %.2f\n",num1, num2, num1 - num2); break;
case 'm': printf("%.2f * %.2f = %.2f\n",num1, num2, num1 * num2); break;
case 'd': printf("%.2f / %.2f = %.2f\n",num1, num2, num1 / num2); break;
default : break;
}
}
return();
} float get_float(void) //得到一个合适的浮点数,滤除非法数
{
float num;
char str[];
while(scanf("%f",&num)!=)
{
gets(str);
printf("%s is not a number.\n",str);
printf("Please enter a numbe, such as 2.5, -1.78E8, or 3:");
}
while ( getchar() != '\n');
return num;
} char get_first(void) //得到字符串中的第一个字符,滤除其他字符
{
int ch;
while( isspace( ch = getchar() ) );
while ( getchar() != '\n');
return ch;
}