码迷,mamicode.com
首页 > 其他好文 > 详细

C Primer Plus 第三章 编程练习

时间:2018-09-28 13:43:42      阅读:632      评论:0      收藏:0      [点我收藏+]

标签:info   使用   Fix   value   star   print   如何   字符   float   

 

1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。

#include<stdio.h>

int main(void)
{
    unsigned int a = 4294967295;
    float b = 3.4e38;
    float c = b*10;
    float d = 0.1234e-2;
    printf("a=%u a+1=%u\n",a,a+1);
    printf("b=%e c=%e\n",b,c);
    printf("d=%f d/10=%f\n",d,d/10);
    
    return 0;
}

运行结果:

技术分享图片

2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入字符。

#include<stdio.h>

int main(void)
{
    char a;
    printf("请输入一个ASCII码值:");
    scanf("%d",&a);
    printf("%c\n",a);
    return 0;
}

运行结果:

技术分享图片

3.编写一个程序,发出一声警报,然后打印下面的文本:

Startled by the sudden sound,Sally shouted,

“By the Great Pumpkin,what was that!”

#include<stdio.h>
int main(void)
{
    printf("\aStartled by the sudden sound, Sally shouted,\n");
    printf("\"By the  Great Pumpkin,what was that!\"\n");
    return 0;
}

运行结果:

技术分享图片

4. 编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,在打印成p计数法(即十六进制计数法)形式。

Enter a floating-point value :64.25

fixed-point notation:64.250000

exponential notation:6.425000e+01

p notation:0x1.01p+6

#include<stdio.h>
int main(void)
{
        float  i;

        printf("Enter a floating-point value :");
        scanf("%f",&i);
        printf("fixed-point notation:%f\n",i);
        printf("exponential notation:%e\n",i);
        printf("p notation:%a\n",i);
        return 0;
}

运行结果:

技术分享图片

5. 一年大约有3.156X107秒,编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。

#include<stdio.h>
int main(void)
{
        double seconds = 3.156e7;
        unsigned age;

        printf("请输入年龄:");
        scanf("%d",&age);
        printf("该年龄对应的秒数是:%f\n",age * seconds);
        return 0;
}

运行结果:

技术分享图片

6. 一个水分子的质量约为3.0X1023克,1夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。

#include <stdio.h>

#define WATER 3.0e-23  //1个水分子的质量
#define DEHYDRATION 950  //1夸脱水的质量

int main(int argc, char *argv[])
{
        float i, total; //total表示水分子的数量
        printf("please input:");
        scanf("%f", &i);
        total = i * DEHYDRATION / WATER;
        printf("%e\n", total);
        return 0;
}

运行结果:

技术分享图片

7.1英寸相当于2.54厘米。编写一个程序,提示用户输入升高(/英寸),然后以厘米为单位显示升高。

#include<stdio.h>

#define INCH 2.54

int main(void)
{
    float i,height;
    printf("请输入身高(英寸):");
    scanf("%f",&i);
    height = i*INCH;
    printf("height = %f\n",height);
    return 0;
}

运行结果:

技术分享图片

8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8中盎司,1盎司等于2答汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用浮点类型比整数类型更合适?

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    float cup, pint, ounce, spoon, teaspoon;
    printf("请输入杯数:");
    scanf("%f", &cup);
    pint = cup / 2;
    ounce = 8 * cup;
    spoon = 8 * cup * 2;
    teaspoon = 8 * cup * 2 * 3;
    printf("品脱:%f  盎司:%f  汤勺:%f  茶勺:%f\n", pint, ounce, spoon, teaspoon);
    return 0;
}

运行结果:

技术分享图片

技术分享图片

如果用pint用整数类型的话,若杯数是6.5杯,那么pint就会舍去小数位的数字,因此浮点类型比整数类型更合适。

不妥之处欢迎批评指正,谢谢大家!

 

C Primer Plus 第三章 编程练习

标签:info   使用   Fix   value   star   print   如何   字符   float   

原文地址:https://www.cnblogs.com/520hyf/p/9718024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!