C primer plus第8章(字符输入/输出和输入确认)习题

#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main (void)
{
    int choice;
    //void count(void);//main内部声明只能在main1内用
    while((choice = get_choice()) != 'q')
    {
        switch(choice)
        {
        case 'a':
            printf("Buy low,sell high.\n");
            break;
        case 'b':
            printf("\a");
            break;
        case 'c':
            count();
            break;
        default:
            printf("Program error!\n");
            break;
        }
    }
    printf("Bye.\n");
    return 0;
}

void count(void)
{
    int n,i;
    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for(i = 1; i <= n; i++)
        printf("%d\n", i);
    while(getchar() != '\n')
        continue;
}

char get_choice(void)
{
    int ch;
    printf("Enter the letter of your choice:\n");
    printf("a. advice              b. bell\n");
    printf("c. count               q. quit\n");
    ch = get_first();
    while((ch < 'a' || ch > 'c') && ch != 'q')
    {
        printf("Please respond with a, b, c, or q.\n");
        ch = get_first();
    }
    return ch;
}

char get_first(void)
{
    int ch;
    ch = getchar();
    while(getchar() != '\n')
    {
        continue;
    }
    return ch;
}

int get_int(void)
{
    int input;
    char ch;
    while(scanf("%d",&input) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf("不是数字\n请输入数字\n");
    }
    return input;
}


//1
#include <stdio.h>
int main (void)
{
    char ch;
    int con = 0;
    while((ch = getchar()) != EOF) {
        con++;
    }
    printf("%d",con);
    return 0;
}
//2
#include <stdio.h>
int main (void)
{
    char ch;
    int con = 0;
    while((ch = getchar()) != EOF)
    {
        con ++;
        if(ch >= 0 && ch <= 9)
        {
            printf("^%c",'A' + ch);
        }
        else if (ch >= ' ' && ch <= '~')
        {
            printf("(%c,%d)",ch,ch);
        }
        else if (ch == '\n')
        {
            printf("\n\\n");
        }
        else if (ch == '\t')
        {
            printf("\\t");
        }
        if(con%10 == 0)
        {
            printf("\n");
        }
    }
    return 0;
}
//3
#include <stdio.h>
#include <ctype.h>
int main (void)
{
    char ch;
    int uCon,lCon = 0;
    while((ch = getchar()) != EOF)
    {
        if(isupper(ch))
        {
            uCon++;
        }
        else if(islower(ch))
        {
            lCon++;
        }
    }
    printf("大写字母:%d\n小写字母:%d\n",uCon,lCon);
    return 0;
}
//4.
#include <stdio.h>
#include <ctype.h>
int main (void)
{
    char ch,perch;
    int letterCon = 0;
    int wordCon = 0;
    while((ch = getchar()) != EOF)
    {
        if(isalpha(ch))
        {
            letterCon++;
        }
        else if(isalpha(perch))
        {
            wordCon++;
        }
        perch = ch;
    }
    printf("共输入%d个字母%d个单词,平均每个单词%d个字母",letterCon,wordCon,letterCon/wordCon);
    return 0;
}
//5
#include <stdio.h>
char getFirst(void);
int main (void)
{
    int guess;
    int lowNum = 1;
    int maxNum = 100;
    char ch;
    do
    {
        if(ch == '+')
        {
            lowNum = guess;
        }
        else if (ch =='-')
        {
            maxNum = guess;
        }
        else
        {
            printf("数字大则请输入+,数字小请输入-,正确请输入 y\n");
        }
        guess = (maxNum + lowNum) / 2;
        printf("是%d?\n",guess);
    }
    while((ch = getFirst()) != 'y');
    return 0;
}

char getFirst(void)
{
    int ch;
    ch = getchar();
    while(getchar() != '\n')
        continue;
    return ch;
}

//6.
#include <stdio.h>
#include <ctype.h>
char getFirst(void);
int main (void)
{
    printf("%c",getFirst());
    return 0;
}

char getFirst(void)
{
    int ch;
    while(isblank(ch = getchar()))
    {
        ch = NULL;
        continue;
    }
    return ch;
}

7.

#include <stdio.h>
#define L1 8.75
#define L2 9.33
#define L3 10.00
#define L4 11.20
#define TAXNE300 0.85
#define TAX300 300 * TAXNE300
#define TAX450 TAX300 + 150* TAXO300
#define TAXO300 0.8
#define TAXO450 0.75
char get_first(void);
int main (void)
{
    while(1)
    {
        fflush(stdin);
        int hour = 0;
        int select;
        double pay = 0.00;
        double sph;
        do
        {
            printf("请选择工资级别\n");
            printf("a:8.75/h    b:9.33/h\n");
            printf("c:10.00/h   d:11.20/h\n");
            printf("q:quit\n");
        }
        while((select = get_first()) != 'a' && select != 'b' && select != 'c' && select != 'd' && select != 'q');
        switch(select)
        {
        case 'a':
            sph = L1;
            break;
        case 'b':
            sph = L2;
            break;
        case 'c':
            sph = L3;
            break;
        case 'd':
            sph = L4;
            break;
        case 'q':
            return;
            break;
        }
        do
        {
            printf("请输入一周的工作时间1-168小时之间的整数:");
        }
        while((scanf("%d", &hour)) != 1 || hour < 1 || hour > 168);
        //不计税金应得
        if(hour < 40)
        {
            pay = hour * sph;
        }
        else if(hour >= 40)
        {
            pay = sph * 40 + (hour - 40) * sph * 1.5;
        }
        printf("本周应得报酬税前%lf元\n",pay);
        //计算税金
        if(pay < 300)
        {
            pay = pay * TAXNE300;
        }
        else if(pay >= 300 && pay <450)
        {
            pay = TAX300 + (pay-300) * TAXO300;
        }
        else if(pay >= 450)
        {
            pay = TAX450 + (pay-450) * TAXO450;
        }
        printf("本周应得报酬税后%lf元\n",pay);
    }
    return 0;
}

char get_first(void)
{
    int ch;
    ch = getchar();
    while(getchar() != '\n')
    {
        continue;
    }
    return ch;
}

8

#include <stdio.h>
#define ADD 'a'
#define ADDSYM '+'
#define SUB 's'
#define SUBSYM '-'
#define MUL 'm'
#define MULSYM '*'
#define DIV 'd'
#define DIVSYM '/'
#define EQUALSYM '='
char get_first(void);

float enterNumber(int type);
double getAdd(float f1, float f2);
double getSub(float f1, float f2);
double getMul(float f1, float f2);
double getDiv (float f1, float f2);

int main (void)
{
    while(1)
    {
        fflush(stdin);
        char select,symble;
        float f1,f2;
        double result;
        do
        {
            printf("请选择运算类型:\n");
            printf("a:add           s:subtract\n");
            printf("m:multiply      d:divide\n");
            printf("q:quit\n");
        }
        while((select = get_first()) != 'q' && select != ADD && select != SUB && select != MUL && select != DIV);
        if(select == 'q')
        {
            return;
        }
        printf("请输入第一个数字:");
        f1 = enterNumber(select);
        printf("请输入第二个数字:");
        f2 = enterNumber(select);
        switch(select)
        {
        case ADD:
            result = getAdd(f1,f2);
            symble = ADDSYM;
            break;
        case SUB:
            result = getSub(f1,f2);
            symble = SUBSYM;
            break;
        case MUL:
            result = getMul(f1,f2);
            symble = SUBSYM;
            break;
        case DIV:
            result = getDiv(f1,f2);
            symble = DIVSYM;
            break;
        }
        printf("%f%c%f%c%f",f1,symble,f2,EQUALSYM,result);
    }
    return 0;
}
double getAdd(float f1, float f2)
{
    return f1 + f2;
}
double getSub(float f1, float f2)
{
    return f1 - f2;
}
double getMul(float f1, float f2)
{
    return f1 * f2;
}
double getDiv (float f1, float f2)
{
    return f1 / f2;
}

float enterNumber(int type)
{
    float f;
    while(scanf("%f",&f) != 1 || (type == DIV && f == 0))
    {
        printf("这不是一个数字。\n");
        printf("请输入一个数字,类似2.5,-1.78E8,或者3\n");
        if(type == DIV)
        {
            printf("该数字不能为0\n");
        }
        printf("重新输入:");
    }
    return f;
}

char get_first(void)
{
    int ch;
    ch = getchar();
    while(getchar() != '\n')
    {
        continue;
    }
    return ch;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值