Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //preprocessing directives//
- #include <stdio.h>
- #include <math.h>
- #define INCHES_TO_FEET (1/12.0)
- #define METERS_TO_FEET 0.3048
- #define CENTIMETERS_TO_METER (1/100.0)
- #define POUNDS_TO_KILO (1/2.2046)
- #define GRAMS_TO_KILO (1/1000.0)
- #define OUNCES_TO_POUND (1/16.0)
- //function declarations//
- void clear_keyboard_buffer(void);
- void convert_length(void);
- void convert_weight(void);
- void length_to_metric(void);
- void length_to_us(void);
- void weight_to_metric(void);
- void weight_to_us(void);
- double us_to_metric_length_input(void);
- double us_to_metric_length_conversion(double us_length);
- void print_metric_length_conversion(double converted_length);
- double metric_to_us_length_input(void);
- double metric_to_us_length_conversion(double metric_length);
- void print_us_length_conversion(double converted_length);
- double us_to_metric_weight_input(void);
- double us_to_metric_weight_conversion(double us_weight);
- void print_metric_weight_conversion(double converted_weight);
- double metric_to_us_weight_input(void);
- double metric_to_us_weight_conversion(double metric_weight);
- void print_us_weight_conversion(double converted_weight);
- int main(int argc, char* argv[])
- {
- int choice;
- //asks user what they wish to convert, keeps asking until valid input//
- do
- {
- printf("Press (1) to convert lengths, (2) to convert weights, or (0) to exit: ");
- scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice != 0 && choice != 1 && choice != 2)
- {
- printf("Please enter either a 1 for converting lengths, 2 for converting weights, or 0 to exit.\n");
- }
- switch (choice)
- {
- case 1:
- convert_length();
- break;
- case 2:
- convert_weight();
- break;
- }
- } while (choice != 0);
- return 0;
- }
- void convert_length(void)
- {
- int choice;
- //asks user if they wish to convert to metric, US, or exit to main//
- do
- {
- printf("\nPress (1) to convert from feet/inches to meters/centimeters\n");
- printf("Press (2) to convert from meters/centimeters to feet/inches\n");
- printf("Press (0) to exit.\n");
- scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice != 0 && choice != 1 && choice != 2)
- {
- printf("Please enter a valid input.\n");
- }
- //switch to handle user input//
- switch (choice)
- {
- case 1:
- length_to_metric();
- break;
- case 2:
- length_to_us();
- break;
- }
- } while (choice != 0);
- return;
- }
- void convert_weight(void)
- {
- int choice;
- //asks user what direction they wish to convert their weight or exit to main//
- do
- {
- printf("\nPress (1) to convert from pounds/ounces to kilograms/grams\n");
- printf("Press (2) to convert from kilograms/grams to pounds/ounces\n");
- printf("Press (0) to exit.\n");
- scanf("%d", &choice);
- clear_keyboard_buffer();
- if (choice != 0 && choice != 1 && choice != 2)
- {
- printf("Please enter a valid input.\n");
- }
- switch (choice)
- {
- case 1:
- weight_to_metric();
- break;
- case 2:
- weight_to_us();
- break;
- }
- } while (choice != 0);
- return;
- }
- void length_to_metric(void)
- {
- double total_length;
- double converted_length;
- //takes input for the total length in us
- total_length = us_to_metric_length_input();
- //converts the length from us to metric
- converted_length = us_to_metric_length_conversion(total_length);
- //prints out the metric lenth in meters and centimeters
- print_metric_length_conversion(converted_length);
- }
- void length_to_us(void)
- {
- double total_length;
- double converted_length;
- //takes input for the total length in metric
- total_length = metric_to_us_length_input();
- //converts the length from metric to us
- converted_length = metric_to_us_length_conversion(total_length);
- //prints out the metric lenth in feet and inches
- print_us_length_conversion(converted_length);
- }
- void weight_to_metric(void)
- {
- double total_weight;
- double converted_weight;
- //takes input for the total length in us
- total_weight = us_to_metric_weight_input();
- //converts the length from us to metric
- converted_weight = us_to_metric_weight_conversion(total_weight);
- //prints out the metric lenth in meters and centimeters
- print_metric_weight_conversion(converted_weight);
- }
- void weight_to_us(void)
- {
- double total_weight;
- double converted_weight;
- //takes input for the total length in metric
- total_weight = metric_to_us_weight_input();
- //converts the length from metric to us
- converted_weight = metric_to_us_weight_conversion(total_weight);
- //prints out the us lenth in feet and inches
- print_us_weight_conversion(converted_weight);
- }
- double us_to_metric_length_input(void)
- {
- //variable delcaration//
- int feet;
- double inches;
- double total_length_in_feet;
- //asks user to input total amount of feet and inches, then aggregates them into
- //total length in feet variable. returns the total length in feet to the metric conversion function
- printf("Please input the total number of feet: \n");
- scanf("%d", &feet);
- printf("Please enter the total number of inches: \n");
- scanf("%f", &inches);
- total_length_in_feet = feet + inches * INCHES_TO_FEET;
- return total_length_in_feet;
- }
- double us_to_metric_length_conversion(double us_length)
- {
- //converts the us length into metric length //
- double metric_converted_length = us_length * METERS_TO_FEET;
- return metric_converted_length;
- }
- void print_metric_length_conversion(double converted_length)
- {
- int meters;
- double cm;
- //takes the meters out of the total meters by taking the floor of the total meters
- meters = floor(converted_length);
- //converts the reamining length into centimeters
- cm = (converted_length - meters) / CENTIMETERS_TO_METER;
- //prints out the meters and centimeters
- printf("The converted length is %d meters and %f centimeters\n", meters, cm);
- }
- double metric_to_us_length_input(void)
- {
- //variable delcaration//
- int meters;
- int centimeters;
- double total_length_in_meters;
- //asks user to input total amount of meters and cm, then aggregates them into
- //total length in meters variable. returns the total length in feet to the us conversion function
- printf("Please input the total number of meters: \n");
- scanf("%d", &meters);
- printf("Please enter the total number of centimeters: \n");
- scanf("%d", ¢imeters);
- total_length_in_meters = meters + (centimeters * CENTIMETERS_TO_METER);
- return total_length_in_meters;
- }
- double metric_to_us_length_conversion(double metric_length)
- {
- //converts the metric length into us length //
- double us_converted_length = metric_length / METERS_TO_FEET;
- return us_converted_length;
- }
- void print_us_length_conversion(double converted_length)
- {
- int feet;
- double inches;
- //takes the feet out of the total feet by taking the floor of the total feet
- feet = floor(converted_length);
- //converts the reamining length into inches
- inches = (converted_length - feet) / INCHES_TO_FEET;
- //prints out the feet and inches
- printf("The converted length is %d feet and %f inches\n", feet, inches);
- }
- double us_to_metric_weight_input(void)
- {
- //variable delcaration//
- int pounds;
- int ounces;
- double total_weight_in_pounds;
- //asks user to input total amount of pounds and ounces, then aggregates them into
- //total weight in pounds variable. returns the total weight in pounds to the metric conversion function
- printf("Please input the total number of pounds: \n");
- scanf("%d", £s);
- printf("Please enter the total number of ounces: \n");
- scanf("%d", &ounces);
- total_weight_in_pounds = pounds + (ounces * OUNCES_TO_POUND);
- return total_weight_in_pounds;
- }
- double us_to_metric_weight_conversion(double us_weight)
- {
- //converts the us length into metric length //
- double metric_converted_weight = us_weight * POUNDS_TO_KILO;
- return metric_converted_weight;
- }
- void print_metric_weight_conversion(double converted_weight)
- {
- int kilograms;
- double grams;
- //takes the kilos out of the total kilos by taking the floor of the total kilos
- kilograms = floor(converted_weight);
- //converts the reamining fractions of kilos into grams
- grams = (converted_weight - kilograms) / GRAMS_TO_KILO;
- //prints out the kilos and grams
- printf("The converted length is %d kilograms and %f grams\n", kilograms, grams);
- }
- double metric_to_us_weight_input(void)
- {
- //variable delcaration//
- int kilograms;
- int grams;
- double total_weight_in_kilograms;
- //asks user to input total amount of kilos and g's, then aggregates them into
- //total weight in kilos variable. returns the total weight in kilos to the us conversion function
- printf("Please input the total number of kilograms: \n");
- scanf("%d", &kilograms);
- printf("Please enter the total number of grams: \n");
- scanf("%d", &grams);
- total_weight_in_kilograms = kilograms + (grams * GRAMS_TO_KILO);
- return total_weight_in_kilograms;
- }
- double metric_to_us_weight_conversion(double metric_weight)
- {
- //converts the metric length into us length //
- double us_converted_weight = metric_weight / POUNDS_TO_KILO;
- return us_converted_weight;
- }
- void print_us_weight_conversion(double converted_weight)
- {
- int pounds;
- double ounces;
- //takes the pounds out of the total pounds by taking the floor of the total pounds
- pounds = floor(converted_weight);
- //converts the reamining fractions of pounds into ounces
- ounces = (converted_weight - pounds) / OUNCES_TO_POUND;
- //prints out the pounds and ounces
- printf("The converted length is %d pounds and %f ounces\n", pounds, ounces);
- }
- void clear_keyboard_buffer(void)
- {
- char c;
- scanf("%c", &c);
- while (c != '\n') {
- scanf("%c", &c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement