1.客戶說:幫我開發一個複利計算軟體。
客戶提出:
2.如果按照單利計算,本息又是多少呢?
3.假如30年之後要籌措到300萬元的養老金,平均的年回報率是3%,那麼,現在必須投入的本金是多少呢?
客戶又想:
4.利率這麼低,複利計算收益都這麼厲害了,如果拿100萬元去買年報酬率10%的股票,若一切順利,過多長時間,100萬元就變成200萬元呢?
1 #include<stdio.h>
2 #include<math.h>
3 void meun();
4 void captical_profit(int temp);
5 void captica(int temp);
6 void time(int temp);
7 main()
8 {
9 int temp;
10 char temp0,temp1;
11 meun();
12 lab: printf("請選擇種類:");
13 scanf("%d",&temp);
14 if(temp>0&&temp<6) //判斷輸入是否正确
15 {
16 while(1) //循環選擇
17 {
18 if(temp>0&&temp<4)
19 captical_profit(temp); //求本利和
20 else if(temp==4)
21 captica(temp); //求本金
22 else if(temp==5)
23 time(temp);
24 else if(temp==6)
25 return 0;
26 meun();
27 printf("是否需要繼續選擇(y/n):");
28 getchar();
29 scanf("%c",&temp0);
30 if(temp0=='y')
31 goto lab;
32 else
33 break;
34 }
35 }
36 else //輸入錯誤
37 {
38 printf("輸入錯誤!\n\n是否要重新輸入(y/n):");
39 getchar();
40 scanf("%c",&temp1);
41 if(temp1=='y')
42 goto lab;
43 else
44 return 0;
45 }
46 }
47 //菜單
48 void meun()
49 {
50 printf("*******************************\n");
51 printf("* 1 一次支付複利 *\n");
52 printf("* 2 等額多次支付複利 *\n");
53 printf("* 3 單利計算 *\n");
54 printf("* 4 投入的本金計算 *\n");
55 printf("* 5 計息期數 *\n");
56 printf("* 6 退出 *\n");
57 printf("*******************************\n");
58 }
59 void captical_profit(int temp)
60 {
61 int n;
62 double P,i,F;
63 printf("請輸入期初金額:");
64 scanf("%lf",&P);
65 printf("請輸入利率:");
66 scanf("%lf",&i);
67 printf("請輸入計息期數:");
68 scanf("%d",&n);
69 switch(temp)
70 {
71 case 1:
72 F=P*(pow((1+i),n)); //一次支付複利計算
73 break;
74 case 2:
75 F=(P*(pow((1+i),n)-1))/i; //多次等額複利計算
76 break;
77 case 3:
78 F=P*(1+i*n); //單利計算
79 break;
80 }
81 printf("本利和為:%lf\n\n",F);
82 }
83 //求本金
84 void captica(int temp)
85 {
86 int n;
87 double P,i,F;
88 printf("請輸入本利和:");
89 scanf("%lf",&F);
90 printf("請輸入年回報率:");
91 scanf("%lf",&i);
92 printf("請輸入計息期數:");
93 scanf("%d",&n);
94 P=F/(pow((1+i),n));
95 printf("本金為:%lf\n\n",P);
96 }
97
98 //求計息期數
99 void time(int temp)
100 {
101 int n,flat=0;
102 double P,i,F;
103 printf("請輸入本利和:");
104 scanf("%lf",&F);
105 printf("請輸入年回報率:");
106 scanf("%lf",&i);
107 printf("請輸入本金:");
108 scanf("%lf",&P);
109 for(n=1;n<100;n++) //窮舉法求100年滿足的計息期數
110 {
111 if((P*(pow((1+i),n)))>=F)
112 {
113 printf("計息期數:%d\n\n",n);
114 flat=1;
115 break;
116 }
117 }
118 if(flat==0)
119 printf("在100年内沒有符合計息期數!\n\n");
120
121 }