Thread: Units Calculator Program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Units Calculator Program

    So I wrote this code a while back and now I want to change it to accept units as their symbol rather than their exponent.

    Say when it asks me for the number, I want it to accept it when I type in something such as "5 Hz" for Hertz instead of "5 mag" and then "0" for everything else and "-1" for time.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    //create structure
    struct u_val
    	{
    		float mag;
    		float mass;
    		float length;
    		float lum;
    		float time;
    		float current;
    		float temp;
    		float quan;
    		int NaN;
    	};
    
    //get the values for the structures
    struct u_val Get_u (void)
    {
    	struct u_val number;
    
    		printf("Please enter a number (magnitude): ");
    		scanf_s("%f", &number.mag);
    		printf("Please enter the mass (kg) exponent: ");
    		scanf_s("%f", &number.mass);
    		printf("Please enter the length (m) exponent: ");
    		scanf_s("%f", &number.length);
    		printf("Please enter the luminous intensity (cd) exponent: ");
    		scanf_s("%f", &number.lum);
    		printf("Please enter the time (s) exponent: ");
    		scanf_s("%f", &number.time);
    		printf("Please enter the current (A) exponent: ");
    		scanf_s("%f", &number.current);
    		printf("Please enter the temperature (K) exponent: ");
    		scanf_s("%f", &number.temp);
    		printf("Please enter the amount of substance (mol) exponent: ");
    		scanf_s("%f", &number.quan);
    		printf("\n");
    		return number;
    }
    
    //check to see if the units are the same for addition and subtraction using if statements within if statements to continue going through with the actions if one of the if statements are not satisfied it NaN will remain 1
    int Check_units (struct u_val x1, struct u_val x2)
    {
    	int NaN=1;
    	if(x1.mass == x2.mass)
    		if(x1.length == x2.length)
    			if(x1.lum == x2.lum)
    				if(x1.time == x2.time)
    					if(x1.current == x2.current)
    						if(x1.temp == x2.temp)
    							if(x1.quan == x2.quan)
    								NaN = 0;//if all of the if statements are satisfied NaN will be set to 0
    
    	return NaN;
    }
    
    //check for electrical units
    int electrical_units (struct u_val x1)
    {
    	int NaN = 1;
    
    	const struct u_val 
    		volt  =  {0,  1,  2, 0, -3, -1, 0, 0, 0},
    		ohm   =  {0,  1,  2, 0, -3, -2, 0, 0, 0},
    		watt  =  {0,  1,  2, 0, -3,  0, 0, 0, 0},
    		joule =  {0,  1,  2, 0, -2,  0, 0, 0, 0},
    		farad =  {0, -1, -2, 0,  4,  2, 0, 0, 0},
    		henry =  {0,  1,  2, 0, -2, -2, 0, 0, 0},
    		hertz =  {0,  0,  0, 0, -1,  0, 0, 0, 0},
    		coul  =  {0,  0,  0, 0,  1,  1, 0, 0, 0},
    		siem  =  {0, -1, -2, 0,  3,  2, 0, 0, 0},
    		newton = {0,  1,  1, 0, -2,  0, 0, 0, 0};
    
    
    	{//check volts
    		if(x1.mass == volt.mass && x1.length == volt.length && x1.lum == volt.lum && x1.time == volt.time && x1.current == volt.current && x1.temp == volt.temp && x1.quan == volt.quan)
    								{
    									printf(" volt\n");
    									NaN = 0;
    								}
    	}
    	{//check ohms
    		if(x1.mass == ohm.mass && x1.length == ohm.length && x1.lum == ohm.lum && x1.time == ohm.time && x1.current == ohm.current && x1.temp == ohm.temp && x1.quan == ohm.quan)
    								{	
    									printf(" ohm\n");
    									NaN = 0;
    								}
    	}
    	{//check watts
    		if(x1.mass == watt.mass && x1.length == watt.length && x1.lum == watt.lum && x1.time == watt.time && x1.current == watt.current && x1.temp == watt.temp && x1.quan == watt.quan)
    								{	
    									printf(" watt\n");
    									NaN = 0;
    								}
    	}
    	{//check for joules
    		if(x1.mass == joule.mass && x1.length == joule.length && x1.lum == joule.lum && x1.time == joule.time && x1.current == joule.current && x1.temp == joule.temp && x1.quan == joule.quan)
    								{	
    									printf(" joule\n");	
    									NaN = 0;
    								}
    	}
    	{//check for farads
    		if(x1.mass == farad.mass && x1.length == farad.length && x1.lum == farad.lum && x1.time == farad.time && x1.current == farad.current && x1.temp == farad.temp && x1.quan == farad.quan)
    								{	
    									printf(" farad\n");
    									NaN = 0;
    								}
    	}
    	{//check for henries
    		if(x1.mass == henry.mass && x1.length == henry.length && x1.lum == henry.lum && x1.time == henry.time && x1.current == henry.current && x1.temp == henry.temp && x1.quan == henry.quan)
    								{	
    									printf(" henry\n");
    									NaN = 0;
    								}
    	}
    	{//check for hertz
    		if(x1.mass == hertz.mass && x1.length == hertz.length && x1.lum == hertz.lum && x1.time == hertz.time && x1.current == hertz.current && x1.temp == hertz.temp && x1.quan == hertz.quan)
    								{
    									printf(" hertz\n");
    									NaN = 0;
    								}
    	}
    	{//check for coulombs
    		if(x1.mass == coul.mass && x1.length == coul.length && x1.lum == coul.lum && x1.time == coul.time && x1.current == coul.current && x1.temp == coul.temp && x1.quan == coul.quan)
    								{
    									printf(" coul\n");
    									NaN = 0;
    								}
    	}
    	{//check for siemens
    		if(x1.mass == siem.mass && x1.length == siem.length && x1.lum == siem.lum && x1.time == siem.time && x1.current == siem.current && x1.temp == siem.temp && x1.quan == siem.quan)
    								{
    									printf(" siem\n");
    									NaN = 0;
    								}
    	}
    	{//check for newtons
    		if(x1.mass == newton.mass && x1.length == newton.length && x1.lum == newton.lum && x1.time == newton.time && x1.current == newton.current && x1.temp == newton.temp && x1.quan == newton.quan)
    								{
    									printf(" newton\n");
    									NaN = 0;
    								}
    	}
    	return NaN;
    }
    
    
    int Divide_by_zero (struct u_val x2)
    {
    	int NaN;
    	if (x2.mag == 0)
    		NaN = 1;
    	else
    		NaN = 0;
    	return NaN;
    }
    
    //addition
    struct u_val add_numbers (struct u_val x1, struct u_val x2)
    {
    	struct u_val sum;
    	sum.mag = x1.mag + x2.mag;
    	sum.mass = x1.mass;
    	sum.length = x1.length;
    	sum.lum = x1.lum;
    	sum.time = x1.time;
    	sum.current = x1.current;
    	sum.temp = x1.temp;
    	sum.quan = x1.quan;
    	return sum;
    }
    
    //subtraction
    struct u_val subtract_numbers (struct u_val x1, struct u_val x2)
    {
    	struct u_val diff;
    	diff.mag = x1.mag - x2.mag;
    	diff.mass = x1.mass;
    	diff.length = x1.length;
    	diff.lum = x1.lum;
    	diff.time = x1.time;
    	diff.current = x1.current;
    	diff.temp = x1.temp;
    	diff.quan = x1.quan;
    	return diff;
    }
    
    //multiply the numbers and add units
    struct u_val multiply_numbers (struct u_val x1,struct u_val x2)
    {
    	struct u_val product;
    	product.mag = x1.mag * x2.mag;
    	product.mass = x1.mass + x2.mass;
    	product.length = x1.length + x2.length;
    	product.lum = x1.lum + x2.lum;
    	product.time = x1.time + x2.time;
    	product.current = x1.current + x2.current;
    	product.temp = x1.temp + x2.temp;
    	product.quan = x1.quan + x2.quan;
    	return product;
    }
    //divide numbers if number_2 is not 0 and subtract units
    struct u_val divide_numbers (struct u_val x1, struct u_val x2)
    {
    	struct u_val quotient;
    	quotient.mag = x1.mag / x2.mag;
    	quotient.mass = x1.mass - x2.mass;
    	quotient.length = x1.length - x2.length;
    	quotient.lum = x1.lum - x2.lum;
    	quotient.time = x1.time - x2.time;
    	quotient.current = x1.current - x2.current;
    	quotient.temp = x1.temp - x2.temp;
    	quotient.quan = x1.quan - x2.quan;
    	return quotient;
    }
    
    void print (struct u_val x1)
    {	
    	
    	if(electrical_units(x1) == 1)
    	{
    		if(x1.mass !=0)
    			printf("kg^%.2f ", x1.mass);
    		if(x1.length !=0)
    			printf("m^%.2f ", x1.length);
    		if(x1.lum !=0)
    			printf("cd^%.2f ", x1.lum);
    		if(x1.time !=0)
    			printf("s^%.2f ", x1.time);
    		if(x1.current !=0)
    			printf("A^%.2f ", x1.current);
    		if(x1.temp !=0)
    			printf("K^%.2f ", x1.temp);
    		if(x1.quan !=0)
    			printf("mol^%.2f ", x1.quan);
    	}
    }
    
    
    
    
    int main (void)
    {	
    	struct u_val x1, x2, sum, diff, product, quotient;
    	int NaN;
    
    	printf("Let's get information about your first value!\n");
    	x1 = Get_u ();
    	printf("Let's get information about your second value!\n");
    	x2 = Get_u ();
    
    	NaN = Check_units (x1, x2);
    	sum = add_numbers (x1, x2);
    	//check for units being equal
    	if(NaN == 0)
    	{
    		printf("Addition yields: %f",sum.mag);
    		print (sum);
    		diff = subtract_numbers (x1, x2);
    		printf("\nSubtraction yields: %f",diff.mag);
    		print (diff);
    	}
    	if(Check_units (x1, x2) == 1)
    	{//compute when units arent equal
    			printf("Can't add; units are not equal!\nCan't subtract; units are not equal!");
    	}
    	//no need to run tests for multiplication
    	product = multiply_numbers (x1, x2);
    	printf("\nMultiplication yields: %f",product.mag);
    	print (product);
    	quotient = divide_numbers (x1, x2);
    	if (Divide_by_zero (x2) == 0)
    	{//if 2nd number has a magnitude that is not 0 compute the amount and print with units
    		printf("\nDivision yields: %f ",quotient.mag);
    		print (quotient);
    	}
    	else
    	{//when the 2nd number is 0 you can't divide
    		printf("\nCan't divide by 0!");
    	}
    
    
    	getchar();
    	getchar();
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... was there a question in there someplace?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    My bad I was wondering how I should go about doing this.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... not like that...

    Think about functions for single purposes... you feed them a couple of variables and get a single result back...
    Think about a way to "menuize" the user's choices...
    And while you're at it you may want to consult a C tutorial or book about the difference between structs and functions.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you expect number and units, perhaps
    Code:
    char buffer[100], units[10];
    double num;
    ...
    sscanf(buffer, "%lf %s", &num, units);
    if (!stricmp("Hz", units)) {
        ....
        }
    else if (!stricmp("mag", units)) {
        ...
        }

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Well... not like that...

    Think about functions for single purposes... you feed them a couple of variables and get a single result back...
    Think about a way to "menuize" the user's choices...
    And while you're at it you may want to consult a C tutorial or book about the difference between structs and functions.
    I know the difference and everything. I want to edit this code that I already wrote to accept values such as "5 Hz", and when it sees "Hz" is sets the mag value to "5" seconds to "-1" and everything else to "0".

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Think about writing a menu to allow your user to select the calculation he wants to perform then a small function to do each calculation.

    I'm not quite grasping why every function has to deal with a struct containing every calculation... Break it down into smaller blobs one function to calculate voltage, one for temperature, etc.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Think about writing a menu to allow your user to select the calculation he wants to perform then a small function to do each calculation.

    I'm not quite grasping why every function has to deal with a struct containing every calculation... Break it down into smaller blobs one function to calculate voltage, one for temperature, etc.
    The functions tests to see what units are being used. Now I want to edit it to just take the units as letters in the scanf convert it to the basic SI units and then add everything and test for same units.

    My professor was talking about a constant structure and using a character string, however I do not see why that would be reasonable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with calculator program.
    By sadr in forum C Programming
    Replies: 9
    Last Post: 09-10-2009, 10:30 AM
  2. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2008, 09:36 AM
  3. Calculator program.
    By omnificient in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:44 PM
  4. Help with Tax calculator program
    By Mshock in forum C++ Programming
    Replies: 1
    Last Post: 06-28-2006, 03:28 PM