更改单位计算器以接受单位为字母 [英] Changing a Unit Calculator to Accept Units as Letters

查看:52
本文介绍了更改单位计算器以接受单位为字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我前一段时间写了这段代码,现在我想更改它以接受单位作为其符号而不是指数.

例如,当它问我要输入的数字时,我希望它在我输入诸如"5 Hz"(代表赫兹)而不是"5"(代表mag),然后为其他所有内容输入"0"(除了时间是- 1.

因此,我要输入"5 Hz",然后程序将其转换为SI单位,并将结构的所有值都填充为正确的值,这样我就可以进行计算了.该程序会自动加,减,乘,除.我只需要实现一种方法即可将单位设置为字符串并对其进行测试以查看其内容,并将SI单位设置为所键入单位的单位.

这是当前代码

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;
}

推荐答案

您正在以错误的方式处理此问题,将输入作为文本字符串,使用空格作为分隔符...分隔为两个字符串现在将您的第一个字符串转换为数字.

[edit]
如果要同时支持这两种方式,则只需编写一些额外的逻辑即可找出初始字符串中有多少个单词,如果只有一个,则转换为数字然后从那里开始.
[/edit]
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]


这篇关于更改单位计算器以接受单位为字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆