简单的C语言房贷计算器
因要购房,按揭贷款需要考虑资金压力,就要计算贷款成本,于是做了这个简单的房贷计算器程序。现分享给需要的同好。MySpringC是一个简化的C语言编译器。主要用于科学计算、个人娱乐和个性化的设备控制等场合。当前运行在Android平台上,是Android平台的一款应用。使用MySpringC可以书写简单的C语言程序,编写完成后可以编译、运行并获得 结果。还可以做出安卓手机桌面app应用。MySpringC支持的C与普通的C非常类似,程序从main开始,支持全局变量和局部变量,支持各种数据类型和常用的控制流。MySpringC它没有复杂、庞大的编程环境,只要你了解一点C或java,只需要一部手机,在家里、在公共汽车上任何方便的时候,就可以编写满足自己特定需求的小程序。
MySpringC不仅可以编写简单的C语言程序,而且它能读取手机中各种传感器的数值,控制各种多媒体设备,从而实现让普通用户用C语言控制手机,编写出意想不到的、适合自己使用的应用。
v.2.7是目前的完善版本。下面展示的房贷计算器就是用它在安卓手机上编写的,可制作成安卓手机桌面app应用程序。此样例可复制黏贴到编译器直接使用,亦可用 VB6 ,C++ , java 改写。
以下是源码:
// 最简单的 C 语言编程
// myspringc v2.7 可编译安卓本机 app 应用
// 此样例可复制黏贴到 myspringc 编译
// 此文档可用 VB,VC , java 改写
// ******** 房贷计算器**** *************
// 制作人:张纯叔( micelu@126. com)
//*************************************
Canvas cs; //绘图画布
//**** test tab(), alignment **********
//****** calculate Loan *************
string sBarDes[10];
int nBarId[10];
string sMenu[50];
int nMenu[50];
float src[4];
string tab3,tab11,tab12,tab14;
string tab1,tab2,tab4;
string tab5,tab6,tab7;
string tab8,tab9,tab10;
string s,ss,s1,s2,s3,s4,s5,s6;
double BTotal,P1,P2,P3,P4,P5,P6;
double pp1,pp2;
int p1,p2,p3,p6;
string sp1,sp2,sp3,sp6,spp;
int ps;
//double yearnum,monthnum;
double ynum,mnum;
double mRate,mRate1;
string sRate,sRate1;
int i, j,k, n;
string fname; //filename
int size[2];
string ss1,ss2,ss3;
int sebjbx;
cal_Loan_bj (){//等额本金 ************
setDisplay (0);
if (BTotal==0)BTotal=300000;
if (ynum==0)ynum=20.5;
if (mRate==0){mRate=3.25; sRate="3.25"; }
mnum = ynum* 12;
if (BTotal>0&&ynum>0){
mRate1= mRate/100/12;
s=doubleToString (mRate1);
sRate1=subString (s,0,10);
P1 = BTotal / mnum; //月还本金
P6 = BTotal; //贷款总额
pp1 = 0;
pp2 = 0;
//** calculate Loan pp1,pp2 ******
for (i=1;i<=mnum;i++){
pp1 = pp1 + P1; //还本金总数
P2 = P6 * mRate1; //月还利息
pp2 = pp2 + P2; //利息总数
P3= P1+P2; //月还款合计
P6 = P6-P1; } //贷款余额
if (BTotal>999999){s5="";}else{s5=" ";}
if (BTotal<100000) s5=" ";
if ((pp1+pp2)>999999){s6="";}else{s6=" ";}
if ((pp1+pp2)<100000) s6=" ";
if ((pp2)>99999){s4="";}else{s4=" ";}
print "************ 房贷计算:等额本金 *************";
print "贷款总额:",s5,(int)BTotal," 元 ";
print "贷款年数: ",ynum," 年 ";
print "贷款月数: ",(int)mnum," 月 ";
print "贷款利率: "+sRate+ " % "+" 月利率 "+sRate1;
print "还款总额:",s6,(int)BTotal+(int)pp2," 元 ";
print "利息总额: ",s4,(int)pp2," 元 ";
print " ";
print " 期数 月本金 月利息 月合计 贷款余额";
print "----------------------------------------------------------------------------- ";
P1 = BTotal / mnum; //月还本金
P6 = BTotal; //贷款总额
pp1 = 0;
pp2 = 0;
//** start calculate Loan ******
for (i=1;i<=mnum;i++){
pp1 = pp1 + P1; //还款总数
P2 = P6 * mRate1; //月还利息
pp2 = pp2 + P2; //利息总数
P3= P1+P2; //月还款合计
P6 = P6-P1; //贷款余额
p1=(int)P1;
p2=(int)P2;
p3=(int)P3;
p6=(int)P6;
if (i<10)s1=tab5;
if (i>9&&i<100)s1=tab3;
if (i>99)s1=tab1;
s2=tab10;
if (p2<1000)s2=tab10;
if (p2<100)s2=tab11;
if (p2<10)s2=tab12;
print s1,i,tab8, p1, s2,p2,tab10,p3,tab9,p6;
} } //calculate_Loan
print " ";
print "还款总额:",(int)BTotal+(int)pp2;
print "利息总额:",(int)pp2;
}//cal_Loan_bj ()
cal_Loan_bx (){//等额本息 ************
setDisplay (0);
if (BTotal==0)BTotal=300000;
if (ynum==0)ynum=20.5;
if (mRate==0){mRate=3.25; sRate="3.25"; }
mnum = ynum* 12;
if (BTotal>0&&ynum>0){
mRate1= mRate/100/12;
s=doubleToString (mRate1);
sRate1=subString (s,0,10);
P3=BTotal*(mRate1*pow((1+mRate1),mnum))/(pow((1+mRate1),mnum)-1); //月还本息
P6 = BTotal; //贷款总额
pp1 = 0;
pp2 = 0;
//** calculate Loan pp1,pp2 ******
for (i=1;i<=mnum;i++){
P2 = P6 * mRate1; //月还利息
pp2 = pp2 + P2; //利息总数
P1= P3-P2; //月还款合计
pp1 = pp1 + P1; //还本金总数
P6 = P6-P1; } //贷款余额
if (BTotal>999999){s5="";}else{s5=" ";}
if (BTotal<100000) s5=" ";
if ((pp1+pp2)>999999){s6="";}else{s6=" ";}
if ((pp1+pp2)<100000) s6=" ";
if ((pp2)>99999){s4="";}else{s4=" ";}
print "************ 房贷计算:等额本息 *************";
print "贷款总额:",s5,(int)BTotal," 元 ";
print "贷款年数: ",ynum," 年 ";
print "贷款月数: ",(int)mnum," 月 ";
print "贷款利率: "+sRate+ " % "+" 月利率 "+sRate1;
print "还款总额:",s6,(int)BTotal+(int)pp2," 元 ";
print "利息总额: ",s4,(int)pp2," 元 ";
print " ";
print " 期数 月本金 月利息 月合计 贷款余额";
print "------------------------------------------------------------------------------ ";
P3=BTotal*(mRate1*pow((1+mRate1),mnum))/(pow((1+mRate1),mnum)-1); //月还本息
P6 = BTotal; //贷款总额
pp1 = 0;
pp2 = 0;
//** start calculate Loan ******
for (i=1;i<=mnum;i++){
P2 = P6 * mRate1; //月还利息
pp2 = pp2 + P2; //利息总数
P1= P3-P2; //月还款合计
pp1 = pp1 + P1; //还款总数
P6 = P6-P1; //贷款余额
p1=(int)P1;
p2=(int)P2;
p3=(int)P3;
p6=(int)P6;
if (i<10)s1=tab5;
if (i>9&&i<100)s1=tab3;
if (i>99)s1=tab1;
s2=tab6; s3=tab9;
if (p1<1000)s2=tab8;
if (p1<100)s2=tab10;
if (p2<1000)s3=tab10;
if (p2<100)s3=tab11;
if (p2<10)s3=tab12;
print s1,i,s2,p1, s3, p2,tab9,p3,tab9,p6;
} } //calculate_Loan
print " ";
print "还款总额:",(int)BTotal+(int)pp2;
print "利息总额:",(int)pp2;
}//cal_Loan_bx ()
drawBackoval (){
float pi=3.1415926535;
float sx1,sy1;
float r,L,a;
int px,py;//图中心点
px=260;
py=560;
cs.SetStrokeWidth(2);
cs.SetFillMode(1);
for(i=0;i<68;i++){
a=pi/30*i;
cs.SetColor(10,10,200,250);
sx1=(float)(px+sin(a));
sy1=(float)(py+cos(a));
cs.DrawOval(sx1,sy1,3+i*3,16+i*6,i*3); }
cs.Update ();
}//draw background Oval ()
drawcover (){
cs.SetFillMode (1);//0不填色,1填色
cs.SetStrokeWidth (1);
cs.SetColor (255,200,200,250);
cs.DrawRect (0,2,720,990);
cs.SetColor (255,80,80,80);
cs.DrawRect (100,100,630,420);
cs.SetColor (255,240,240,240);
cs.DrawRect (94,95,625,415);
drawBackoval (); //draw ******
cs.SetStrokeWidth (1);
cs.SetTextSize (30);
cs.SetColor (255,250,250,10);
cs.DrawText ("上海房产均价趋势图 ",220,140);
ss3="房产贷款计算 ";
cs.SetTextSize (80);
cs.SetColor (255,60,60,60);
cs.DrawText (ss3,134,554);
cs.SetColor (255,20,250,250);
cs.DrawText (ss3,130,550);
cs.SetTextSize (20);
cs.SetColor (255,150,20,250);
s="1970 1980 1990 2000 2010 2020 ";
cs.DrawText (s,200,364);
cs.DrawText ("10",128,197);
cs.DrawText ("8",135,227);
cs.DrawText ("6",135,257);
cs.DrawText ("4",135,287);
cs.DrawText ("2",135,317);
cs.DrawText ("0",135,347);
cs.SetTextSize (14);
cs.DrawText ("万元 | 年份",127,364);
cs.DrawText ("@模拟数据 不究版权",480,400);
cs.SetTextSize (30);
cs.SetColor (255,50,50,200);
cs.DrawText ("Version 1.02 ",440,650);
cs.SetTextSize (80);
cs.DrawText ("________ ",110,660);
cs.SetTextSize (28);
s="编译人: 张纯叔 ( micelu@126.com ) ";
cs.DrawText (s,20,955);
cs.SetFillMode (0);//0不填色,1填色
cs.SetTextSize (80);
cs.SetColor (255,250,250,20);
cs.DrawText (ss3,130,550);
//draw table *********
cs.SetColor (255,250,0,250);
cs.DrawRect (100,100,620,410);
cs.DrawRect (103,103,617,407);
cs.DrawRect (5,920,710,923);
cs.SetTextSize (30);
cs.SetColor (255,0,80,20);
cs.DrawText ("上海房产均价趋势图 ",220,140);
cs.DrawLine (210,150,490,150);
cs.DrawLine (210,153,490,153);
cs.DrawLine (159,341,570,341);
cs.DrawLine (159,170,159,342);
for (i=0;i<7;i++){
cs.DrawLine(i*60+160,170,i*60+160,340); }
for (i=0;i<6;i++){
cs.DrawLine(160,i*30+190,570,i*30+190); }
cs.SetStrokeWidth (4);
cs.SetColor (255,250,0,0);
cs.DrawLine(161,338,220,335);
cs.DrawCircle (220,335,3);
cs.DrawLine(220,335,280,332);
cs.DrawCircle (280,332,3);
cs.DrawLine(280,332,340,326);
cs.DrawCircle (340,326,3);
cs.DrawLine(340,326,400,308);
cs.DrawCircle (400,308,3);
cs.DrawLine(400,308,460,288);
cs.DrawCircle (460,288,3);
cs.DrawLine(460,288,520,228);
cs.DrawCircle (520,228,3);
cs.Update ();
}//drawcover ()
main(){
setDisplay(1);
cs.SetBackground(255,255,255);
cs.Active();
setTitle("房产贷款计算 ");
sBarDes[0]="输入总额";
nBarId[0]=100;
sBarDes[1]="输入年数";
nBarId[1]=101;
sBarDes[2]="输入利率";
nBarId[2]=102;
sBarDes[3]="开始计算";
nBarId[3]=103;
sBarDes[4]="退出程序";
nBarId[4]=104;
sBarDes[5]="V.";
nBarId[5]=105;
setToolBarHeight(6);
setButtonTextSize(13);
setToolBarBackgroundColor(255,192,192,192);
setButtonColor(255,0,0,240);
setButtonTextColor(255,255,255,0);
setToolBar(100,myToolBarProc,sBarDes,nBarId,6);
sMenu[0]="输入> 贷款总额";
nMenu[0]=200;
sMenu[1]="输入> 贷款年数";
nMenu[1]=201;
sMenu[2]="输入> 贷款利率";
nMenu[2]=202;
sMenu[3]="等额本金> 计算";
nMenu[3]=203;
sMenu[4]="等额本息> 计算";
nMenu[4]=204;
sMenu[5]="显示> 计算结果";
nMenu[5]=205;
sMenu[6]="版本信息";
nMenu[6]=206;
sMenu[7]="退出程序";
nMenu[7]=207;
setMenu(200,myMenuProc,sMenu,nMenu,8);
tab1=" ";
tab2=" ";
tab3=" ";
tab4=" ";
tab5=" ";
tab6=" ";
tab7=" ";
tab8=" ";
tab9=" ";
tab10=" ";
tab11=" ";
tab12=" ";
tab14=" ";
drawcover ();
while (){}
}//main ()
myToolBarProc(int nBtn,int nContext){
if(nBtn==100){//输入贷款总额
BTotal=doubleInput("输入贷款总额"," 输入格式: 1000000 输入( 0 )或( 空 )> 取消");
if(BTotal==0)return;
}
if(nBtn==101){//输入期数 年
ynum =doubleInput("输入贷款年数"," 输入格式: 20.5 小数部分为月数 输入( 0 )或( 空 )> 取消");
if(ynum==0)return;
}
if(nBtn==102){//输入贷款利率
mRate=doubleInput("输入贷款利率"," 输入格式: 3.25 输入( 0 )或( 空 )> 取消");
sRate=doubleToString (mRate);
if(mRate==0)return;
}
if(nBtn==103){//开始计算
clearOutput ();
cs.ClearDraw (0,src);
setDisplay (0);
if (sebjbx==0)cal_Loan_bj ();
if (sebjbx==1)cal_Loan_bx ();
}
if(nBtn==104){//退出程序
clearOutput();
cs.ClearDraw (0,src);
setDisplay (0);
exit (0);
}
if(nBtn==105){//版本信息
drawcover ();
setDisplay (1);
}
}//my toolbar()
myMenuProc(int nMen,int nContext)
{
if(nMen==200){ //input BRotal
BTotal=doubleInput("输入贷款总额"," 输入格式: 1000000 输入( 0 )或( 空 )> 取消");
if(BTotal==0)return;
}
if(nMen==201){ //input yearnum
ynum =doubleInput("输入贷款年数"," 输入格式: 20.5 小数部分为月数 输入( 0 )或( 空 )> 取消");
if(ynum==0)return;
}
if(nMen==202){ //input mRate
mRate=doubleInput("输入贷款利率"," 输入格式: 3.25 输入( 0 )或( 空 )> 取消");
sRate=doubleToString (mRate);
if(mRate==0)return;
}
if (nMen==203){//cal_Loan bj 等本
clearOutput ();
sebjbx=0;
cal_Loan_bj ();
}
if (nMen==204){//cal_Loan bx 等息
clearOutput ();
sebjbx=1;
cal_Loan_bx ();
}
if(nMen==205){//close canvas
setDisplay (0);
}
if(nMen==206){//show canvas
setDisplay (1);
}
if (nMen==207){//Exit
clearOutput();
cs.ClearDraw (0,src);
exit (0);
}
}//myMenu()