複利計算(更新)

14馮梓凡發表於2016-03-17
  1 #include<stdio.h>
  2 #include<math.h>
  3 void view()
  4 {
  5     printf("              |---------------------------------------------|\n");
  6     printf("              |                 單/複利計算                 |\n");
  7     printf("              |---------------------------------------------|\n");
  8     printf("              |1.單利計算                                   |\n");
  9     printf("              |---------------------------------------------|\n");
 10     printf("              |2.複利計算                                   |\n");
 11     printf("              |---------------------------------------------|\n");
 12     printf("              |3.求年限                                     |\n");
 13     printf("              |---------------------------------------------|\n");
 14     printf("              |4.求年利率                                   |\n");
 15     printf("              |---------------------------------------------|\n");
 16     printf("              |5.固定存錢                                   |\n");
 17     printf("              |---------------------------------------------|\n");
 18     printf("              |6.貸款                                       |\n");
 19     printf("              |---------------------------------------------|\n");
 20     printf("              |0.退出系統                                   |\n");
 21     printf("              |---------------------------------------------|\n");
 22 }
 23 
 24 
 25 
 26 main()
 27 {
 28     int i,n,N=100;
 29     double v,money;  //本金 
 30     int years;    //存錢年數、月數 
 31     double sum;  //存款總額 
 32     double r;   //年利率
 33     double A,B,C,y,Money;
 34     while(1)
 35     {
 36 la1:view();
 37         printf("請選擇(0-6):");
 38         scanf("%d",&n);
 39         switch(n)
 40         {
 41     case 1:
 42         printf("請輸入本金:");
 43         scanf("%lf",&money);
 44           printf("請輸入存錢年數:");
 45         scanf("%d",&years);
 46         printf("請輸入利率:");
 47         scanf("%lf",&r);
 48         for(i = 1 ; i<=years; i++)
 49         {
 50             sum = r*i*money+money;  
 51             printf("存入第%d年後的存款總額為:%.2lf\n\n\n",i,sum);
 52         }
 53         break;
 54     case 2:
 55         printf("請選擇:\n");
 56         printf("1.通過本金求存款總額\n");
 57         printf("2.通過目標總額求本金\n\n");
 58         scanf("%d",&n);
 59         switch(n)
 60         {
 61         case 1:
 62             printf("請輸入本金:");
 63             scanf("%lf",&money);
 64             printf("請輸入存錢年數:");
 65             scanf("%d",&years);
 66             printf("請輸入利率:");
 67             scanf("%lf",&r);
 68             for(i = 1 ; i<=years; i++)
 69             {
 70                 v=pow(1+r,i);
 71                 sum = v*money;  
 72                 printf("存入第%d年後的存款總額為:%.2lf\n",i,sum); 
 73             }
 74             goto la1;
 75         case 2:
 76             printf("請輸入目標金錢:");
 77             scanf("%lf",&sum);
 78             printf("請輸入存錢年數:");
 79             scanf("%d",&years);
 80             printf("請輸入利率:");
 81             scanf("%lf",&r);
 82             for(i = 1 ; i<=years; i++)
 83             {
 84                 v=pow(1+r,i);
 85             }
 86             money=sum/v;  
 87             printf("現在必須存入本金為:%.2lf\n\n\n",money); 
 88             goto la1;
 89         }
 90         case 3:
 91             printf("請輸入本金:");
 92             scanf("%lf",&money);
 93             printf("請輸入目標金錢:");
 94             scanf("%lf",&sum);
 95             printf("請輸入利率:");
 96             scanf("%lf",&r);
 97             C=A=1+r;
 98             B=sum/money;
 99             for(i=1;i<N;i++)
100             {
101                 C=C*A;
102                 if(C>=B)
103                 {
104                     break;
105                 }
106             }
107             printf("所需年數為%d\n\n\n",i+1);
108             break;
109         case 4:
110             printf("請輸入本金:");
111             scanf("%lf",&money);
112             printf("請輸入存錢年數:");
113             scanf("%d",&years);
114             printf("請輸入目標金錢:");
115             scanf("%lf",&sum);
116             B=sum/money;
117             y=1.0/years;
118             r=pow(B,y)-1;
119             printf("所求年利率為:%-.3lf\n\n\n",r);
120             break;
121         case 5:printf("1.每月定投\n");
122                printf("2.每年定投\n");
123                scanf("%d",&n);
124             switch(n)
125             {
126             case 1:
127             printf("每月投入本金:");
128             scanf("%lf",&money);
129             printf("請輸入存錢年數:");
130             scanf("%d",&years);
131             printf("請輸入年利率:");
132             scanf("%lf",&r);
133             money=money*12;
134             for(i=1;i<=years;i++)
135             {
136                 v=pow(1+r,i);
137             }
138             Money=money*(1+r)*(-1+v)/r;
139             printf("%d年後本金和利息之和為:%lf\n\n\n",years,Money);
140             goto la1;
141             case 2:
142                 printf("每年投入本金:");
143                 scanf("%lf",&money);
144                 printf("請輸入存錢年數:");
145                 scanf("%d",&years);
146                 printf("請輸入年利率:");
147                 scanf("%lf",&r);
148                 for(i=1;i<=years;i++)
149                 {
150                      v=pow(1+r,i);
151                 }
152                 Money=money*(1+r)*(-1+v)/r;
153                 printf("%d年後本金和利息之和為:%lf\n\n\n",years,Money);
154                 goto la1;
155             }
156             case 6:
157                 printf("貸款數目:");
158                 scanf("%lf",&Money);
159                 printf("還錢期限(年):");
160                 scanf("%d",&years);
161                 printf("請輸入年利率:");
162                 scanf("%lf",&r);
163                 for(i=1;i<=years;i++)
164                 {
165                      v=pow(1+r,i);
166                 }
167                 money=Money*r/(-1+v)/(1+r);
168                 money=money/12;
169                 printf("每月等額本息應還款:%lf\n\n\n",money);
170                 break;
171 
172     case 0:
173         return 0;
174     }
175     }
176 }

相關文章