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 ...
随机推荐
- WPF之Binding
Binding就是将数据源和目标联系起来,一般来说可以是将逻辑层对象和UI层的控件对象相关联. 有连接就有通道,就可以在通道上建立相应的验证等关卡来验证数据有效性,或是其它处理工作:同时它也支持对数据 ...
- Swift学习(三):闭包(Closures)
定义 闭包(Closures)是独立的函数代码块,能在代码中传递及使用. 语法 {(parameters) -> return type in statements } 注:闭包表达式语法可以使 ...
- 在intellj idea下用sbt的坑
version : SBT 0.13.7 intellij 14 新建SBT项目以后无法编译,总是显示 Can not resolve symble stackoverflow 给出了暂时的解决办法. ...
- 学python
1.*和** def sum(*x): ans=0 for i in x: ans+=i return ans def haha(one,two): print(one,' ',two) print( ...
- 【IIS】iis6.1下添加两个ftp站点,
1,添加本地账户或密码||组 :[控制面板-->管理工具-->计算机管理器-->系统工具-->本地用户和组] 2,IIS站点目录先(添加FTP站点)[注意:多个站点多个端口] ...
- bzoj2683
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1018 Solved: 413[Submit][Status][Discuss] ...
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...
- bzoj 4318 OSU!
期望dp. 考虑问题的简化版:一个数列有n个数,每位有pi的概率为1,否则为0.求以每一位结尾的全为1的后缀长度的期望. 递推就好了. l1[i]=(l1[i-1]+1)*p[i]+0*(1-p[i] ...
- Notepad++编写Markdown
Markdown语法高亮 下载userDefineLang_markdown.xml 打开Notepad++的 Language 菜单,选中底部的 Define your language... 在 ...
- BZOJ3505 [Cqoi2014]数三角形
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...