标签:
程序完成时间为3月20日13:35分。
发表时间为3月20日13:37分。
此版本是在前面三个版本的基础上进行完善,实现了等额本息还款计算,解决了如果用户输入一部分不恰当的数据会提示输入不合法,请重新输入,增强了系统的运行性,不会因此而导致系统会奔溃
不断修改的可观性,使用户更加直观的运用此运算器。
1 #include<stdio.h> 2 #include<math.h> 3 #define x1 while(0 == 4 #define x2 ){ while(‘\n‘ != getchar()) { } printf("输入不合法!\n请重新输入: ---"); } 5 6 double danli(double i,int N,double P) 7 { 8 double nlx,F; 9 nlx = P*i; 10 F = nlx*N + P; 11 return F; 12 13 } 14 15 double fuli(double i,int W,int N,double P) 16 { 17 double F; 18 i=pow(1+i/W,W)-1; 19 F=P*pow((i+1),N); 20 return F; 21 } 22 23 double gusuan(double i,int W,int N,double F) 24 { 25 double P; 26 i = pow(1+i/W,W)-1; 27 P = F/pow((i+1),N); 28 return P; 29 } 30 double shijian(double P,double i,double F) 31 { 32 double N,x; 33 x=F/P; 34 N=(log(x))/(log(i+1)); 35 return N; 36 37 } 38 double nianxian(double P,double N,double F) 39 { 40 double i; 41 //i = W*(pow(pow(F/P,1.0/N),1.0/W)-1.0); 42 i = pow(F/P,1.0/N)-1.0; 43 return i; 44 } 45 double sum() 46 { 47 double y,yue,F=0.0,i,mi; 48 double nl,yl; 49 int N,W; 50 int ch,j; 51 printf("****** 1-年投资 2-月投资 ******\n"); 52 scanf("%d",&ch); 53 if(ch == 1) 54 { 55 printf("请输入年投资额:\n"); 56 x1 scanf("%lf",&y) x2; 57 } 58 else 59 { 60 printf("请输入月投资额:\n"); 61 x1 scanf("%lf",&yue) x2; 62 } 63 64 printf("请输入年利率\n"); 65 x1 scanf("%lf",&i) x2; 66 printf("请输入年限\n"); 67 x1 scanf("%d",&N) x2; 68 69 if(ch == 1) 70 { 71 F = y; 72 for(j=0 ; j<N ; j++) 73 { 74 nl = F*i; 75 F=y+nl+F; 76 } 77 return F-y; 78 } 79 else 80 { 81 F = yue; 82 mi = i/12.0; 83 for(j=0 ; j<N*12 ; j++) 84 { 85 yl = F*mi; 86 F=yue+yl+F; 87 } 88 return F-yue; 89 } 90 91 92 } 93 double refund() 94 { 95 double P,i,month_amount; 96 int N,month; 97 printf("************您进入本息还款计算************\n"); 98 printf("请输入贷款金额:\n"); 99 x1 scanf("%lf",&P) x2 100 printf("请输入年利率:\n"); 101 x1 scanf("%lf",&i) x2 102 printf("请输入归还年限:\n"); 103 x1 scanf("%d",&N) x2 104 105 //每月还款额=[贷款本金×月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1] 106 107 i = i/12; 108 month = N*12; 109 month_amount = P*i*pow((1.0+i),month)/(pow(1.0+i,month)-1); 110 return month_amount; 111 112 } 113 void main() 114 { 115 double P,F1,F,i; //P是本金,F是收益,i年利率 116 int N,W,ch2; //是期限 117 while(1) 118 { 119 printf("\t\t***************欢迎使用复利计算器*************\n\n"); 120 printf("\t\t| -----1--------为复利--------------- |\n\n"); 121 printf("\t\t| -----2--------为单利--------------- |\n\n"); 122 printf("\t\t| -----3-------为估算---------------- |\n\n"); 123 printf("\t\t| -----4-------为年限---------------- |\n\n"); 124 printf("\t\t| -----5-------为年利率-------------- |\n\n"); 125 printf("\t\t| -----6-------为月年收益-------------- |\n\n"); 126 printf("\t\t| -----7-------等额本息还款-------------- |\n\n"); 127 printf("\t\t| -----0-------为退出---------------- |\n\n"); 128 printf("\t\t**************请选择想要的功能************\n"); 129 x1 scanf("%d",&ch2) x2;//控制选择 130 switch(ch2){ 131 case 1: 132 printf("请输入本金:\n"); 133 x1 scanf("%lf",&P) x2; 134 printf("请输入年利率\n"); 135 x1 scanf("%lf",&i) x2; 136 printf("请输入年限:\n"); 137 x1 scanf("%d",&N) x2; 138 printf("请输入复利次数:\n"); 139 x1 scanf("%d",&W) x2; 140 F1 = fuli(i,W,N,P); break; 141 case 2: 142 printf("请输入本金:\n"); 143 x1 scanf("%lf",&P) x2; 144 printf("请输入年利率\n"); 145 x1 scanf("%lf",&i) x2; 146 printf("请输入年限:\n"); 147 x1 scanf("%d",&N) x2; 148 F1 = danli(i,N,P); break; 149 case 3: 150 printf("请输入本利\n"); 151 x1 scanf("%lf",&F) x2; 152 printf("请输入年利率\n"); 153 x1 scanf("%lf",&i) x2; 154 printf("请输入年限:\n"); 155 x1 scanf("%d",&N) x2; 156 printf("请输入复利次数:\n"); 157 x1 scanf("%d",&W) x2; 158 F1 = gusuan(i,W,N,F); 159 break; 160 161 case 4: 162 printf("请输入本金\n"); 163 x1 scanf("%lf",&P) x2; 164 printf("请输入年利率\n"); 165 x1 scanf("%lf",&i) x2; 166 printf("请输入本利\n"); 167 x1 scanf("%lf",&F) x2; 168 F1=shijian(P,i,F); 169 break; 170 case 5: 171 172 printf("请输入本金:\n"); 173 x1 scanf("%lf",&P) x2; 174 printf("请输入本利:\n"); 175 x1 scanf("%lf",&F) x2; 176 printf("请输入年限\n"); 177 x1 scanf("%d",&N) x2; 178 F1=nianxian(P,N,F); 179 180 // i=pow((F/P),(1.0/N))-1; 181 break; 182 case 6: 183 F1=sum(); 184 break; 185 case 7: 186 F1=refund(); 187 break; 188 case 0:break; 189 default: 190 printf("输入不合法!!\n"); 191 continue ; 192 } 193 if(ch2==0) 194 break; 195 printf("结果为:%.2lf\n",F1); 196 } 197 }
标签:
原文地址:http://www.cnblogs.com/caicaihong/p/5297624.html