BeautifulMan

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  16 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks

2015年11月14日 #

     摘要: 复习题1、将下列十进制数转换为二进制形式:a. 3b. 13c. 59d. 119答:a. 11b. 1101c. 111011d. 11101112、将下列二进制值转换为十进制、八进制和十六进制形式:a. 00010101b. 01010101c. 01001100d. 10011101答:a. 21, 025, 0x15b. 85, 0125, 0x55c. 76, 0114, 0x4Cd. ...  阅读全文
posted @ 2016-01-06 09:27 李阿昀 阅读(457) | 评论 (0)编辑 收藏

这是王爽老师的《汇编语言(第3版)》,经知友推荐确实是一本极好的书!

实验4 [bx]和loop的使用
(1)、(2)
assume cs:code
code segment
    mov ax,0020h
    mov ds,ax
    mov bx,0
    mov cx,64
  s:mov [bx],bl   ;这里必须是mov [bx],bl,而不能是mov [bx],bx,否则会出现类型不匹配
    inc bl
    loop s
    mov ax,4c00h
    int 21h
code ends
end









posted @ 2015-12-15 09:06 李阿昀 阅读(318) | 评论 (0)编辑 收藏

     摘要: 复习题1、以下模板有什么错误?Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->structure {    char itable;    int&nb...  阅读全文
posted @ 2015-12-10 16:53 李阿昀 阅读(1541) | 评论 (0)编辑 收藏

     摘要: 书中的一个例子,我也是想了半天了!!!有点难度!!!Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/* 把多个文件的内容追加到一个文件中 */#include <stdio.h>#include &...  阅读全文
posted @ 2015-12-07 12:02 李阿昀 阅读(1038) | 评论 (0)编辑 收藏

     摘要: 复习题1、哪一存储类生成的变量对于包含他们的函数来说是局部变量?答:自动存储类、寄存器存储类和静态空链接存储类2、哪一存储类的变量在包含它们的程序运行时期内一直存在?答:静态空链接存储类、静态内部链接存储类和静态外部链接存储类3、哪一存储类的变量可以在多个文件中使用?哪一存储类的变量只限于在一个文件中使用?答:静态外部链接存储类和静态内部链接存储类4、代码块作用域变量具有哪种链接?答:空链接5、关...  阅读全文
posted @ 2015-12-04 20:03 李阿昀 阅读(475) | 评论 (0)编辑 收藏

     摘要: 今天学到了一个新知识——选择排序算法核心思想:(查找和放置)选择剩余最大值的一个办法就是比较剩余数组的第一和第二个元素。如果第二个元素大,就交换这两个数据。现在比较第一个和第三个元素。如果第三个大,就交换这两个数据。每次交换都把大的元素移到上面。继续这种方法,直到比较第一个和最后一个元素。完成以后,最大的数就在剩余数组的第一个元素中。此时第一个元素已经排好了序,但是数组中的...  阅读全文
posted @ 2015-11-30 09:47 李阿昀 阅读(775) | 评论 (0)编辑 收藏

     摘要: 这一章感觉好难啊!!!学习笔记:(关于指针和多维数组)Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// 多维数组和指针#include <stdio.h>int main(void){  ...  阅读全文
posted @ 2015-11-24 22:31 李阿昀 阅读(806) | 评论 (0)编辑 收藏

问题:编写一个函数将一个整数转换成二进制形式?(扩展请移步编程练习9)
#include <stdio.h>
void to_binary(unsigned long n);
int main(void)
{
    unsigned long number;
    printf("Enter an integer (q to quit): \n");
    while(scanf("%lu", &number) == 1)
    {
        printf("Binary equivalent: ");
        to_binary(number);
        putchar('\n');
        printf("Enter an integer (q to quit): \n");
    }
    printf("Done!\n");
    return 0;
}
void to_binary(unsigned long n)
{
    int r;
    r = n % 2;
    if(n >= 2)
        to_binary(n / 2);
    putchar('0' + r);
    return;
}
复习题
1、实际参数和形式参量有何不同?
答:
形式参量(也被称为形式参数)是一个变量,它在被调函数中进行定义。实际参数是在函数调用中出现的值,它被赋值给形式参量。可以把实际参数认为是在函数被调用时用来初始化形式参量的值。
2、写出下面所描述的各个函数的ANSI函数头。注意:只写出函数头即可,不需要实现。
a.donut()接受一个int类型的参数,然后输出若干个0,输出0的数目等于参数的值。
b.gear()接受两个int类型的参数并返回int类型的值。
c.stuff_it()的参数包括一个double类型的值以及一个double类型变量的地址,功能是把第一个数值存放到指定的地址中。
答:
a.void donut(int n)
b.int gear(int n, int m)
c.void stuff_it(double n, double * d)
3、只写出下列函数的ANSI C函数头,不需要实现函数。
a.n_to_char()接受一个int类型的参数并返回一个char类型的值。
b.digits()接受的参数是一个double类型的数值和一个int类型的数值,返回值类型是int。
c.random()不接受参数,返回int类型的数值。
答:
a.char n_to_char(int n)
b.int digits(double n, int m)
c.int random(void)
4、设计一个实现两整数相加并将结果返回的函数。
答:
int plus(int n, int m)
{
    return n + m;
}
5、假如问题4中的函数实现两个double类型的数值相加,那么应该如何修改原函数?
答:
double plus(double n, double m)
{
    return n + m;
}
6、设计函数alter(),其输入参数是两个int类型的变量x和y,功能是分别将这两个变量的数值改为它们的和以及它们的差。
答:(注意:下面这种写法是错误的!!!)
void alter(int x, int y)
{
    x = x + y;
    y = x - y;
}
正确的写法如下:
void alter(int * u, int * v)
{
    int temp;

    temp = *u;
    *u = *u + *v;
    *v = temp - *v;
}
7、判断下面的函数定义是否正确。
void salami(num)
{
    int num, count;

    for(count = 1; count <= num; num++)
        printf("O salami mio!\n");
}
答:
有错误。num应该在salami()的参数列表中而不是在花括号之后声明,而且应该是count++而不是num++。
8、编写一个函数,使其返回3个整数参数中的最大值。
答:
int max(int x, int y, int z)
{
    int max;
    if(x > y)
        if(x > z)
            max = x;
        else
            max = z;
    else
        if(y > z)
            max = y;
        else
            max = z;
    return max;
}
or (更简洁一点)
int max(int x, int y, int z)
{
    int max = x;
    if(y > max)
        max = y;
    if(z > max)
        max = z;
    return max;
}
9、给定下面的输出:
Please choose one of the following:
1)copy files 2)move files
3)remove files 4)quit
Enter the number of your choice:
a.用一个函数实现菜单的显示,且该菜单有4个用数字编号的选项并要求你选择其中之一(输出应该如题中所示)。
b.编写一个函数,该函数接受两个int类型的参数:一个上界和一个下界。在函数中,首先从输入终端读取一个整数,如果该整数不在上下界规定的范围内,则函数重新显示菜单(使用本题目a部分中的函数)以再次提醒用户输入新值。如果输入数值在规定的范围内,那么函数应该将数值返回给调用函数。
c.使用本题目a和b部分中的函数编写一个最小的程序。最小的意思是该程序不需要实现菜单中所描述的功能;它只需要显示这些选项并能获取正确的响应即可。
答:(参考课后答案)
#include <stdio.h>
void menu(void);
int get_input(intint);
int main(void)
{
    int res;

    menu();
    while((res = get_input(1, 4)) != 4)
        printf("I like choice %d.\n", res);
    printf("Bye!\n");
    return 0;
}
void menu(void)
{
    printf("Please choose one of the following: \n");
    printf("1)copy files          2)move files\n");
    printf("3)remove files        4)quit\n");
    printf("Enter the number of your choice: \n");
}
int get_input(int min, int max)
{
    int number;

    scanf("%d", &number);
    while(number < min || number > max)
    {
        printf("%d is not a valid choice; try again.\n", number);
        menu();
        scanf("%d", &number);
    }
    return number;
}
编程练习
1、
#include <stdio.h>
double min(doubledouble);
int main(void)
{
    printf("One of the smaller of the two numbers is %.2f", min(23.34, 12.11));
    return 0;
}
double min(double x, double y)
{
    return x < y ? x : y;
}
2、
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
    chline('$', 3, 5);
    return 0;
}
void chline(char ch, int i, int j)
{
    int index;

    for(index = 1; index < i; index++)
        putchar(' ');
    for(index = 1; index <= j - i + 1; index++)
        putchar(ch);
}
3、
#include <stdio.h>
void chline(char ch, int col, int row);
int main(void)
{
    chline('$', 3, 5);
    return 0;
}
void chline(char ch, int col, int row)
{
    int i, j;

    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
           putchar(ch);
        putchar('\n');
    }
}
4、
#include <stdio.h>
double computer(double a, double b);
int main(void)
{
    printf("%.2f和%.2f的谐均值是:%.3f\n", 0.3, 0.5, computer(0.3, 0.5));
    return 0;
}
double computer(double a, double b)
{
    double result;

    result = 1 / ((1/a + 1/b) / 2);
    return result;
}
5、
#include <stdio.h>
void larger_of(double *, double *);
int main(void)
{
    double x = 23.3;
    double y = 34.4;
    printf("Originally x = %.1f; y = %.1f\n", x, y);
    larger_of(&x, &y);
    printf("Now x = %.1f; y = %.1f\n", x, y);
    return 0;
}
void larger_of(double * u, double * v)
{
    double temp;
    temp = *u > *v ? *u : *v;
    *u = temp;
    *v = temp;
}
6、(第一次码的程序读取到换行符的时候也会打印出来,会给人看不明白的感觉,索性按[Enter]键的时候就退出循环,不要读到EOF)
#include <stdio.h>
#include <ctype.h>
void printchar(char ch);
int main(void)
{
    char ch;

    printf("请输入要分析的东西:\n");
    while((ch = getchar()) != EOF)
    {
        printchar(ch);
    }
    return 0;
}
void printchar(char ch)
{
    if(isalpha(ch))
    {
        printf("%c %d\n", ch, toupper(ch) % 'A' + 1);
    }
}
修改之后,程序如下:
#include <stdio.h>
#include <ctype.h>
int show_c_location(char ch);

int main(void)
{
    char ch;

    printf("Please enter some characters: \n");
    while((ch = getchar()) != '\n')
        printf("%c-%d ", ch, show_c_location(ch));
    return 0;
}
int show_c_location(char ch)
{
    int result;

    if(isalpha(ch))
        result = toupper(ch) - 'A' + 1;
    else
        result = -1;
    return result;
}
7、
#include <stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the positive integer power");
    printf(" to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while(scanf("%lf%d", &x, &exp) == 2)
    {
        xpow = power(x, exp);
        printf("%.3g to power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");
    return 0;
}
double power(double n, int p)
{
    int i;
    double result = 1;

    if(n != 0)
    {
        if(p > 0)
        {
            for(i = 1; i <= p; i++)
                result *= n;
        }
        else if(p < 0)
        {
            for(i = 1; i <= -p; i++)
                result *= (1 / n);
        }
        else
            result = 1;
    }
    else
    {
        if(p == 0)
            result = 1;// 0的0次方是一个有争议的数,本题认为会得到1
        else
            result = 0;
    }
    return result;
}
8、
#include <stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the positive integer power");
    printf(" to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while(scanf("%lf%d", &x, &exp) == 2)
    {
        xpow = power(x, exp);
        printf("%.3g to power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");
    return 0;
}
double power(double n, int p)
{
    double result = 1;

    if(n != 0)
    {
        if(p > 0)
            result = n * power(n, p-1);
        else if(p < 0)
            result = (1/n) * power(n, p+1);
        else
            result = 1;
    }
    else
    {
        if(p == 0)
            result = 1;// 0的0次方是一个有争议的数,本题认为会得到1
        else
            result = 0;
    }
    return result;
}
9、
#include <stdio.h>
void to_base_n(unsigned long n, int range);
int main(void)
{
    unsigned long number;
    int range;
    printf("请输入要转换的无符号整数和所规定的进制数: \n");
    while(scanf("%lu %d", &number, &range) == 2)
    {
        if(range >= 2 && range <= 10)
        {
            printf("无符号整数%lu转换成%d进制数为: ", number, range);
            to_base_n(number, range);
            putchar('\n');
            printf("请输入要转换的无符号整数和所规定的进制数: \n");
        }
        else
            printf("所规定的进制数的范围是2~10,请输入正确的数字\n");
    }
    printf("Done!\n");
    return 0;
}
void to_base_n(unsigned long n, int range)
{
    int r;

    r = n % range;
    if(n >= range)
        to_base_n(n / range, range);
    putchar('0' + r);
    return;
}
10、(题意理解不清楚,借鉴CSDN——vs9841原作者的做法,脑子太笨,实在想不出来)
#include <stdio.h>
int Fibonacci(int n);
int main(void)
{
    int n = 9;
    printf("当n为%d时,斐波纳契数值为%d", n, Fibonacci(9));
    return 0;
}
int Fibonacci(int n)
{
    int a, b, i;
    a = 0;
    b = 1;
    int sum;
    if(n == 0)
        return 0;
    if(n == 1)
        return 1;
    else
    {
        for(i = 2; i <= n; i++)
        {
            sum = a + b;
            a = b;
            b = sum;
        }
        return sum;
    }
}
总结:总体来说编程练习相对以往来说要简单了,但第10题没明白什么意思,所以只能借鉴别人的了,真是天下文章一大抄!
posted @ 2015-11-22 23:03 李阿昀 阅读(1022) | 评论 (0)编辑 收藏

复习题
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 @ 2015-11-21 20:12 李阿昀 阅读(407) | 评论 (0)编辑 收藏

     摘要: 复习题1、确定哪个表达式为true,哪个为false。Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    a.100 > 3 && 'a' >&nbs...  阅读全文
posted @ 2015-11-20 00:02 李阿昀 阅读(555) | 评论 (0)编辑 收藏

     摘要: 复习题1、给出每行之后quack的值Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    int quack = 2;    quack +...  阅读全文
posted @ 2015-11-19 14:04 李阿昀 阅读(586) | 评论 (0)编辑 收藏

复习题
1、假定所有的变量都是int类型。找出下面每一个变量的值:
a.x = (2 + 3) * 6;
b.x = (12 + 6) / 2 * 3;
c.y = x = (2 + 3) / 4;
d.y = 3 + 2 * (x = 7 / 2);
答:
a.
x = 30
b.
x = 27
c.
x = 1
y = 1
d.
x = 3
y = 9
2、假定所有的变量都是int类型。找出下面每一个变量的值:
a.x = (int) 3.8 + 3.3;
b.x = (2 + 3) * 10.5;
c.x = 3 / 5 * 22.0;
d.x = 22.0 * 3 /5;
答:
a.
x = 6
b.
x = 52
c.
x = 0
d.
x = 13
3、您怀疑下面的程序里有一些错误。您能找出这些错误吗?
 1 int main(void)
 2 {
 3     int i = 1,
 4     float n;
 5     printf("Watch out! Here come a bunch of fractions!\n");
 6     while(i < 30)
 7         n = 1/i;
 8         printf("%f", n);
 9     printf("That's all, folks!\n");
10     return;
11 }
答:
第0行:应该有#include <stdio.h>。
第3行:应该以分号而不是逗号结尾。
第6行:while语句建立一个无限循环。因为i的值保持为1,所以它总是小于30。推测一下它的意思大概是要写成while(i++ < 30)。
第6到8行:这样的缩排说明我们想要使第7行和8行组成一个代码块,但是缺少了花括号会使while循环只包括第7行。应该添加花括号。
第7行:因为1和i都是整数,所以当i为1时除法运算的结果会是1,而当i为更大的数时结果为0。使用n = 1.0/i;会使i在进行除法运算之前先转换为浮点数,这样就会产生非0的答案。
第8行:我们在控制语句中漏掉了换行符(\n),这会使数字只要可能就在一行中打印。
第10行:应该是return 0;。
下面是一个正确的版本:
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int i = 1;
 5     float n;
 6     printf("Watch out! Here come a bunch of fractions!\n");
 7     while(i++ < 30)
 8     {
 9         n = 1.0/i;
10         printf("%f\n", n);
11     }
12     printf("That's all, folks!\n");
13     return 0;
14 }
4、这是程序清单5.9的其他一种设计方法。表面上看,它使用了一个scanf()函数替代了程序清单5.9中的两个scanf()。但是该程序不令人满意。和程序清单5.9相比,它有什么缺点?
 1 #include <stdio.h>
 2 #define S_TO_M 60
 3 int main(void)
 4 {
 5     int sec, min, left;
 6     printf("This program converts seconds to minutes and ");
 7     printf("seconds.\n");
 8     printf("Just enter the number of seconds.\n");
 9     printf("Enter 0 to end the program.\n");
10     while(sec > 0)
11     {
12         scanf("%d", &sec);
13         min = sec / S_TO_M;
14         left = sec % S_TO_M;
15         printf("%d sec is %d min, %d sec.\n", sec, min, left);
16         printf("Next input?\n");
17     }
18     printf("Bye!\n");
19     return 0;
20 }
答:(参考课后答案)
主要问题在于判断语句(sec是否大于0?)和获取sec值的scanf()语句之间的关系。具体地说,第一次进行判断时,程序还没有机会来获得sec的值,这样就会对碰巧处在那个内存位置的一个垃圾值进行比较。一个比较笨拙的解决方法是对sec进行初始化,比如把它初始化为1,这样就可以通过第一次判断。但是还有一个问题,当最后输入0来停止程序时,在循环结束之前不会检查sec,因而0秒的结果也被打印出来。更好的方法是使scanf()语句在进行while判断之前执行。可以通过像下面这样改变程序的读取部分来做到这一点:
1 scanf("%d", &sec);
2 while(sec > 0)
3 {
4      min = sec / S_TO_M;
5      left = sec % S_TO_M;
6      printf("%d sec is %d min, %d sec.\n", sec, min, left);
7      printf("Next input?\n");
8      scanf("%d", &sec);
9 }
第一次获取输入使用循环外部的scanf(),以后就使用在循环结尾处(也即在循环再次执行之前)的scanf()语句。这是处理这类问题的一个常用方法。
5、下面的程序将打印什么?
 1 #include <stdio.h>
 2 #define FORMAT "%s! C is cool!\n"
 3 int main(void)
 4 {
 5     int num = 10;
 6 
 7     printf(FORMAT, FORMAT);
 8     printf("%d\n", ++num);
 9     printf("%d\n", num++);
10     printf("%d\n", num--);
11     printf("%d\n", num);
12     return 0;
13 }
答:
%s! C is cool!
! C is cool!
11
11
12
11
6、下面的程序将打印什么?
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     char c1, c2;
 5     int diff;
 6     float num;
 7 
 8     c1 = 'S';
 9     c2 = 'O';
10     diff = c1 - c2;
11     num = diff;
12     printf("%c%c%c: %d %3.2f\n", c1, c2,c1, diff, num);
13     return 0;
14 }
答:
SOS: 4 4.00
7、下面的程序将打印出什么?
 1 #include <stdio.h>
 2 #define TEN 10
 3 int main(void)
 4 {
 5     int n = 0;
 6     while(n++ < TEN)
 7         printf("%5d", n);
 8     printf("\n");
 9     return 0;
10 }
答:
   1   2   3   4   5   6   7   8   9   10(注意:每个数字占据5列的宽度)  
8、修改上一个程序,让它打印从a到g的字母。
答:
 1 #include <stdio.h>
 2 #define CHARACTER 'g'
 3 int main(void)
 4 {
 5     char ch = 'a' - 1;
 6     while(ch++ < CHARACTER)
 7         printf("%3c", ch);
 8     printf("\n");
 9     return 0;
10 }
9、如果下面的片段是一个完整程序的一部分,它们将打印出什么?
a.
1 int x = 0;
2 while(++x < 3)
3    printf("%4d", x);
b.(注意:使第二个printf()语句缩进并不能使它成为while循环的一部分。因此它只是在while循环结束之后被调用一次,我看成一个代码块了)
1 int x = 100;
2 
3 while(x++ < 103)
4    printf("%4d\n", x);
5    printf("%4d\n", x);
c.
1 char ch = 's';
2 
3 while(ch < 'w')
4 {
5     printf("%c", ch);
6     ch++;
7 }
8 printf("%c\n", ch);
答:
a.
   1   2
b.
 101
 102
 103
 104
c.
stuvw
10、下面的程序将打印什么?
 1 #define MESG "COMPUTER BYTES DOG"
 2 #include <stdio.h>
 3 int main(void)
 4 {
 5     int n = 0;
 6 
 7     while(n < 5)
 8         printf("%s\n", MESG);
 9         n++;
10     printf("That's all.\n");
11     return 0;
12 }
答:
这是一个构造有缺陷的程序。因为while语句没有使用花括号,只有printf()语句作为循环的一部分,所以程序无休止地打印消息COMPUTER BYTES DOG直到您强行关闭程序为止。
11、构造完成下面功能(或者用一个术语来说,有下面的副作用)的语句:
a.把变量x的值增加10
b.把变量x的值增加1
c.将a与b之和的两倍赋给c
d.将a与两倍的b之和赋给c
答:
a.x = x + 10;
b.x++; or ++x; or x = x + 1;
c.c = (a + b) * 2;
d.c = a + 2 * b;
12、构造具有下面功能的语句:
a.把变量x的值减1
b.把n除以k所得的余数赋给m
c.用b减去a的差去除q,并将结果赋给p
d.用a与b的和除以c与d的乘积,并将结果赋给x
答:
a.x--; or --x; or x = x - 1;
b.m = n % k;
c.p = q / (b - a);
d.x = (a + b) / (c * d);
编程练习
1、
 1 #include <stdio.h>
 2 const int PARAM = 60;
 3 int main(void)
 4 {
 5     int min, hour, left;
 6 
 7     printf("Convert minutes to hours and minutes!\n");
 8     printf("Enter the number of minutes (<=0 to quit):\n");
 9     scanf("%d", &min);
10     while(min > 0)
11     {
12         hour = min / PARAM;
13         left = min % PARAM;
14         printf("%d minutes is %d hours, %d minutes.\n", min, hour, left);
15         printf("Enter next value (<=0 to quit): \n");
16         scanf("%d", &min);
17     }
18     printf("Done!\n");
19     return 0;
20 }
2、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int number, maxnum;
 5     printf("Please enter a int number:\n");
 6     scanf("%d", &number);
 7     maxnum = number + 10;
 8     while(number <= maxnum)
 9     {
10         printf("%5d", number++);
11     }
12     printf("\n");
13     return 0;
14 }
3、(与题目1类似)
 1  #include <stdio.h>
 2  const int PARAM = 7;
 3  int main(void)
 4  {
 5      int day, week, left;
 6 
 7      printf("Convert days to weeks and days!\n");
 8      printf("Enter the number of days (<=0 to quit):\n");
 9      scanf("%d", &day);
10      while(day > 0)
11      {
12          week = day / PARAM;
13          left = day % PARAM;
14          printf("%d days are %d weeks, %d days.\n", day, week, left);
15          printf("Enter next value (<=0 to quit): \n");
16          scanf("%d", &day);
17      }
18      printf("Done!\n");
19      return 0;
20 }
4、
 1  #include <stdio.h>
 2  #define CM_PER_INCH 0.3937
 3  #define FEET_PER_INCH 12
 4  int main(void)
 5  {
 6      float cm, inch, left;
 7      int feet;
 8 
 9      printf("Enter a height in centimeters: ");
10      scanf("%f", &cm);
11      while(cm > 0)
12      {
13          inch = cm * CM_PER_INCH;
14          feet = inch / FEET_PER_INCH;
15          left = (inch / FEET_PER_INCH - feet) * FEET_PER_INCH ;
16          printf("%.1f cm = %d feet, %.1f inches.\n", cm, feet, left);
17          printf("Enter a height in centimeters (<=0 to quit): ");
18          scanf("%f", &cm);
19      }
20      printf("bye\n");
21      return 0;
22 }
5、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int count, sum, limit;
 5     count = 0;
 6     sum = 0;
 7 
 8     printf("Please enter a limit number: ");
 9     scanf("%d", &limit);
10     while(count++ < limit)
11     {
12         sum = sum + count;
13     }
14     printf("sum = %d\n", sum);
15     return 0;
16 }
6、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int count, sum, limit;
 5     count = 0;
 6     sum = 0;
 7 
 8     printf("Please enter a limit number: ");
 9     scanf("%d", &limit);
10     while(count++ < limit)
11     {
12         sum = sum + count * count;
13     }
14     printf("sum = %d\n", sum);
15     return 0;
16 }
7、
 1 #include <stdio.h>
 2 float cube(float num);
 3 int main(void)
 4 {
 5     float number;
 6     printf("Please enter a number: ");
 7     scanf("%f", &number);
 8     printf("The cube of the %.2f is %.2f", number, cube(number));
 9     return 0;
10 }
11 float cube(float num)
12 {
13     return num * num * num;
14 }
8、(注意:我用到了<string.h>头文件中的getchar()函数,还是用目前的知识弄不出来,啊!
 1 #include <stdio.h>
 2 #include <string.h>
 3 const float ONE_PARAM = 1.8;
 4 const float TWO_PARAM = 32.0;
 5 const float THREE_PARAM = 273.16;
 6 void temperatures(double fahrenheit);
 7 int main(void)
 8 {
 9     float number;
10     while(1==1)
11     {
12         printf("Please again enter a fahrenheit's temperature: ");
13         scanf("%f", &number);
14         if(getchar() == 'q')
15         {
16             break;
17         }
18         temperatures(number);
19     }
20     printf("Done!\n");
21     return 0;
22 }
23 void temperatures(double fahrenheit)
24 {
25     float celsius, kelvin;
26     celsius = ONE_PARAM * fahrenheit + 32.0;
27     kelvin = celsius + 273.16;
28     printf("fahrenheit is %.2f, celsius is %.2f, kelvin is %.2f.\n", fahrenheit, celsius, kelvin);
29 }
今天看到第6章 循环部分,原来可以这样做,是我读书太不用心了!
 1  #include <stdio.h>
 2  #include <string.h>
 3  const float ONE_PARAM = 1.8;
 4  const float TWO_PARAM = 32.0;
 5  const float THREE_PARAM = 273.16;
 6  void temperatures(double fahrenheit);
 7  int main(void)
 8  {
 9     float number;
10 
11     printf("Please again enter a fahrenheit's temperature: ");
12     while(scanf("%f", &number) == 1)
13     {
14         temperatures(number);
15         printf("Please again enter a fahrenheit's temperature: ");
16     }
17     printf("Done!\n");
18     return 0;
19 }
20 void temperatures(double fahrenheit)
21 {
22     float celsius, kelvin;
23     celsius = ONE_PARAM * fahrenheit + 32.0;
24     kelvin = celsius + 273.16;
25     printf("fahrenheit is %.2f, celsius is %.2f, kelvin is %.2f.\n", fahrenheit, celsius, kelvin);
26 }
posted @ 2015-11-14 16:33 李阿昀 阅读(458) | 评论 (0)编辑 收藏

复习题
1、再次运行程序清单4.1,但是在要求您输入名字时,请输入您的名字和姓氏。发生了什么?为什么?
答:
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define DENSITY 62.4
 4 int main(void)
 5 {
 6     float weight, volume;
 7     int size, letters;
 8     char name[40]; // name是一个有40个字符的数组
 9 
10     printf("Hi! What's your first name?\n");
11     scanf("%s", name);
12     printf("%s, what's your weight in pounds?\n", name);
13     scanf("%f", &weight);
14     size = sizeof(name);
15     letters = strlen(name);
16     volume = weight / DENSITY;
17     printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
18     printf("Also, your first name has %d letters, \n", letters);
19     printf("and we have %d bytes to store it in.\n", size);
20     return 0;
21 }
如果输入名字和姓氏,会输出如下结果:
Hi! What's your first name?
Li Ayun
Li, what's your weight in pounds?
Well, Li, your volume is 0.00 cubic feet.
Also, your first name has 2 letters,
and we have 40 bytes to store it in.
原因:(参考课后答案)程序不能正常工作。第一个scanf()语句只是读入您的名而没有读入您的姓,您的姓依然存储在输入“缓冲区”(缓冲区只是一块用来存放输入的临时存储区域)中。当下一个scanf()语句想要读入您的体重时,它从上次读入结束的地方开始,这样就试图把您的姓作为体重来读取。这会使scanf()失败。一方面,如果您对姓名请求做出像Li 123这样的响应,程序会使用123作为您的体重,虽然您是在程序请求体重之前输入123的。
2、假定下列每个示例都是某个完整程序的一部分。它们的打印结果分别是什么?
a.printf("He sold the painting for $%2.2f.\n", 2.345e2);
b.printf("%c%c%c\n", 'H', 105, '\41');
c.#define Q "His Hamlet was funny without being vulgar. "
         printf("%s\nhas %d characters.\n", Q, strlen(Q));
d.printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);
答:
a.He sold the painting for $234.50.
b.Hi!
c.His Hamlet was funny without being vulgar. (注意,与课后答案不一样,是因为细看题目的话,此句末尾有一个空格,strlen()函数输出字符串中字符(包括空格和标点符号)的准确数目)
   has 43 characters.
d.Is 1.20e+003 the same as 1201.00?
3、在问题2c中,应进行哪些修改以使字符串Q引在双引号中输出?
答:
使用\"。示例如下:
printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q));
4、找出下列程序中的错误。
 1 define B booboo
 2 define X 10
 3 main(int)
 4 {
 5     int age;
 6     char name;
 7 
 8     printf("Please enter your first name. ");
 9     scanf("%s", name);
10     printf("All right, %c, what's your age?\n", name);
11     scanf("%f", age);
12     xp = age + X;
13     printf("That's a %s! You must be at least %d.\n", B, xp);
14     rerun 0;
15 }
答:
下面是一个正确的版本:
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define B "booboo"
 4 #define X 10
 5 int main(void)
 6 {
 7     int age;
 8     int xp;
 9 
10     char name[40];
11 
12     printf("Please enter your first name. \n");
13     scanf("%s", name);
14     printf("All right, %s, what's your age?\n", name);
15     scanf("%d", &age);
16     xp = age + X;
17     printf("That's a %s! You must be at least %d.\n", B, xp);
18     return 0;
19 }
5、假设一个程序这样开始:
1 #define BOOK "War and Peace"
2 int main(void)
3 {
4     float cost = 12.99;
5     float precent = 80.0;
请构造一个printf()语句,使用BOOK、cost和percent打印下列内容:
This copy of "War and Peace" sells for $12.99.
That is 80% of list.
答:
printf("This copy of \"%s\" sells for $%.2f.\nThat is %.0f%% of list.", BOOK, cost, percent);
6、您会使用什么转换说明来打印下列各项内容?
a.一个字段宽度等于数字位数的十进制整数。
b.一个形如8A、字段宽度为4的十六进制整数。
c.一个形如232.346、字段宽度为10的浮点数。
d.一个形如2.33e+002、字段宽度为12的浮点数。
e.一个字段宽度为30、左对齐的字符串。
答:
a.%d
b.%4X
c.%10.3f
d.%12.2e
e.%-30s
对于浮点数来说,字段宽度包含了小数点右边的数字的数目
7、您会使用哪个转换说明来打印下列各项内容?
a.一个字段宽度为15的unsigned long整数
b.一个形如0x8a、字段宽度为4的十六进制整数
c.一个形如2.33E+02、字段宽度为12、左对齐的浮点数
d.一个形如+232.346、字段宽度为10的浮点数
e.一个字符串的前8个字符,字段宽度为8字符
答:
a.%15lu
b.%#4x(字段宽度应放在#和x之间)
c.%-12.2E("-"修饰符使浮点数左对齐输出)
d.%+10.3f
e.%-8.8s("-"修饰符使文本左对齐输出)
8、您会使用什么转换说明来打印下列各项内容?
a.一个字段宽度为6、最少有4位数字的十进制整数
b.一个字段宽度在参数列表中给定的八进制整数
c.一个字段宽度为2的字符
d.一个形如+3.13、字段宽度等于数字中字符个数的浮点数
e.一个字符串的前5个字符,字段宽度为7、左对齐
答:
a.%6.4d
b.%*o(此处为小写字母o,而不是数字0)
c.%2c
d.%+.2f
e.%-7.5s
9、为下列每个输入行提供一个对其进行读取的scanf()语句,并声明语句中用到的所有变量或数组。
a.101
b.22.32 8.34E-09
c.linguini
d.catch 22
e.catch 22(但是跳过catch)
答:
a.
1 int num;
2 scanf("%d", &num);
b.
1 float kgs, share;
2 scanf("%f%f", &kgs, &share);
c.
1 char str[40];
2 scanf("%s", str);
d.
1 char str[40];
2 int number;
3 scanf("%s %d", str,&number);
e.
1 char str[40];
2 int number;
3 scanf("%s %d", str, &number);
10、什么是空白字符?
答:
空白字符包括空格、制表符和换行符。C使用空白字符分隔各个语言符号;scanf()使用空白字符分隔相邻的输入项。
11、假设您想在程序中使用圆括号代替花括号。以下方法可以吗?
#define ( {
#define ) }
答:
会发生替换。但不幸的是,预处理器不能区别哪些圆括号应该被替换成花括号,哪些圆括号不应该被替换成花括号。因此:
1 #include <stdio.h>
2 #define ( {
3 #define ) }
4 int main(void)
5 (
6     printf("Hello World!\n");
7     return 0;
8 )
会变为:
1 #include <stdio.h>
2 int main{void}
3 {
4     printf{"Hello World!\n"};
5     return 0;
6 }
所有圆括号都要替换为花括号。
编程练习
1、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     char name[40]; // 名字
 5     char surname[40]; // 姓氏
 6 
 7     printf("Please enter your name and surname: \n");
 8     scanf("%s%s", name, surname);
 9     printf("%s,%s", surname, name);
10     return 0;
11 }
2、
 1 #include <stdio.h>
 2 #include <string.h>
 3 int main(void)
 4 {
 5     char name[40]; // 名字
 6 
 7     printf("Please enter your name: \n");
 8     scanf("%s", name);
 9     printf("\"%s\"\n", name);
10     printf("\"%20s\"\n", name);
11     printf("\"%-20s\"\n", name);
12     printf("%*s\n", strlen(name)+3, name);
13     return 0;
14 }
3、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     float number;
 5 
 6     printf("Please enter a float number: \n");
 7     scanf("%f", &number);
 8     printf("The input is %.1f or %.1e\n", number, number);
 9     printf("The input is %+.3f or %.3E\n", number, number);
10     return 0;
11 }
4、
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     /* 以厘米为单位输入身高,并以米为单位进行显示 */
 5     float weight;
 6     char name[40];
 7 
 8     printf("Please enter your weight(cm): \n");
 9     scanf("%f", &weight);
10     printf("Please enter your name: \n");
11     scanf("%s", name);
12     printf("%s, you are %.2f meter tall\n", name, weight/100);
13     return 0;
14 }
5、
 1 #include <stdio.h>
 2 #include <string.h>
 3 int main(void)
 4 {
 5     char name[40]; // 名字
 6     char surname[40]; // 姓氏
 7 
 8     printf("Please enter your name: \n");
 9     scanf("%s", name);
10     printf("Please enter your surname: \n");
11     scanf("%s", surname);
12     printf("%10s %10s\n", surname, name);
13     printf("%10d %10d\n", strlen(surname), strlen(name));
14     printf("%-10s %-10s\n", surname, name);
15     printf("%-10d %-10d\n", strlen(surname), strlen(name));
16     return 0;
17 }
结果为:(看起来有一点怪啊!!!)
Please enter your name:
Ayun
Please enter your surname:
li
        li       Ayun
         2           4
li         Ayun
2         4
6、
 1 #include <stdio.h>
 2 #include <float.h>
 3 int main(void)
 4 {
 5     double dblnum = 1.0/3.0;
 6     float fltnum = 1.0/3.0;
 7 
 8     printf("%.4f\n", dblnum);
 9     printf("%.12f\n", dblnum);
10     printf("%.16f\n", dblnum);
11     printf("%.4f\n", fltnum);
12     printf("%.12f\n", fltnum);
13     printf("%.16f\n", fltnum);
14     printf("double precision = %d digits\n", DBL_DIG);
15     printf("float precision = %d digits\n", FLT_DIG);
16     return 0;
17 }
7、(定义浮点类型的时候是使用float,还是double好???)
 1 #include <stdio.h>
 2 #define LITRE 3.785
 3 #define KM 1.609
 4 int main(void)
 5 {
 6     float mile; // 英里数
 7     float gallon; // 加仑数
 8 
 9     printf("Please enter your mile: \n");
10     scanf("%f", &mile);
11     printf("Please enter your gallon: \n");
12     scanf("%f", &gallon);
13     printf("Miles per gallon of gasoline: %.1f\n", mile/gallon);
14     printf("Liters per 100 kilometers: %.1f\n", gallon*LITRE*100/(mile*KM));
15     return 0;
16 }
posted @ 2015-11-14 02:01 李阿昀 阅读(785) | 评论 (0)编辑 收藏