Advertisements
Advertisements
Advertisements
编写的时候将章节弄错了,代码是第三章的课后习题的答案
编程环境为Visual Studio 2017
#include <stdio.h>
/************************************************************************/
/* practice 2 */
/************************************************************************/
void p2_(void)
{
int ch = 0;
printf("please input the value of ascii:");
scanf_s("%d", &ch);
getchar(); // 删除输入缓冲区中的回车键
printf("the value of ascii : %d\n", ch);
printf("the character : %c\n", (char)ch);
return;
}
/************************************************************************/
/* practice 3 */
/************************************************************************/
void p2_3(void)
{
printf("\a");
printf("Startled by the sudden sound, sally shouted,\n\"By the Great Pumpkin, what was that!\"");
return;
}
/************************************************************************/
/* practice 4 */
/************************************************************************/
void p2_4(void)
{
float input = 0.0;
printf("Enter a floating-point value:");
scanf_s("%f", &input);
getchar();
printf("fixed-point notation:%f\n", input);
printf("exponential notation:%e\n", input);
printf("p notation:%a\n", input);
return;
}
void p2_4_version2(void)
{
double input = 0.0;
printf("Enter a floating-point value:");
scanf_s("%lf", &input);
getchar();
printf("fixed-point notation:%f\n", input);
printf("exponential notation:%le\n", input);
printf("p notation:%la\n", input);
return;
}
/************************************************************************/
/* practice 5 */
/************************************************************************/
#define SECONDS_PER_YEAR (3.156e7)
void p2_5(void)
{
int year = 0;
printf("How old are you:");
scanf_s("%d", &year);
getchar();
double live_seconds = year * SECONDS_PER_YEAR;
printf("You have live %f days\n", live_seconds);
printf("You have live %le days\n", live_seconds);
return;
}
/************************************************************************/
/* practice 6 */
/************************************************************************/
#define QUALITY_OF_WATER_MOLECULE (3.0e-23)
#define NUM_OF_WATER_MOLECULE_PER_QUART (950/QUALITY_OF_WATER_MOLECULE)
void p2_6(void)
{
double num_of_quart = 0;
printf("How much quart of water:");
scanf_s("%lf", &num_of_quart);
getchar();
double num_of_water_molecule = num_of_quart * NUM_OF_WATER_MOLECULE_PER_QUART;
printf("there are %f quart water and %le water molecule", num_of_quart, num_of_water_molecule);
return;
}
/************************************************************************/
/* practice 7 */
/************************************************************************/
#define CM_PER_INCH (2.54)
void p2_7(void)
{
float height_in_inch = 0;
float height_in_cm = 0;
printf("What's your height in inch:");
scanf_s("%f", &height_in_inch);
getchar();
height_in_cm = height_in_inch * CM_PER_INCH;
printf("Your height is %f in cm\n", height_in_cm);
return;
}
/************************************************************************/
/* practice 8 */
/************************************************************************/
#define CUPS_PER_PINT (2)
#define OUNCES_PER_CUP (8)
#define BIG_SPOONS_PER_OUNCE (2)
#define TEA_SPIINS_PER_BIG_SPOON (3)
void p2_8(void)
{
float cups = 0;
float pints = 0;
float ounces = 0;
float big_spoons = 0;
float tea_spoons = 0;
printf("How many cups of tea do you want:");
scanf_s("%f", &cups);
getchar();
pints = cups / CUPS_PER_PINT;
ounces = cups * OUNCES_PER_CUP;
big_spoons = ounces * BIG_SPOONS_PER_OUNCE;
tea_spoons = big_spoons * TEA_SPIINS_PER_BIG_SPOON;
printf("pints:%f, cups:%f, ounces:%f, big_spoons:%f, tea_spoons:%f\n", pints, cups, ounces, big_spoons, tea_spoons);
return;
}
/************************************************************************/
/* main to test */
/************************************************************************/
int main(int argc, char **argv)
{
p2_8();
getchar();
return 0;
}
本文为博主原创文章,转载请标注出处
Advertisements
Advertisements
Advertisements
初学者表示看不懂 没有简单的写法吗
这个,博主表示这一章还是挺简单的吧,哪里看不懂了?
1.为什么double类型函数,在打印的时候使用的是%lf转换输出而不是%f呢?书上写的是%f转换说明打印十进制记数法的float和double类型浮点数
2.printf("品脱:%f 盎司:%f 汤勺%f 茶勺%f", cup / 2, 8 * cup, 16 * cup, 48 * cup);
比如说最后一道题。我在printf()里直接计算输出,跟您那种方式具体有什么区别吗?哪种方式更好?
1. 这个问题我之前也没有注意到,特意再看了下,为了确保移植性,应该按照书上说的,使用%f来打印float和double类型的浮点数,%lf是用来表示long double的
2. 就这题的语境见仁见智了,按照你的写简洁些,但是博主个人觉得这样写在可阅读性上面可能差了点。
请问开头为什么要把所有变量初始化为零呢?有什么特殊含义吗?刚开始学,有点不太懂,求指教。
如果不初始化的话,有些变量的值就是变量所在内存地址上的值,这个值不一定为零,容易导致后面计算的时候出现奇怪的问题
楼主第5个为什么中间要加gerchar呢,才知道变量定义不是必须在函数开始就定义完
读取回车键用的
老师,最后的mian()函数里 你在调用的p2_8(); 后 加上的()getchar();是干什么用的?
我的是visual sthdio 2017 不会一闪而过哎
你的代码让我改正不少编程风格,谢谢啦
博主你好,我是一名C语言初学者,有个地方我很搞不懂,为什么scanf函数那里是scanf_s,没有见过这个函数啊,编译器也不识别,这是怎么回事呢?
因为博主用的是visual studio 2017,但是不会设置,直接用scanf会函数不安全而编译报错,所以才直接用scanf_s的,要是你的编译器可以过就直接用scanf就行
你好,您的practice 6错了,最后的printf函数里的最后一个变量打错了,应该是num_of_water_molecule,而不是NUM_OFWATER_MOLECULE_PER_QUART
已修改,谢谢提醒

第五题开头为什么要这样写?#define SECONDS_PER_YEAR (3.156e7)
void p2_5(void)
不应该写#include什么什么的吗?
以前用Windows7现在突然用10可能还没适应。
C语言宏定义,至于宏定义是什么,百度一下估计就会有比较详细的解释了
想问一下老师,最后一题为什么使用浮点类型比整数类型合适呢?单纯只是因为“杯”转换成“品脱”的时候可能出现无法被2整除的问题吗?
是的
谢谢老师!
越级了啊 #define 是第四章才讲的为啥第三章就有呢
呃,这简单的内容,知道就行