EX3.20+3 计算存款的本金合计
已知银行整存整取存款不同期限的利率分别为:半年2.55%,一年2.75%,二年3.35%,三年4.00%,五年4.75%。要求输入存钱的本金和期限,输出到期时的本金和利息合计。输入格式:
"%f%f"
输出格式:
"%.2f"
输入样例:
在这里给出一组输入。例如:
1000 0.5
输出样例:
在这里给出相应的输出。例如:
1012.75
#include <stdio.h>
#include <math.h>
int main()
{
int n;
double a,rate,capital,deposit;
printf( "Please enter capital,year:\n");
scanf("%f%f",&capital,&n);
switch(n)
{
case(0.5) :a=pow(1+2.55%,n);break;
case(1) :a=pow(1+2.75%,n);break;
case(2):a=pow(1+3.35%,n);break;
case(3):a=pow(1+4.00%,n);break;
case(4):a=pow(1+4.75%,n);break;
}
deposit=capital*a;
printf("deposit=%.2f\n",deposit);
}
return 0;
}
12 11 case label does not reduce to an integer constant
[错误]case标号不能简化为整数常量
12 27 expected primary-expression before ',' token
[错误]在','标记之前期望主表达式 #include <stdio.h>
#include <math.h>
int main()
{
int n;
double a, rate, capital, deposit;
printf("Please enter capital,year:\n");
scanf("%f%f", &capital, &n);
/*
*
* ---------------> switch ... case ... 只能是整型,不能是浮点!case 没有括号!
* ---------------> 百分比符号在 C 里面是取模,想要百分比用小数点!
*
switch (n)
{
case(0.5):a = pow(1 + 2.55 %, n); break;
case(1):a = pow(1 + 2.75 %, n); break;
case(2):a = pow(1 + 3.35 %, n); break;
case(3):a = pow(1 + 4.00 %, n); break;
case(4):a = pow(1 + 4.75 %, n); break;
}
*/
if (n < 1) {
a = pow(1 + .0255, n);
}
else if (n < 2) {
a = pow(1 + .0275, n);
}
else if (n < 3) {
a = pow(1 + .0335, n);
}
else if (n < 4) {
a = pow(1 + .04, n);
}
else if (n < 5) {
a = pow(1 + .0475, n);
}
deposit = capital * a;
printf("deposit = %.2f\n", deposit);
return 0;
} 傻眼貓咪 发表于 2022-6-12 09:38
根据要求和你的提醒修改了一下,但结果还是不对QAQ
Please enter capital,year:
1000 0.5
deposit = 1012.67
#include <stdio.h>
#include <math.h>
int main()
{
float n,a, rate, capital, deposit;
scanf("%f%f", &capital, &n);
if (n>=0&&n < 1)
{
a = pow(1 + .0255, n);
}
else if (n < 2)
{
a = pow(1 + .0275, n);
}
else if (n < 3)
{
a = pow(1 + .0335, n);
}
else if (n < 4)
{
a = pow(1 + .04, n);
}
else if (n < 5)
{
a = pow(1 + .0475, n);
}
deposit = capital * a;
printf("%.2f", deposit);
return 0;
} 我爱橙 发表于 2022-6-12 11:54
根据要求和你的提醒修改了一下,但结果还是不对QAQ
Please enter capital,year:
1000 0.5
我的公式是用你写的,我没有验证对错,如果答案不对,表示你的公式也有错 整数年份利率例题
#include <stdio.h>
#include <math.h>
int main()
{
int year;
double rate, capital, deposit;
printf("Please enter year,capital:");
scanf("%d,%lf", &year, &capital);
switch (year)
{
case 1: rate = 0.0225;break;
case 2: rate = 0.0243;break;
case 3: rate = 0.0270;break;
case 5: rate = 0.0288;break;
case 8: rate = 0.0300;break;
default: printf("Error rate!\n");
exit(0);
}
deposit =capital * pow(1+rate, year);
printf("rate = %f, deposit = %f\n", rate, deposit);
return 0;
}
页:
[1]