衎衍 2017-10-28 01:18 采纳率: 0%
浏览 4748

编程c语言 存款利率计算器

设capital是最初的存款总额(即本金),rate是整存整取的存款年利率,n 是储蓄的年份,deposit是第n年年底账号里的存款总额。已知如下两种本利之和的计算方式:
按复利方式计息的本利之和计算公式为:deposit = capital * (1 + rate) n
按普通计息方式计算本利之和的公式为:deposit = capital * (1 + rate * n)
已知银行整存整取不同期限存款的年息利率分别为:
存期1年,利率为 0.0225
存期2年,利率为 0.0243
存期3年,利率为 0.0270
存期5年,利率为 0.0288
存期8年,利率为 0.0300
若输入其他年份,则输出"Error year!"

编程从键盘输入存钱的本金和存款期限,然后再输入按何种方式计息,最后再计算并输出到期时能从银行得到的本利之和,要求结果保留到小数点后4位。

程序的运行结果示例1:
Input capital, year:10000,2↙
Compound interest (Y/N)?Y↙
rate = 0.0243, deposit = 10491.9049

程序的运行结果示例2:
Input capital, year:10000,2↙
Compound interest (Y/N)?n↙
rate = 0.0243, deposit = 10486.0000

程序的运行结果示例3:
Input capital, year:1000,4↙
Compound interest (Y/N)?y↙
Error year!

输入提示信息:"Input capital, year:"
输入提示信息:"Compound interest (Y/N)?"
存期输入错误的提示信息: "Error year!\n"
本金及存款期限的输入格式: "%lf,%d"
是否选择复利计算的输入格式: " %c" (注意:%c的前面有一个空格。输入的字符大小写皆可,即Y或y,N或n皆可)
输出格式:"rate = %.4f, deposit = %.4f\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb

#include
#include
int main()
{
int year;
double rate, deposit, capital;
char x, y, Y, n, N;
printf("Input capital, year:");
printf("Compound interest (Y/N)?");
scanf("%lf,%d", &capital, &year);
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 year!\n");
}
switch (x)
{
case'y':
deposit = capital * pow((1 + rate), year);
printf("deposit = %.4f\n", deposit);
break;
case'Y':
deposit = capital * pow((1 + rate), year);
printf("deposit = %.4f\n", deposit);
break;
case'N':
deposit = capital * (1 + rate * year);
printf("deposit = %.4f\n", deposit);
break;
case'n':
deposit = capital * (1 + rate * year);
printf("deposit = %.4f\n", deposit);
break;
}
printf("rate = %.4f, deposit = %.4f\n", rate, deposit);
return 0;
}

  • 写回答

1条回答 默认 最新

  • 檀车 侠影 2020-05-09 14:19
    关注
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        char Ci;
        int year;
        double capital, rate;
        printf("Input capital, year:");
        scanf("%lf,%d", &capital, &year);
        printf("Compound interest (Y/N)?");
        scanf(" %c",&Ci);
        if(Ci=='Y'||Ci=='y')
        {
            switch(year)
            {
                case 1 : rate = 0.0225;
                printf("rate = 0.0225, deposit = %.4lf\n", capital * pow(1+rate,year));break;
                case 2 : rate = 0.0243;
                printf("rate = 0.0243, deposit = %.4lf\n", capital * pow(1+rate,year));break;
                case 3 :rate = 0.0270;
                printf("rate = 0.0270, deposit = %.4lf\n", capital * pow(1+rate,year));break;
                case 5 :rate = 0.0288;
                printf("rate = 0.0288, deposit = %.4lf\n", capital * pow(1+rate,year));break;
                case 8 :rate = 0.0300;
                printf("rate = 0.0300, deposit = %.4lf\n", capital * pow(1+rate,year));break;
                default : printf("Error year!\n");
            }
        }
         else
        {
            switch(year)
            {
                case 1 : rate = 0.0225;
                printf("rate = 0.0225, deposit = %.4lf\n", capital * (1+rate*year));break;
                case 2 : rate = 0.0243;
                printf("rate = 0.0243, deposit = %.4lf\n", capital * (1+rate*year));break;
                case 3 :rate = 0.0270;
                printf("rate = 0.0270, deposit = %.4lf\n", capital * (1+rate*year));break;
                case 5 :rate = 0.0288;
                printf("rate = 0.0288, deposit = %.4lf\n", capital * (1+rate*year));break;
                case 8 :rate = 0.0300;
                printf("rate = 0.0300, deposit = %.4lf\n", capital * (1+rate*year));break;
                default : printf("Error year!\n");
            }
        }
    
    return 0;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能