/*Copyright ElohimTov LLC*/ /* //Name: ElohimTov Solve Code - Operators - C Solution //Problem: HackerRank - 30 Days of Code - Operators //Current File: ElohimTovSolve_Operators.(c).txt //Compile-able File: ElohimTovSolve_Operators.c //Date: 05/23/2020 //Tale: ElohimTov Solve's Solution to HackerRank.com's // Operators problem, in C programming language. // Problem link at https://elohimtov.com/elohimtov.info. */ #include #include #include #include #include #include #include #include #include //define globals char* readline(); double HUNDRED = 100.00; // Complete the solve function below. void solve(double meal_cost, int tip_percent, int tax_percent) { double tip_amount = meal_cost * (tip_percent/HUNDRED); double tax_amount = meal_cost * (tax_percent/HUNDRED); double total_meal_cost = meal_cost + tip_amount + tax_amount; int total_rounded_cost = round(total_meal_cost); printf("%d\n", total_rounded_cost); }//ends solve(...) function /* //CREDIT: The following main() function was provided by HackerRank.com */ int main() { //input meal cost char* meal_cost_endptr; char* meal_cost_str = readline(); double meal_cost = strtod(meal_cost_str, &meal_cost_endptr); if(meal_cost_endptr == meal_cost_str || *meal_cost_endptr != '\0') { exit(EXIT_FAILURE); } //input tip percent char* tip_percent_endptr; char* tip_percent_str = readline(); int tip_percent = strtol(tip_percent_str, &tip_percent_endptr, 10); if(tip_percent_endptr == tip_percent_str || *tip_percent_endptr != '\0') { exit(EXIT_FAILURE); } //input tax cost char* tax_percent_endptr; char* tax_percent_str = readline(); int tax_percent = strtol(tax_percent_str, &tax_percent_endptr, 10); if(tax_percent_endptr == tax_percent_str || *tax_percent_endptr != '\0') { exit(EXIT_FAILURE); } //computer total cost of meal solve(meal_cost, tip_percent, tax_percent); return 0; }//ends main() function /* //CREDIT: The following readline() function was provided by HackerRank.com */ char* readline() { size_t alloc_length = 1024; size_t data_length = 0; char* data = malloc(alloc_length); while (true) { char* cursor = data + data_length; char* line = fgets(cursor, alloc_length - data_length, stdin); if (!line) { break; } data_length += strlen(cursor); if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } size_t new_length = alloc_length << 1; data = realloc(data, new_length); if (!data) { break; } alloc_length = new_length; } if (data[data_length - 1] == '\n') { data[data_length - 1] = '\0'; } data = realloc(data, data_length); return data; }//ends readline() function /*Copyright ElohimTov LLC*/