| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1561 人关注过本帖
标题:C語言还款程序问题
只看楼主 加入收藏
zzy95621
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-10-26
结帖率:100%
收藏
 问题点数:0 回复次数:6 
C語言还款程序问题
#include <stdio.h>
#include <math.h>
double round2 (double);
int main ()
{

     // Local Declaration
     int NM;
     double IM, P, Q, MP, IY, NY, PR;


     // Statements
     printf("The amount of the loan (principal) :");
     scanf("%lf", &PR);
     printf("Interest rate / year (per cent) :");
     scanf("%lf", &IY);
     printf("Number of Years :");
     scanf("%lf", &NY);

     NM = NY*12;
     IM = IY / 12 / 100;
     P  = pow((1+IM),NM);
     Q  = (P/(P-1));
     MP = PR * IM * Q;


     printf("Interest rate / month (decimal) : %lf\n", IM);
     printf("Number of Months : %d\n", NM);
     printf("Monthly payment : %lf\n", MP);


     int Month;
     double OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance;


     OldBalance        = PR - NewBalance;
     MonthlyPayment    = MP;
     Interestpaid      = OldBalance * IM;
     Principalpaid     = MP - (OldBalance * IM);
     NewBalance        = OldBalance - Principalpaid;

printf("\n            Old          Monthly      Interestpaid   Principal        New");
printf("\nMonth     Banlace        Payment        paid          paid          Balance\n");
     printf("=====    =========      =========     =========     =========      =========\n");

for(Month=1;Month<=12;Month++)



     printf(" %d      %lf    %lf    %lf      %4lf       %4lf\n", Month, OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance);



这样写,它不会循环计算啊,直接给我一大堆负数
怎么写才能让输入的金额一直循环计算到0啊???求救啊大神
还有写了round2 printf出来的数字怎么不是两位小数的?
搜索更多相关主题的帖子: principal include double amount 
2016-11-01 21:24
yanjunfeng10
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:44
注 册:2016-10-27
收藏
得分:0 
建议你命名变量的时候根据英文意思来简写,这样易读,或者增加注释,前面的部分我懒得看了,两位小数使用%.2f输出即可。

[此贴子已经被作者于2016-11-1 22:53编辑过]

2016-11-01 22:49
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
收藏
得分:0 
首先回复第一问: 这样写能循环不过循环的结果不合乎你的本意,因为你最后的for循环没有主体(暂时就说它主体吧,想不到其他的好说的名字),只有输出.是否输出负数可能与你的输入的数据有关(可能是其他原因).
第二问: 修改后的代码如下,把原来放在main函数中间的一些赋值计算放到了最后的for循环里
第三问:round()函数是用来四舍五入的,我也是第一次遇到可能讲的不对,想要输出两位小数可以按2楼说得来.最后的输出是要舍去小数点后几位的,不然要输入的数据过大窗口都放不下
#include <stdio.h>
 #include <math.h>
 //double round2 (double);
 int main ()
 {

      // Local Declaration
      int NM;
      double IM, P, Q, MP, IY, NY, PR;


      // Statements
      printf("The amount of the loan (principal) :");
      scanf("%lf", &PR);
      printf("Interest rate / year (per cent) :");
      scanf("%lf", &IY);
      printf("Number of Years :");
      scanf("%lf", &NY);

      NM = NY*12;
      IM = IY / 12 / 100;
      P  = pow((1+IM),NM);
      Q  = (P/(P-1));
      MP = PR * IM * Q;


      printf("Interest rate / month (decimal) : %lf\n", IM);
      printf("Number of Months : %d\n", NM);
      printf("Monthly payment : %lf\n", MP);


      int Month;
      double OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance;


      

 printf("\n            Old          Monthly      Interestpaid   Principal        New");
 printf("\nMonth     Banlace        Payment        paid          paid          Balance\n");
      printf("=====    =========      =========     =========     =========      =========\n");

 for(Month=1;Month<=12;Month++)
 {   
      OldBalance        = PR - NewBalance;
      MonthlyPayment    = MP;
      Interestpaid      = OldBalance * IM;
      Principalpaid     = MP - (OldBalance * IM);
      NewBalance        = OldBalance - Principalpaid;
 
 printf(" %d      %.2lf     %.2lf    %.2lf      %.2lf       %.2lf\n", Month, OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance);
 
 }
 return 0;
}


[此贴子已经被作者于2016-11-1 23:56编辑过]


早知做人那么辛苦!  当初不应该下凡
2016-11-01 23:52
zzy95621
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-10-26
收藏
得分:0 
回复 3楼 炎天
其他的通过自己努力搞定了。
只是for那一部分,
能不能让这个for无限循环
只有满足NewBalance<=0 然后用 IF  这个指令让停止循环
2016-11-02 00:09
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
收藏
得分:0 
for(Month=1;NewBalance>0;Month++)
上面不对 我在想想

[此贴子已经被作者于2016-11-2 00:17编辑过]


早知做人那么辛苦!  当初不应该下凡
2016-11-02 00:16
zzy95621
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-10-26
收藏
得分:0 
回复 5楼 炎天
这是我自己写好的
看这个比较好
我尝试了for(Month=1;NewBalance>0;Month++)
输入1000, 10, 0.5
第七个月能不显示?

#include <stdio.h>
#include <math.h>
double round2 (double);
int main ()
{

printf("Assignment - chung chun yu, EG114402-1C, 160224757\n");
     // Local Declaration
     int NM;
     double IM, P, Q, MP, IY, NY, PR;


     // Statements
     printf("The amount of the loan (principal) :");
     scanf("%lf", &PR);
     printf("Interest rate / year (per cent) :");
     scanf("%lf", &IY);
     printf("Number of Years :");
     scanf("%lf", &NY);

     NM = NY*12;
     IM = IY / 12 / 100;
     P  = pow((1+IM),NM);
     Q  = (P/(P-1));
     MP = PR * IM * Q;

     printf("The amount of the loan (principal) : %.2lf\n", PR);
     printf("Interest rate / month (decimal) : %lf\n", IM);
     printf("Number of Months : %d\n", NM);
     printf("Monthly payment : %.2lf\n", MP);


     int Month;
     double OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance;

{


        OldBalance        = PR;
        NewBalance        = PR - Principalpaid;
        MonthlyPayment    = MP;
        Interestpaid      = OldBalance * IM;
        Principalpaid     = MP - (OldBalance * IM);


printf("\n            Old          Monthly      Interestpaid     Principal        New");
printf("\nMonth     Banlace        Payment        paid            paid          Balance\n");
     printf("=====    =========      =========     =========       =========      =========\n");

}

for(Month=1;Month<=12;Month++)
{

        OldBalance        = NewBalance;
        NewBalance        = OldBalance - Principalpaid;
        MonthlyPayment    = MP;
        Interestpaid      = OldBalance * IM;
        Principalpaid     = MP - (OldBalance * IM);



     printf(" %d        %.2lf\t%.2lf     \t%.2lf    \t%.2lf    \t%.2lf\n", Month, OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance);

}
 }
      double round2 (double x)
     {
         long xInt;
         double result;

         xInt   = (int)(x*100+0.5);
         result = (double)xInt/100;
         return result;
     }



  

[此贴子已经被作者于2016-11-2 00:21编辑过]

2016-11-02 00:19
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
收藏
得分:0 
回复 4楼 zzy95621
for循环改用do ..while
  Month=0;
do{   
       Month++;
       OldBalance        = PR - NewBalance;
       MonthlyPayment    = MP;
       Interestpaid      = OldBalance * IM;
       Principalpaid     = MP - (OldBalance * IM);
       NewBalance        = OldBalance - Principalpaid;
  
  printf(" %d      %.2lf     %.2lf    %.2lf      %.2lf       %.2lf\n", Month, OldBalance, MonthlyPayment, Interestpaid, Principalpaid, NewBalance);
  
  }while(NewBalance>0);



double round2 (double x)    //result回到double 最后输出还会是小数点6位,还是用%.2lf吧,
                           
      {
          long xInt;
          double result;

          xInt   = (int)(x*100+0.5);
          result = (double)xInt/100;
          return result;
      }

引用"我尝试了for(Month=1;NewBalance>0;Month++)" NewBalance>0一开始循环是NewBalance是一个随机值

[此贴子已经被作者于2016-11-2 00:37编辑过]


早知做人那么辛苦!  当初不应该下凡
2016-11-02 00:21
快速回复:C語言还款程序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030007 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved