Click here to Skip to main content
15,568,965 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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" for mag and then "0" for everything else except for time which is "-1".

So I was to type in "5 Hz", and have the program convert that to SI units and fill all of the values to the struct to the right values so then I can do the calculations. The program automatically adds, subtracts, multiplies and divides. I just need to implement a way to get the unit set it to a character string and test it to see what it is and set the SI units to those of the unit typed.

Here's the current 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;
}
Posted
Comments
Sergey Alexandrovich Kryukov 14-Apr-11 20:43pm    
Very bad ad-hoc approach. No value.
--SA

1 solution

You're approaching this the wrong way, take the input as a text string, use the space as delimiter... separate to two strings now convert your first string to a number.

[edit]
If you want to support both ways, you just have to write in some extra logic that figures out how many words there is in the initial string, if there's only one, convert to a number and go from there.
[/edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-11 20:45pm    
Good advice, my 5.

Anyway, there is nothing reasonable in the project, no real value, all is done ad-hoc. Unit values are hard-coded in the code, what's the use? There should be data structures with conversion separate from UI, meta-data file with all unit descriptions and conversion factors, etc. As is -- useless exercise, even for practice.

--SA
Albert Holguin 14-Apr-11 20:47pm    
to be honest i didn't even look at his code, just looked at what he was trying to do in his description paragraph. seemed backwards from what i would do.
Sergey Alexandrovich Kryukov 14-Apr-11 21:43pm    
Well, a quick look would be enough. "I don't have to eat all egg to tell it's bad."
--SA
Member 7842562 14-Apr-11 23:01pm    
Yeah, my professor assigned this assignment. Did it no problem, now I have to edit this code to take a character string convert that into the SI units and then do the calculations with the units. So rather than taking the SI units it needs to take a character string for the units.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900