Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************
- Program: daily16.c
- Author: Thanh Ly
- Date: 6/14/2019
- Sources of Help: Dr. Adams
- Time Spent: 15 minutes
- Purpose: Given three options 1. Ask user to convert to lengths.
- 2. Ask user to convert to weights. 0. Ask user to exit program
- Convert user input from emperical to metric or vice versa.
- ***********************************************/
- #include <stdio.h>
- #include <math.h>
- #pragma warning(disable : 4996);
- void convert_lengths(void);
- void convert_weights();
- void length_to_metric(void);
- void length_to_us(void);
- void weight_to_metric();
- void weight_to_us(void);
- void clear_keyboard_buffer(void);
- void userInput(double* first_number, double* second_number);
- /* Lengths Conversions */
- #define INCES_TO_FEET 12
- #define FEET_TO_METERS 0.3048
- #define METERS_TO_CM 100
- /*Weights Conversions */
- #define OUNCES_TO_POUND 16
- #define POUND_TO_KILOGRAM 2.2046
- #define KILOGRAM_TO_GRAM 1000
- int main(int argc, char* argv[])
- {
- int options;
- //asks user if they want to convert values that are lengths or weights.
- do {
- printf("\t[1] Convert Lengths\n");
- printf("\t[2] Convert Weights\n");
- printf("\t[0] Exit\n");
- printf("Choose from the menu: ");
- scanf("%d", &options);
- printf("\n");
- clear_keyboard_buffer();
- switch (options)
- {
- case 1:
- convert_lengths();
- break;
- case 2:
- convert_weights();
- break;
- }
- } while (options != 0);
- return 0;
- }
- /***********************************************************/
- /*Start of convert lengths functions*/
- /***********************************************************/
- void convert_lengths(void)
- {
- int options;
- do {
- printf("\t\t[1] Convert feet/inches to meters/centimeters: \n");
- printf("\t\t[2] Convert meters/centimeters to feet/inches: \n");
- printf("\t\t[0] Exit: \n");
- printf("Choose from the menu: ");
- scanf("%d", &options);
- clear_keyboard_buffer();
- switch (options)
- {
- case 1:
- length_to_metric();
- break;
- case 2:
- length_to_us();
- break;
- }
- } while (options != 0);
- return;
- }
- void length_to_metric(void)
- {
- double total_meters = 0, temp = 0, temp2 = 0;
- double feet, inches;
- printf("Enter emperical unit in feet , inches: ");
- userInput(&feet, &inches);
- printf("%1.0lf feet %.2lf inches to %.2lf meters %.2lf centimeters\n\n", feet, inches, temp2, temp);
- }
- void length_to_metric_math(double* total_meters, double* temp, double* temp2)
- {
- if(inches >= 12)
- {
- inches = inches / INCES_TO_FEET;
- }
- total_meters = ((feet * INCES_TO_FEET) + inches) / INCES_TO_FEET;
- total_meters = total_meters * FEET_TO_METERS;
- temp = total_meters * 100;
- temp = fmod(temp, 100);
- temp2 = ((total_meters * 100) - temp) / 100;
- }
- void length_to_us(void)
- {
- double total_feet = 0, temp = 0;
- double meters, centimeters;
- printf("Enter metrics unit in meters , centimeters: ");
- userInput(&meters, ¢imeters);
- total_feet = ((meters * METERS_TO_CM) + centimeters) / METERS_TO_CM;
- temp = total_feet;
- total_feet = total_feet / FEET_TO_METERS;
- printf("%lf meters/centimeters to %lf feet/inches \n\n", temp, total_feet);
- }
- /***********************************************************/
- /* start of convert weights function */
- /***********************************************************/
- void convert_weights(void)
- {
- int options;
- do {
- printf("\t\t[1] Convert weights pounds/ounces to kilograms/grams: \n");
- printf("\t\t[2] Convert weights kilograms/grams to pounds/ounces: \n");
- printf("\t\t[0] Exit: \n");
- printf("Choose from the menu: ");
- scanf("%d", &options);
- clear_keyboard_buffer();
- switch (options)
- {
- case 1:
- weight_to_metric();
- break;
- case 2:
- weight_to_us();
- break;
- }
- } while (options != 0);
- return;
- }
- void weight_to_metric(void)
- {
- double total_pounds = 0, temp = 0;
- double pounds, ounces;
- printf("Enter emperical unit in pounds , ounces: ");
- userInput(£s, &ounces);
- total_pounds = ((pounds * OUNCES_TO_POUND) + ounces) / OUNCES_TO_POUND;
- temp = total_pounds;
- total_pounds = total_pounds / POUND_TO_KILOGRAM;
- printf("%lf pounds/ounces to %lf kilograms/grams\n\n", temp, total_pounds);
- }
- void weight_to_us(void)
- {
- double total_kilograms = 0, temp = 0;
- double kilograms, grams;
- printf("Enter emperical unit in kilograms , grams: ");
- userInput(&kilograms, &grams);
- total_kilograms = ((kilograms * KILOGRAM_TO_GRAM) + grams) / KILOGRAM_TO_GRAM;
- temp = total_kilograms;
- total_kilograms = total_kilograms * POUND_TO_KILOGRAM;
- printf("%lf kilograms/grams to %lf pounds/ounces \n\n", temp, total_kilograms);
- }
- /***********************************************************/
- /* Start of userInput function */
- /***********************************************************/
- void userInput(double* first_number, double* second_number)
- {
- int noc;
- noc = scanf("%lf %lf", first_number, second_number);
- clear_keyboard_buffer();
- while (noc == 0)
- {
- printf("Please enter two number seperated by a space: ");
- noc = scanf("%lf %lf", first_number, second_number);
- clear_keyboard_buffer();
- }
- return;
- }
- void clear_keyboard_buffer(void)
- {
- char c;
- int noc;
- noc = scanf("%c", &c);
- while (c != '\n' && noc != EOF)
- {
- noc = scanf("%c", &c);
- }
- }
- /* End of userInput function */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement