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>
struct u_val
{
float mag;
float mass;
float length;
float lum;
float time;
float current;
float temp;
float quan;
int NaN;
};
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;
}
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;
return NaN;
}
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};
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
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;
}
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;
}
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;
}
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);
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)
{
printf("Can't add; units are not equal!\nCan't subtract; units are not equal!");
}
product = multiply_numbers (x1, x2);
printf("\nMultiplication yields: %f",product.mag);
print (product);
quotient = divide_numbers (x1, x2);
if (Divide_by_zero (x2) == 0)
{
printf("\nDivision yields: %f ",quotient.mag);
print (quotient);
}
else
{
printf("\nCan't divide by 0!");
}
getchar();
getchar();
return 0;
}