1.
#include <stdio.h>
int main(){
char ch;
int ct = 0;
while ((ch=getchar()) != EOF)
ct++;
printf("%d characters read.", ct);
return 0;
}
2.
#include <stdio.h>
int main(){
char ch;
int ct = 0;
while ((ch = getchar()) != EOF)
{
if (ch >= 32){
putchar(ch);
printf("/%d ",ch);
ct++;
}
else if (ch == '\n'){
printf("\\n");
putchar(ch);
ct = 0; //因为要换新的一行了所以ct清0
}
else if (ch == '\t'){ printf("\\t");
ct++;
}
else //打印控制字符
{
putchar('^');
putchar(ch + 64);
printf("/%d ", ch);
}
if (ct == 10){
printf("\n");
ct = 0;
}
}
return 0;
}
3.
#include <stdio.h>
#include <ctype.h>
int main(){
char ch;
int a,b,c;
a = b = c = 0;
while ((ch = getchar()) != '#')
{
if (isupper(ch))
a++;
else if (islower(ch))
b++;
else c++;
}
printf("capital:%d lower case:%d other:%d", a, b, c);
return 0;
}
4.
#include <stdio.h>
#include <ctype.h> //用来区分字母和字母的大小写
#include <stdbool.h>
int main(){
char ch;
bool inword = false;
int n_chars = 0;
int n_words = 0;
int value;
while ((ch = getchar()) != '#')
{
if (isalpha(ch))
n_chars++;
if (!isspace(ch) && !inword)
{
inword = true; //开始一个新的单词
n_words++; //单词加1
}
if (isspace(ch) && inword)
inword = false; //单词结束
}
value = n_chars / n_words ;
printf("chars : %d\n", n_chars);
printf("words : %d\n", n_words);
printf("average value : %d", value);
return 0;
}
5.
#include <stdio.h>
//如果50太大,则50变为最大值。反之则为最小值。
int main(){
char ch;
int guess = 50, MAX = 100, MIN = 0;
printf("Uh...is your number is %d\n",guess);
while ((ch = getchar()) != 'y')
{
if (ch == 'b') {
MAX = guess;
guess = (guess + MIN) / 2;
printf("Well, then, is it %d\n",guess);
}
else if (ch == 'l') {
MIN = guess;
guess = (guess + MAX) / 2;
printf("Well, then, is it %d\n",guess);
}
else printf("Sorry, I understand only big , less , yes.\n");
while ((ch = getchar()) != '\n')
continue;
}
printf("I konw i could do it");
return 0;
}
6.
//重要!!
char get_first(void){
char ch;
while((ch = getchar()) == '\n')
continue;
while (getchar() != '\n')
continue;
return ch;
}
7.
#include <stdio.h>
char get_choice(void);
char get_first(void);
#define WORK_OVERTIME 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300.0
#define BREAK2 450.0
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))
int main(void) {
int hr;
char choice;
double wage,income,tax,wage2;
printf("------------------------------------------------------------------------\n");
printf("Enter the number number corresponding to the desired pay rate or action:\n");
printf("a) $8.15/hr b) $9.33/hr\n");
printf("c) $10.00/hr d) $11.20/hr\n");
printf("q) quit\n");
printf("------------------------------------------------------------------------\n");
printf("Please enter your choise:");
while (choice = get_choice() ){
if (choice == 'q')
break;
switch (choice){
case 'a': wage2 = 8.75;
break;
case 'b': wage2 = 9.33;
break;
case 'c': wage2 = 10.00;
break;
case 'd': wage2 = 11.20;
break;
}
printf("Please enter the hours:");
scanf("%d",&hr);
if (hr <= WORK_OVERTIME)
wage = hr * wage2;
else wage = WORK_OVERTIME * wage2 +((hr - WORK_OVERTIME) * wage2 * 1.5);
if (wage <= BREAK1){
tax = wage * RATE1;
}
else if (wage > 300 && wage <= 450 ){
tax = BASE1 + (wage - BREAK1) * RATE2;
}
else tax = BASE2 + (wage - BREAK2) * RATE3;
income = wage - tax;
printf("The wage: %.2f income:%.2lf tax:%.2lf\n", wage, income, tax);
printf("------------------------------------------------------------------------\n");
printf("Enter the number number corresponding to the desired pay rate or action:\n");
printf("1) $8.15/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("------------------------------------------------------------------------\n");
printf("Please enter your choise:");
}
return 0;
}
//get_choice 输入正确的符号
char get_choice(void){
char ch;
ch = get_first();
while (ch != 'q' && (ch < 'a' || ch > 'd')){
printf("Please enter the : a b c d q\n");
printf("Please enter your choise:");
ch = get_first();
}
return ch;
}
//get_first 读取第一个符号 扔掉其他符号
char get_first(void){
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
8.
#include <stdio.h> char get_choice(void);
char get_first(void);
double get_number(void); int main(void){
char choice;
double num1,num2,result; while ((choice = get_choice()) != 'q'){
printf("Enter the first number:");
num1 = get_number();
printf("Enter the second number:");
num2 = get_number();
if (choice == 'a') {
result = num1 + num2;
printf("%.1lf + %.1lf = %.1lf\n", num1, num2, result);
}
if (choice == 's') {
result = num1 - num2;
printf("%.1f - %.1f = %.1f\n", num1, num2, result);
}
if (choice == 'm') {
result = num1 * num2;
printf("%.1f * %.1f = %.1f\n", num1, num2, result);
}
if (choice == 'd') {
if (num2 == 0) {
printf("Enter a number other than 0: ");
num2 = get_number();
result = num1 / num2;
printf("%.1f / %.1f = %.1f\n", num1, num2, result);
} else
printf("%.1f / %.1f = %.1f\n", num1, num2, result);
}
} return 0;
} char get_choice(void){
char ch;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
ch = get_first();
while ((ch != 'a') && (ch != 's') && (ch != 'm') && (ch != 'd') && (ch != 'q')){
printf("Please choice such as a s m d q:");
ch = get_first();
}
return ch;
} char get_first(void){
char ch;
while((ch = getchar()) == '\n')//ch = getchar();之前用的的是这个,结果总是把回车给ch
continue; //还总是检查不出来问题,终于找出来问题在哪里了。!!
while (getchar() != '\n')
continue;
return ch;
} double get_number(void){
double num;
char ch;
while (scanf("%lf", &num) != 1){
while ((ch = getchar()) != '\n')
putchar(ch); //处理错误的输入。
printf("is not an number.\n");
printf("Please enter a number , such as 2.5, -1.78E8, or 3:");
}
return num;
}

C Primer Plus 第8章 字符输入/输出和验证输入 编程练习的更多相关文章

  1. 【C语言学习】《C Primer Plus》第8章 字符输入/输出和输入确认

    学习总结 1.缓冲区分为完全缓冲区(fully buffered)I/O和行缓冲区(line-buffered)I/O.对完全缓冲输入来说,当缓冲区满的时候会被清空(缓冲区内容发送至其目的地).这类型 ...

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

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

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

    1. #include <stdio.h> ; int main(void) { int min, hour, lmin; printf("请输入分钟数: \n"); ...

  4. C Primer Plus_第8章_字符输入输出和输入确认_编程练习

    1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + ...

  5. 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 ...

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

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

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

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

  8. Windows程序设计(第五版)学习:第四章 文本输出

    第四章 文本输出 1,客户区:整个应用程序窗口中没有被标题栏.边框.菜单栏.工具栏.状态栏和滚动条占用的区域.简而言之,客户区就是窗口中程序可以在上面绘制并向用户传达可视化信息的区域.   2,大多数 ...

  9. C++ Primer 5th 第2章 变量和基本类型

    *****代码在Debian g++ 5.3.1 / clang++ 3.8(C++11)下编写调试***** 由于部分编译器对标准遵循的不同以及自身额外的扩展,本章书中的少数知识点与实际实现存在偏差 ...

随机推荐

  1. AngularJS中的依赖注入

    依赖注入 | Dependency Injection 原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所 ...

  2. Mahout 系列之----共轭梯度

    无预处理共轭梯度 要求解线性方程组 ,稳定双共轭梯度法从初始解 开始按以下步骤迭代: 任意选择向量 使得 ,例如, 对 若 足够精确则退出 预处理共轭梯度 预处理通常被用来加速迭代方法的收敛.要使用预 ...

  3. Linux进程状态解析之R、S、D、T、Z、X

    文章转载自:http://hi.baidu.com/shining_pc/item/21abcb32a4d2d484c3cf2950 Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程 ...

  4. React Native之携程Moles框架

    因为支持用javascript开发原生应用,React Native一推出就受到不少公司热捧,各家都跃跃欲试.但有一个痛点是,在移动端,我们是否有必要开发多套程序:iOS.Android和H5?本次将 ...

  5. Ubuntu_ROS中应用kinect v2笔记

    Ubuntu_ROS中应用kinect v2笔记 个人觉得最重要的资料如下: 1. Microsoft Kinect v2 Driver Released http://www.ros.org/new ...

  6. Android调试工具之ADB

    Android调试工具之ADB 1.     什么是ADB adb的全称为Android Debug Bridge,顾名思义,这个是PC机与Android设备的连接桥.简单的说,就是通过adb ,PC ...

  7. 写一个dup2功能相同的函数,不能调用 fcntl 函数,并且要有出错处理

    实现的时候用到系统原来的dup函数 // mydup2.c // 2015/08/17 Lucifer Zhang version1.0 // write my own dup2 function / ...

  8. Unix - 文件中构成一个空洞的分析

    lseek函数显示地为一个打开文件设置偏移量,文件偏移量可以大于文件的当前长度,在这种情况下,对该文件的下一次写将加长该文件,并在文件中构成一个空洞,这一点是允许的.位于文件中但没有写过的字节都被读为 ...

  9. Leetcode_228_Summary Ranges

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted integer ...

  10. Android Data Binding高级用法-Observable、动态生成Binding Class(三)

    设置View的id 虽然说Data Binding这种分层模式使得我们对数据的传递简单明了,一般情况下我们可以不设置View的id,不使用findViewById即可对View进行数据上一系列的操作, ...