《C Primer Plus》部分课后习题

《C Primer Plus》读书笔记:https://www.notion.so/C-Primer-Plus-b425e2717b5e44809ea9287a870d6d5d

/*worpress 文本编辑器不支持tab键,所有后面的代码块基本不加tab了*/
/*这里只挑了一些自己觉得比较复杂的题目,没有列出所有题目的答案,持续更新中*/

第二章

2.11-7

int main()
{
   
    int words, lines;
    words = 3020;
    lines = 350;

    printf("There were %d words and %d lines.\n", words, lines);
    return 0;
}

2.12-3

int main()
{
   
	int age_days;
	age_days = 23 * 365;

	printf("%d\n", age_days);

	return 0;
}</code></pre>

2.12-4

//包含三个函数jolly(), dent()
#include &lt;stdio.h>
void jolly(void);
void dent(void);
int main()
{
   
jolly();
jolly();
jolly();
dent();
printf("\n");

return 0;
getchar();
}
void jolly(void)
{
   
printf("For he's a jolly good fellow!\n");
}
void dent(void)
{
   
printf("Which nobody can deny!");
}</code></pre>

2.12-8

<pre class="wp-block-code"><code>#include &lt;stdio.h>
void one_three(void);
void two(void);
int main()
{
   
printf("starting now:\n");
one_three();
two();
printf("done!\n");

return 0;
}
void one_three(void)
{
   
printf("one\n");
}
void two(void)
{
   
printf("two\nthree\n");
}</code></pre>

第三章

3.10-9

<pre class="wp-block-code"><code>#include &lt;stdio.h>
int main(void)
{
   
char ch = 'ch';

printf("%d, %o, %x", ch, ch, ch);
printf("%d, %#o, %#x", ch, ch, ch);
getchar();
return 0;
}</code></pre>

3.10-10

void main(int) / this program is perfect /
{
cows, legs integer;
printf("How many cow legs did you count?\n);
scanf("%c", legs);
cows = legs / 4;
printf("That implies there are %f cows.\n", cows)
}

<pre class="wp-block-code"><code>#include &lt;stdio.h>
int main(void) // this program is perfect 
{
   
int cows, legs;

printf("How many cow legs did you count?\n");
scanf("%d", &legs);
getchar();

cows = legs / 4;
printf("That implies there are %d cows.\n", cows);

return 0;
}
</code></pre>

3.11-1 呃?我没看懂题目

<pre class="wp-block-code"><code>#include &lt;stdio.h>
int main(void)
{
   
	int integer;
	float overflow, underflow;

	integer = 32768;
	overflow = 3.4e38;
	underflow = 10.0/3;

	printf("%d, %f, %f\n", integer, overflow, underflow);
	printf("%d, %f, %f\n", integer+1, overflow, underflow); //观察系统如何处理整数上溢,浮点数上溢,浮点数下溢的情况
	getchar();

	return 0;
}</code></pre>

3.11-2

<pre class="wp-block-code"><code>#include &lt;stdio.h>>
int main(void)
{
   
int num;

printf("you need print a ASCII number:\n");
scanf_s("%d", &num);
printf("your number is %d, and char is %c.\n",  num, num);
getchar();

return 0;
}</code></pre>

3.11-3

<pre class="wp-block-code"><code>#include &lt;stdio.h>>
int main(void)
{
   
	char ch = '\a';

	printf("%c", ch);
	printf("Startled by the sudden sound, Sally shouted,\n");
	printf("\"By the Great Pumpkin, what was that!\"\n");
	gerchar();

	return 0;
}</code></pre>

3.11-4

<pre class="wp-block-code"><code>#include &lt;stdio.h>
int main(void)
{
   
	float num = 64.25;

	printf("Enter a floating-point value: ____\b\b\b\b\b");
	scanf_s("%f", &num);
	getchar();
	printf("\nfixed-point natation: %lf", num);
	printf("\nexponential notation: %e", num);
	printf("\np notation: %a", num);

	getchar();
	return 0;
}</code></pre>

3.11-5

<pre class="wp-block-code"><code>scanf_s("%f", &amp;s);
printf("year=%f", 3.156e7 * s);</code></pre>

3.11-6

<pre class="wp-block-code"><code>#include &lt;stdio.h>
#define MOLE 3.0e-23
 \\通过预编译指令定义一个水分子的质量为3.0e-32
#define QUART 950
int main(void)
{
   
	int quart;
	float hydrone;

	printf("Enter quart water:");
	scanf_s("%d", &quart);
	hydrone = quart * QUART / MOLE;
	printf("the %d water have %e hydrone.\n", quart, hydrone);
	getchar();
	getchar();

	return 0;
}</code></pre>

3.11-7 英寸和厘米的进制转换

<pre class="wp-block-code"><code>#include &lt;stdio.h>
#define CM_TO_INCH 2.54
int main(void)
{
   
	float cm, inch;
	scanf_s("%f", &cm);
	inch = cm / CM_TO_INCH;
	printf("%f inch, %0.2f inch, %.1f cm", inch, inch, cm);

	getchar();
	getchar();

	return 0;
}</code></pre>

3.11-8

<pre class="wp-block-code"><code>#include &lt;stdio.h>
#define PINTUO_BEI 2
#define BEI_ANGSI 8
#define ANGSI_SHAO 2
#define SHAO_XIAOSHAO 3
int main(void)
{
   
	float beishu, pingtuo, angsi, tangshao, xiaoshao;

	scanf_s("%f", &beishu);
	pingtuo = beishu / PINTUO_BEI;
	angsi = beishu * BEI_ANGSI;
	tangshao = angsi * ANGSI_SHAO;
	xiaoshao = tangshao * SHAO_XIAOSHAO;

	printf("\n%.1f bei, %.1f pingtuo, %.1f angsi, %.1f tangshao, %.1f xiaoshao", beishu, pingtuo, angsi, tangshao, xiaoshao);
	getchar();
	getchar();

	return 0;
}</code></pre>

第四章

4.7-1

scanf_s读取字符时的使用格式为:scanf_s("%c",变量地址,读取字符长度上限)
会忽略空格后的字符,因为scanf的%s转换说明,识别到空格、回车、tab时便会停止读取

<pre class="wp-block-code"><code>#include &lt;stdio.h>
#include &lt;string.h>  // 提供strlen()函数的原型
#define DENSITY 62.4  // 人体密度(单位:磅/立方英尺)
int main()
{
   
	float weight, volume;
	int size, letters;
	char name [40]; // name是一个可容纳40个字符的数组

	printf("Hi! What's your first name?\\n");
	scanf_s("%s", name, 10); 
	//scanf_s读取字符时的使用格式为:scanf_s("%c",变量地址,读取字符长度上限)
	
	printf("%s, what's your weight in pounds?\\n", name);
	scanf_s("%f", &weight);

	size = sizeof name;
	letters = strlen(name);
	volume = weight / 62.4;

	printf("Well, %s, your volume is %2.2f cubic feet.\\n",
		name, volume);
	printf("Also, your first name has %d letters,\\n",
		letters);
	printf("and we have %d bytes to store it.\\n", size);
	return 0;
}</code></pre>

4.7-4 改错

#define B "booboo"
#define X 10
int main()
{
   
	int age, xp;
	char name[20];

	printf("Please enter your first name:\\n");
	scanf_s(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值