C语言程序设计_现代方法(第2版)——第2 章编程题(自己写的,只供参考)

第二章 练习题

第一题

没有警告信息

第二题

 (1)指令: #include<stdio.h>
 语句:带分号 ;
(2)Parkinson's Law 
Work expands so as to fill the time 
available for its completion.

第三题

 编程题: 略

第四题

第五题

第六题

第七题

第八题

第二章 编程题

chapter 2 -1

编写一个程序,使用printf在屏幕上显示下面的图形∶
在这里插入图片描述

#include <stdio.h>

int main()
{
    int i ;
    int j ;
    for ( i = 0 ; i< 6 ; i++)
    {
        if ( i == 0)
        {
            for ( j = 0 ; j < 8 ; j ++)
            {
                if (j == 7)
                {
                    printf("*\n");
                }
                else
                {
                    printf(" ");
                }
            }
        }
        if ( i ==1 )
        {
            for( j = 0 ; j < 8 ; j++)
            {
                if (j == 6)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
        if( i ==2)
        {
            for( j = 0 ; j < 8 ; j++)
            {
                if (j == 5)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
        if ( i ==3 )
        {
            for( j = 0 ; j < 8 ; j++)
            {
                if (j == 4 || j == 0 )
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
        if ( i == 4 )
        {
            for( j = 0 ; j < 8 ; j++)
            {
                if (j == 3 || j ==1 )
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
        if (i ==5 )
        {
            for( j = 0 ; j < 8 ; j++)
            {
                if (j == 2)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }


}

这道题的编写,我才用了很暴力的方式,所以代码有点冗杂,虽然有简单方法进行稍微稍微的处理,不过我觉得还是比较复杂,所以就QAQ…

chapter 2 - 2 和chapter 2 - 3

2.编写一个计算球体体积的程序,其中球体半径为10 m,参考公式v=4/3元³。注意,分数4/3应写为
4.0f/3.0f。(如果分数写成4/3会产生什么结果?)提示; C语言没有指数运算符,所以需要对r 自乘两次来计算r。
3.修改上题中的程序,使用户可以自行录入球体的半径。

注意的是,圆周率需要自己定义一个常量, 2和3相比,就是3增加一个输入语句。

#include <stdio.h>

#define p 3.141592

int main()
{
    const int r = 10 ;
    double volume = 0.0 ;
    volume = 4.0/3.0 * p * r * r * r ;
    printf("%.6f" , volume) ;
    return 0 ;

}

```c
//chapter 2 - 3 
#include <stdio.h>

#define p 3.141592

int main()
{
    int r = 0 ;
    double volume = 0.0 ;
    scanf("%d", &r) ;
    volume = 4.0/3.0 * p * r * r * r ;
    printf("%.6f" , volume) ;
    return 0 ;
}

我的输出格式是采用保留数据后六位的形式。

chapter 2 -4

4.编写一个程序,要求用户输入一个美金数量,然后显示出增加5%税率后的相应金额。格式如下所示∶

#include <stdio.h>

int main()
{
    double amount ;
    scanf("%lf", &amount) ;
    double tax ;
    tax = amount + amount * 0.05 ;
    printf("%.2lf", tax ) ;
    return 0;
}

chapter 2 - 5


#include <stdio.h>

int main()
{

    double  x ;
    scanf("%lf", &x ) ;
    double S = 0.00 ;
    S = 3 * x * x * x * x * x + 2 * x * x * x * x - 5 * x * x * x - x * x + 7 * x - 6 ;
    printf("%.3lf", S) ;
    return 0 ;
}


chapter 2 -6

#include <stdio.h>

int main()
{
    double x ;
    scanf("%lf", &x) ;
    double S = 0.00 ;
    S = ((((3 * x + 2) * x - 5) * x -1) * x + 7 ) * x - 6 ;
    printf("%.2lf", S);
    return 0 ;
}

chapter 2 -7

#include <stdio.h>

int main()
{
    int amount ;
    scanf("%d", &amount) ;
    int amount_20 = 0;
    int amount_10 = 0 ;
    int amount_5 = 0 ;
    int amount_1 = 0;
    amount_20 = amount / 20 ;
    if ( amount % 20 != 0 )
    {
        int y_1 = amount % 20 ;
        amount_1 = y_1 / 10 ;
        if ( y_1 % 10 != 0)
        {
            int y_2 = y_1 % 10;
            amount_5 = y_2 / 5;
            if (y_2 % 5 != 0)
            {
                int y_3 = y_2 % 5;
                amount_1 = y_3 / 1;
            }
        }
    }
    printf("$20 bills : %d \n", amount_20);
    printf("$10 bills : %d \n", amount_10);
    printf("$5 bills : %d \n", amount_5) ;
    printf("$1 bills : %d ", amount_1) ;
    return 0 ;
}

这貌似是一道常用的算法,叫做啥方法,我忘记了,离散数学讲过QAQ

chapter 2 -8

#include <stdio.h>

int main()
{
    double loan, rate , payment  ;
    printf("Enter amount of loan: ");
    scanf("%lf", &loan) ;
    printf("Enter interest rate : ");
    scanf("%lf", &rate) ;
    printf("Enter monthly payment : ");
    scanf("%lf", &payment) ;
    double Balance_1 , Balance_2 , Balance_3 ;
    Balance_1 = (loan - payment)  + loan * rate / 1200 ;
    Balance_2 = (Balance_1 - payment) + Balance_1 * rate / 1200 ;
    Balance_3 = (Balance_2 - payment) + Balance_2 * rate / 1200  ;
    printf("Balance remaining after first payment : $%.2lf\n", Balance_1) ;
    printf("Balance remaining after first payment : $%.2lf\n", Balance_2) ;
    printf("Balance remaining after first payment : $%.2lf", Balance_3) ;
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影不在遗忘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值