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);
}
输出结果: