输入整存整取存款金额以及存期 这个程序怎么写啊?

img


#include"stdio.h"
#include"math.h"
int main()
{
    int year;
    double rate,money,depoist;
    printf("请输入金额和存期:");
    scanf("%f %d",&year,&money);
    depoist=money*rate*year;
    switch(year)
    {
    
    case 1:
        rate=0.03;
        break;
    case 2:
        rate=0.0375;
        break;
    case 3:
        rate=0.0425;
        break;
    case 5:
        rate=0.0475;
        break;
    default:
        printf("输入错误,请重新输入\n");
        break;
    }
    printf("利息是:%f\n",depoist);
    return 0;
}

你这是c啊,不是c++

#include"stdio.h"
#include"math.h"
int main()
{
    int year;
    double rate,money,depoist;
    printf("请输入金额和存期:");
    scanf("%f %d",&year,&money);
    switch(year)
    {
    case 3:
        rate=0.026;
        break;
    case 6:
        rate=0.028;
        break;
    case 12:
        rate=0.03;
        break;
    case 24:
        rate=0.0375;
        break;
    case 36:
        rate=0.0425;
        break;
    case 60:
        rate=0.0475;
        break;
    default:
        printf("输入错误,请重新输入\n");
        break;
    }
    depoist=money*rate*year/12;
    printf("利息是:%f\n",depoist);
    return 0;
}

不能用switch ...case吧,因为存期不一定是那几个月,也有可能存了8/9个月或者其他,应该这样写:


#include"stdio.h"
#include"math.h"
int main()
{
    int month;
    double rate, year, money, depoist;
    printf("请输入金额和存期:");
    scanf_s("%d %lf", &month, &money);
    if (month < 3)
        rate = 0;
    else if (month > 3 && month < 6)
        rate = 0.026;
    else if (month > 6 && month < 12)
        rate = 0.028;
    else if (month > 12 && month < 24)
        rate = 0.03;
    else if (month > 24 && month < 36)
        rate = 0.0375;
    else if (month > 36 && month < 60)
        rate = 0.0425;
    else if (month > 6 && month < 12)
        rate = 0.0475;
    else
        printf("请重新输入存期");
    year= month / 12.0;
    depoist = money * rate * year;
    printf("利息是:%f\n", depoist);
    return 0;
}