C Primer Plus_第8章_字符输入输出和输入确认_编程练习
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章_字符输入输出和输入确认_编程练习的更多相关文章
- C学习笔记(八)字符输入输出和输入确认
缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...
- 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 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- C Primer Plus_第10章_数组和指针_编程练习
1. /*rain.c 针对若干年的降水量数据,计算年降水总量.年降水平均量,以及月降水平均量*/ #include <stdio.h> #define MONTHS 12 #define ...
- C Primer Plus_第三章_数据和C_复习题与编程练习
Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...
- C Primer Plus_第9章_函数_编程练习
1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) ...
- C Primer Plus 第7章 C控制语句:分支和跳转 编程练习
作业练习 1. #include <stdio.h> int main(void) { char ch; int spare, other, n; //空格,其他字符,换行 spare = ...
- C Primer Plus 第8章 字符输入/输出和验证输入 编程练习
1. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch=getchar()) != EOF) ct++; pri ...
随机推荐
- C#错过的10年
不知不觉,c#已经诞生n年了,人生有几个十年?c#就浪费了整整一个十年. 这十年里面,电脑发展缓慢,而服务端和手机发展迅速,这是一个移动和后端化的十年,而这个方向,正正是c#没有关注到的,c#把注意力 ...
- Node+Socketio实现消息群发功能
注:本博文是作者原创,转载请注明出处. 在项目中时常会使用到socketio,今天我们就来实现Node+socketio实现群发消息功能, 项目源码:https://github.com/zhangx ...
- JQuery 获得div绝对,相对位置的坐标方法
获取页面某一元素的绝对X,Y坐标 var X = $('#DivID').offset().top; var Y = $('#DivID').offset().left; 获取相对(父元素)位置: v ...
- IE下a标签后面的span元素向右浮动后错位
错误原因span放在了a标签之后 正确写法是放在之前 如下: <li><span>2016-07-29</span><a href="#" ...
- spring-初始化完成后运行指定内容
方案1:继承ApplicationListener public class InstantiationTracingBeanPostProcessor implements ApplicationL ...
- 微信JS接口
微信JS接口 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置来源:http://www.cnblogs.com/txw1958/p/ ...
- MySQL连表操作之一对多
引入 当我们在数据库中创建表的时候,有可能某些列中值内容量很大,而且重复. 例子:创建一个学生表,按学校年纪班级分,表的内容大致如下: id name partment 1 xxx x学校x年级x班级 ...
- NGUI 滑动与点击事件冲突处理
弄一个既能滑动,又能点击的Scroll View.发现弄完后不能拖动了~ 因为点击事件需要Box Collider覆盖掉了Drag Scroll View的Box Collider.注意是Drag S ...
- miniui无法传递input值
miniui获取到的值死活传不到php去处理,input框里有值php接收到却一直为空,又是查资料.测试.搜索.提问,最后才在文档中发现尼玛miniui不用name的,其实是<input tex ...
- Beta阶段第五次Scrum Meeting
情况简述 BETA阶段第二次Scrum Meeting 敏捷开发起始时间 2016/12/15 00:00 敏捷开发终止时间 2016/12/16 00:00 会议基本内容摘要 平稳推进 参与讨论人员 ...