Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double convert_lengths();
- double convert_weights();
- double length_to_metric();
- double length_to_us();
- double weight_to_metric();
- double weight_to_us();
- void read_length_us(int* ft, double* in);
- void convert_length_to_metric(int ft, double in, int* m, double* cm);
- void output_length_metric(int ft, double in, int m, double cm);
- void read_length_metric(int* m, double* cm);
- void convert_length_to_us(int m, double cm, int* ft, double* in);
- void output_length_us(int m, double cm, int ft, double in);
- void read_weight_us(int* lb, double* oz);
- void convert_weight_to_metric(int lb, double oz, int* kg, double* g);
- void output_weight_metric(int lb, double oz, int kg, double g);
- void read_weight_metric(int* kg, double* g);
- void convert_weight_to_us(int kg, double g, int* lb, double* oz);
- void output_weight_us(int kg, double g, int lb, double oz);
- void clear_keyboard_buffer();
- #include <stdio.h>
- int main(int argc, char * argv[])
- {
- int temp, input;
- while(temp < 1 || input > 2 || input != 0)
- {
- temp = 0;
- printf("1. convert length\n2. convert weight\n0. Exit\nPlease choose from (1, 2, 0):\n");
- temp = scanf("%d", &input);
- clear_keyboard_buffer();
- if(input == 1)
- {
- convert_lengths();
- }
- if(input == 2)
- {
- convert_weights();
- }
- if(input == 0)
- {
- printf("User chose to exit.\n");
- break;
- }
- }
- return 0;
- }
- double convert_lengths()
- {
- int input = 99, temp;
- while(temp < 1 || input > 2 || input == 99 || input != 0)
- {
- printf("1. convert lengths to metrics\n");
- printf("2. convert lengths to US\n");
- printf("0. Return to Main Menu\n");
- temp = scanf("%d", &input);
- clear_keyboard_buffer();
- if(input == 1)
- {
- length_to_metric();
- }
- if(input == 2)
- {
- length_to_us();
- }
- if(input == 0)
- {
- printf("User chose to go main menu.\n");
- break;
- }
- }
- return 0;
- }
- double length_to_metric()
- {
- printf("The user wants to convert length_to_metric.\n");
- int m, ft;
- double cm, in;
- read_length_us(&ft, &in);
- convert_length_to_metric(ft, in, &m, &cm);
- output_length_metric(ft, in, m, cm);
- return 0;
- }
- void read_length_us(int* ft, double* in)
- {
- printf("Enter feet and inches (seperated by a space):\n");
- scanf("%d %lf", ft, in);
- }
- void convert_length_to_metric(int ft, double in, int* m, double* cm)
- {
- double totalft = ft + (in / 12);
- double totalm = totalft * .3048;
- *m = (int)totalm;
- *cm = (totalm - (*m)) * 100.0;
- }
- void output_length_metric(int ft, double in, int m, double cm)
- {
- printf("%d feet and %.4lf inches converted to %d meters and %.4lf centimeters.\n", ft, in, m, cm);
- }
- double length_to_us()
- {
- printf("The user wants to convert length_to_us.\n");
- int m, ft;
- double cm, in;
- read_length_metric(&m, &cm);
- convert_length_to_us(m, cm, &ft, &in);
- output_length_us(m, cm, ft, in);
- return 0;
- }
- void read_length_metric(int* m, double* cm)
- {
- printf("Enter meter and centimeters (seperated by a space):\n");
- scanf("%d %lf", m, cm);
- }
- void convert_length_to_us(int m, double cm, int* ft, double* in)
- {
- double totalm = m + (cm / 100);
- double totalft = totalm / .3048;
- *ft = (int)totalft;
- *in = (totalft - (*ft)) * 12.0;
- }
- void output_length_us(int m, double cm, int ft, double in)
- {
- printf("%d meters and %.4lf centimeters converted to %d feet and %.4lf inches.\n", m, cm, ft, in);
- }
- double convert_weights()
- {
- int input = 99, temp;
- while(temp < 1 || input > 2 || input == 99 || input != 0)
- {
- printf("1. convert weights to metrics\n");
- printf("2. convert weights to US\n");
- printf("0. Return to Main Menu\n");
- temp = scanf("%d", &input);
- clear_keyboard_buffer();
- if(input == 1)
- {
- weight_to_metric();
- }
- if(input == 2)
- {
- weight_to_us();
- }
- if(input == 0)
- {
- printf("User chose to go main menu.\n");
- break;
- }
- }
- return 0;
- }
- double weight_to_metric()
- {
- printf("The user wants to convert weight_to_metric.\n");
- int lb, kg;
- double oz, g;
- read_weight_us(&lb, &oz);
- convert_weight_to_metric(lb, oz, &kg, &g);
- output_weight_metric(lb, oz, kg, g);
- return 0;
- }
- void read_weight_us(int* lb, double* oz)
- {
- printf("Enter pound and ounces (seperated by a space):\n");
- scanf("%d %lf", lb, oz);
- }
- void convert_weight_to_metric(int lb, double oz, int* kg, double* g)
- {
- double totallb = lb + (oz / 16);
- double totalkg = totallb / 2.2046;
- *kg = (int)totalkg;
- *g = (totalkg - (*kg)) * 1000;
- }
- void output_weight_metric(int lb, double oz, int kg, double g)
- {
- printf("%d pounds and %.4lf ounces converted to %d kilograms and %.4lf grams.\n", lb, oz, kg, g);
- }
- double weight_to_us()
- {
- printf("The user wants to convert weight_to_us.\n");
- int lb, kg;
- double oz, g;
- read_weight_metric(&kg, &g);
- convert_weight_to_us(kg, g, &lb, &oz);
- output_weight_us(kg, g, lb, oz);
- return 0;
- }
- void read_weight_metric(int* kg, double* g)
- {
- printf("Enter kilograms and grams (seperated by a space):\n");
- scanf("%d %lf", kg, g);
- }
- void convert_weight_to_us(int kg, double g, int* lb, double* oz)
- {
- double totalkg = kg + (g / 1000);
- double totallb = totalkg * 2.2046;
- *lb = (int)totallb;
- *oz = (totallb - (*lb)) * 16;
- }
- void output_weight_us(int kg, double g, int lb, double oz)
- {
- printf("%d kilograms and %.4lf grams converted to %d pounds and %.4lf ounces.\n", kg, g, lb, oz);
- }
- void clear_keyboard_buffer(void)
- {
- char ch;
- scanf("%c", &ch);
- while (ch != '\n' && ch != '\0')
- {
- scanf("%c", &ch);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement