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. TCP/IP、Http、Socket、XMPP-从入门到深入

    TCP/IP.Http.Socket.XMPP-从入门到深入 终极iOS程序猿 2016-12-29 18:27 为了便于大家理解和记忆,我们先对这几个概念进行的介绍,然后分析他们的不同,再进行详细的 ...

  2. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  3. OpenGL在Ubuntu 14.04 中的设置与编程

    在sudo apt-get install XXX,别的教程讲的很详细了. 编写好程序需要在shell中链接 g++ teapot.c -o teapot -lglut -lGL -lGLU 此处要注 ...

  4. Unit Test测试框架中的测试的执行顺序

    [ClassInitialize()] [ClassCleanup()] [TestInitialize()] [TestMethod] [TestCleanup()] 在执行一个或多个[TestMe ...

  5. c# Newtonsoft.Json序列化和反序列 js解析

    Newtonsoft.Json下载地址:http://www.newtonsoft.com/products/json/ 参考:      http://www.cnblogs.com/yanweid ...

  6. 使用antd UI 制作菜单

    antd 主页地址:https://ant.design/docs/react/introduce 在使用过程中,不能照搬antd的组件代码,因为有些并不合适.首先,菜单并没有做跳转功能,仅仅是菜单, ...

  7. CSS备忘录

    1, 浮动框(float)会使得元素脱离文档流, 因此不占用文档空间; 因此当一个Div的子元素都为float时会导致该Div高度为0, 这一问题称为"高度塌陷" 为了解决这个问题 ...

  8. c# WebClient文件下载

    public void HttpDownload(string url, string path, ResourceType type) { using (var client = new WebCl ...

  9. BZOJ 2743: [HEOI2012]采花

    2743: [HEOI2012]采花 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 2056  Solved: 1059[Submit][Status ...

  10. BZOJ3631: [JLOI2014]松鼠的新家

    传送门 树上的差分优化,很简单的一道题,应该属于NOIP2015TGD2T3的子问题. //BZOJ 3631 //by Cydiater //2016.10.25 #include <iost ...