C语言程序设计:现代设计方法习题笔记《chapter2》

本系列仅学习交流使用。


 

第一题 

                ​​​​​​​        ​​​​​​​        

示例代码: 

#include<stdio.h>

int main(void) {
	printf_s("          *\n");
	printf_s("         *\n");
	printf_s("        *\n");
	printf_s("   *   *\n");
	printf_s("    * *\n");
	printf_s("     *\n");

	return 0;
}

注意点:换行符“\n” 

第二、三题

          ​​​​​​​

首先看一下4/3在代码运行后输出值:

#include<stdio.h>

int main(void)
{
	printf("4 / 3 = %d\n", 4 / 3);
	printf("4.0f / 3.0f = %f", 4.0f / 3.0f);
	return 0;
}

输出为1. 

                                          ​​​​​​​

分析:计算时强制做了一个取整的操作,实际上为浮点数。 

示例代码:

#include<stdio.h>
#define pi 3.14
#define r 10.0

int main(void)
{
	float vol1 = 4.0f / 3.0f * pi * r * r * r;
	printf("第二题计算得到体积为:%f\n", vol1);

	float a = 1.0;
	printf("输入半径:");
	scanf_s("%f", &a);
	float vol = 4.0f / 3.0f * pi * a * a * a;
	printf("第三题计算得到体积为:%f", vol);
	return 0;

}

终端输入,得到输出

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​

 第四题

        

示例代码:

#include<stdio.h>

int main()
{
	printf("Enter an amount: ");
	float a = 1.0;
	scanf_s("%f", &a);
	float tax_a = a * (1 + 0.05);
	printf("With tax added: $%.2f", tax_a);
	return 0;
}

 输出

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        

第五题

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​         

示例代码:

#include<stdio.h>

int main()
{
	float x = 1.0;
	printf("请赋予x初始值:");
	scanf_s("%f", &x);
	float result = 3 * x * x * x * x * x + 2 * x * x * x * x + 7 * x - 5 * x * x * x - x * x - 6;
	printf("%.3f", result);
	return 0;
}

 输入输出:

                                 

第六题

示例代码:

#include<stdio.h>

int main()
{
	float x = 1.0;
	printf("请赋予x初始值:");
	scanf_s("%f", &x);
	float result = ((((3*x+2)*x-5)*x-1)*x+7)*x-6;
	printf("%.3f", result);
	return 0;
}

 输出:

        ​​​​​​​        ​​​​​​​         

第七题

        ​​​​​​​        

题目分析:这个题的意思就是,每次都付20,一直到付20付多了的那次,选用10元;然后付10,10最多也就一次,结合题目中的解释应该不难理解。 

示例代码:

#include<stdio.h>

int main()
{

	printf("Enter a dollar amount: ");
	int amount = 0, twenty_amount = 0, five_amount = 0, one_amount = 0, ten_amount = 0;
	scanf_s("%d", &amount);
	twenty_amount = amount / 20;
	ten_amount = (amount - twenty_amount * 20)/10;
	five_amount = (amount - twenty_amount * 20 - ten_amount * 10) / 5;
	one_amount = amount - twenty_amount * 20 - ten_amount * 10 - five_amount * 5;
	printf("\n");
	printf("$20 bills: %d\n", twenty_amount);
	printf("$10 bills: %d\n", ten_amount);
	printf(" $5 bills: %d\n", five_amount);
	printf(" $1 bills: %d\n", one_amount);
	return 0;
}

输出:

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        

分析:巧妙利用"/"在进行整数间的除法运算时是向下取整的。 

第八题

        ​​​​​​​        

 示例代码:

#include<stdio.h>

int main()
{
	printf("Enter amount of loan: ");
	float loan = 0.0f;
	scanf_s("%f", &loan);
	printf("Enter interest rate: ");
	float rate = 0.0f;
	scanf_s("%f", &rate);
	printf("Enter monthly payment: ");
	float payment = 0.0f;
	scanf_s("%f", &payment);
	
	float first_month, second_month, third_month;
	printf("\n");
	//first_month = (loan - payment) * (1 + ((rate / 100.0f) / 12.0f));
	first_month = (loan - payment) + loan * ((rate / 100.0f) / 12.0f);
	second_month = (first_month - payment) + first_month * ((rate / 100.0f) / 12.0f);
	third_month = (second_month - payment) + second_month * ((rate / 100.0f) / 12.0f);
	printf("Balance remaining after first payment: %.2f\n", first_month);
	printf("Balance remaining after second payment: %.2f\n", second_month);
	printf("Balance remaining after third payment: %.2f\n", third_month);
	return 0;
}

输出:

        ​​​​​​​        ​​​​​​​        

分析,这个代码实现上不难,但是我在第一次审题的时候没写对,错误请看代码注释部分处,输出结果不对反过来再去看题,原来是文字游戏。。。。

在代码实现第八题的过程中,发现有一部分可以抽象出来,单独写一个函数的,这样可以使代码看起来没那么臃肿,下面给出我的另一个代码实现:

#include<stdio.h>

float money(float, float, float);

int main()
{
	float loan = 0.0f;
	float payment = 0.0f;
	float rate = 0.0f;
	printf("Enter amount of loan: ");
	scanf_s("%f", &loan);
	printf("Enter interest rate: ");
	scanf_s("%f", &rate);
	printf("Enter monthly payment: ");
	scanf_s("%f", &payment);
	
	float first_month, second_month, third_month;
	printf("\n");
	first_month = money(loan, rate, payment);
	second_month = money(first_month, rate, payment);
	third_month = money(second_month, rate, payment);
	printf("Balance remaining after first payment: %.2f\n", first_month);
	printf("Balance remaining after second payment: %.2f\n", second_month);
	printf("Balance remaining after third payment: %.2f\n", third_month);
	return 0;
}


float money(float balance, float rate, float payment)
{
	float num_balance = (balance - payment) + balance * ((rate / 100.0f) / 12.0f);
	return num_balance;
}

输出:

        ​​​​​​​        ​​​​​​​        

重复操作的东西都可以进行抽象,以一种简单的形式去实现,上述代码可以继续精简,这里就不给出啦,后续可以自己试一下。

上一篇:DevOps的文化观与工具


下一篇:git 上传项目到 github 并生成二维码