/* Program: unitcon2.c */
/* Converts user input from one set of units of
weight to another. */
/* User types in input as follows: "3gl".
*/
/* Francis O'Donovan 24-11-97*/
#include <stdio.h>
#define LBS_LBS (1)
#define LBS_GRAMS (453.5147)
#define LBS_KILOGRAMS (0.4535)
#define GRAMS_LBS (0.0022)
#define GRAMS_GRAMS (1)
#define GRAMS_KILOGRAMS (0.001)
#define KILOGRAMS_LBS (2.205)
#define KILOGRAMS_GRAMS (1000)
#define KILOGRAMS_KILOGRAMS (1)
main()
{
/*
Define variables */
float weight;
char unit, convert;
/* Intro */
printf( "Program: unitcon2.c\n\n" );
printf( "Converts user input from one set of units of weight
to another.\n" );
printf( "User types input as follows \"3gl\".\n\n"
);
printf( "Francis O'Donovan 24-11-97\n\n\n" );
/* Ask user for the weight, unit, convert */
printf( "Please type in the weight, followed by the unit
the weight is in, followed by the weight you want to convert it
to.\n" );
printf( "(No spaces!\n)" );
printf( "For lbs, type l, for grams, type g, for kilograms,
type k.\n" );
printf( "(e.g. 3gl)\n" );
scanf( "%f %c %c", &weight, &unit, &convert
);
/* Convert weight and print result */
if( unit == 'l' && convert == 'l' )
printf( "%.4f lbs is %.4f lbs.\n\n", weight, weight*LBS_LBS
);
else if ( unit == 'l' && convert == 'g' )
printf( "%.4f lbs is %.4f grams.\n\n", weight, weight*LBS_GRAMS
);
else if ( unit == 'l' && convert == 'k' )
printf( "%.4f lbs is %.4f kilograms.\n\n", weight, weight*LBS_KILOGRAMS
);
else if( unit == 'g' && convert == 'l' )
printf( "%.4f grams is %.4f lbs.\n\n", weight, weight*GRAMS_LBS
);
else if ( unit == 'g' && convert == 'g' )
printf( "%.4f grams is %.4f grams.\n\n", weight, weight*GRAMS_GRAMS
);
else if ( unit == 'g' && convert == 'k' )
printf( "%.4f grams is %.4f kilograms.\n\n", weight,
weight*GRAMS_KILOGRAMS );
else if( unit == 'k' && convert == 'l' )
printf( "%.4f kilograms is %.4f lbs.\n\n", weight, weight*KILOGRAMS_LBS
);
else if ( unit == 'k' && convert == 'g' )
printf( "%.4f kilograms is %.4f grams.\n\n", weight,
weight*KILOGRAMS_GRAMS );
else if ( unit == 'k' && convert == 'k' )
printf( "%.4f kilograms is %.4f kilograms.\n\n", weight,
weight*KILOGRAMS_KILOGRAMS );
else
printf( "Something is wrong." );
}
© Francis O'Donovan
1999.