C程序设计第三章习题(上)

3.1 复利问题

代码:

#include "stdio.h"

float getP(float r, int n);

void main() {
    //p=(1+r)n, r=7%, n=10,求p
    float r = 0.07;
    int n = 10;
    float p = getP(r,n);
    printf("The p = %f after 10 years.", getP(r,n));
}

float getP(float r, int n) {
    float result = 1;
    for(int i = 0; i < n; i++) {
        result = result * (1 + r);
    }
    return result;
}

输出结果:

 

 3.2 贷款问题

代码:

#include "stdio.h"
#include "math.h"

float getMonth(float d, float p, float r);

void main () {

    //m = [log p – log(p-d*r)]/(log(1+r))]
    //d = 300000; p = 6000; r = 1%.
    printf("The month is : %f\n", getMonth(300000, 6000, 0.01));
    printf("The month is : %.1f\n", getMonth(300000, 6000, 0.01));

}

float getMonth(float d, float p, float r) {
    return (log(p) - log(p - d*r))/log(1+r);
}

 输出结果:

 3.4 char与int类型问题

代码:

#include "stdio.h"

void main() {
    char c1 = 97;
    char c2 = 98;
    //c1 = 0 110 0001
    //c2 = 0 110 0002

    printf("c1=%c, c2=%c\n", c1, c2);
    //字符类型的存储是8位,输出的时候通过ASCII码值转换成相应的字符,'a'的ASCII码值是97,'b'的ASCII码值是98
    printf("c1=%d, c2=%d\n", c1, c2);
    //整数类型的存储是32位,输出97和98两个整数

    c1 = 197;
    c2 = 198;
    //c1 = 1 100 0101
    //c2 = 1 100 0110
    printf("c1=%c, c2=%c\n", c1, c2);
    //197,198的ASCII码值没有对应到相应的字符,所以输出的时候是问号
    printf("c1=%d, c2=%d\n", c1, c2);

    //197,198存储的时候,符号位=1,以整形数据类型读取时,输出的是负数,而不是正数

    int c3 = 97;
    int c4 = 198;
    printf("c1=%c, c2=%c\n", c3, c4);
    printf("c1=%d, c2=%d\n", c3, c4);

}

输出结果:

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值