📄 task_2_mortgage_ calculator.cpp
字号:
// Task_2_Mortgage_ Calculator.cpp : Defines the entry point for the console application.
// 2.5.cpp : Defines the entry point for the console application.
//Task 2
#include "stdafx.h"
#include <stdio.h>
double I=0, H=0;
char file_name[30];
//The other variables are in double format from -1.7*10^(-308)---3.4*10^38 in order to calculate accurately.
int Y=0,Y_=0, month=0, month_=0; //year and month
int end_mon=0, end_Y=0; //the last month and year in the schedule
struct print_
{
int m; //month
int y; //year
double M; //monthly payment
double h; //current monthly interest paid
double total_h; //Total interest
double c;//Amount principal paid
double p; //Monthly principle balance
}mon[500*12]; //the maximum year: 500 years
int transfer(char a[], int _int) //only for the input of the coordinates x and y.
{
int j=0;
int count=0; //if this change to -1, means the digital point exists.
int node=0; //use to get the final number
long double num=0; //in order to have a very long number
char b[25]; //this is another string, of which value is copied from a[]
char *p=b; //a pointer point to b[]
for (int i=0; i<100; i++)
{
if ((a[i]=='E'||a[i]=='e')&&i==0)
return -2;
if (a[i]=='\0') //finish position
break;
if (a[i]=='.')
{
if (_int==1) //only for some compulsory integer input.
{
printf("This number must be a integer! input again!\n");
return -1;
}
node=i;
i++;
count=-1;
}
*p=a[i]-48; //change from ASCII code to decimal number
p++; //add one to the string pointer
}
j=i+count; //if there is a point, the total digits must be i-1
if (count==-1)
node=j-node; // if there is a point, node is the effective digits behind the point
for (int i=0; i<j; i++)
{
if (a[i]=='.'&&count==-1) //if the point appear, the function will not return -1
{
a[i]=5+48;
count++; //but the point cannot appear second time, since there is no two points in a number
}
if (a[i]<0+48||a[i]>9+48) //check if this element is a part of invalid input
{
printf("The input must be valid digits\n");
return -1;
}
}
//printf("j=%d\n", j);
// j=j;
long double temp=0; //it is the product of each digit multiply certain number of 10
for (int i=0;i<j;i++) //there are ii number of digit
{
temp=b[i];
for (int ii=0;ii<j-1-i;ii++) //the number of the multiplied 10 for each digit
temp=temp*10;
num=num+temp; //the number. after all of cycles, it is the dimension of the maze
}
//printf("D=N=%lf\n", num);
for (int i=0; i<node; i++)
num=num/10;
// printf("D=N=%lf\n", num);
return num;
}
double current_paid ( double p, double j , int n) //This is a function for the calculation of the monthly current payment
{
double m=0;
double a=1;
for (int i=0; i<n; i++) //to get the value of (1+J)^N
a=a*(1+j);
m=p*j*(1/(1-(1/a))); //to calculate M=P* J/(1-(1+J)^(-N))
return m; //Return the value of M
}
void schedule(double P, double J, int N, double M)
{ //It will calculate N times monthly interest paid, principal Paid and balance
int i=0;
double C=0, Q=0;
double Total_H=0; //the total interest
printf("\n Mortgage Calculator\n");
printf(" Schedule of Payments\n");
printf ("\nThe amount of loan: $%.2f", P); //to ignore the '0's after the point.
printf ("\nThe annual interest rate(from 1 to 100 percent): %.2f%", I);
printf ("\nThe length (in year )of the loan: %d", N/12);
printf ("\nThe schedule begins from: %d/%d to %d/%d\n", month, Y, end_mon, end_Y);
printf(" \nYear Payment Interest Total Interest Principal Principal \n" );
printf( "Month Paid Paid Paid Paid Balance\n" );
i=0;
while (i!=N)
{ //The 'while (p!=0)' cannot be used, because the last month balance always connot be exactly zero.
H=P*J; //current monthly interest paid
Total_H=Total_H + H; //Total interest
C=M-H; //Amount principal paid
Q=P-C; //Monthly new balance
P=Q;
if (i==N-1) //use this to avoid the vary little money left on the principle balance
{
M=M+P;
C=C+P;
P=0;
}
mon[i].y=Y; //save details for print function
mon[i].m=month;
mon[i].M=M;
mon[i].h=H;
mon[i].total_h=Total_H;
mon[i].c=C;
mon[i].p=P;
printf("\n%3d \n", Y); // year
printf("%3d %11.2f%11.2f%15.2f%15.2f%14.2f\n",month , M, H, Total_H, C, P);
//print the details of payments for each month
i++; //This is a counter to control the progress of program.
month++;
if (month==13)
{
month=1;
Y++;
}
}
// The loop cannot be ended by that the last balance is equal to 0, so the use of 'i' can avoid the infinity cycles
//printf("%f, %f, %d", P,I,L);
}
void sub_extension()
{
int ii=0;
printf ("\nPlease input the name of the file\n");
printf("(only the <.txt> type can be without the extension)\n");
scanf("%s", file_name); //store the file name in file_name
for (int i=0; i< strlen(file_name); i++) //check if the file_name include the file name extension
if (file_name[i]== '.') //if the '.' is found, the file name will not be added by .txt
ii++;
if (ii==0) //if the '.'is not found, the file name will be add .txt as the note pad form
strcpy(file_name, strcat(file_name, ".txt")); //use the string function to add extension to the file name
}
int save (double P, int N)
{
//double Total_H=0; //the total interest
char CC[2]="N"; //This is a small string for receiving 'Y' if users need to print the schedule
FILE *stream;
printf ("\n\nIf you want to save the schedule to a notepad file, \nplease input Y or y, \notherwise press any key to stop\n\n");
int numclosed;
scanf ("%s", CC); //Ask users if they need to print the schedule by enter Y or y
if ((strcmp(CC, "Y")==0) ||(strcmp(CC, "y")==0) ) //If the user needed, print
{
sub_extension();
// Open for write
if( (stream = fopen(file_name, "w" )) == NULL )
{printf( "The file '%s' was not opened\n\n", file_name );
return 0;}
else
printf( "The file '%s' was opened\n\n", file_name);
fprintf(stream," Mortgage Calculator\n Statement of Payments\n");
fprintf (stream,"\nThe amount of loan: $ %.2f", P);
fprintf (stream,"\nThe annual interest rate(from 1 to 100 percent): %.2f%", I);
fprintf (stream,"\nThe length (in year )of the loan: %d", N/12);
fprintf (stream,"\nThe schedule begin from: %d/%d to %d/%d\n", month_, Y_, end_mon, end_Y);
fprintf(stream," \nYear Payment Interest Total Interest Principal Principal \n" );
fprintf(stream, "Month Paid Paid Paid Paid Balance\n" );
for (int i=0;i<N;i++)
{
//print the details of payments for each month
fprintf(stream,"\n%3d \n", mon[i].y); // month
fprintf(stream,"%3d %11.2f%11.2f%15.2f%15.2f%14.2f\n", mon[i].m, mon[i].M, mon[i].h, mon[i].total_h, mon[i].c, mon[i].p);
} //continue to print next month payment schedule
numclosed = fclose( stream); // Close the file
printf( "\n\nThe file closed by fclose: %u\n\n\n", numclosed );
printf ("\nNow, you may print the schedule from the note pad file - '%s'\n\n", file_name);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[]) //This is the main function
{
int N=0; //L=0; //N is the number of months. L is number of years
int L=0;
double P=0, M=0, J=0; //P: principle balance. M:monthly payment. J: monthly interest rate
char CC[2]="N"; //This is a small string for receiving 'Y' if users need to print the schedule
char num[25]; //this is for receiving numbers from inputs
while (-1)
{
printf ("This is a mortgage calculator.\n"); //Ask users to input the amount of loan
printf("Exit by enter 'E' or 'e'\n\n");
printf ("Please input the amount of loan: $"); //Ask users to input the amount of loan
//scanf ("%lf", &P);
scanf ("%s", &num); //receiving numbers from inputs safely, without the programme crashes.
P=transfer(num,0); //Store the value in P variable
while (P==-1)
{
printf ("Please input the amount of loan: $");
scanf ("%s", &num);
P=transfer(num,0);
}
if (P==-2)
return 0;
printf ("\nPlease input the annual interest rate(from 1 to 100 percent):");
//Ask users to input the annual interest rate
scanf ("%s", &num); //receiving numbers from inputs safely, without the programme crashes.
I=transfer(num,0); //Store the value in I variable
while (I==-1)
{
printf ("\nPlease input the annual interest rate(from 1 to 100 percent):");
scanf ("%s", &num);
I=transfer(num,0);
}
if (I==-2)
return 0;
printf ("\nPlease input the length ( in year )of the loan: "); //Ask users to input the length (in year)
scanf ("%s", &num); //receiving numbers from inputs safely, without the programme crashes.
L=transfer(num,1); //Store the value in P variable
while (L==-1)
{
printf ("\nPlease input the length ( in year )of the loan: ");
scanf ("%s", &num);
L=transfer(num,1);
}
if (L==-2)
return 0;
J=(I/12)/100; //Monthly interest rate
N=L*12; //The number of months needed
M= current_paid ( P, J ,N); //The use of function of Current (P, J, N) can get the current monthly payment
printf ("\n\nThe Monthly Payment is: $%3.2f\n\n", M);
printf ("\n\nIf you want to print the schedule on the screen, please input Y or y, \notherwise press any key to stop\n\n");
scanf ("%s", CC); //Ask users if they need to print the schedule by enter Y or y
if ( (strcmp(CC, "Y")==0) ||(strcmp(CC, "y")==0) ) //If the user needed, print
{
printf("Please input the starting month and year respectively:\n");
printf("Starting month:(mm)\n");
scanf ("%s", &num); //receiving numbers from inputs safely, without the programme crashes.
month=transfer(num,1);
while (month==-1||month>12||month<1)
{
printf("Please input valid month!\n");
printf("Starting month:(mm)\n");
scanf ("%s", &num);
month=transfer(num,1);
}
if (month==-2)
return 0;
printf("Year:(yyyy)\n");
scanf("%s", &num);
Y=transfer(num,1);
while (Y==-1||Y<0)
{
printf("Please input valid year!\n");
printf("Year:(yyyy)\n");
scanf("%s", &num);
Y=transfer(num,1);
}
if (Y==-2)
return 0;
month_=month; //for the starting month of the print function
Y_=Y;
end_mon=month-1; //for the last month in the schedule
if (end_mon<=0)
end_mon=12+end_mon;
if (month==1) //for the end year
end_Y=Y+L-1;
else end_Y= Y+L;
schedule( P, J, N, M); //output all of monthly payment details
save (P,N); //output the schedule in the note pad file 'Schedule.txt'
}
// else break;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -