BeautifulMan

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  16 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
复习题
1、putchar(getchar())是一个有效的表达式,它实现什么功能?getchar(putchar())也有效吗?
答:
语句putchar(getchar())使程序读取下一个输入字符并打印它,getchar()的返回值作为putchar()的参数。getchar(putchar())则不是合法的,因为getchar()不需要参数而putchar()需要一个参数。
2、下面的每个语句实现什么功能?
    a.putchar('H');
    b.putchar('\007');
    c.putchar('\n');
    d.putchar('\b');
答:
a. 显示字符H
b.如果系统使用ASCII字符编码,则发出一声警报
c.把光标移动到下一行的开始
d.退后一格
3、假设您有一个程序count,该程序对输入的字符进行统计。用count程序设计一个命令行命令,对文件essay中的字符进行计数并将结果保存在名为essayct的文件中。
答:
count < essay > essayct
4、给定问题3中的程序和文件,下面哪个命令是正确的?
答:
a.essayct <essay
b.count essay
c.essay >count
答:
c是正确的。
5、EOF是什么?
答:
它是由getchar()和scanf()返回的信号(一个特定的值),用来表明已经到达了文件的结尾。
6、对给出的输入,下面每个程序段的输出是什么(假定ch是int类型的,并且输入是缓冲的)?
a. 输入如下所示:
    If you quit, I will.[enter]
    程序段如下所示:
    while ((ch = getchar()) != 'i')
            putchar(ch);
b. 输入如下所示:
    Harhar[enter]
    程序段如下所示:
    while ((ch = getchar()) != '\n')
    {
               putchar(ch++);
               putchar(++ch);
    }
答:
a.If you qu
b.HJacrthjacrt
7、C如何处理具有不同文件和换行约定的不同计算机系统?
答:
C的标准I/O库把不同的文件形式映射为统一的流,这样就可以按相同的方式对它们进行处理。
8、在缓冲系统中把数值输入与字符输入相混合时,您所面临的潜在问题是什么?
答:
数字输入跳过空格和换行符,但是字符输入并不是这样。假设您编写了这样的代码:
    int score;
    char grade;
    printf("Enter the score.\n");
    scanf("%d", &score);
    printf("Enter the letter grade.\n");
    grade = getchar();
假设您输入分数98,然后按下回车键来把分数发送给程序,您同时也发送了一个换行符,它会成为下一个输入字符被读取到grade中作为等级的值。如果在字符输入之前进行了数字输入,就应该添加代码以在获取字符输入之前剔除换行字符。
编程练习
1、
#include <stdio.h>
int main(void)
{
    int ch;
    int count = 0;
    while((ch = getchar()) != EOF) // 包括换行符
        count++;
    printf("The number of characters is %d\n", count);
    return 0;
}
2、(觉得这题超难的!!!看了一些他人写的例子,简直胡说八道!!!不过还是完美解决了)
#include <stdio.h>
int main(void)
{
    int ch;
    int i = 0;

    while((ch = getchar()) != EOF)
    {
        if(ch >= 32)  // 可打印字符
        {
            putchar(ch);
            printf("/%d  ", ch);
            i++;
        }
        else if(ch == '\n')  // 打印换行符
        {
            printf("\\n");
            printf("/%d  ", ch);
            putchar(ch); // 清除输入缓冲区里面的换行符
            = 0// i置为0重新开始计数,因为题目要求每次遇到一个换行符时就要开始打印一个新行
        }
        else if(ch == '\t')  // 打印制表符
        {
            printf("\\t");
            printf("/%d  ", ch);
            i++;
        }
        else // 打印控制字符
        {
            putchar('^');
            putchar(ch + 64);
            printf("/%d  ", ch);
        }
        if(i == 10)
        {
            putchar('\n');
            i = 0;
        }
    }
    return 0;
}
运行结果如下:
I love you!
I/73   /32  l/108  o/111  v/118  e/101   /32  y/121  o/111  u/117(每行打印10个值)
!/33  \n/10(每次遇到一个换行符时就开始一个新行)
My hello world^A
M/77  y/121   /32  h/104  e/101  l/108  l/108  o/111   /32  w/119(每行打印10个值)
o/111  r/114  l/108  d/100  ^A/1  \n/10(每次遇到一个换行符时就开始一个新行)
^Z
3、
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    int ch;
    int low_count = 0, up_count = 0;

    while((ch = getchar()) != EOF)
    {
        if(islower(ch))
            low_count++;
        if(isupper(ch))
            up_count++;
    }
    printf("A number of capital letters: %d\n", up_count);
    printf("A number of lower case letters: %d\n", low_count);
    return 0;
}
4、
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
    char ch;
    long chars = 0L; // 统计单词的字符数
    int words= 0; // 单词数
    bool inword = false// 如果ch在一个单词中,则inword为true

    printf("Enter text to be analyzed: \n");
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch) && !ispunct(ch))
            chars++;
        if(!isspace(ch) && !inword)
        {
            inword = true;
            words++;
        }
        if(isspace(ch) && inword)
            inword = false;
    }
    printf("The average number of words per word: %ld\n", chars / words);
    return 0;
}
5、(二分搜索算法第一次碰见,搞了大半天了,借鉴的是CSDN-----vs9841作者的做法,不过稍微加了下工)
#include <stdio.h>
char get_choice(void);
char get_first(void);
int main(void)
{
    int low = 1, high = 100, guess = 50;
    char ch;

    printf("Pick an integer from 1 to 100. I will try to guess it\n");
    printf("Unis your number %d?\n", guess);
    while((ch = get_choice()) != 'q')
    {
        if(ch == 'a')
        {
            printf("I knew I could do it!\n");
            break;
        }
        else if(ch == 'b')
        {
            printf("It is too small!\n");
            low = guess + 1;
        }
        else if(ch == 'c')
        {
            printf("It is too big!\n");
            high = guess - 1;
        }
        guess = (low + high) / 2;
        printf("Unis your number %d?\n", guess);
    }
    printf("Done!\n");
    return 0;
}
char get_choice(void)
{
    int ch;

    printf("Enter the letter of your choice: \n");
    printf("a. right       b. too small\n");
    printf("c. too big     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;
}
6、
char get_first(void)
{
    int ch;

    while((ch = getchar()) == '\n')
        continue;
    while(getchar() != '\n')
        continue;
    return ch;
}
7、
#include <stdio.h>
#define WORK_OVERTIME 40
#define MULTIPLE 1.5
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define BASE1 (BREAK1 * RATE1)
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
char get_choice(void);
char get_first(void);
int main(void)
{
    int hour, choise;
    double total, tax, net_pay;
    double base_pay; // 基本工资等级不能用#define来定义了,因为它要随着程序而改变了,书上真是胡说八道

    while((choise = get_choice()) != 'q')
    {
        switch(choise)
        {
        case 'a':
            base_pay = 8.15;
            break;  // break只是导致程序脱离switch语句,跳到switch之后的下一条语句!!!
        case 'b':
            base_pay = 9.33;
            break;
        case 'c':
            base_pay = 10.00;
            break;
        case 'd':
            base_pay = 11.20;
            break;
        default:
            printf("Program error!\n");
            break;
        }
        printf("Please enter the hour used: ");
        scanf("%d", &hour); // 获取每周工作小时数时没有像书上那样判断,我偷懒了!!!
        if(hour <= WORK_OVERTIME)
        {
            total = hour * base_pay;
            if (total <= BREAK1)
            {
                tax = total * RATE1;
                net_pay = total - tax;
            }
            else
            {
                tax = BASE1 + (total - BREAK1) * RATE2;
                net_pay = total - tax;
            }
        }
        else
        {
            total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
            if(total <= BREAK2)
            {
                tax = BASE1 + (total - BREAK1) * RATE2;
                net_pay = total - tax;
            }
            else
            {
                tax = BASE2 + (total - BREAK2) * RATE3;
                net_pay = total - tax;
            }
        }
        printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
    }
    printf("Bye!\n");
    return 0;
}
char get_choice(void)
{
    int ch;

    printf("*****************************************************************\n");
    printf("Enter number corresponding to the desired pay rate or action:\n");
    printf("a) $8.75/hr\tb) $9.33/hr\n");
    printf("c) $10.00/hr\td) $11.20/hr\n");
    printf("q) quit\n");
    printf("*****************************************************************\n");
    printf("Please enter your choise: ");
    ch = get_first();
    while((ch < 'a' || ch > 'd') && ch != 'q')
    {
        printf("Please respond with a, b, c, d, or q.\n");
        ch = get_first();
    }
    return ch;
}
char get_first(void)
{
    int ch;

    while((ch = getchar()) == '\n')
        continue;
    while(getchar() != '\n')
        continue;
    return ch;
}
8、
#include <stdio.h>
char get_choice(void);
char get_first(void);
float get_float(void);
int main(void)
{
    char choise;
    float first_number, second_number;

    while((choise = get_choice()) != 'q')
    {
        printf("Enter first number: ");
        first_number = get_float();
        printf("Enter second number: ");
        second_number = get_float();
        switch(choise)
        {
        case 'a':
            printf("%.1f + %.1f = %.1f\n", first_number, second_number, first_number + second_number);
            break;
        case 's':
            printf("%.1f - %.1f = %.1f\n", first_number, second_number, first_number - second_number);
            break;
        case 'm':
            printf("%.1f * %.1f = %.1f\n", first_number, second_number, first_number * second_number);
            break;
        case 'd':
            if(second_number == 0)
            {
                printf("Enter a number other than 0: ");
                second_number = get_float();
                printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
            }
            else
                printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
            break;
        default:
            printf("Program error!\n");
            break;
        }
    }
    printf("Bye.\n");
    return 0;
}
char get_choice(void)
{
    int ch;

    printf("Enter the operation of your choice: \n");
    printf("a. add\ts. subtract\n");
    printf("m. multiply\td. divide\n");
    printf("q. quit\n");
    ch = get_first();
    while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
    {
        printf("Please respond with a, s, m, d, or q.\n");
        ch = get_first();
    }
    return ch;
}
char get_first(void)
{
    int ch;

    while((ch = getchar()) == '\n')
        continue;
    while(getchar() != '\n')
        continue;
    return ch;
}
float get_float(void)
{
    float input;
    char ch;

    while((scanf("%f", &input)) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not a number.\nPlease enter a ");
        printf("number, such as 2.5, -1.78E8, or 3: ");
    }
    return input;
}

posted on 2015-11-21 20:12 李阿昀 阅读(405) 评论(0)  编辑  收藏 所属分类: C Primer Plus 复习题与编程练习

只有注册用户登录后才能发表评论。


网站导航: