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;
}

C Primer Plus_第8章_字符输入输出和输入确认_编程练习的更多相关文章

  1. C学习笔记(八)字符输入输出和输入确认

    缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...

  2. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  3. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  4. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  5. C Primer Plus_第10章_数组和指针_编程练习

    1. /*rain.c 针对若干年的降水量数据,计算年降水总量.年降水平均量,以及月降水平均量*/ #include <stdio.h> #define MONTHS 12 #define ...

  6. C Primer Plus_第三章_数据和C_复习题与编程练习

    Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...

  7. C Primer Plus_第9章_函数_编程练习

    1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) ...

  8. C Primer Plus 第7章 C控制语句:分支和跳转 编程练习

    作业练习 1. #include <stdio.h> int main(void) { char ch; int spare, other, n; //空格,其他字符,换行 spare = ...

  9. C Primer Plus 第8章 字符输入/输出和验证输入 编程练习

    1. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch=getchar()) != EOF) ct++; pri ...

随机推荐

  1. 第一次使用Linux服务器所栽之坑

    一直以来想拥有一个服务器,但是一直舍不得.9.21日终于心血来潮,买了一个腾讯云服务器.对比了一下腾讯云和阿里云,发现腾讯云比较良心,不仅有学生价1元/月,而且整体价格也比阿里云便宜.之前用过一次阿里 ...

  2. VIM编辑器简单总结

    第一讲小结  1. 光标在屏幕文本中的移动既可以用箭头键,也可以使用 hjkl 字母键.         h (左移)       j (下行)       k (上行)     l (右移)  2. ...

  3. Android BLE 蓝牙编程(一)

    最近在研究这个,等我有时间来写吧! 终于在端午节给自己放个假,现在就来说说关于android蓝牙ble的 最近的学习成果吧!! 需要材料(写个简单教程吧--关于小米手环的哦!嘿嘿) Android 手 ...

  4. 神经网络和Deep Learning

    参考资料: 在线免费书籍 http://neuralnetworksanddeeplearning.com/chap1.html Chapter 1 1.  perceptron 感知机 it's a ...

  5. div

    <div id="container"> <div id="left">左边</div> <div id=" ...

  6. Java开发环境搭建——Maven配置

    创建Java应用程序项目mvn archetype:generate -DgroupId=com.liq -DartifactId=firstApp -DarchetypeArtifactId=mav ...

  7. Alpha阶段发布说明

    Alpha版本功能介绍 机器法官功能已实现 这是我们统计了当下所有存在的狼人APP的共同缺点.也是用户最主要的痛点.现在所有已知存在的类似APP都不能提供法官功能,我们的APP将该功能革命性的自动实现 ...

  8. jQuery 复习

    jQuery 复习 基础知识 1, window.onload $(function(){});   $(document).ready(function(){}); 只执行函数体重的最后一个方法,事 ...

  9. druid数据库密码加密程序编写

    第一步:引入 druid-1.0.1.jar 架包 第二步: 编写程序 package nihao; import com.alibaba.druid.filter.config.ConfigTool ...

  10. Echart的简单例子

    [转载自:http://echarts.baidu.com/echarts2/doc/start.html] <%@ page language="java" content ...