C++ C语言程序设计 题目:贷款计算器
发布网友
发布时间:2022-04-30 18:31
我来回答
共3个回答
热心网友
时间:2022-06-29 06:03
/*
* main.c
*
* Created on: 2011-6-8
* Author: icelights
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define APR1 0.0747 /*<1年(含1年)年利率*/
#define APR2 0.0756 /*1-3年(含3年)年利率*/
#define APR3 0.0774 /*3-5年(含5年)年利率*/
#define APR4 0.0783 /*5年以上年利率*/
#define A_TO_M 1/12 /*月利率 = 年利率 / 12*/
#define RTP 12 /*Reimbursement total periods还款总期数 =年限*12*/
#define LENGTH 80
struct LoanInfo
{
/*姓名*/
char name[LENGTH];
/*贷款总额*/
double LoanAmount;
/*贷款年限*/
double LoanYear;
/*月付*/
double MonthlyPayment;
/*总利息*/
double TotalInterest;
/*还款总额*/
double ReimbursementAmount;
/*年利率*/
double apr;
struct LoanInfo * next;
};
void CalcShow(struct LoanInfo * cur, struct LoanInfo * hd,
struct LoanInfo * prv);
int main(void)
{
int temp;
struct LoanInfo * head = NULL;
struct LoanInfo * prev, * current;
current = (struct LoanInfo *)malloc(sizeof(struct LoanInfo));
if (NULL == head)
{
head = current;
}
else
{
prev->next = current;
}/*End of if (NULL == head)*/
puts("请输入姓名");
gets(current->name);
fflush(stdin);
puts("请输入贷款数额(单位:万元)");
scanf("%lf", ¤t->LoanAmount);
fflush(stdin);
puts("请输入贷款年限");
scanf("%lf", ¤t->LoanYear);
fflush(stdin);
printf("姓名:%s,贷款年限:%lf, 贷款数额%lf",
current->name, current->LoanYear, current->LoanAmount);
prev = current;
puts("请确认Y/N");
temp = getchar();
switch(toupper(temp))
{
case 'Y' : CalcShow(current, head, prev);
break;
case 'N' : free(current);
main();
break;
default : puts("输入错误");
free(current);
break;
}
return 0;
}
void CalcShow(struct LoanInfo * cur, struct LoanInfo * hd,
struct LoanInfo * prv)
{
char lcv_temp;
if (cur->LoanYear <= 1)
cur->apr = APR1;
else if (cur->LoanYear <= 3)
cur->apr = APR2;
else if (cur->LoanYear <= 5)
cur->apr = APR3;
else
cur->apr = APR4;
/*End of if (year <= 1)*/
cur->LoanAmount = 10000 * cur->LoanAmount;
cur->ReimbursementAmount = cur->LoanAmount * pow((1 + cur->apr), cur->LoanYear);
cur->MonthlyPayment = cur->ReimbursementAmount / (cur->LoanYear * RTP);
cur->TotalInterest = cur->ReimbursementAmount - cur->LoanAmount;
printf("姓名:%s 贷款年限:%.0lf\n"
"贷款数额:%.2lf 每月还款额:%.2lf\n"
"利息合计:%.2lf 还款总额:%.2lf\n",
cur->name, cur->LoanYear, cur->LoanAmount,
cur->MonthlyPayment, cur->TotalInterest, cur->ReimbursementAmount);
puts("是否继续计算Y/N");
lcv_temp = getchar();
switch(toupper(lcv_temp))
{
case 'Y' : free(cur);
main();
break;
case 'N' : free(cur);
exit(0);
default : puts("输入错误");
free(cur);
main();
break;
}
system("pause");
}
热心网友
时间:2022-06-29 06:04
#include<iostream>
#include<iomanip>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
char a='Y',b='N';
char name[20];
double n;
double j,k,l;
int m;
while (a=='Y')
{
b='N';
while (b=='N')
{
for(int i=0;i<=79;i++)
cout<<'^';
cout<<endl;
cout<<"输入用户姓名:";
cin>>name;
cout<<"输入贷款金额(万元):";
cin>>n;
cout<<"输入贷款年限:";
cin>>m;
cout<<"用户姓名:"<<name<<"贷款金额(万元):"<<n<<"贷款年限:"<<m<<endl;
for(int p=0;p<=79;p++)
cout<<'^';
cout<<"正确?(Y/N)";
cin>>b;
if(b=='Y') break;
}
if(m<=1)
{
n=n*10000;
j=n*0.0747/12;
k=n*0.0747;
l=n+k;
}
else if(m>1&&m<=3)
{
n=n*10000;
j=n*0.0756/12;
k=n*0.0756;
l=n+k;
}
else if(m>3&&m<=5)
{
n=n*10000;
j=n*0.0774/12;
k=n*0.0774;
l=n+k;
}
else
{
n=n*10000;
j=n*0.0783/12;
k=n*0.0783;
l=n+k;
}
cout<<"姓名:"<<name<<" "<<"贷款年限:"<<m<<endl;
cout<<"贷款数额(元):"<<setw(10)<<n<<"每月还款额(元):"<<setw(10)<<j<<endl;
cout<<"利息合计(元):"<<setw(10)<<k<<"还款总额(元):"<<setw(10)<<l<<endl;
cout<<"是否进行新的计算(Y/N)?";
cin>>a;
if(b=='N') break;
else system("cls");
}
return 0;
}
热心网友
时间:2022-06-29 06:04
支持 加减乘除括号负数开根乘方
#include<stdio.h>
#include<math.h>
#include<malloc.h>
double jisuan(char a[])
{
int i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0;
char nibo[50],zhan2[50];
double x,n,l,z=0,zhan3[50];
typedef struct
{
double d1;
int d2;
}dd;
typedef struct
{
dd data[50];
int top;
}zhan1;
zhan1 *shu;
shu=(zhan1 *)malloc(sizeof(zhan1));
shu->top=0;
while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9')
{
z=0;
j=i+1;
while(a[j]>='0'&&a[j]<='9')
j--;
for(k=i;k<=j;k++)
{
z=z*10+a[k]-'0';
}
j=j+1;
x=z;
if(a[j]=='.')
{
l=1;
i=j+1;
j=i+1;
while(a[j]>='0'&&a[j]<='9')
j--;
for(k=i;k<=j;k++)
{
n=pow(0.1,l);
l=l+1;
x=x+n*(a[k]-'0');
}
i=j+1;
}
else i=j;
shu->data[++shu->top].d1=x;
shu->data[shu->top].d2=++cnt;
nibo[++t1]='0'+shu->data[shu->top].d2;
nibo[t1+1]='\0';
}
else if(a[i]=='(')
{
zhan2[++t2]=a[i];
i++;
}
else if(a[i]==')')
{
j=t2;
while(zhan2[j]!='(')
{
nibo[++t1]=zhan2[j];
nibo[t1+1]='\0';
j--;
}
t2=j-1;
i++;
}
else if(a[i]=='+')
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
else if(a[i]=='-')
{
if(a[i-1]=='$')
{
a[0]='0';
i=0;
}
else if(a[i-1]=='(')
{
a[i-1]='0';
a[i-2]='(';
i=i-2;
t2--;
}
else
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
}
else if(a[i]=='*'||a[i]=='/')
{
while(zhan2[t2]=='*'||zhan2[t2]=='/'||zhan2[t2]=='^'||zhan2[t2]=='#')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
else if(a[i]=='^'||a[i]=='#')
{
while(zhan2[t2]=='^'||zhan2[t2]=='#')
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
zhan2[++t2]=a[i];
i++;
}
}
while(t2>0)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]='\0';
t2--;
}
j=1;t3=0;
while(j<=t1)
{
if(nibo[j]>='0'&&nibo[j]!='^'&&nibo[j]!='#')//
{
for(i=1;i<=shu->top;i++)
{
if((int)(nibo[j]-'0')==shu->data[i].d2)
{
m=i;
break;
}
}
zhan3[++t3]=shu->data[m].d1;
}
else if(nibo[j]=='+')
{
zhan3[t3-1]=zhan3[t3-1]+zhan3[t3];
t3--;
}
else if(nibo[j]=='-')
{
zhan3[t3-1]=zhan3[t3-1]-zhan3[t3];
t3--;
}
else if(nibo[j]=='*')
{
zhan3[t3-1]=zhan3[t3-1]*zhan3[t3];
t3--;
}
else if(nibo[j]=='/')
{
zhan3[t3-1]=zhan3[t3-1]/zhan3[t3];
t3--;
}
else if(nibo[j]=='^')
{
zhan3[t3-1]=pow(zhan3[t3-1],zhan3[t3]);
t3--;
}
else if(nibo[j]=='#')
{
zhan3[t3]=sqrt(zhan3[t3]);
}
j++;
}
return zhan3[t3];
}
void main()
{
for(;;)
{
char x,a[50];
double jieguo;
int i=0;
a[0]='$';
printf("#表示开方,^表示乘方(支持负数)\n");
printf("请输入表达式,退出请输入q:\n\n");
scanf("%c",&x);
if(x=='q') break;
while(x!='\n')
{
a[++i]=x;
scanf("%c",&x);
}
a[i+1]='\0';
jieguo=jisuan(a);
printf("\n");
printf("结果为:%lf",jieguo);
printf("\n\n\n\n\n");
}
}