1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 void view() 5 { 6 printf(" |---------------------------------------------| "); 7 printf(" | 单/复利计算 | "); 8 printf(" |---------------------------------------------| "); 9 printf(" |1.单利计算 | "); 10 printf(" |---------------------------------------------| "); 11 printf(" |2.复利计算 | "); 12 printf(" |---------------------------------------------| "); 13 printf(" |3.求年限 | "); 14 printf(" |---------------------------------------------| "); 15 printf(" |4.求年利率 | "); 16 printf(" |---------------------------------------------| "); 17 printf(" |5.固定存钱 | "); 18 printf(" |---------------------------------------------| "); 19 printf(" |6.贷款 | "); 20 printf(" |---------------------------------------------| "); 21 printf(" |0.退出系统 | "); 22 printf(" |---------------------------------------------| "); 23 } 24 25 void danli() 26 { 27 int i,years; //存钱年数 28 double money,r,sum; //本金,年利率 29 printf("请输入本金:"); 30 scanf("%lf",&money); 31 printf("请输入存钱年数:"); 32 scanf("%d",&years); 33 printf("请输入利率:"); 34 scanf("%lf",&r); 35 for(i = 1 ; i<=years; i++) 36 { 37 sum = r*i*money+money; 38 printf("存入第%d年后的存款总额为:%.2lf ",i,sum); 39 } 40 } 41 42 void year() 43 { 44 int i,years,N=100; 45 double money,r,sum,A,B,C; 46 printf("请输入本金:"); 47 scanf("%lf",&money); 48 printf("请输入目标金钱:"); 49 scanf("%lf",&sum); 50 printf("请输入利率:"); 51 scanf("%lf",&r); 52 C=A=1+r; 53 B=sum/money; 54 for(i=1;i<N;i++) 55 { 56 C=C*A; 57 if(C>=B) 58 { 59 break; 60 } 61 } 62 years=i+1; 63 printf("所需年数为%d ",years); 64 } 65 66 void nianlilv() 67 { 68 int years; 69 double money,r,sum,B,y; 70 printf("请输入本金:"); 71 scanf("%lf",&money); 72 printf("请输入存钱年数:"); 73 scanf("%d",&years); 74 printf("请输入目标金钱:"); 75 scanf("%lf",&sum); 76 B=sum/money; 77 y=1.0/years; 78 r=pow(B,y)-1; 79 printf("所求年利率为:%-.3lf ",r); 80 } 81 82 void dingtou1() 83 { 84 int years,i; 85 double money,r,Money,v; 86 printf("每月投入本金:"); 87 scanf("%lf",&money); 88 printf("请输入存钱年数:"); 89 scanf("%d",&years); 90 printf("请输入年利率:"); 91 scanf("%lf",&r); 92 money=money*12; 93 for(i=1;i<=years;i++) 94 { 95 v=pow(1+r,i); 96 } 97 Money=money*(1+r)*(-1+v)/r; 98 printf("%d年后本金和利息之和为:%lf ",years,Money); 99 } 100 101 void dingtou2() 102 { 103 int years,i; 104 double money,r,Money,v; 105 printf("每年投入本金:"); 106 scanf("%lf",&money); 107 printf("请输入存钱年数:"); 108 scanf("%d",&years); 109 printf("请输入年利率:"); 110 scanf("%lf",&r); 111 for(i=1;i<=years;i++) 112 { 113 v=pow(1+r,i); 114 } 115 Money=money*(1+r)*(-1+v)/r; 116 printf("%d年后本金和利息之和为:%lf ",years,Money); 117 } 118 119 void daikuan() 120 { 121 int years,i; 122 double money,r,Money,v; 123 printf("贷款数目:"); 124 scanf("%lf",&Money); 125 printf("还钱期限(年):"); 126 scanf("%d",&years); 127 printf("请输入年利率:"); 128 scanf("%lf",&r); 129 for(i=1;i<=years;i++) 130 { 131 v=pow(1+r,i); 132 } 133 money=Money*r/(-1+v)/(1+r); 134 money=money/12; 135 printf("每月等额本息应还款:%lf ",money); 136 } 137 138 void SUM() 139 { 140 int years,i; 141 double money,r,v,sum; 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 sum = v*money; 152 printf("存入第%d年后的存款总额为:%.2lf ",i,sum); 153 } 154 } 155 156 void benjin() 157 { 158 int years,i; 159 double money,r,v,sum; 160 printf("请输入目标金钱:"); 161 scanf("%lf",&sum); 162 printf("请输入存钱年数:"); 163 scanf("%d",&years); 164 printf("请输入利率:"); 165 scanf("%lf",&r); 166 for(i = 1 ; i<=years; i++) 167 { 168 v=pow(1+r,i); 169 } 170 money=sum/v; 171 printf("现在必须存入本金为:%.2lf ",money); 172 } 173 174 main() 175 { 176 int n; 177 while(1) 178 { 179 la1:view(); 180 printf("请选择(0-6):"); 181 scanf("%d",&n); 182 switch(n) 183 { 184 case 1: 185 danli(); 186 break; 187 case 2: 188 printf("请选择: "); 189 printf("1.通过本金求存款总额 "); 190 printf("2.通过目标总额求本金 "); 191 scanf("%d",&n); 192 switch(n) 193 { 194 case 1: 195 SUM(); 196 goto la1; 197 case 2: 198 benjin(); 199 goto la1; 200 } 201 case 3: 202 year(); 203 break; 204 case 4: 205 nianlilv(); 206 break; 207 case 5:printf("1.每月定投 "); 208 printf("2.每年定投 "); 209 scanf("%d",&n); 210 switch(n) 211 { 212 case 1: 213 dingtou1(); 214 goto la1; 215 case 2: 216 dingtou2(); 217 goto la1; 218 } 219 case 6: 220 daikuan(); 221 break; 222 223 case 0: 224 n=0;exit(0); 225 } 226 } 227 }
这个程序主要增加了贷款这个功能,并且设置了循环的功能,使得程序能根据使用者意愿执行所需的功能,不会自动退出。