我对编码还比较陌生,但并不完全没有经验。从事有关金融计算器的学校作业。如果你们中的任何一个可以看看我的代码以了解不良做法/可能的改进等,那就太好了。
我确实添加了一个“动画”创业公司(带有很多printf来显示“财务计算器”,但无法容纳它,这样做是个好主意吗?)
option_2和option_4暂时留在我的小组使用其他2个计算器的位置。
//ASSIGNMENT_041118
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <signal.h>
//files
//colours
#define ANSI_COLOR_BLACK "\x1b[30m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_WHITE "\x1b[37m"
#define ANSI_COLOR_BBLACK "\x1b[90m"
#define ANSI_COLOR_BRED "\x1b[91m"
#define ANSI_COLOR_BGREEN "\x1b[92m"
#define ANSI_COLOR_BYELLOW "\x1b[93m"
#define ANSI_COLOR_BBLUE "\x1b[94m"
#define ANSI_COLOR_BMAGENTA "\x1b[95m"
#define ANSI_COLOR_BCYAN "\x1b[96m"
#define ANSI_COLOR_BWHITE "\x1b[97m"
#define ANSI_COLOR_RESET "\x1b[0m"
//function prototype declaration
int main_menu();
int login();
float compound();
float compound_calc(float n, float initial_savings, float rate, float time);
float option_2();
float mortgage();
float option_4();
int main()
{
system("color 0F");
login();
return (0);
}
int login()
{
char username[20];
char password[20];
int l_option;
printf("=========================================================================================================\n");
printf("Enter your username (case sensitive): ");
scanf("%s", &username);
printf("\nEnter your password (case sensitive): ");
scanf("%s", &password);
if (strcmp(username, "user") == 0)
{
if (strcmp(password, "0000") == 0)
{
system("cls");
return main_menu();
}
else
{
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR - WRONG PASSWORD\n"ANSI_COLOR_RESET);
do {
printf("---------------------------------------------------------------------------------------------------------\n");
printf("[1]Retry\n[0]Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &l_option);
switch (l_option)
{
case 1:
system("cls");
return login();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("EXIT\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED "INVALID OPTION - CHOOSE AGAIN\n" ANSI_COLOR_RESET);
break;
}
} while (l_option != 1 && l_option != 0);
}
}
else
{
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR - USER DOESN'T EXIST\n"ANSI_COLOR_RESET);
do {
printf("---------------------------------------------------------------------------------------------------------\n");
printf("[1]Retry\n[0]Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &l_option);
switch (l_option)
{
case 1:
system("cls");
return login();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("EXIT\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED "INVALID OPTION - CHOOSE AGAIN\n" ANSI_COLOR_RESET);
break;
}
} while (l_option != 1 && l_option != 0);
}
}
int main_menu()
{
int option_main;
do {
printf("=========================================================================================================\n");
printf("Choose an option:\n"); //tells user to select an option
printf("---------------------------------------------------------------------------------------------------------\n");
printf(ANSI_COLOR_BGREEN"[1] <Savings Calculator - Compounded Interest> \n"ANSI_COLOR_RESET); //displays options
printf(ANSI_COLOR_BMAGENTA"[2] <place_holder_2> \n"ANSI_COLOR_RESET);
printf(ANSI_COLOR_BYELLOW"[3] <Mortage calculator> \n"ANSI_COLOR_RESET);
printf(ANSI_COLOR_BCYAN"[4] <place_holder_3> \n"ANSI_COLOR_RESET);
printf("\n[0] <Exit system>\n");
printf("=========================================================================================================\n");
scanf("%d", &option_main); //accepts input for function selection
printf("=========================================================================================================\n");
system("cls");
switch (option_main) //switch case for main option
{
case 1:
{
compound();
break;
}
case 2:/*option_2*/
{
option_2();
break;
}
case 3:/*option_3*/
{
mortgage();
break;
}
case 4:/*option_4*/
{
option_4();
break;
}
case 0:/*exit*/
{
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
}
default:
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);
break;
}
}
} while (option_main != 1 && option_main != 2 && option_main != 3 && option_main != 4 && option_main != 0);
return (0);
}
float compound() //compound_interest_calculation_self_defined function
{
int compound_type, ci_option;
float n, initial_savings, rate, time, final_savings;
do {
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BGREEN"<Savings Calculator - Compounded Interest>\n"ANSI_COLOR_RESET); //displays option selected (Savings Calculator - Compounded Interest)
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter compound type:\n"); //Prompts user to choose
printf("[1] Monthly\n[2] Quarterly\n[3] Semiannually\n[4] Annually\n\n[9]Return to menu\n[0] Exit\n");//displays options
printf("=========================================================================================================\n");
scanf("%d", &compound_type); //accepts input for option selection
printf("=========================================================================================================\n");
system("cls");
switch (compound_type) //internal switch case for compounded interest calculator
{
case 1: /*monthly*/
n = 12;
printf("=========================================================================================================\n");
printf("<Monthly compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM "); //prompts user for initial saving amount (principle)
scanf("%f", &initial_savings); //accepts input for initial savings
/*printf("Please enter monthly deposit: RM "); //prompts user input for monthly deposit
scanf("%f", &monthly_deposit); */ //accepts input for monthly deposit
printf("Please enter interest rate (decimal): "); //prompts user input for interest rate
scanf("%f", &rate); //accepts input for interest rate
printf("Please enter time (year): "); //prompts input for saving duration
scanf("%f", &time); //accepts input for saving duration
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings); //dispays final savings amount
printf("=========================================================================================================\n");
break;
case 2: /*quarterly*/
n = 4;
printf("=========================================================================================================\n");
printf("<Quarterly compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings);
printf("=========================================================================================================\n");
break;
case 3: /*semiannually*/
n = 2;
printf("=========================================================================================================\n");
printf("<Semiannual compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f \n", final_savings);
printf("=========================================================================================================\n");
break;
case 4: /*annually*/
n = 1;
printf("=========================================================================================================\n");
printf("<Annual compound>\n");
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Please enter initial amount of savings: RM ");
scanf("%f", &initial_savings);
/*printf("Please enter monthly deposit: RM "); //temporarily removed from calculation
scanf("%f", &monthly_deposit);*/
printf("Please enter interest rate (decimal): ");
scanf("%f", &rate);
printf("Please enter time (year): ");
scanf("%f", &time);
final_savings = compound_calc(n, initial_savings, rate, time);
printf("\nThe final savings is: RM %.2f\n", final_savings);
printf("=========================================================================================================\n");
break;
case 9: /*return to menu*/
return main_menu();
break;
case 0: /*exit*/
printf("=========================================================================================================\n");
printf("<EXIT> \n");
printf("=========================================================================================================\n");
exit(0);
default:/*any other input*/
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);//When user inputs invalid option for compound type
}
} while (compound_type != 0 && compound_type != 1 && compound_type != 2 && compound_type != 3 && compound_type != 4 && compound_type != 9);
do
{
printf("[1]Back to compound interest calculator\n[9]Return to menu\n[0] Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &ci_option);
switch (ci_option)
{
case 1:
system("cls");
return compound();
break;
case 9:
system("cls");
return main_menu();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:/*any other input*/
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"INVALID OPTION - CHOOSE AGAIN\n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
}
} while (ci_option != 1 && ci_option != 9 && ci_option != 0);
return(0);
}
float compound_calc(float n, float initial_savings, float rate, float time)
{
float CI;
CI = (initial_savings*(pow((1 + rate / n), (n * time))));
return (CI);
}
float option_2()
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BMAGENTA"<place_holder_2> selected \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
return(0);
}
float mortgage()
{
float house_price, down_percentage, interest_rate, monthly_payment, r, p;//variables declaration
int loan_period, n,m_option;
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BYELLOW"<Home mortgage calculator>\n"ANSI_COLOR_RESET); //displays option selected (Savings Calculator - Compounded Interest)
printf("---------------------------------------------------------------------------------------------------------\n");
printf("Insert property price: RM ");//prompt input
scanf("%f", &house_price);//get input
printf("Insert down percentage (decimal): ");
scanf("%f", &down_percentage);
printf("Insert loan period (Years): ");
scanf("%d", &loan_period);
printf("Insert interest rate (decimal):");
scanf("%f", &interest_rate);
r = interest_rate / 12;
n = loan_period * 12;
p = house_price - house_price *down_percentage;
monthly_payment = p *(r*pow((1 + r), n)) / (pow((1 + r), n) - 1);//calculate monthly payment //NOTE:MOVE TO SELF DEFINED FUNCTION
printf("\nYour monthly payment is %.2f \n", monthly_payment);//display output
do
{
printf("=========================================================================================================\n");
printf("[3]Recalculate\n[9]Return to menu\n[0] Exit\n");
printf("=========================================================================================================\n");
scanf("%d", &m_option);
switch (m_option)
{
case 3:
system("cls");
return mortgage();
break;
case 9:
system("cls");
return main_menu();
break;
case 0:
system("cls");
printf("=========================================================================================================\n");
printf("<EXIT>\n");
printf("=========================================================================================================\n");
exit(0);
break;
default:/*any other input*/
system("cls");
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BRED"ERROR: INVALID OPTION \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
}
} while (m_option != 3 && m_option != 9 && m_option != 0);
return (0);
}
float option_4()
{
printf("=========================================================================================================\n");
printf(ANSI_COLOR_BCYAN"<place_holder_4> selected \n"ANSI_COLOR_RESET);
printf("=========================================================================================================\n");
return(0);
}
答案 0 :(得分:2)
替换您的“ scanf();”到“ fgets();”
char * fgets(char * str,int n,FILE * stream)
示例:
#include <stdio.h>
#include <string.h>
char Name[50];
int main(){
printf("What is your name?\n> ");
fgets(Name,50,stdin); //Gets user input from stdin (not from file)
/* But the problem is that fgets also includes '\n' so we put: */
Name[strcspn(Name,"\r\n")]=0; //Removes '\n'
printf("Your name is: <%s>\n",Name);
return 0;
}