C语言程序设计 现代设计方法——第二章编程题答案

C语言程序设计 现代设计方法——第二章编程题答案,第1张

project01

#include 

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

	return 0;
}
#include 

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

    return 0;
}

project02

#include 
#define SCALLE_FACTOR (4.0f / 3.0f)
#define PI 3.141592653f

int main(void)
{
	int radius = 10;
	float volume;

	volume = SCALLE_FACTOR * radius * radius * radius * PI;

	printf("Volume: %.2f\n", volume);

	return 0;
}
#include 

int main(void) {

    printf("Sphere volume: %.2f cubic meters\n", 4.0f/3.0f * 3.14f * 1000);
    return 0;
}

project03

#define _CRT_SECURE_NO_WARNINGS
#include 
#define SCALLE_FACTOR (4.0f / 3.0f)
#define PI 3.141592653f

int main(void)
{
	int radius;
	float volume;

	printf("Enter the radius: ");
	scanf("%d", &radius);

	volume = SCALLE_FACTOR * radius * radius * radius * PI;

	printf("Volume: %.2f\n", volume);

	return 0;
}

存在问题:
为什么输入与输出分开显示在两行,但输出的语句与输入的语句之间却没有换行??

#include 

int main(void) {

    int r = 0;

    printf("Enter radius: ");
    scanf("%d", &r);

    printf("\nSphere volume: %.2f cubic meters\n",
           4.0f/3.0f * 3.14f * r * r * r);
    return 0;
}

project04

#define _CRT_SECURE_NO_WARNINGS
#include 
#define RATE 0.05

int main(void)
{
	int amount;
	float tax_added;

	printf("Enter a amount: ");
	scanf("%d", &amount);

	tax_added = (float)(amount * (1 + RATE));

	printf("With tax added: $%.2f\n", tax_added);

	return 0;
}

存在问题:
1也是常量,应该用宏定义
涉及到强制类型转换,好还是不好?
多定义了一个变量,增加了存储空间

#include 

int main(void) {

    float money = 0.0f;
    printf("Enter an amount: ");
    scanf("%f", &money);
    printf("With tax added: $%.2f\n", money * 1.05f);

    return 0;
}

project05

#define _CRT_SECURE_NO_WARNINGS
#include 

int main(void) 
{

    int x;

    printf("Enter x: ");
    scanf("%d", &x);

    printf("3x^5+2x^4-5x^3-x^2+7x-6 = %d\n",
        3*x*x*x*x*x + 2*x*x*x*x + 5*x*x*x - x*x + 7*x - 6);

    return 0;
}
#include 

int main(void) {

    int x = 0;

    printf("Enter value for x: ");
    scanf("%d", &x);
    printf("Result: %d\n",
           (3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x)
           - (x * x) + (7 * x) - 6);

    return 0;
}

project06

#define _CRT_SECURE_NO_WARNINGS
#include 

int main(void)
{

    int x;

    printf("Enter x: ");
    scanf("%d", &x);

    printf("3x^5+2x^4-5x^3-x^2+7x-6 = %d\n",
        ((((3*x + 2)*x - 5)*x - 1)*x + 7)*x - 6);

    return 0;
}
#include 

int main(void) {

    int x = 0;

    printf("Enter value for x: ");
    scanf("%d", &x);
    printf("Result: %d\n",
           ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6);

    return 0;
}

project07

#define _CRT_SECURE_NO_WARNINGS
#include 

int main(void)
{

    int money;
    int dollar_20, dollar_10, dollar_5, dollar_1;

    printf("Enter a dollar amount: ");
    scanf("%d", &money);

    dollar_20 = money / 20;
    dollar_10 = (money - dollar_20 * 20) / 10;
    dollar_5 = (money - dollar_20 * 20 - dollar_10 * 10) / 5;
    dollar_1 = money - dollar_20 * 20 - dollar_10 * 10 - dollar_5 * 5;

    printf(" bills: %d\n", dollar_20);
    printf(" bills: %d\n", dollar_10);
    printf("  bills: %d\n", dollar_5);
    printf("  bills: %d\n", dollar_1);

    return 0;
}

存在问题:
把存储输入数据的变量看成是一个不变的量了

#include 

int main(void) {

    int money = 0;

    printf("Enter a dollar amount: ");
    scanf("%d", &money);

    printf(" bills: %d\n", money/20);
    money -= 20 * (money/20);

    printf(" bills: %d\n", money/10);
    money -= 10 * (money/10);

    printf("  bills: %d\n", money/5);
    money -= 5 * (money/5);

    printf("  bills: %d\n", money);

    return 0;
}

project08

#define _CRT_SECURE_NO_WARNINGS
#include 

int main(void)
{

    float amountOfLoan, interestRate, monthlyPayment;
    float afterFirstPayment, afterSecondPayment, afterThridPayment;

    printf("Enter amount of loan:");
    scanf("%f", &amountOfLoan);    
    printf("Enter interest rate:");
    scanf("%f", &interestRate);    
    printf("Enter monthly payment:");
    scanf("%f", &monthlyPayment);

    afterFirstPayment = amountOfLoan + amountOfLoan * (interestRate / 100 / 12) - monthlyPayment;
    afterSecondPayment = afterFirstPayment + afterFirstPayment * (interestRate / 100 / 12) - monthlyPayment;
    afterThridPayment = afterSecondPayment + afterSecondPayment * (interestRate / 100 / 12) - monthlyPayment;

    printf("Balance remaining after first payment: $%0.2f\n", afterFirstPayment);
    printf("Balance remaining after second payment: $%0.2f\n", afterSecondPayment);
    printf("Balance remaining after third payment: $%0.2f\n", afterThridPayment);

    return 0;
}

存在问题:
同第7题同样的错误

#include 

int main(void) {

    float loan = 0.0f,
          rate = 0.0f,
          payment = 0.0f;

    printf("Enter amount of loan: ");
    scanf("%f", &loan);

    printf("Enter interest rate: ");
    scanf("%f", &rate);

    printf("Enter monthly payment: ");
    scanf("%f", &payment);


    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after first payment: $%.2f\n", loan);

    
    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after second payment: $%.2f\n", loan); 

    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after third payment: $%.2f\n", loan);

    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/674070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存